Skip to main content

Create LoadBalancer Service

under construction

The English version of this document is under construction and will be available soon.

本文提供虛擬 K8s 服務的進階使用範例,包含 Volume 的建立/掛載/刪除以及 Service type 為 LoadBalancer 的相關使用說明。

設定 Auto Scaling

詳細說明請參考 Kubernetes Documentation - Horizontal Pod Autoscaling


使用 Kubernetes Service,Type: LoadBalancer

LoadBalancer 是 Service 類型中的一種,它是將應用程式公開到網路的典型方式,依賴雲端提供者創建具有相關網路空間中的 IP 位址的外部負載平衡器。然後定向到該 IP 位址的流量都會轉送到應用程式的服務以將應用程式公開到網際網路。

以下範例將建立一個 echo server pod,接著建立 service type=Loadbalancer 並將 service 指向 echo server pod。使用者即可使用 curl 指令透過 loadbalancer 從網際網路連線至 echo server pod。

Step 1. 建立 echo server pod

kubectl run echoserver --image=gcr.io/google-containers/echoserver:1.10 --port=8080

Step 2. 建立服務

  • 建立 Service 為 loadbalanced-service
  • type 為 Loadbalancer
  • 服務指向 echo server pod
  • 設定 pod port 為 8080
  • 設定 loadbalancer 對外 pod 為 80
cat <<EOF | kubectl apply -f -
---
kind: Service
apiVersion: v1
metadata:
name: loadbalanced-service
spec:
selector:
run: echoserver
type: LoadBalancer
ports:
- port: 80
targetPort: 8080
protocol: TCP
EOF

  • 使用以下指令,直到新建立的 Service 狀態不再為 pending 且產生 EXTERNL-IP
kubectl get service loadbalanced-service

  • 使用 curl 指令測試是否可以由外部連至開啟的服務
curl {EXTERNAL-IP:80}