---
# System prepended metadata

title: K8S lab4(Service 延伸)
tags: [k8s]

---

---
title: K8S lab4(Service 延伸)
tags: k8s
---
K8S lab4(Service 延伸)
===
[TOC]

## 網路隔離下的溝通

### 情境
namespace: deposit
- pod
- service

namespace: customer
- pod
- service

### 架構圖
![](https://hackmd.io/_uploads/HJQd4yAZq.png)


### 解決
在服務被隔離的情況下(namespace 隔離)，
讓 deposit pod 可以使用到 customer pod 的服務

#### What is service domian name
Kubernetes 為 Service 和 Pod 創建 DNS  record。連接服務時可以使用一致的 DNS 名稱而不是 IP。

#### 方案1. deposit 直接用 customer service 的 domain name
1. 調整 deposit configmap
    將deposit 的 configMap backend-endpoint 的 domain name 調整為 `<customer-service-name>.<customer-service-namespace>.svc.cluster.local`
    > Ex: my-customer-service.default.svc.cluster.local
    > 
    configMap.yml:
    ```yaml=
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: deposit-config
    data:
      application.yml: |-
        server:
          port: 8080
          shutdown: graceful

        spring:
          application:
            name: deposit

        management:
          endpoint:
            shutdown:
              enabled: true
          endpoints:
            web:
              exposure:
                include: '*'

        endpoints:
          shutdown:
            enabled: true

        version: 'v1.0.3'

        backend-endpoint: 'http://<customer-service-name>.<customer-service-namespace>.svc.cluster.local:8080/api/customer'
    ```
2. 更新 configmap
    ```shell=
    kubectl apply -f <path-to-configmap-yaml> -n <namespace-name>
    ```
3. 重新佈署 pod
    刪除 pod
    ```shell=
    kubectl delete pod <pod-name> -n <namespace-name>
    ```
    建立 pod
    ```shell=
    kubectl apply -f <path-to-pod-yaml> -n <namespace-name>
    ```
4. 測試 API
    ```shell=
    kubectl exec -it net-tool -n <namespace-name> sh
    ```
    測試 API
    ```
    /app # curl --location --request GET "http://<deposit-clusterip-service-ip>:8080/api/deposit"
    {"id":"001","name":"王大明","balance":"1000"}
    ```

#### 方案2. deposit 透過 External Name Service 連到 customer service
1. 建立 External Name Service
    extrnal-service.yaml:
    ```yaml=
    apiVersion: v1
    kind: Service
    metadata:
      name: <service-name>    
    spec:
      type: ExternalName
      externalName: <customer-service-name>.<customer-service-namespace>.svc.cluster.local
    ```
    其中`externalName`放 customer service 的 domain name
    > Ex: my-customer-service.default.svc.cluster.local
    
2. 建立 Service
    ```shell=
    kubectl apply -f <path-to-service.yaml> -n <namespace-name>
    ```
    **p.s.** 要跟 deposit 放在同一 namespace 底下
3. 確認 Service
    ```shell=
    kubectl get service -n <namespace-name> -o yaml
    ```
4. 調整 deposit configmap
    將deposit 的 configMap backend-endpoint 的 domain name 調整為 `<exteranl-service-name>`
    configMap.yml:
    ```yaml=
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: deposit-config
    data:
      application.yml: |-
        server:
          port: 8080
          shutdown: graceful

        spring:
          application:
            name: deposit

        management:
          endpoint:
            shutdown:
              enabled: true
          endpoints:
            web:
              exposure:
                include: '*'

        endpoints:
          shutdown:
            enabled: true

        version: 'v1.0.3'

        backend-endpoint: 'http://<exteranl-service-name>:8080/api/customer'
    ```
5. 更新 configmap
    ```shell=
    kubectl apply -f <path-to-configmap-yaml> -n <namespace-name>
    ```
6. 重新佈署 pod
    刪除 pod
    ```shell=
    kubectl delete pod <pod-name> -n <namespace-name>
    ```
    建立 pod
    ```shell=
    kubectl apply -f <path-to-pod-yaml> -n <namespace-name>
    ```
7. 測試 API
    進到 net-tool pod 中
    ```shell=
    kubectl exec -it net-tool -n <namespace-name> sh
    ```
    測試 API
    ```
    /app # curl --location --request GET "http://<deposit-clusterip-service-ip>:8080/api/deposit"
    {"id":"001","name":"王大明","balance":"1000"}
    ```

## 參考
* [DNS for Services and Pods](https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/)
* [GKE - Service discovery and DNS](https://cloud.google.com/kubernetes-engine/docs/concepts/service-discovery) 

