EKS Fargate 支援 - Eksctl 使用者指南

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

EKS Fargate 支援

AWS Fargate 是 Amazon ECS 的受管運算引擎,可執行容器。在 Fargate 中,您不需要管理伺服器或叢集。

Amazon EKS 現在可以在 AWS Fargate 上啟動 Pod。如此一來,您就不必擔心如何為 Pod 佈建或管理基礎設施,也能更輕鬆地在 AWS 上建置和執行高效能的 Kubernetes 應用程式。

使用 Fargate 支援建立叢集

您可以使用下列方式新增具有 Fargate 支援的叢集:

eksctl create cluster --fargate [ℹ] eksctl version 0.11.0 [ℹ] using region ap-northeast-1 [ℹ] setting availability zones to [ap-northeast-1a ap-northeast-1d ap-northeast-1c] [ℹ] subnets for ap-northeast-1a - public:192.168.0.0/19 private:192.168.96.0/19 [ℹ] subnets for ap-northeast-1d - public:192.168.32.0/19 private:192.168.128.0/19 [ℹ] subnets for ap-northeast-1c - public:192.168.64.0/19 private:192.168.160.0/19 [ℹ] nodegroup "ng-dba9d731" will use "ami-02e124a380df41614" [AmazonLinux2/1.14] [ℹ] using Kubernetes version 1.14 [ℹ] creating EKS cluster "ridiculous-painting-1574859263" in "ap-northeast-1" region [ℹ] will create 2 separate CloudFormation stacks for cluster itself and the initial nodegroup [ℹ] if you encounter any issues, check CloudFormation console or try 'eksctl utils describe-stacks --region=ap-northeast-1 --cluster=ridiculous-painting-1574859263' [ℹ] CloudWatch logging will not be enabled for cluster "ridiculous-painting-1574859263" in "ap-northeast-1" [ℹ] you can enable it with 'eksctl utils update-cluster-logging --enable-types={SPECIFY-YOUR-LOG-TYPES-HERE (e.g. all)} --region=ap-northeast-1 --cluster=ridiculous-painting-1574859263' [ℹ] Kubernetes API endpoint access will use default of {publicAccess=true, privateAccess=false} for cluster "ridiculous-painting-1574859263" in "ap-northeast-1" [ℹ] 2 sequential tasks: { create cluster control plane "ridiculous-painting-1574859263", create nodegroup "ng-dba9d731" } [ℹ] building cluster stack "eksctl-ridiculous-painting-1574859263-cluster" [ℹ] deploying stack "eksctl-ridiculous-painting-1574859263-cluster" [ℹ] building nodegroup stack "eksctl-ridiculous-painting-1574859263-nodegroup-ng-dba9d731" [ℹ] --nodes-min=2 was set automatically for nodegroup ng-dba9d731 [ℹ] --nodes-max=2 was set automatically for nodegroup ng-dba9d731 [ℹ] deploying stack "eksctl-ridiculous-painting-1574859263-nodegroup-ng-dba9d731" [✔] all EKS cluster resources for "ridiculous-painting-1574859263" have been created [✔] saved kubeconfig as "/Users/marc/.kube/config" [ℹ] adding identity "arn:aws:iam::123456789012:role/eksctl-ridiculous-painting-157485-NodeInstanceRole-104DXUJOFDPO5" to auth ConfigMap [ℹ] nodegroup "ng-dba9d731" has 0 node(s) [ℹ] waiting for at least 2 node(s) to become ready in "ng-dba9d731" [ℹ] nodegroup "ng-dba9d731" has 2 node(s) [ℹ] node "ip-192-168-27-156.ap-northeast-1.compute.internal" is ready [ℹ] node "ip-192-168-95-177.ap-northeast-1.compute.internal" is ready [ℹ] creating Fargate profile "default" on EKS cluster "ridiculous-painting-1574859263" [ℹ] created Fargate profile "default" on EKS cluster "ridiculous-painting-1574859263" [ℹ] kubectl command should work with "/Users/marc/.kube/config", try 'kubectl get nodes' [✔] EKS cluster "ridiculous-painting-1574859263" in "ap-northeast-1" region is ready

此命令將已建立叢集和 Fargate 設定檔。此設定檔包含 AWS 在 Fargate 中執行個體化 Pod 所需的特定資訊。這些時間為:

  • Pod 執行角色,以定義執行 Pod 所需的許可,以及執行 Pod 的聯網位置 (子網路)。這可讓相同的聯網和安全性許可套用至多個 Fargate Pod,並更輕鬆地將叢集上的現有 Pod 遷移至 Fargate。

  • 選取器以定義哪些 Pod 應在 Fargate 上執行。這是由 namespace和 組成labels

