• 防止Pod失聯
• 定義一組Pod的訪問策略
• 支持ClusterIP,NodePort以及LoadBalancer三種類型
• Service的底層實現主要有Iptables和IPVS二種網絡模式nginx
Pod與Service的關係vim
• 經過label-selector相關聯
• 經過Service實現Pod的負載均衡( TCP/UDP 4層)後端
# 版本 apiVersion: v1 # 資源對象 kind: Service # 元數據 metadata: # 指定service名稱 name: my-service # 命名空間 namespace: default spec: # 分配IP clusterIP: 10.0.0.1 # 指定端口 ports: # 端口名稱 - name: http # 指定service端口 port: 80 # service使用協議 protocol: TCP # 容器端口,轉發後端容器端口 targetPort: 80 # 標籤選擇器,經過標籤匹配關聯的pod selector: # 標籤 app: nginx
一、建立service yml文件api
apiVersion: v1 kind: Service metadata: name: my-service namespace: default spec: clusterIP: 10.0.0.123 ports: - name: http port: 80 protocol: TCP targetPort: 80 selector: app: nginx
二、建立service網絡
kubectl apply -f service.yaml
三、查看建立的serviceapp
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
my-service ClusterIP 10.0.0.123 <none> 80/TCP 36s
四、動態感知查看分配到的Node負載均衡
NAME ENDPOINTS AGE kubernetes 192.168.1.108:6443,192.168.1.109:6443 2d16h my-service <none> 2m54s nginx-service 172.17.1.2:80,172.17.1.3:80,172.17.1.6:80 44h nginx-service2 <none> 17h
注:每一個service對應一個ENDPOINTS控制器,service則用來關聯pod。ide
五、查看ENDPOINTS詳細信息spa
Name: my-service Namespace: default Labels: <none> Annotations: kubectl.kubernetes.io/last-applied-configuration: {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"my-service","namespace":"default"},"spec":{"clusterIP":"10.0.0.12... Selector: app=nginx Type: ClusterIP IP: 10.0.0.123 Port: http 80/TCP TargetPort: 80/TCP Endpoints: <none> Session Affinity: None Events: <none>