Service 存在的意义: 防止Pod失联(服务发现) 定义一组Pod的访问策略(负载均衡) Pod与Service的关系: 通过label-selector相关联 通过Service实现Pod的负载均衡( TCP/UDP 4层) Service三种常用类型: ClusterIP:集群内部使用 NodePort:对外暴露应用 LoadBalancer:对外暴露应用,适用公有云 ClusterIP:默认,分配一个稳定的IP地址,即VIP,只能在集群内部访问(同Namespace内的Pod)。 NodePort:在每个节点上启用一个端口来暴露服务,可以在集群外部访问。也会分配一个稳定内部集群IP地址。访问地址:<NodeIP>:<NodePort> LoadBalancer:与NodePort类似,在每个节点上启用一个端口来暴露服务。除此之外,Kubernetes会请求底层云平台上的负载均衡器,将每个Node([NodeIP]:[NodePort])作为后端添加进去。 [root@centos7 demo]# [root@centos7 demo]# kubectl get pods NAME READY STATUS RESTARTS AGE web-5c987b8447-kptld 1/1 Running 0 2d6h web-5c987b8447-nwhcd 1/1 Running 0 8m39s web-5c987b8447-qjpz4 1/1 Running 0 8m39s [root@centos7 demo]# [root@centos7 demo]# kubectl get srv error: the server doesn‘t have a resource type "srv" [root@centos7 demo]# [root@centos7 demo]# [root@centos7 demo]# clear [root@centos7 demo]# [root@centos7 demo]# [root@centos7 demo]# [root@centos7 demo]# kubectl get pods NAME READY STATUS RESTARTS AGE web-5c987b8447-kptld 1/1 Running 0 2d6h web-5c987b8447-nwhcd 1/1 Running 0 8m51s web-5c987b8447-qjpz4 1/1 Running 0 8m51s [root@centos7 demo]# [root@centos7 demo]# [root@centos7 demo]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 8d [root@centos7 demo]# [root@centos7 demo]# [root@centos7 demo]# cat service.yml apiVersion: v1 kind: Service metadata: labels: app: web name: web spec: ports: - port: 80 protocol: TCP targetPort: 80 selector: app: webapp type: NodePort [root@centos7 demo]# [root@centos7 demo]# kubectl apply -f service.yml service/web created [root@centos7 demo]# [root@centos7 demo]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 8d web NodePort 10.0.0.19 <none> 80:32045/TCP 4s [root@centos7 demo]# [root@centos7 demo]# kubectl get endpoints NAME ENDPOINTS AGE kubernetes 192.168.0.11:6443 8d web 10.244.0.4:80,10.244.1.6:80,10.244.2.5:80 3m48s [root@centos7 demo]# [root@centos7 demo]# [root@centos7 demo]# kubectl get node -o wide NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME k8s-master1 Ready <none> 8d v1.16.0 192.168.0.11 <none> CentOS Linux 7 (Core) 3.10.0-693.el7.x86_64 docker://18.9.6 k8s-node1 Ready <none> 8d v1.16.0 192.168.0.13 <none> CentOS Linux 7 (Core) 3.10.0-693.el7.x86_64 docker://18.9.6 k8s-node2 Ready <none> 8d v1.16.0 192.168.0.14 <none> CentOS Linux 7 (Core) 3.10.0-693.el7.x86_64 docker://18.9.6 [root@centos7 demo]# [root@centos7 demo]# 在浏览器访问: 访问地址:<NodeIP>:<NodePort> 即:192.168.0.13:32045 [root@centos7 demo]# [root@centos7 demo]# kubectl get svc -o wide NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 8d <none> web NodePort 10.0.0.19 <none> 80:32045/TCP 18m app=web web-1 ClusterIP 10.0.0.151 <none> 80/TCP 108s app=web [root@centos7 demo]# ClusterIP:集群内部使用--任意节点执行curl <ClusterIP>:<ClusterIP-Port>,不能浏览器访问 NodePort:对外暴露应用---在浏览器访问地址:<NodeIP>:<NodePort> 互联网外部如何访问内网机器的端口呢? NodePort访问流程: user ->域名(公网IP)-> node ip:port ->? -> pod LoadBalancer访问流程: user ->域名(公网IP)-> 公有云上的负载均衡器-> node ip:port 一般生产环境node都是部署在内网,那32045这个端口怎么让互联网用户访问呢? 1,找一台有公网IP的服务器,装一个nginx,反向代理->node ip:port 2, 直接用你们外部负载均衡器(nginx,lvs,haproxy)->node ip:port
原文:https://www.cnblogs.com/k8s-pod/p/13210859.html