設定 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 角色並附加到實例
新增到叢集的所有節點都必須能夠與 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 雲端提供者
- RKE2
- RKE
-
節點命名約定和其他先決條件必須遵循, 以便雲端提供者正確找到實例。
-
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'
-
如果依賴上述機制來設定提供者 ID,請選擇
Amazon。否則,選擇 External (out-of-tree) 雲端提供者,它會為 Kubernetes 元件設定--cloud-provider=external。 -
指定
aws-cloud-controller-managerHelm 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
- 必須遵循節點命名約定和其他先決條件,以便雲端提供者能夠找到實例。Rancher 佈建的叢集不支援配置
providerID。
如果你使用基於 IP 的命名,節點必須以實例名稱後跟區域網域名稱 (ip-xxx-xxx-xxx-xxx.ec2.<region>.internal) 命名。如果你在 DHCP 選項中設定了自訂網域名稱,則必須在 kube-proxy 和 kubelet 上設定 --hostname-override 以符合此命名約定。
為了符合節點命名約定,Rancher 允許在選擇 External Amazon 雲端提供者時設定 useInstanceMetadataHostname。啟用 useInstanceMetadataHostname 將查詢 ec2 metadata service 並將 /hostname 設定為 kubelet 和 kube-proxy 的 hostname-override:
rancher_kubernetes_engine_config:
cloud_provider:
name: external-aws
useInstanceMetadataHostname: true
當為自訂叢集設定 hostname-override 的自訂值時,不得啟用 useInstanceMetadataHostname。當你建立自訂叢集時,在 docker run 節點註冊命令中新增 --node-name 以設定 hostname-override - 例如,"$(hostname -f)"。這可以手動完成,或透過在 Rancher UI 中使用 Show Advanced Options 來新增 Node Name。
- 選擇雲端提供者。
選擇 External Amazon (out-of-tree) 會設定 --cloud-provider=external 並啟用 useInstanceMetadataHostname。如步驟 1 所述,啟用 useInstanceMetadataHostname 將查詢 EC2 metadata service 並將 http://169.254.169.254/latest/meta-data/hostname 設定為 kubelet 和 kube-proxy 的 hostname-override。
當透過 node-name 為自訂叢集設定自訂節點名稱時,必須停用 useInstanceMetadataHostname。
rancher_kubernetes_engine_config:
cloud_provider:
name: external-aws
useInstanceMetadataHostname: true/false
使用 External 雲端提供者的現有叢集將為 Kubernetes 元件設定 --cloud-provider=external,但不會設定節點名稱。
- 在叢集完成佈建後安裝 AWS cloud controller manager。請注意,在你部署 cloud controller manager 之前,叢集不會成功佈建,節點仍處於
uninitialized狀態。這可以手動完成,或透過 UI 中的 Helm charts 完成。
有關 cloud controller manager 的官方 AWS 上游文件,請參閱 cloud controller manager。
從 CLI 安裝 Helm Chart
- RKE2
- RKE
官方上游 Helm chart 安裝文件可以在 GitHub 上找到。
- 新增 Helm 儲存庫:
helm repo add aws-cloud-controller-manager https://kubernetes.github.io/cloud-provider-aws
helm repo update
- 建立一個
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
- 安裝 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
- (選擇性) 驗 證 cloud controller manager 更新是否成功:
kubectl rollout status daemonset -n kube-system aws-cloud-controller-manager
官方上游 Helm chart 安裝文件可以在 GitHub 上找到。
- 新增 Helm 儲存庫:
helm repo add aws-cloud-controller-manager https://kubernetes.github.io/cloud-provider-aws
helm repo update
- 建立一個
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/controlplane
nodeSelector:
node-role.kubernetes.io/controlplane: '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
- 安裝 Helm chart:
helm upgrade --install aws-cloud-controller-manager -n kube-system aws-cloud-controller-manager/aws-cloud-controller-manager --values values.yaml
驗證 Helm chart 是否成功安裝:
helm status -n kube-system aws-cloud-controller-manager
- 如果存在,編輯 Daemonset 以移除預設的節點選擇器
node-role.kubernetes.io/control-plane: "":
kubectl edit daemonset aws-cloud-controller-manager -n kube-system
- (選擇性) 驗證 cloud controller manager 更新是否成功:
kubectl rollout status daemonset -n kube-system aws-cloud-controller-manager
從 UI 安裝 Helm Chart
- RKE2
- RKE
-
點擊 ☰,然後從左側導覽選擇叢集名稱。
-
選擇 Apps > Repositories。
-
點擊 Create 按鈕。
-
在 Index URL 欄位中輸入
https://kubernetes.github.io/cloud-provider-aws。 -
從左側導覽選擇 Apps > Charts 並安裝 aws-cloud-controller-manager。
-
選擇命名空間
kube-system,並啟用 Customize Helm options before install。 -
新增以下容器參數:
- '--use-service-account-credentials=true'
- '--configure-cloud-routes=false'
- 在
clusterRoleRules中為serviceaccounts資源的verbs新增get。這允許 cloud controller manager 在啟動時取得 service accounts。
- apiGroups:
- ''
resources:
- serviceaccounts
verbs:
- create
- get
- 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'
- 安裝 chart 並確認 Daemonset
aws-cloud-controller-manager正在執行。驗證aws-cloud-controller-managerpods 是否在目標命名空間中執行(除非在步驟 6 中修改,否則為kube-system)。
-
點擊 ☰,然後從左側導覽選擇叢集名稱。
-
選擇 Apps > Repositories。
-
點擊 Create 按鈕。
-
在 Index URL 欄位中輸入
https://kubernetes.github.io/cloud-provider-aws。 -
從左側導覽選擇 Apps > Charts 並安裝 aws-cloud-controller-manager。
-
選擇命名空間
kube-system,並啟用 Customize Helm options before install。 -
新增以下容器參數:
- '--use-service-account-credentials=true'
- '--configure-cloud-routes=false'
- 在
clusterRoleRules中為serviceaccounts資源的verbs新增get。這允許 cloud controller manager 在啟動時取得 service accounts:
- apiGroups:
- ''
resources:
- serviceaccounts
verbs:
- create
- get
- Rancher 佈建的 RKE 節點被標記為
node-role.kubernetes.io/controlplane。更新 tolerations 和 nodeSelector:
tolerations:
- effect: NoSchedule
key: node.cloudprovider.kubernetes.io/uninitialized
value: 'true'
- effect: NoSchedule
value: 'true'
key: node-role.kubernetes.io/controlplane
nodeSelector:
node-role.kubernetes.io/controlplane: 'true'
目前存在一個已知問題,無法從 Rancher UI 更新 nodeSelector。繼續安裝 chart,然後手動編輯 Daemonset 以設定 nodeSelector:
nodeSelector:
node-role.kubernetes.io/controlplane: 'true'
- 安裝 chart 並確認 Daemonset
aws-cloud-controller-manager成功部署:
kubectl rollout status deployment -n kube-system aws-cloud-controller-manager