天天看點

Kubernetes - 5.1 Discovery and Load Balancing - Service

什麼是Service

Service是為一組Pod提供入口排程服務,并可以在Pod之間實作負載均衡。由于Pod是有生命周期的,每當Pod進行銷毀而啟動時IP位址也會随之改變,這将無法被調用者發現到,而Service出新就是為了解決服務發現這個問題,提供一個穩定的入口已便于服務調用者,而不用去關心Pod的IP位址變化。

什麼是EndPoint

Service會根據資源定義清單中的選擇器選擇與之綁定的Pod成員,EndPoint就是存儲這些Pod成員的IP及端口資訊。

Service類型

ClusterIP

将Service提供一個穩定的IP位址供Kubernetes内部的資源對象通路

kubectl apply -f service-clusterip.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  type: ClusterIP
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80           

檢視service詳細資訊

kubectl get service nginx-service -oyaml

Kubernetes - 5.1 Discovery and Load Balancing - Service

kubectl describe service nginx-service

Kubernetes - 5.1 Discovery and Load Balancing - Service
NodePort

将Service的Port暴露到Node上,并可以指定Node上的暴露Port (端口範圍30000-32767)

kubectl apply -f service-nodeport.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  type: NodePort
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
    nodePort: 30000           

kubectl get service nginx-service -o yaml

Kubernetes - 5.1 Discovery and Load Balancing - Service

kubectl describe service nginx-service

Kubernetes - 5.1 Discovery and Load Balancing - Service
LoadBalancer

将Service暴露在雲産商提供的負載均衡服務

kubectl apply -f service-loadbalance.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  type: LoadBalancer
  ports:
  - port: 80
    targetPort: 80           

kubectl get service nginx-service -o yaml

Kubernetes - 5.1 Discovery and Load Balancing - Service

kubectl describe service nginx-service

Kubernetes - 5.1 Discovery and Load Balancing - Service
ExternalName

将Service映射成外部的DNS名稱,請求該Service時将會重定向到外部别名。

kubectl apply -f service-externalname.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  type: ExternalName
  externalName: example.com           

kubectl get service nginx-service -o yaml

Kubernetes - 5.1 Discovery and Load Balancing - Service

kubectl describe service nginx-service

Kubernetes - 5.1 Discovery and Load Balancing - Service
Headless

将Service不配置設定ClusterIP,直接将EndPoint清單傳回,讓用戶端自行決定通路哪一個Pod

kubectl apply -f service-headless.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  clusterIP: None
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80           

kubectl get service nginx-service -oyaml

Kubernetes - 5.1 Discovery and Load Balancing - Service

kubectl describe service nginx-service

Kubernetes - 5.1 Discovery and Load Balancing - Service

Service基本操作

檢視Service清單

kubectl get service -o yaml | kubectl get service -o wide

Kubernetes - 5.1 Discovery and Load Balancing - Service

通過kubectl create建立Service

kubectl create service clusterip nginx--tcp=80:80

通過kubectl expose建立Service

kubectl expose deployment nginx-deployment --type=ClusterIP --name=nginx-service --port=80 --target-port=80

通過kubectl delete删除Service

kubectl delete service [serviceName]

kubectl delete -f service-clusterip.yaml

使用技巧

  1. Kubernetes Service中不建議使用NodePort去指定某一個IP,這樣容易造成端口綁定沖突,最好是由系統自動去配置設定端口。
  2. Kubernetes 服務調用請通過Service去指定,直接通過Pod IP的方式将容易造成問題,因為Pod在重新開機後會丢失原有的狀态。

繼續閱讀