k8s之service
阿新 • • 發佈:2021-07-02
使用deploy-nginx.yaml建立控制器
apiVersion: apps/v1 kind: Deployment metadata: name: nginx namespace: dev spec: replicas: 3 selector: matchLabels: run: nginx template: metadata: labels: run: nginx spec: containers: - image: nginx:1.17.1 name: nginx ports:- containerPort: 80 protocol: TCP [root@master ~]# kubectl create -f deploy-nginx.yaml deployment.apps/nginx created
檢視pod列表
[root@master ~]# kubectl get pod -n dev -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx-64777cd554-4m2nj 1/1 Running 0 3m30s 10.244.1.15 node2 <none> <none> nginx-64777cd554-lp8v2 1/1 Running 0 3m30s 10.244.1.16 node2 <none> <none> nginx-64777cd554-w2m2t 1/1 Running 0 3m30s 10.244.2.9 node1 <none> <none>
刪除其中一個pod
[root@master ~]# kubectl delete pod nginx-64777cd554-4m2nj -n dev pod "nginx-64777cd554-4m2nj" deleted
檢視新的pod
[root@master ~]# kubectl get pod -n dev -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx-64777cd554-lp8v2 1/1 Running 0 7m32s 10.244.1.16 node2 <none> <none> nginx-64777cd554-rlbm8 1/1 Running 0 47s 10.244.1.17 node2 <none> <none> nginx-64777cd554-w2m2t 1/1 Running 0 7m32s 10.244.2.9 node1 <none> <none>
發現ip地址和以前不一樣了
Service介紹
雖然每個pod都會分配一個單獨的pod ip,然而卻存在如下問題:
- pod ip 會隨著pod的重建產生變化
- pod ip僅僅是叢集內可見的虛擬ip,外部無法訪問
這樣對於訪問這個服務帶來了難度,因此k8s設計了service來解決這個問題
service可以看作是一組同類pod對外的訪問介面,藉助service,應用可以方便地實現服務發現和負載均衡
建立叢集內部可訪問的service
暴露service
[root@master ~]# kubectl expose deployment nginx --name=svc-nginx1 --type=ClusterIP --port=80 --target-port=80 -n dev service/svc-nginx1 exposed [root@master ~]# kubectl get service -n dev NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE svc-nginx1 ClusterIP 10.100.210.11 <none> 80/TCP 44s #這裡產生了一個ClusterIP,這就是service的ip,在service的生命週期中,這個地址是不會變的,可以通過這個Ip訪問當前service對應的pod
#訪問service [root@master ~]# curl 10.100.210.11:80 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
建立叢集外部也可以訪問的service
上面搭建的service的type型別為ClusterIP,這個ip地址只能叢集內部可訪問
如果需要建立外部也可以訪問的Service,需要修改type為NodePort
[root@master ~]# kubectl expose deploy nginx --name=svc-nginx2 --type=NodePort --port=80 --target-port=80 -n dev service/svc-nginx2 exposed [root@master ~]# kubectl get svc -n dev NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE svc-nginx1 ClusterIP 10.100.210.11 <none> 80/TCP 20m svc-nginx2 NodePort 10.104.222.53 <none> 80:32714/TCP 67s
在瀏覽器中訪問
刪除service
[root@master ~]# kubectl delete svc svc-nginx2 -n dev service "svc-nginx2" deleted [root@master ~]# kubectl delete svc svc-nginx1 -n dev service "svc-nginx1" deleted
使用配置方式
apiVersion: v1 kind: Service metadata: name: svc-nginx namespace: dev spec: ports: - port: 80 protocol: TCP targetPort: 80 selector: run: nginx type: ClusterIP
使用配置檔案
[root@master ~]# vim svc-nginx.yaml [root@master ~]# kubectl create -f svc-nginx.yaml service/svc-nginx created [root@master ~]# kubectl get svc -n dev NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE svc-nginx ClusterIP 10.102.204.135 <none> 80/TCP 14s
刪除
[root@master ~]# kubectl delete -f svc-nginx.yaml service "svc-nginx" deleted [root@master ~]# kubectl get svc -n dev No resources found in dev namespace.