公開外部 IP 地址以訪問叢集中的應用程式
本頁面展示瞭如何建立一個暴露外部 IP 地址的 Kubernetes Service 物件。
準備工作
- 安裝 kubectl。
- 使用 Google Kubernetes Engine 或 Amazon Web Services 等雲提供商來建立一個 Kubernetes 叢集。本教程將建立一個外部負載均衡器,這需要雲提供商。
- 配置
kubectl
以與你的 Kubernetes API 伺服器通訊。有關說明,請參閱你的雲提供商的文件。
目標
- 執行五個 Hello World 應用程式例項。
- 建立一個暴露外部 IP 地址的 Service 物件。
- 使用 Service 物件訪問正在執行的應用程式。
為執行在五個 Pod 中的應用程式建立 Service
在叢集中執行 Hello World 應用程式
apiVersion: apps/v1 kind: Deployment metadata: labels: app.kubernetes.io/name: load-balancer-example name: hello-world spec: replicas: 5 selector: matchLabels: app.kubernetes.io/name: load-balancer-example template: metadata: labels: app.kubernetes.io/name: load-balancer-example spec: containers: - image: gcr.io/google-samples/hello-app:2.0 name: hello-world ports: - containerPort: 8080
kubectl apply -f https://k8s.io/examples/service/load-balancer-example.yaml
上述命令建立一個Deployment 和一個關聯的ReplicaSet。該 ReplicaSet 有五個Pod,每個 Pod 都執行 Hello World 應用程式。
顯示有關 Deployment 的資訊
kubectl get deployments hello-world kubectl describe deployments hello-world
顯示 ReplicaSet 物件的資訊
kubectl get replicasets kubectl describe replicasets
建立一個暴露 Deployment 的 Service 物件
kubectl expose deployment hello-world --type=LoadBalancer --name=my-service
顯示 Service 的資訊
kubectl get services my-service
輸出類似於:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE my-service LoadBalancer 10.3.245.137 104.198.205.71 8080/TCP 54s
注意
如果外部 IP 地址顯示為 <pending>,請等待一分鐘,然後再次輸入相同的命令。顯示 Service 的詳細資訊
kubectl describe services my-service
輸出類似於:
Name: my-service Namespace: default Labels: app.kubernetes.io/name=load-balancer-example Annotations: <none> Selector: app.kubernetes.io/name=load-balancer-example Type: LoadBalancer IP: 10.3.245.137 LoadBalancer Ingress: 104.198.205.71 Port: <unset> 8080/TCP NodePort: <unset> 32377/TCP Endpoints: 10.0.0.6:8080,10.0.1.6:8080,10.0.1.7:8080 + 2 more... Session Affinity: None Events: <none>
記下你的服務暴露的外部 IP 地址 (
LoadBalancer Ingress
)。在本例中,外部 IP 地址為 104.198.205.71。還要注意Port
和NodePort
的值。在本例中,Port
為 8080,NodePort
為 32377。在上述輸出中,你可以看到該服務有幾個端點:10.0.0.6:8080,10.0.1.6:8080,10.0.1.7:8080 + 另外 2 個。這些是執行 Hello World 應用程式的 Pod 的內部地址。要驗證這些是 Pod 地址,請輸入此命令
kubectl get pods --output=wide
輸出類似於:
NAME ... IP NODE hello-world-2895499144-1jaz9 ... 10.0.1.6 gke-cluster-1-default-pool-e0b8d269-1afc hello-world-2895499144-2e5uh ... 10.0.1.8 gke-cluster-1-default-pool-e0b8d269-1afc hello-world-2895499144-9m4h1 ... 10.0.0.6 gke-cluster-1-default-pool-e0b8d269-5v7a hello-world-2895499144-o4z13 ... 10.0.1.7 gke-cluster-1-default-pool-e0b8d269-1afc hello-world-2895499144-segjf ... 10.0.2.5 gke-cluster-1-default-pool-e0b8d269-cpuc
使用外部 IP 地址 (
LoadBalancer Ingress
) 訪問 Hello World 應用程式curl http://<external-ip>:<port>
其中
<external-ip>
是你的服務的外部 IP 地址 (LoadBalancer Ingress
),<port>
是你的服務描述中Port
的值。如果你正在使用 minikube,輸入minikube service my-service
將自動在瀏覽器中開啟 Hello World 應用程式。成功請求的響應是 hello 訊息
Hello, world! Version: 2.0.0 Hostname: 0bd46b45f32f
清理
要刪除 Service,請輸入此命令
kubectl delete services my-service
要刪除執行 Hello World 應用程式的 Deployment、ReplicaSet 和 Pod,請輸入此命令
kubectl delete deployment hello-world
下一步
瞭解更多關於使用服務連線應用程式的資訊。