【K8S】Kubernetes中暴露外部IP地址來訪問集群中的應用


本文是Kubernetes.io官方文檔中介紹如何創建暴露外部IP地址的Kubernetes Service 對象。

學習目標

  • 運行Hello World應用程序的五個實例。
  • 創建一個暴露外部IP地址的Service對象。
  • 使用Service對象訪問正在運行的應用程序。

准備工作

  • 安裝kubectl
  • 使用Google提供商(如Google Container Engine或Amazon Web Services)創建Kubernetes群集。本教程創建一個 外部負載均衡器,它需要一個雲提供商。
  • 配置kubectl與Kubernetes API服務器通信。有關說明,請參閱雲提供商的文檔。

在五個pod中運行的應用程序創建一個Service

1、在群集中運行Hello World應用程序:

kubectl run hello-world --replicas=5 --labels="run=load-balancer-example" --image=gcr.io/google-samples/node-hello:1.0  --port=8080

以上命令創建一個 Deployment 對象和一個關聯的 ReplicaSet 對象。ReplicaSet 有五個 Pods,每個Pods都運行Hello World應用程序。

2、顯示有關Deployment的信息:

kubectl get deployments hello-world
kubectl describe deployments hello-world

3、顯示有關ReplicaSet對象的信息:

kubectl get replicasets
kubectl describe replicasets

4、使用deployment創建暴露的Service對象

kubectl expose deployment hello-world --type=LoadBalancer --name=my-service

5、顯示有關Service的信息:

kubectl get services my-service

輸出:

NAME         CLUSTER-IP     EXTERNAL-IP      PORT(S)    AGE
my-service   10.3.245.137   104.198.205.71   8080/TCP   54s

注意:如果外部IP地址顯示為 ,請等待一分鍾再次輸入相同的命令。

6、顯示Service有關詳細信息:

kubectl describe services my-service

輸出:

Name:           my-service
Namespace:      default
Labels:         run=load-balancer-example
Selector:       run=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:

記錄Service公開的外部IP地址。在此例子中,外部IP地址為104.198.205.71。還要注意Port的值。在這個例子中,端口是8080。

7、在上面的輸出中,您可以看到該服務有多個端點:10.0.0.6:8080,10.0.1.6:8080,10.0.1.7:8080 + 2 more…。這些是運行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 ...  0.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

8、使用外部IP地址訪問Hello World應用程序:

curl http://<external-ip>:<port>

是你Service的外部IP地址,並且 是Port你的Service描述中的值。

對成功請求的響應是一個hello消息:

Hello Kubernetes!

刪除方法

要刪除服務,請輸入以下命令:

kubectl delete services my-service

要刪除Deployment,ReplicaSet和運行Hello World應用程序的Pods,請輸入以下命令:

kubectl delete deployment hello-world

本文轉自中文社區-Kubernetes中暴露外部IP地址來訪問集群中的應用


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM