跳至主要内容

設定 Amazon 雲端提供者

重要:

在 Kubernetes 1.27 及更高版本中,你必須使用 out-of-tree AWS 雲端提供者。In-tree 雲端提供者已被棄用。Amazon 雲端提供者已被完全移除,在升級到 Kubernetes 1.27 後將無法運作。下面列出的步驟仍然需要設定 Amazon 雲端提供者。你可以在建立 IAM 角色和配置 ClusterID 後,設定 out-of-tree 雲端提供者。

你也可以在 Kubernetes 1.26 及更早版本上從 in-tree 遷移到 out-of-tree AWS 雲端提供者。所有現有叢集必須在升級到 v1.27 之前進行遷移,以保持功能正常。

從 Kubernetes 1.23 開始,你必須停用 CSIMigrationAWS 功能閘道才能使用 in-tree AWS 雲端提供者。你可以透過在進階叢集配置中為叢集的 Kubelet、Controller Manager、API Server 和 Scheduler 設定 feature-gates=CSIMigrationAWS=false 作為額外參數來實現此操作。

當你使用 Amazon 作為雲端提供者時,你可以利用以下功能:

  • 負載平衡器: 當你在連接埠對應中選擇 Layer-4 Load Balancer 或啟動 type: LoadBalancer 的 Service 時,啟動 AWS Elastic Load Balancer (ELB)。
  • 持久磁碟區: 使用 AWS Elastic Block Stores (EBS) 作為持久磁碟區。

有關 Amazon 雲端提供者的更多資訊,請參閱 cloud-provider-aws README。

要設定 Amazon 雲端提供者,

  1. 建立 IAM 角色並附加到實例
  2. 配置 ClusterID

1. 建立 IAM 角色並附加到實例​

新增到叢集的所有節點都必須能夠與 EC2 互動,以便它們可以建立和移除資源。你可以透過使用附加到實例的 IAM 角色來啟用此互動。請參閱 Amazon 文件:建立 IAM 角色了解如何建立 IAM 角色。有兩個範例政策:

  • 第一個政策適用於具有 controlplane 角色的節點。這些節點必須能夠建立/移除 EC2 資源。以下 IAM 政策是一個範例,請為你的使用案例移除任何不需要的權限。
  • 第二個政策適用於具有 etcd 或 worker 角色的節點。這些節點只需要能夠從 EC2 檢索資訊。

在建立 Amazon EC2 叢集時,你必須在建立節點範本時填寫已建立的 IAM 角色的 IAM Instance Profile Name(不是 ARN)。

在建立自訂叢集時,你必須手動將 IAM 角色附加到實例。

具有 controlplane 角色的節點的 IAM 政策:

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"autoscaling:DescribeAutoScalingGroups",
"autoscaling:DescribeLaunchConfigurations",
"autoscaling:DescribeTags",
"ec2:DescribeInstances",
"ec2:DescribeRegions",
"ec2:DescribeRouteTables",
"ec2:DescribeSecurityGroups",
"ec2:DescribeSubnets",
"ec2:DescribeVolumes",
"ec2:CreateSecurityGroup",
"ec2:CreateTags",
"ec2:CreateVolume",
"ec2:ModifyInstanceAttribute",
"ec2:ModifyVolume",
"ec2:AttachVolume",
"ec2:AuthorizeSecurityGroupIngress",
"ec2:CreateRoute",
"ec2:DeleteRoute",
"ec2:DeleteSecurityGroup",
"ec2:DeleteVolume",
"ec2:DetachVolume",
"ec2:RevokeSecurityGroupIngress",
"ec2:DescribeVpcs",
"elasticloadbalancing:AddTags",
"elasticloadbalancing:AttachLoadBalancerToSubnets",
"elasticloadbalancing:ApplySecurityGroupsToLoadBalancer",
"elasticloadbalancing:CreateLoadBalancer",
"elasticloadbalancing:CreateLoadBalancerPolicy",
"elasticloadbalancing:CreateLoadBalancerListeners",
"elasticloadbalancing:ConfigureHealthCheck",
"elasticloadbalancing:DeleteLoadBalancer",
"elasticloadbalancing:DeleteLoadBalancerListeners",
"elasticloadbalancing:DescribeLoadBalancers",
"elasticloadbalancing:DescribeLoadBalancerAttributes",
"elasticloadbalancing:DetachLoadBalancerFromSubnets",
"elasticloadbalancing:DeregisterInstancesFromLoadBalancer",
"elasticloadbalancing:ModifyLoadBalancerAttributes",
"elasticloadbalancing:RegisterInstancesWithLoadBalancer",
"elasticloadbalancing:SetLoadBalancerPoliciesForBackendServer",
"elasticloadbalancing:AddTags",
"elasticloadbalancing:CreateListener",
"elasticloadbalancing:CreateTargetGroup",
"elasticloadbalancing:DeleteListener",
"elasticloadbalancing:DeleteTargetGroup",
"elasticloadbalancing:DescribeListeners",
"elasticloadbalancing:DescribeLoadBalancerPolicies",
"elasticloadbalancing:DescribeTargetGroups",
"elasticloadbalancing:DescribeTargetHealth",
"elasticloadbalancing:ModifyListener",
"elasticloadbalancing:ModifyTargetGroup",
"elasticloadbalancing:RegisterTargets",
"elasticloadbalancing:SetLoadBalancerPoliciesOfListener",
"iam:CreateServiceLinkedRole",
"kms:DescribeKey"
],
"Resource": [
"*"
]
}
]
}