未指定設定檔時,但使用--fargate預設 Fargate 設定檔啟用 Fargate 支援。此設定檔以 defaultkube-system 命名空間為目標,因此這些命名空間中的 Pod 將在 Fargate 上執行。

您可以使用下列命令來檢查已建立的 Fargate 設定檔:

eksctl get fargateprofile --cluster ridiculous-painting-1574859263 -o yaml - name: fp-default podExecutionRoleARN: arn:aws:iam::123456789012:role/eksctl-ridiculous-painting-1574859263-ServiceRole-EIFQOH0S1GE7 selectors: - namespace: default - namespace: kube-system subnets: - subnet-0b3a5522f3b48a742 - subnet-0c35f1497067363f3 - subnet-0a29aa00b25082021

若要進一步了解選取器,請參閱設計 Fargate 設定檔

使用組態檔案建立具有 Fargate 支援的叢集

下列組態檔案會使用由一個 EC2 m5.large執行個體和兩個 Fargate 設定檔組成的節點群組宣告 EKS 叢集。defaultkube-system 命名空間中定義的所有 Pod 都會在 Fargate 上執行。dev 命名空間中也有標籤的所有 Pod dev=passed也會在 Fargate 上執行。任何其他 Pod 將在 的節點上排程ng-1

# An example of ClusterConfig with a normal nodegroup and a Fargate profile. --- apiVersion: eksctl.io/v1alpha5 kind: ClusterConfig metadata: name: fargate-cluster region: ap-northeast-1 nodeGroups: - name: ng-1 instanceType: m5.large desiredCapacity: 1 fargateProfiles: - name: fp-default selectors: # All workloads in the "default" Kubernetes namespace will be # scheduled onto Fargate: - namespace: default # All workloads in the "kube-system" Kubernetes namespace will be # scheduled onto Fargate: - namespace: kube-system - name: fp-dev selectors: # All workloads in the "dev" Kubernetes namespace matching the following # label selectors will be scheduled onto Fargate: - namespace: dev labels: env: dev checks: passed
eksctl create cluster -f cluster-fargate.yaml [ℹ] eksctl version 0.11.0 [ℹ] using region ap-northeast-1 [ℹ] setting availability zones to [ap-northeast-1c ap-northeast-1a ap-northeast-1d] [ℹ] subnets for ap-northeast-1c - public:192.168.0.0/19 private:192.168.96.0/19 [ℹ] subnets for ap-northeast-1a - public:192.168.32.0/19 private:192.168.128.0/19 [ℹ] subnets for ap-northeast-1d - public:192.168.64.0/19 private:192.168.160.0/19 [ℹ] nodegroup "ng-1" will use "ami-02e124a380df41614" [AmazonLinux2/1.14] [ℹ] using Kubernetes version 1.14 [ℹ] creating EKS cluster "fargate-cluster" in "ap-northeast-1" region with Fargate profile and un-managed nodes [ℹ] 1 nodegroup (ng-1) was included (based on the include/exclude rules) [ℹ] will create a CloudFormation stack for cluster itself and 1 nodegroup stack(s) [ℹ] will create a CloudFormation stack for cluster itself and 0 managed nodegroup stack(s) [ℹ] if you encounter any issues, check CloudFormation console or try 'eksctl utils describe-stacks --region=ap-northeast-1 --cluster=fargate-cluster' [ℹ] CloudWatch logging will not be enabled for cluster "fargate-cluster" in "ap-northeast-1" [ℹ] you can enable it with 'eksctl utils update-cluster-logging --enable-types={SPECIFY-YOUR-LOG-TYPES-HERE (e.g. all)} --region=ap-northeast-1 --cluster=fargate-cluster' [ℹ] Kubernetes API endpoint access will use default of {publicAccess=true, privateAccess=false} for cluster "fargate-cluster" in "ap-northeast-1" [ℹ] 2 sequential tasks: { create cluster control plane "fargate-cluster", create nodegroup "ng-1" } [ℹ] building cluster stack "eksctl-fargate-cluster-cluster" [ℹ] deploying stack "eksctl-fargate-cluster-cluster" [ℹ] building nodegroup stack "eksctl-fargate-cluster-nodegroup-ng-1" [ℹ] --nodes-min=1 was set automatically for nodegroup ng-1 [ℹ] --nodes-max=1 was set automatically for nodegroup ng-1 [ℹ] deploying stack "eksctl-fargate-cluster-nodegroup-ng-1" [✔] all EKS cluster resources for "fargate-cluster" have been created [✔] saved kubeconfig as "/home/user1/.kube/config" [ℹ] adding identity "arn:aws:iam::123456789012:role/eksctl-fargate-cluster-nod-NodeInstanceRole-42Q80B2Z147I" to auth ConfigMap [ℹ] nodegroup "ng-1" has 0 node(s) [ℹ] waiting for at least 1 node(s) to become ready in "ng-1" [ℹ] nodegroup "ng-1" has 1 node(s) [ℹ] node "ip-192-168-71-83.ap-northeast-1.compute.internal" is ready [ℹ] creating Fargate profile "fp-default" on EKS cluster "fargate-cluster" [ℹ] created Fargate profile "fp-default" on EKS cluster "fargate-cluster" [ℹ] creating Fargate profile "fp-dev" on EKS cluster "fargate-cluster" [ℹ] created Fargate profile "fp-dev" on EKS cluster "fargate-cluster" [ℹ] "coredns" is now schedulable onto Fargate [ℹ] "coredns" is now scheduled onto Fargate [ℹ] "coredns" is now scheduled onto Fargate [ℹ] "coredns" pods are now scheduled onto Fargate [ℹ] kubectl command should work with "/home/user1/.kube/config", try 'kubectl get nodes' [✔] EKS cluster "fargate-cluster" in "ap-northeast-1" region is ready

