Share Your AWS re:Post Experience - Quick 3 Question Survey
Help us improve AWS re:Post! We're interested in understanding how you use re:Post and its impact on your AWS journey. Please take a moment to complete our brief 3-question survey.
如何公開在我的 Amazon EKS 叢集上執行的 Kubernetes 服務?
我想公開在我的 Amazon Elastic Kubernetes Service (Amazon EKS) 叢集上執行的 Kubernetes 服務。
解決方法
若要公開在叢集上執行的 Kubernetes 服務,請先建立範例應用程式。然後,將 ClusterIP、NodePort 或 LoadBalancer Kubernetes ServiceTypes 套用至範例應用程式。如需詳細資訊,請參閱 Kubernetes 網站上的 服務類型。
建立範例應用程式
請完成下列步驟:
-
在 Kubernetes 中定義並套用部署檔案。以下範例命令會建立一個檔案,名稱為 nginx-deployment.yaml,接著會建立增加兩個 nginx Pod 的 ReplicaSet:
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
-
若要建立部署,請執行下列命令:
kubectl apply -f nginx-deployment.yaml
-
若要驗證您的 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,類型: NodePort,然後類型: LoadBalancer上的負載平衡器。
ClusterIP 服務類型
請完成下列步驟:
-
建立一個名為 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
-
在 Kubernetes 中建立 ClusterIP 物件,請使用宣告式或命令式命令。
若要建立物件並套用 clusterip.yaml 檔案,請執行下列宣告式命令:kubectl create -f clusterip.yaml
範例輸出結果:
service/nginx-service-cluster-ip created>/code>
-or-
若要公開 ClusterIP 類型的部署,請執行下列命令式命令:
kubectl expose deployment nginx-deployment --type=ClusterIP --name=nginx-service-cluster-ip
注意:****公開命令會建立服務,但不會建立 YAML 檔案。但是,kubectl 會將您的命令式命令轉換為宣告式 Kubernetes 部署物件。
範例輸出結果:service "nginx-service-cluster-ip" exposed
-
若要取得 CLUSTER-IP 位址,請執行下列命令:
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
-
若要使用 CLUSTER-IP 位址來存取應用程式,請執行下列命令:
curl -silent 10.100.12.153:80 | grep title
**注意:**若要存取服務,您必須登入工作節點或位於 Pod 的容器內。
-
若要刪除 ClusterIP 服務,請執行下列命令:
kubectl delete service nginx-service-cluster-ip
範例輸出結果:
service "nginx-service-cluster-ip" deleted
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
-
使用宣告式或命令式命令,在 Kubernetes 中建立 NodePort 物件。
若要建立物件並套用 nodeport.yaml 檔案,請執行下列宣告式命令:kubectl create -f nodeport.yaml
-or-
若要公開 NodePort 類型的部署,請執行下列命令式命令:
kubectl expose deployment nginx-deployment --type=NodePort --name=nginx-service-nodeport
範例輸出結果:
service/nginx-service-nodeport exposed
-
若要取得有關 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
**重要:**NodePort 建立可在叢集內存取的服務 IP 位址,並使用指定的連接埠在每個節點上顯示服務。上述命令的輸出顯示 NodePort 服務類型會在可用工作節點的 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體的連接埠外部公開。在從叢集外部存取 NodeIP:NodePort 之前,請設定節點的安全群組,以允許透過輸出列出的連接埠允許傳入流量。
-
對於位於公有子網路中的節點,並且可從網際網路進行連線,請檢查節點的公有 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
-or-
對於位於私有子網路中且只能透過虛擬私有雲端 (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
-
若要使用節點 IP 和 NodePort 存取應用程式,請執行下列命令:
curl -silent <Public/PrivateNodeIP>:30994 | grep title
-
若要刪除 NodePort 服務,請執行下列命令:
kubectl delete service nginx-service-nodeport
範例輸出結果:
service "nginx-service-nodeport" deleted
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
-
若要套用 loadbalancer.yaml 檔案,請執行下列命令:
kubectl create -f loadbalancer.yaml
範例輸出結果:
service/nginx-service-loadbalancer created
-or-
若要公開 LoadBalancer 類型的部署,請執行下列命令:
kubectl expose deployment nginx-deployment --type=LoadBalancer --name=nginx-service-loadbalancer
範例輸出結果:
service "nginx-service-loadbalancer" exposed
-
若要取得有關 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
-
若要確認您是否可以外部存取負載平衡器,請執行下列命令:
curl -silent *****.eu-west-1.elb.amazonaws.com:80 | grep title
-
若要刪除 LoadBalancer 服務,請執行下列命令:
kubectl delete service nginx-service-loadbalancer
範例輸出結果:
service "nginx-service-loadbalancer" deleted
**注意:**依預設,LoadBalancer 服務類型會建立 Classic Load Balancer。
若要使用執行個體類型目標建立 Network Load Balancer,請將下列註釋新增至服務清單檔案:
service.beta.kubernetes.io/aws-load-balancer-type: nlb
-or-
若要使用 IP 目標建立 Network Load Balancer,請部署 AWS Load Balancer 控制器,然後建立使用 IP 目標的負載平衡器。
相關內容
- 已提問 2 年前lg...
- 已提問 4 個月前lg...
- 已提問 4 個月前lg...
- 已提問 2 年前lg...
- AWS 官方已更新 7 個月前
- AWS 官方已更新 2 年前
- AWS 官方已更新 2 年前