# ExternalName * 為了能夠讓 pod 訪問位於 kubernetes 叢集之外的應用程序,我們使用名為 ExternalName 的服務。  * 例如,我們有一個在 kubernetes 集群之外運行的應用程序,地址為 `dt.external.com`。 * ExternalName 的設定其實就是把 Service 導向指定的 DNS name,而不是 service 中 label selector 所設定的 pod ``` apiVersion: v1 kind: Service metadata: name: web-svc namespace: default spec: externalName: dt.example.com type: ExternalName ``` ``` $ kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.98.224.1 <none> 443/TCP 29h web-svc ExternalName <none> dt.example.com <none> 3m8s # 10.98.224.10 是 coredns service ip $ dig @10.98.224.10 web-svc.default.svc.cluster.local +short dt.example.com. ``` ## K8S Service ExternalName 實做範例 * 因為 service 沒辦法跨 namespace 透過 label 找到 pod,所以透過 externalname service 就可以訪問到不同 namespace 的服務 ``` $ echo 'apiVersion: v1 kind: Namespace metadata: name: j --- apiVersion: v1 kind: Pod metadata: name: pod-j namespace: j labels: dt: hdp spec: containers: - name: j100 image: quay.io/cloudwalker/nginx --- apiVersion: v1 kind: Service metadata: name: svc-j namespace: j spec: selector: dt: hdp ports: - name: http protocol: TCP port: 80 targetPort: 80' | kubectl apply -f - ``` * 在 namespace j 建立 pod-j 及 svc-j ``` $ kubectl get all -n j NAME READY STATUS RESTARTS AGE pod/pod-j 1/1 Running 0 6s NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/svc-j ClusterIP 10.98.64.97 <none> 80/TCP 6s # svc-j 的 endpoints 有選到 pod-j $ kubectl get endpoints -n j NAME ENDPOINTS AGE svc-j 10.244.64.143:80 68s ``` * 在 namespace k 建立 svc-k Service ``` $ echo 'apiVersion: v1 kind: Namespace metadata: name: k --- kind: Service apiVersion: v1 metadata: name: svc-k namespace: k spec: selector: dt: hdp ports: - name: http protocol: TCP port: 80 targetPort: 80' | kubectl apply -f - $ kubectl get all -n k NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/svc-k ClusterIP 10.98.64.63 <none> 80/TCP 9s ``` * 在 k namespace 的 Service 無法選到 namespace j 的 Pod, 如下: ``` $ kubectl get endpoints -n k NAME ENDPOINTS AGE svc-k <none> 35s ``` * 上述錯誤原因是 Service selection absolutely happens in a single namespace. * 上述錯誤的解答, 請參考以下文章 1. Service located in another namespace https://stackoverflow.com/questions/37221483/service-located-in-another-namespace * 使用 Service 的 ExternalName 解決上述錯誤, 範例如下 : ``` $ echo 'kind: Service apiVersion: v1 metadata: name: svc-k-exname namespace: k spec: type: ExternalName externalName: svc-j.j.svc.cluster.local' | kubectl apply -f - $ kubectl get all -n k NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/svc-k ClusterIP 10.98.64.63 <none> 80/TCP 15m service/svc-k-exname ExternalName <none> svc-j.j.svc.tklkh.k8s <none> 16s $ kubectl -n k run test --image=quay.io/cooloo9871/nginx $ kubectl get po -n k NAME READY STATUS RESTARTS AGE test-5fbd7df9dd-rmzc5 1/1 Running 0 5s $ kubectl -n k exec -it test-5fbd7df9dd-rmzc5 -- bash $ curl http://svc-k-exname.k <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> ........ ``` ###### tags: `K8S`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up