建立一個Service和一個Pod做爲其後端。經過kubectl describe得到該Service和對應Endpoints的信息。nginx
<hwcka-004-1-svc-你的華爲雲id>
<hwcka-004-1-pod-你的華爲雲id>
首先建立 pod,注意添加標籤。由於Pod和Service會經過標籤關聯起來。後端
[root@svn ch4]# cat pod.yaml apiVersion: v1 kind: Pod metadata: name: hwcka-004-1-pod-joyo labels: app: nginx spec: containers: - name: nginx image: nginx:alpine [root@svn ch4]# kubectl apply -f pod.yaml pod/hwcka-004-1-pod-joyo created [root@svn ch4]# kubectl get pod NAME READY STATUS RESTARTS AGE busybox 1/1 Running 13 20h hwcka-004-1-pod-joyo 1/1 Running 0 13s [root@svn ch4]#
接着把 pod 暴露爲 serviceapi
kubectl expose pod hwcka-004-1-pod-joyo --port=8888 --target-port=80 --name=hwcka-004-1-svc-joyo
測試和驗證:app
[root@svn ch4]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE hwcka-004-1-svc-joyo ClusterIP 10.101.211.222 <none> 8888/TCP 4m19s kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 41d [root@svn ch4]# kubectl get ep NAME ENDPOINTS AGE hwcka-004-1-svc-joyo 10.244.2.179:80 8m22s kubernetes 192.168.202.130:6443 41d [root@svn ch4]# k describe svc hwcka-004-1-svc-joyo Name: hwcka-004-1-svc-joyo Namespace: default Labels: app=nginx Annotations: <none> Selector: app=nginx Type: ClusterIP IP: 10.101.211.222 Port: <unset> 8888/TCP TargetPort: 80/TCP Endpoints: 10.244.2.179:80 Session Affinity: None Events: <none>
而且運行curl 10.244.2.179:80
和curl 10.101.211.222:8888
都能看到Welcome to nginx!
的歡迎頁面。curl
建立一個Service和一個Pod做爲其後端。經過nslookup查詢該Service的Pod的域名信息。svn
<hwcka-004-2-你的華爲雲id>
首先建立Pod。測試
kubectl run nginx --image=nginx:alpine --generator=run-pod/v1
而後暴露Pod爲Service。url
kubectl expose pod nginx --port=8888 --target-port=80 --name=hwcka-004-2-joyo
Service和Pod的域名信息spa
[root@svn ch4]# kubectl exec -it busybox -- nslookup hwcka-004-2-joyo Server: 10.96.0.10 Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local Name: hwcka-004-2-joyo Address 1: 10.110.24.8 hwcka-004-2-joyo.default.svc.cluster.local [root@svn ch4]# kubectl exec -it busybox -- nslookup nginx Server: 10.96.0.10 Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local Name: nginx Address 1: ::ffff:67.215.82.154 Address 2: 67.215.82.154
busybox.yaml的內容(考試環境下,不容許下載nslookup軟件包,可是能夠pull 鏡像,因而咱們使用busybox:1.28裏面的nslookup命令)rest
apiVersion: v1 kind: Pod metadata: name: busybox namespace: default spec: containers: - name: busybox image: busybox:1.28 command: - sleep - "3600" imagePullPolicy: IfNotPresent restartPolicy: Always