設計 Fargate 設定檔

每個選取器項目最多有兩個元件:命名空間和索引鍵/值對清單。建立選取器項目只需要 命名空間元件。所有規則 (命名空間、索引鍵值對) 必須套用至 Pod,以符合選取器項目。Pod 只需要符合要在設定檔上執行的一個選取器項目。符合選取器欄位中所有條件的任何 Pod 都會排程在 Fargate 上執行。任何不符合列入允許清單的命名空間,但使用者手動設定排程器的 Pod:提交的 Fargate-scheduler 會卡在待定狀態,因為它們無權在 Fargate 上執行。

設定檔必須符合下列要求:

  • 每個設定檔都必須選取一個選取器

  • 每個選取器都必須包含命名空間;標籤為選用

範例:在 Fargate 中排程工作負載

若要為上述範例排程 Fargate 上的 Pod,例如,可以建立一個名為 的命名空間,dev並在該處部署工作負載:

kubectl create namespace dev namespace/dev created kubectl run nginx --image=nginx --restart=Never --namespace dev pod/nginx created kubectl get pods --all-namespaces --output wide NAMESPACE NAME READY STATUS AGE IP NODE dev nginx 1/1 Running 75s 192.168.183.140 fargate-ip-192-168-183-140.ap-northeast-1.compute.internal kube-system aws-node-44qst 1/1 Running 21m 192.168.70.246 ip-192-168-70-246.ap-northeast-1.compute.internal kube-system aws-node-4vr66 1/1 Running 21m 192.168.23.122 ip-192-168-23-122.ap-northeast-1.compute.internal kube-system coredns-699bb99bf8-84x74 1/1 Running 26m 192.168.2.95 ip-192-168-23-122.ap-northeast-1.compute.internal kube-system coredns-699bb99bf8-f6x6n 1/1 Running 26m 192.168.90.73 ip-192-168-70-246.ap-northeast-1.compute.internal kube-system kube-proxy-brxhg 1/1 Running 21m 192.168.23.122 ip-192-168-23-122.ap-northeast-1.compute.internal kube-system kube-proxy-zd7s8 1/1 Running 21m 192.168.70.246 ip-192-168-70-246.ap-northeast-1.compute.internal

從最後一個kubectl get pods命令的輸出中,我們可以看到 Pod nginx 已部署在名為 的節點中fargate-ip-192-168-183-140.ap-northeast-1.compute.internal

管理 Fargate 設定檔

若要在 Fargate 上部署 Kubernetes 工作負載,EKS 需要 Fargate 設定檔。如上述範例所示建立叢集時, 會透過建立預設設定檔eksctl來處理此問題。假設已有 叢集,也可以使用 eksctl create fargateprofile命令建立 Fargate 設定檔:

注意

只有在 EKS 平台版本 eks.5 或更新版本上執行的叢集才支援此操作。

