如何公開在我的 Amazon EKS 叢集上執行的 Kubernetes 服務?

4 分的閱讀內容
0

我想公開在我的 Amazon Elastic Kubernetes Service (Amazon EKS) 叢集上執行的 Kubernetes 服務。

簡短說明

若要公開在叢集上執行的 Kubernetes 服務,請先建立範例應用程式。然後,將 ClusterIPNodePortLoadBalancer Kubernetes ServiceTypes 套用至範例應用程式。

請記住以下詳細資訊:

  • ClusterIP 會在叢集的內部 IP 地址上公開服務。
  • NodePort 會在每個節點位於靜態連接埠的 IP 地址上公開服務。
  • LoadBalancer 會使用負載平衡器從外部公開服務。

**注意:**Amazon EKS 支援 Network Load Balancer 和 Classic Load Balancer,適用於在 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體工作節點上執行的 Pod。Amazon EKS 使用 LoadBalancer 來提供此支援。您可以將網路流量負載平衡至 Network Load Balancer (執行個體或 IP 目標) 或 Classic Load Balancer (僅限執行個體目標)。

解決方法

建立範例應用程式

1.    定義和套用部署檔案。以下範例會建立可啟動兩個 nginx Pod 的 ReplicaSet,並建立一個名為 nginx-deployment.yaml 的欄位。

cat <<EOF > nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
EOF

2.    建立部署:

kubectl apply -f nginx-deployment.yaml

3.    確認您的 Pod 正在執行,且各自擁有自己的內部 IP 地址:

kubectl get pods -l 'app=nginx' -o wide | awk {'print $1" " $3 " " $6'} | column -t

輸出:

NAME                               STATUS   IP
nginx-deployment-574b87c764-hcxdg  Running  192.168.20.8
nginx-deployment-574b87c764-xsn9s  Running  192.168.53.240

建立 ClusterIP 服務

1.    建立一個名為 clusterip.yaml 的檔案,然後將類型設定為 ClusterIP。例如:

cat <<EOF > clusterip.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-service-cluster-ip
spec:
  type: ClusterIP
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
EOF

2.    使用宣告式或命令式命令,在 Kubernetes 中建立 ClusterIP 物件。

若要建立物件並套用 clusterip.yaml 檔案,請執行下列宣告式命令:

kubectl create -f clusterip.yaml

輸出:

service/nginx-service-cluster-ip created
  • 或 -

若要公開 ClusterIP 類型的部署,請執行下列命令式命令:

kubectl expose deployment nginx-deployment  --type=ClusterIP  --name=nginx-service-cluster-ip

輸出:

service "nginx-service-cluster-ip" exposed

注意:公開命令會建立服務,而不會建立 YAML 檔案。但是,kubectl 會將您的命令式命令轉換為宣告式 Kubernetes 部署物件。

3.    存取應用程式並取得 ClusterIP 號碼:

kubectl get service nginx-service-cluster-ip

輸出:

NAME                       TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
nginx-service-cluster-ip   ClusterIP   10.100.12.153   <none>        80/TCP    23s

4.    刪除 ClusterIP 服務:

kubectl delete service nginx-service-cluster-ip

輸出:

service "nginx-service-cluster-ip" deleted

建立 NodePort 服務

1.    若要建立 NodePort 服務,請建立一個名為 nodeport.yaml 的檔案,然後將類型設定為 NodePort。例如:

cat <<EOF > nodeport.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-service-nodeport
spec:
  type: NodePort
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
EOF

2.    使用宣告式或命令式命令,在 Kubernetes 中建立 NodePort 物件。

若要建立物件並套用 nodeport.yaml 檔案,請執行下列宣告式命令:

kubectl create -f nodeport.yaml
  • 或 -

若要公開 NodePort 類型的部署,請執行下列命令式命令:

kubectl expose deployment nginx-deployment  --type=NodePort  --name=nginx-service-nodeport

輸出:

service/nginx-service-nodeport exposed

3.    取得 nginx-service 的相關資訊:

kubectl get service/nginx-service-nodeport

輸出:

NAME                     TYPE       CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
nginx-service-nodeport   NodePort   10.100.106.151   <none>        80:30994/TCP   27s

**重要事項:**此 ServiceType 是針對服務自動建立的 NodePortClusterIP。前述命令的輸出顯示已從外部在可用工作節點的 EC2 執行個體連接埠 (30994) 上公開 NodePort 服務。從叢集外部存取 NodeIP:NodePort 之前,您必須先將節點的安全群組設定為允許傳入流量。您可以透過前述 kubectl get service 命令的輸出中列出的連接埠 (30994) 來允許傳入流量。

4.    如果節點位於公有子網路中,並且可從網際網路進行連線,請檢查節點的公有 IP 地址:

kubectl get nodes -o wide |  awk {'print $1" " $2 " " $7'} | column -t

輸出:

NAME                                      STATUS  EXTERNAL-IP
ip-10-0-3-226.eu-west-1.compute.internal  Ready   1.1.1.1
ip-10-1-3-107.eu-west-1.compute.internal  Ready   2.2.2.2
  • 或 -

如果節點位於私有子網路中,並且只能在 VPC 內部或透過 VPC 進行連線,請檢查節點的私有 IP 地址:

kubectl get nodes -o wide |  awk {'print $1" " $2 " " $6'} | column -t

輸出:

NAME                                      STATUS  INTERNAL-IP
ip-10-0-3-226.eu-west-1.compute.internal  Ready   10.0.3.226
ip-10-1-3-107.eu-west-1.compute.internal  Ready   10.1.3.107

5.     刪除 NodePort 服務:

kubectl delete service nginx-service-nodeport

輸出:

service "nginx-service-nodeport" deleted

建立 LoadBalancer 服務

1.    若要建立 LoadBalancer 服務,請建立一個名為 loadbalancer.yaml 的檔案,然後將類型設定為 LoadBalancer。例如:

cat <<EOF > loadbalancer.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-service-loadbalancer
spec:
  type: LoadBalancer
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
EOF

2.    套用 loadbalancer.yaml 檔案:

kubectl create -f loadbalancer.yaml

輸出:

service/nginx-service-loadbalancer created
  • 或 -

公開 LoadBalancer 類型的部署:

kubectl expose deployment nginx-deployment  --type=LoadBalancer  --name=nginx-service-loadbalancer

輸出:

service "nginx-service-loadbalancer" exposed

3.    取得 nginx-service 的相關資訊:

kubectl get service/nginx-service-loadbalancer |  awk {'print $1" " $2 " " $4 " " $5'} | column -t

輸出:

NAME                        TYPE          EXTERNAL-IP                        PORT(S)
nginx-service-loadbalancer  LoadBalancer  *****.eu-west-1.elb.amazonaws.com  80:30039/TCP

4.    確認您可以從外部存取負載平衡器:

curl -silent *****.eu-west-1.elb.amazonaws.com:80 | grep title

您應該會在 HTML 標題標籤之間收到下列輸出: 「歡迎使用 nginx!」

5.    刪除 LoadBalancer 服務:

kubectl delete service nginx-service-loadbalancer

輸出:

service "nginx-service-loadbalancer" deleted

**注意:**依預設,前述 LoadBalancer 服務會建立 Classic Load Balancer。

6.    若要使用執行個體類型目標建立 Network Load Balancer,請將下列註釋新增至服務清單檔案:

service.beta.kubernetes.io/aws-load-balancer-type: nlb
  • 或 -

若要使用 IP 目標建立 Network Load Balancer,請部署 AWS Load Balancer 控制器,然後建立使用 IP 目標的負載平衡器


AWS 官方
AWS 官方已更新 2 年前