Service 引入主要是解决 Pod 的动态变化,提供统一访问入口: - 防止 Pod 失联,准备找到提供同一个服务的 Pod (服务发现)
- 定义一组 Pod 的访问策略 (负载均衡)
部署 deploykubectl apply -f deploy.yaml apiVersion: apps/v1kind: Deploymentmetadata: name: chiyi-nginxspec: replicas: 3 selector: matchLabels: app: chiyi-nginx template: metadata: labels: app: chiyi-nginx spec: containers: - name: nginx image: nginx:1.14.2 ports: - containerPort: 80
部署 servicekubectl apply -f service.yaml apiVersion: v1kind: Servicemetadata: name: chiyi-nginxspec: selector: app: chiyi-nginx ports: - protocol: TCP port: 80 targetPort: 80 nodePort: 30002 type: NodePort
查看 service 和 pod 的关系kubectl get epcurl 10.244.1.58:80 说明: Service 通过标签关联一组 Pod Service 为一组 Pod 提供负载均衡能力 [root@k8s-master service]# kubectl get epNAME ENDPOINTS AGEchiyi-nginx 10.244.1.58:80,10.244.1.59:80,10.244.2.46:80 5m19skubernetes 172.17.28.225:6443 23h[root@k8s-master service]# curl 10.244.1.58: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 andworking. 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>
查看 servicekubectl get servicecurl 10.101.104.218 [root@k8s-master service]# kubectl get serviceNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEchiyi-nginx NodePort 10.101.104.218 <none> 80:30002/TCP 6m3skubernetes ClusterIP 10.96.0.1 <none> 443/TCP 23h[root@k8s-master service]# curl 10.101.104.218<!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 andworking. 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-master service]# ss -antp |grep 30002LISTEN 0 128 *:30002 *:* users:(("kube-proxy",pid=3544,fd=13))
导出 yamlkubectl get service chiyi-nginx -o yaml
筛选 service 关联 podkubectl get pods -l app=chiyi-nginx [root@k8s-master service]# kubectl get pods -l app=chiyi-nginxNAME READY STATUS RESTARTS AGEchiyi-nginx-5bbf8bff4b-6bwfz 1/1 Running 0 3m58schiyi-nginx-5bbf8bff4b-bpvvc 1/1 Running 0 3m58schiyi-nginx-5bbf8bff4b-pwwt4 1/1 Running 0 3m58s
扩容测试kubectl scale deployment chiyi-nginx --replicas=1kubectl get service,pods,ep
Service 三种常用类型- ClusterIP 集群内部使用,任一节点服务器和 pod 内部都可以访问
- NodePort 对外暴露应用(端口默认范围:30000-32767),任一节点服务器公网IP+端口号,可在浏览器访问。
- LoadBalancer 对外暴露应用,适合公有云
下载地址: Docker镜像与容器的导入导出操作实践 详解Docker Compose配置文件参数 |