Kubernetes 公開外部IP地址以訪問集群中應(yīng)用程序

2022-06-25 09:42 更新

公開外部 IP 地址以訪問集群中應(yīng)用程序

此頁面顯示如何創(chuàng)建公開外部 IP 地址的 Kubernetes 服務(wù)對象。

在開始之前

  • 安裝 ?kubectl?。
  • 使用 Google Kubernetes Engine 或 Amazon Web Services 等云供應(yīng)商創(chuàng)建 Kubernetes 集群。 本教程創(chuàng)建了一個(gè)外部負(fù)載均衡器, 需要云供應(yīng)商。
  • 配置 ?kubectl ?與 Kubernetes API 服務(wù)器通信。有關(guān)說明,請參閱云供應(yīng)商文檔。

教程目標(biāo)

  • 運(yùn)行 Hello World 應(yīng)用程序的五個(gè)實(shí)例。
  • 創(chuàng)建一個(gè)公開外部 IP 地址的 Service 對象。
  • 使用 Service 對象訪問正在運(yùn)行的應(yīng)用程序。

為一個(gè)在五個(gè) pod 中運(yùn)行的應(yīng)用程序創(chuàng)建服務(wù)

  1. 在集群中運(yùn)行 Hello World 應(yīng)用程序:
  2. 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/node-hello:1.0
       
            name: hello-world
       
            ports:
       
            - containerPort: 8080
       
    kubectl apply -f https://k8s.io/examples/service/load-balancer-example.yaml
    

    前面的命令創(chuàng)建一個(gè) Deployment 對象和一個(gè)關(guān)聯(lián)的 ReplicaSet 對象。 ReplicaSet 有五個(gè) Pods, 每個(gè)都運(yùn)行 Hello World 應(yīng)用程序。

  3. 顯示有關(guān) Deployment 的信息:
  4. kubectl get deployments hello-world
    kubectl describe deployments hello-world
  5. 顯示有關(guān) ReplicaSet 對象的信息:
  6. kubectl get replicasets
    kubectl describe replicasets
  7. 創(chuàng)建公開 Deployment 的 Service 對象:
  8. kubectl expose deployment hello-world --type=LoadBalancer --name=my-service
    
  9. 顯示有關(guān) Service 的信息:
  10. 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

    提示:?type=LoadBalancer? 服務(wù)由外部云服務(wù)提供商提供支持,本例中不包含此部分。

    提示:如果外部 IP 地址顯示為 <pending>,請等待一分鐘再次輸入相同的命令。

  11. 顯示有關(guān) Service 的詳細(xì)信息:
  12. 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>

    記下服務(wù)公開的外部 IP 地址(?LoadBalancer Ingress?)。 在本例中,外部 IP 地址是 104.198.205.71。還要注意 ?Port ?和 ?NodePort ?的值。 在本例中,?Port ?是 8080,?NodePort ?是 32377。

  13. 在前面的輸出中,你可以看到服務(wù)有幾個(gè)端點(diǎn): 10.0.0.6:8080、10.0.1.6:8080、10.0.1.7:8080 和另外兩個(gè), 這些都是正在運(yùn)行 Hello World 應(yīng)用程序的 Pod 的內(nèi)部地址。 要驗(yàn)證這些是 Pod 地址,請輸入以下命令:
  14. 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
  15. 使用外部 IP 地址(?LoadBalancer Ingress?)訪問 Hello World 應(yīng)用程序:
  16. curl http://<external-ip>:<port>
    

    其中 ?<external-ip>? 是你的服務(wù)的外部 IP 地址(?LoadBalancer Ingress?),? <port>? 是你的服務(wù)描述中的 ?port ?的值。 如果你正在使用 minikube,輸入 ?minikube service my-service? 將在瀏覽器中自動(dòng)打開 Hello World 應(yīng)用程序。

    成功請求的響應(yīng)是一條問候消息:

    Hello Kubernetes!
    

清理

要?jiǎng)h除 Service,請輸入以下命令:

kubectl delete services my-service

要?jiǎng)h除正在運(yùn)行 Hello World 應(yīng)用程序的 Deployment,ReplicaSet 和 Pod,請輸入以下命令:

kubectl delete deployment hello-world


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號