具有 etcd 或 worker 角色的節點的 IAM 政策:

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ec2:DescribeInstances",
"ec2:DescribeRegions",
"ecr:GetAuthorizationToken",
"ecr:BatchCheckLayerAvailability",
"ecr:GetDownloadUrlForLayer",
"ecr:GetRepositoryPolicy",
"ecr:DescribeRepositories",
"ecr:ListImages",
"ecr:BatchGetImage"
],
"Resource": "*"
}
]
}

2. 配置 ClusterID​

以下資源需要使用 ClusterID 進行標記:

  • 節點: 在 Rancher 中新增的所有主機。
  • 子網路: 用於你的叢集的子網路。
  • 安全群組: 用於你的叢集的安全群組。
備註

不要標記多個安全群組。標記多個群組會在建立 Elastic Load Balancer (ELB) 時產生錯誤。

當你建立 Amazon EC2 叢集時,會自動為建立的節點配置 ClusterID。其他資源仍需要手動標記。

使用以下標籤:

Key = kubernetes.io/cluster/<cluster-id> Value = owned

將標籤的值設定為 owned 會告訴叢集,所有具有此標籤的資源都由此叢集擁有和管理。

如果你在叢集之間共享資源,可以將標籤變更為:

Key = kubernetes.io/cluster/<cluster-id> Value = shared。

字串值 <cluster-id> 是 Kubernetes 叢集的 ID。

備註

不要為資源標記多個 owned 或 shared 標籤。

使用 Amazon Elastic Container Registry (ECR)​

當 建立 IAM 角色並附加到實例中提到的 IAM 設定檔附加到實例時,kubelet 元件具有自動取得 ECR 憑證的能力。當使用早於 v1.15.0 的 Kubernetes 版本時,需要在叢集中配置 Amazon 雲端提供者。從 Kubernetes 版本 v1.15.0 開始,kubelet 可以在不在叢集中配置 Amazon 雲端提供者的情況下取得 ECR 憑證。

使用 Out-of-Tree AWS 雲端提供者​

  1. 節點命名約定和其他先決條件必須遵循,以便雲端提供者正確找到實例。

  2. Rancher 管理的 RKE2/K3s 叢集不支援配置 providerID。但是,如果在佈建叢集物件上設定以下配置,引擎將正確設定節點名稱:

spec:
rkeConfig:
machineGlobalConfig:
cloud-provider-name: aws

此選項將傳遞給在節點上執行的各種 Kubernetes 元件的配置,並且必須針對每個元件進行覆蓋,以防止 in-tree 提供者無意中執行:

在 Etcd 上覆蓋:

spec:
rkeConfig:
machineSelectorConfig:
- config:
kubelet-arg:
- cloud-provider=external
machineLabelSelector:
matchExpressions:
- key: rke.cattle.io/etcd-role
operator: In
values:
- 'true'

在 Control Plane 上覆蓋:

spec:
rkeConfig:
machineSelectorConfig:
- config:
disable-cloud-controller: true
kube-apiserver-arg:
- cloud-provider=external
kube-controller-manager-arg:
- cloud-provider=external
kubelet-arg:
- cloud-provider=external
machineLabelSelector:
matchExpressions:
- key: rke.cattle.io/control-plane-role
operator: In
values:
- 'true'

在 Worker 上覆蓋:

spec:
rkeConfig:
machineSelectorConfig:
- config:
kubelet-arg:
- cloud-provider=external
machineLabelSelector:
matchExpressions:
- key: rke.cattle.io/worker-role
operator: In
values:
- 'true'
  1. 如果依賴上述機制來設定提供者 ID,請選擇 Amazon。否則,選擇 External (out-of-tree) 雲端提供者,它會為 Kubernetes 元件設定 --cloud-provider=external。

  2. 指定 aws-cloud-controller-manager Helm chart 作為要安裝的額外清單:

