$ k apply -f nginx-deployment.yaml deployment.apps/nginx-deployment created
$ k get po -owide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx-deployment-756d6b7586-7plkf 1/1 Running 0 47s 10.10.1.42 mac-worker <none> <none> nginx-deployment-756d6b7586-jwt5h 1/1 Running 0 47s 10.10.1.43 mac-worker <none> <none>
$ curl 10.10.1.42 srv : 10.10.1.42:80 host: nginx-deployment-756d6b7586-7plkf uri : GET 10.10.1.42 / date: 2022-06-24T06:22:10+00:00
$ curl 10.10.1.43 srv : 10.10.1.43:80 host: nginx-deployment-756d6b7586-jwt5h uri : GET 10.10.1.43 / date: 2022-06-24T06:22:17+00:00
Kubernetes 为 Service 自动分配了一个静态 IP 地址 10.101.182.177(独立于 Pod 地址段)
Service 的 IP 是一个虚地址,不存在实体,只能用来转发流量(无法 ping)
1 2 3 4
$ k get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 6d21h nginx-svc ClusterIP 10.101.182.177 <none> 80/TCP 65s
Service 管理了 Endpoint 对象(Endpoint 对象代表 IP 地址,Service 并不会直接管理 Pod)
$ k describe service nginx-svc Name: nginx-svc Namespace: default Labels: <none> Annotations: <none> Selector: app=nginx-deployment Type: ClusterIP IP Family Policy: SingleStack IP Families: IPv4 IP: 10.101.182.177 IPs: 10.101.182.177 Port: <unset> 80/TCP TargetPort: 80/TCP Endpoints: 10.10.1.42:80,10.10.1.43:80 Session Affinity: None Events: <none>
$ k get endpoints -owide NAME ENDPOINTS AGE kubernetes 192.168.191.144:6443 6d21h nginx-svc 10.10.1.42:80,10.10.1.43:80 9m32s
$ k describe endpoints nginx-svc Name: nginx-svc Namespace: default Labels: <none> Annotations: endpoints.kubernetes.io/last-change-trigger-time: 2022-06-24T06:34:32Z Subsets: Addresses: 10.10.1.42,10.10.1.43 NotReadyAddresses: <none> Ports: Name Port Protocol ---- ---- -------- <unset> 80 TCP
Events: <none>
$ k get po -owide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx-deployment-756d6b7586-7plkf 1/1 Running 0 22m 10.10.1.42 mac-worker <none> <none> nginx-deployment-756d6b7586-jwt5h 1/1 Running 0 22m 10.10.1.43 mac-worker <none> <none>
服务发现 + 负载均衡
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
$ k get po NAME READY STATUS RESTARTS AGE nginx-deployment-756d6b7586-7plkf 1/1 Running 0 30m nginx-deployment-756d6b7586-jwt5h 1/1 Running 0 30m
$ k exec nginx-deployment-756d6b7586-7plkf -- curl -s 10.101.182.177 srv : 10.10.1.43:80 host: nginx-deployment-756d6b7586-jwt5h uri : GET 10.101.182.177 / date: 2022-06-24T06:49:32+00:00
$ k exec nginx-deployment-756d6b7586-7plkf -- curl -s 10.101.182.177 srv : 10.10.1.42:80 host: nginx-deployment-756d6b7586-7plkf uri : GET 10.101.182.177 / date: 2022-06-24T06:49:36+00:00
$ k delete po nginx-deployment-756d6b7586-jwt5h pod "nginx-deployment-756d6b7586-jwt5h" deleted
$ k get po -owide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx-deployment-756d6b7586-44j7q 1/1 Running 0 29s 10.10.1.44 mac-worker <none> <none> nginx-deployment-756d6b7586-7plkf 1/1 Running 0 31m 10.10.1.42 mac-worker <none> <none>
$ k describe service nginx-svc Name: nginx-svc Namespace: default Labels: <none> Annotations: <none> Selector: app=nginx-deployment Type: ClusterIP IP Family Policy: SingleStack IP Families: IPv4 IP: 10.101.182.177 IPs: 10.101.182.177 Port: <unset> 80/TCP TargetPort: 80/TCP Endpoints: 10.10.1.42:80,10.10.1.44:80 Session Affinity: None Events: <none>
$ k exec nginx-deployment-756d6b7586-7plkf -- curl -s 10.101.182.177 srv : 10.10.1.42:80 host: nginx-deployment-756d6b7586-7plkf uri : GET 10.101.182.177 / date: 2022-06-24T06:53:59+00:00
$ k exec nginx-deployment-756d6b7586-7plkf -- curl -s 10.101.182.177 srv : 10.10.1.44:80 host: nginx-deployment-756d6b7586-44j7q uri : GET 10.101.182.177 / date: 2022-06-24T06:54:03+00:00
$ k exec nginx-deployment-756d6b7586-7plkf -- ping -c1 10.101.182.177 PING 10.101.182.177 (10.101.182.177): 56 data bytes
--- 10.101.182.177 ping statistics --- 1 packets transmitted, 0 packets received, 100% packet loss command terminated with exit code 1
DNS
Service 对象的 IP 地址是静态的,Kubernetes 的 DNS 插件可以为 Service 创建 DNS 域名
Namespace 用来在集群中实现对 API 对象的隔离,Kubernetes 会将 Namespace 作为 DNS 域名的一部分
1 2 3 4 5 6 7
$ k get ns NAME STATUS AGE default Active 6d22h kube-flannel Active 6d21h kube-node-lease Active 6d22h kube-public Active 6d22h kube-system Active 6d22h
“ClusterIP” allocates a cluster-internal IP address for load-balancing to endpoints.
Endpoints are determined by the selector or if that is not specified, by manual construction of an Endpoints object or EndpointSlice objects.
If clusterIP is “None”, no virtual IP is allocated and the endpoints are published as a set of endpoints rather than a virtual IP.
NodePort
“NodePort” builds on ClusterIP and allocates a port on every node which routes to the same endpoints as the clusterIP.
LoadBalancer
“LoadBalancer” builds on NodePort and creates an external load-balancer (if supported in the current cloud) which routes to the same endpoints as the clusterIP.
ExternalName
“ExternalName” aliases this service to the specified externalName.
NodePort - 除了对 Pod 做负载均衡之外,还会在集群的每个 Node 上创建一个独立的端口来对外提供服务
$ k apply -f nginx-svc.yaml service/nginx-svc configured
$ k get svc nginx-svc -owide NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR nginx-svc NodePort 10.101.182.177 <none> 80:30862/TCP 57m app=nginx-deployment
$ k describe service nginx-svc Name: nginx-svc Namespace: default Labels: <none> Annotations: <none> Selector: app=nginx-deployment Type: NodePort IP Family Policy: SingleStack IP Families: IPv4 IP: 10.101.182.177 IPs: 10.101.182.177 Port: <unset> 80/TCP TargetPort: 80/TCP NodePort: <unset> 30862/TCP Endpoints: 10.10.1.42:80,10.10.1.44:80 Session Affinity: None External Traffic Policy: Cluster Events: <none>