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

衡子發表於2018-10-17

GoogleKubernetes Engine上釋出service,可以採用除On-Promise相同的Cluster IPNodePort兩種方式外,還可以建立LoadBalanerService

  

其中Load Balancer可以呼叫Google Cloud的介面建立GoogleLoad Balancer。比如下面這個Nginx-1service,採用的就是Load Balancer

 

 

Google Cloud為這個service建立了一個TCP的負載均衡,具體如下:

 

但在實際使用場景中,負載均衡會有要求採用內部IP地址的情況,比如backendcluster。前端呼叫的時候,採用Internal IP,且這個服務不能暴露到外部網路。這時,就需要建立的serviceIP地址採用內網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。且內網IPVPC的子網地址網段:

 

 

 

檢視Service的詳細資訊:

 

 

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

 

檢視Internal Load Balancer資訊:

 

通過這個Internal Load Balancer地址去訪問服務:

 

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

相關文章