注意

如果現有 是在 0.11.0 eksctl之前使用 版本建立,則需要在建立 Fargate 設定檔eksctl upgrade cluster之前執行 。

eksctl create fargateprofile --namespace dev --cluster fargate-example-cluster [ℹ] creating Fargate profile "fp-9bfc77ad" on EKS cluster "fargate-example-cluster" [ℹ] created Fargate profile "fp-9bfc77ad" on EKS cluster "fargate-example-cluster"

您也可以指定要建立的 Fargate 設定檔名稱。此名稱開頭不得為字首 eks-

eksctl create fargateprofile --namespace dev --cluster fargate-example-cluster --name fp-development [ℹ] created Fargate profile "fp-development" on EKS cluster "fargate-example-cluster"

使用此命令搭配 CLI 旗標 eksctl 只能建立具有簡單選擇器的單一 Fargate 設定檔。對於更複雜的選擇器,例如使用更多命名空間,eksctl 支援使用組態檔案:

apiVersion: eksctl.io/v1alpha5 kind: ClusterConfig metadata: name: fargate-example-cluster region: ap-northeast-1 fargateProfiles: - name: fp-default selectors: # All workloads in the "default" Kubernetes namespace will be # scheduled onto Fargate: - namespace: default # All workloads in the "kube-system" Kubernetes namespace will be # scheduled onto Fargate: - namespace: kube-system - name: fp-dev selectors: # All workloads in the "dev" Kubernetes namespace matching the following # label selectors will be scheduled onto Fargate: - namespace: dev labels: env: dev checks: passed
eksctl create fargateprofile -f fargate-example-cluster.yaml [ℹ] creating Fargate profile "fp-default" on EKS cluster "fargate-example-cluster" [ℹ] created Fargate profile "fp-default" on EKS cluster "fargate-example-cluster" [ℹ] creating Fargate profile "fp-dev" on EKS cluster "fargate-example-cluster" [ℹ] created Fargate profile "fp-dev" on EKS cluster "fargate-example-cluster" [ℹ] "coredns" is now scheduled onto Fargate [ℹ] "coredns" pods are now scheduled onto Fargate

若要查看叢集中現有的 Fargate 設定檔:

eksctl get fargateprofile --cluster fargate-example-cluster NAME SELECTOR_NAMESPACE SELECTOR_LABELS POD_EXECUTION_ROLE_ARN SUBNETS fp-9bfc77ad dev <none> arn:aws:iam::123456789012:role/eksctl-fargate-example-cluster-ServiceRole-1T5F78E5FSH79 subnet-00adf1d8c99f83381,subnet-04affb163ffab17d4,subnet-035b34379d5ef5473

若要以 yaml 格式查看它們:

eksctl get fargateprofile --cluster fargate-example-cluster -o yaml - name: fp-9bfc77ad podExecutionRoleARN: arn:aws:iam::123456789012:role/eksctl-fargate-example-cluster-ServiceRole-1T5F78E5FSH79 selectors: - namespace: dev subnets: - subnet-00adf1d8c99f83381 - subnet-04affb163ffab17d4 - subnet-035b34379d5ef5473

json 格式:

eksctl get fargateprofile --cluster fargate-example-cluster -o json [ { "name": "fp-9bfc77ad", "podExecutionRoleARN": "arn:aws:iam::123456789012:role/eksctl-fargate-example-cluster-ServiceRole-1T5F78E5FSH79", "selectors": [ { "namespace": "dev" } ], "subnets": [ "subnet-00adf1d8c99f83381", "subnet-04affb163ffab17d4", "subnet-035b34379d5ef5473" ] } ]

Fargate 設定檔的設計不可變。若要變更某些項目,請使用所需的變更建立新的 Fargate 設定檔,並使用 eksctl delete fargateprofile命令刪除舊的設定檔,如下列範例所示:

eksctl delete fargateprofile --cluster fargate-example-cluster --name fp-9bfc77ad --wait 2019-11-27T19:04:26+09:00 [ℹ] deleting Fargate profile "fp-9bfc77ad" ClusterName: "fargate-example-cluster", FargateProfileName: "fp-9bfc77ad" }

請注意,設定檔刪除程序最多可能需要幾分鐘的時間。未指定--wait旗標時, eksctl 會樂觀地預期刪除設定檔,並在傳送 AWS API 請求後立即傳回 。若要eksctl等待設定檔成功刪除,請使用 ,--wait如上述範例所示。

深入閱讀