天天看點

在Google的GKE上建立支援Internal Load Balancer的Service

在Google的Kubernetes Engine上釋出service,可以采用除On-Promise相同的Cluster IP和NodePort兩種方式外,還可以建立LoadBalaner的Service。

在Google的GKE上建立支援Internal Load Balancer的Service
其中Load Balancer可以調用Google Cloud的接口建立Google的Load Balancer。比如下面這個Nginx-1的service,采用的就是Load Balancer。
在Google的GKE上建立支援Internal Load Balancer的Service
Google Cloud為這個service建立了一個TCP的負載均衡,具體如下:
在Google的GKE上建立支援Internal Load Balancer的Service

但在實際使用場景中,負載均衡會有要求采用内部IP位址的情況,比如backend的cluster。前端調用的時候,采用Internal IP,且這個服務不能暴露到外部網絡。這時,就需要建立的service的IP位址采用内網IP。

可以用下面的指令實作前面的需求:

#在gcloud下,獲得GKE cluster的credential:
gcloud container clusters get-credentials standard-cluster-1 --zone=asia-east1-a

#建立image為nginx的deployment
kubectl run web --image=nginx:latest --port=80

#檢視pods
kubectl get pods

#釋出為Service
kubectl create -f internal.yaml

#檢視Service
kubectl get svc       

Internal.yaml:

apiVersion: v1
kind: Service
metadata:
  annotations:
    cloud.google.com/load-balancer-type: Internal
  labels:
    run: web
  name: web-internal
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    run: web
  sessionAffinity: None
  type: LoadBalancer      

再檢視Service的資訊,可以看到兩個service都是Load Balancer類型的,但一個是公網IP,一個是内網IP。且内網IP是VPC的子網位址網段:

在Google的GKE上建立支援Internal Load Balancer的Service

檢視Service的詳細資訊:

在Google的GKE上建立支援Internal Load Balancer的Service

可以看到也建立了一個Load Balancer,檢視Load Balancer資訊,發現是一個Internal的Load Balancer:

在Google的GKE上建立支援Internal Load Balancer的Service

檢視Internal Load Balancer資訊:

在Google的GKE上建立支援Internal Load Balancer的Service

通過這個Internal Load Balancer位址去通路服務:

在Google的GKE上建立支援Internal Load Balancer的Service

可以看到标準Nginx的歡迎頁面。