Service(服务)
[root@k8s ~]# kubectl get all NAME READY STATUS RESTARTS AGE pod/nginx-deploy-855866bb46-8fgzg 1/1 Running 0 10m pod/nginx-deploy-855866bb46-87scw 1/1 Running 0 10m pod/nginx-deploy-855866bb46-5rqh4 1/1 Running 0 10m NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/kubernetes ClusterIP 10.43.0.1 <none> 443/TCP 39h NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/nginx-deploy 3/3 3 3 81m NAME DESIRED CURRENT READY AGE replicaset.apps/nginx-deploy-855866bb46 3 3 3 22m
创建service
ServiceType 取值
- ClusterIP(默认):将服务公开在集群内部。kubernetes会给服务分配一个集群内部的 IP,集群内的所有主机都可以通过这个Cluster-IP访问服务。集群内部的Pod可以通过service名称访问服务。
- NodePort:通过每个节点的主机IP 和静态端口(NodePort)暴露服务。 集群的外部主机可以使用节点IP和NodePort访问服务。
- ExternalName:将集群外部的网络引入集群内部。
- LoadBalancer:使用云提供商的负载均衡器向外部暴露服务。
ClusterIP
如果不设置ServiceType,默认是ClusterIP
内部主机可以访问ip+端口
内部主机的pod可以访问service名+端口
[root@k8s ~]# kubectl expose deploy nginx-deploy --name=nginx-service --port=8080 --target-port=80 service/nginx-service exposed [root@k8s ~]# kubectl get service NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.43.0.1 <none> 443/TCP 39h nginx-service ClusterIP 10.43.65.176 <none> 8080/TCP 7s
NodePort
可以在外部主机访问
主机ip+NodePort
[root@k8s ~]# kubectl expose deploy nginx-deploy --name=nginx-outside --type=NodePort --port=8081 --target-port=80 service/nginx-outside exposed [root@k8s ~]# kubectl get service NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.43.0.1 <none> 443/TCP 40h nginx-service ClusterIP 10.43.65.176 <none> 8080/TCP 40m nginx-outside NodePort 10.43.127.65 <none> 8081:32109/TCP 10s
服务器是192.168.80.15,192.168.80.16,192.168.80.17
都可以访问
访问服务
集群的每个服务器都能访问10.43.65.176:8080,这个是进群内部分配的ip
[root@k8s ~]# curl 10.43.65.176:8080 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> html { color-scheme: light dark; } 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>
在主机内部可以通过服务名+端口访问,服务器是不能直接访问服务的
[root@k8s ~]# curl nginx-service curl: (6) Could not resolve host: nginx-service; 未知的错误 [root@k8s ~]# kubectl run test -it --image=nginx:1.22 --rm -- bash If you dont see a command prompt, try pressing enter. root@test:/# curl nginx-service:8080 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> html { color-scheme: light dark; } 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>
查看服务
[root@k8s ~]# kubectl describe service nginx-service Name: nginx-service Namespace: default Labels: app=nginx-deploy Annotations: <none> Selector: app=nginx-deploy Type: ClusterIP IP Family Policy: SingleStack IP Families: IPv4 IP: 10.43.65.176 IPs: 10.43.65.176 Port: <unset> 8080/TCP TargetPort: 80/TCP Endpoints: 10.42.0.36:80,10.42.1.26:80,10.42.2.25:80 Session Affinity: None Events: <none>
负载均衡
修改某一个pod的页面为hello,然后多次访问,发现某一次访问到hello
[root@k8s ~]# kubectl get pod NAME READY STATUS RESTARTS AGE nginx-deploy-855866bb46-8fgzg 1/1 Running 0 59m nginx-deploy-855866bb46-87scw 1/1 Running 0 59m nginx-deploy-855866bb46-5rqh4 1/1 Running 0 59m [root@k8s ~]# kubectl exec -it nginx-deploy-855866bb46-5rqh4 -- bash root@nginx-deploy-855866bb46-5rqh4:/# cd /usr/share/nginx/html/ root@nginx-deploy-855866bb46-5rqh4:/usr/share/nginx/html# ls 50x.html index.html root@nginx-deploy-855866bb46-5rqh4:/usr/share/nginx/html# echo hello > index.html root@nginx-deploy-855866bb46-5rqh4:/usr/share/nginx/html# exit exit [root@k8s ~]# curl 10.43.65.176:8080 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> html { color-scheme: light dark; } 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> [root@k8s ~]# curl 10.43.65.176:8080 hello