spec:
rkeConfig:
additionalManifest: |-
apiVersion: helm.cattle.io/v1
kind: HelmChart
metadata:
name: aws-cloud-controller-manager
namespace: kube-system
spec:
chart: aws-cloud-controller-manager
repo: https://kubernetes.github.io/cloud-provider-aws
targetNamespace: kube-system
bootstrap: true
valuesContent: |-
hostNetworking: true
nodeSelector:
node-role.kubernetes.io/control-plane: "true"
args:
- --configure-cloud-routes=false
- --v=5
- --cloud-provider=aws

從 CLI 安裝 Helm Chart​

官方上游 Helm chart 安裝文件可以在 GitHub 上找到。

  1. 新增 Helm 儲存庫:
helm repo add aws-cloud-controller-manager https://kubernetes.github.io/cloud-provider-aws
helm repo update
  1. 建立一個 values.yaml 檔案,內容如下以覆蓋預設的 values.yaml:
# values.yaml
hostNetworking: true
tolerations:
- effect: NoSchedule
key: node.cloudprovider.kubernetes.io/uninitialized
value: 'true'
- effect: NoSchedule
value: 'true'
key: node-role.kubernetes.io/control-plane
nodeSelector:
node-role.kubernetes.io/control-plane: 'true'
args:
- --configure-cloud-routes=false
- --use-service-account-credentials=true
- --v=2
- --cloud-provider=aws
clusterRoleRules:
- apiGroups:
- ""
resources:
- events
verbs:
- create
- patch
- update
- apiGroups:
- ""
resources:
- nodes
verbs:
- '*'
- apiGroups:
- ""
resources:
- nodes/status
verbs:
- patch
- apiGroups:
- ""
resources:
- services
verbs:
- list
- patch
- update
- watch
- apiGroups:
- ""
resources:
- services/status
verbs:
- list
- patch
- update
- watch
- apiGroups:
- ''
resources:
- serviceaccounts
verbs:
- create
- get
- apiGroups:
- ""
resources:
- persistentvolumes
verbs:
- get
- list
- update
- watch
- apiGroups:
- ""
resources:
- endpoints
verbs:
- create
- get
- list
- watch
- update
- apiGroups:
- coordination.k8s.io
resources:
- leases
verbs:
- create
- get
- list
- watch
- update
- apiGroups:
- ""
resources:
- serviceaccounts/token
verbs:
- create
  1. 安裝 Helm chart:
helm upgrade --install aws-cloud-controller-manager aws-cloud-controller-manager/aws-cloud-controller-manager --values values.yaml

驗證 Helm chart 是否成功安裝:

helm status -n kube-system aws-cloud-controller-manager
  1. (選擇性) 驗證 cloud controller manager 更新是否成功:
kubectl rollout status daemonset -n kube-system aws-cloud-controller-manager

從 UI 安裝 Helm Chart​

  1. 點擊 ☰,然後從左側導覽選擇叢集名稱。

  2. 選擇 Apps > Repositories。

  3. 點擊 Create 按鈕。

  4. 在 Index URL 欄位中輸入 https://kubernetes.github.io/cloud-provider-aws。

  5. 從左側導覽選擇 Apps > Charts 並安裝 aws-cloud-controller-manager。

  6. 選擇命名空間 kube-system,並啟用 Customize Helm options before install。

  7. 新增以下容器參數:

  - '--use-service-account-credentials=true'
- '--configure-cloud-routes=false'
  1. 在 clusterRoleRules 中為 serviceaccounts 資源的 verbs 新增 get。這允許 cloud controller manager 在啟動時取得 service accounts。
  - apiGroups:
- ''
resources:
- serviceaccounts
verbs:
- create
- get
  1. Rancher 佈建的 RKE2 節點被標記為 node-role.kubernetes.io/control-plane。更新 tolerations 和 nodeSelector:
tolerations:
- effect: NoSchedule
key: node.cloudprovider.kubernetes.io/uninitialized
value: 'true'
- effect: NoSchedule
value: 'true'
key: node-role.kubernetes.io/control-plane

nodeSelector:
node-role.kubernetes.io/control-plane: 'true'
備註

目前存在一個已知問題,無法從 Rancher UI 更新 nodeSelector。繼續安裝 chart,然後手動編輯 Daemonset 以設定 nodeSelector:

nodeSelector:
node-role.kubernetes.io/control-plane: 'true'
  1. 安裝 chart 並確認 Daemonset aws-cloud-controller-manager 正在執行。驗證 aws-cloud-controller-manager pods 是否在目標命名空間中執行(除非在步驟 6 中修改,否則為 kube-system)。