开发测试环境需要创建redis,mysql等中间件,使用动态pvc存储并挂载卷。
Amazon Elastic Block Store (Amazon EBS) Container Storage Interface (CSI) 驱动程序允许 Amazon Elastic Kubernetes Service (Amazon EKS) 集群管理持久性卷的 Amazon EBS 卷的生命周期。
https://docs.aws.amazon.com/zh_cn/eks/latest/userguide/enable-iam-roles-for-service-accounts.html
1,确定集群是否拥有现有IAM OIDC提供商,检索集群的 OIDC 提供商 ID 并将其存储在变量中
oidc_id=$(aws eks describe-cluster --name my-cluster --query "cluster.identity.oidc.issuer" --output text | cut -d '/' -f 5)
2,确定您的账户中是否已存在具有您的集群 ID 的 IAM OIDC 提供商。
aws iam list-open-id-connect-providers | grep $oidc_id | cut -d "/" -f4
如果返回了输出,则表示您的集群已经有 IAM OIDC 提供商,您可以跳过下一步。如果没有返回输出,则您必须为集群创建 IAM OIDC 提供商。
3,使用以下命令为您的集群创建 IAM OIDC 身份提供商。将 my-cluster 替换为您自己的值。
eksctl utils associate-iam-oidc-provider --cluster my-cluster --approve
eksctl create iamserviceaccount \
--name ebs-csi-controller-sa \
--namespace kube-system \
--cluster test \
--attach-policy-arn arn:aws:iam::aws:policy/service-role/AmazonEBSCSIDriverPolicy \
--approve \
--role-only \
--role-name AmazonEKS_test_EBS_CSI_DriverRole
PS: role-name有多个集群的时候需要修改,不可同名
运行以下命令。将 my-cluster 替换为您的集群的名称,将 111122223333 替换为您的账户 ID,并将 AmazonEKS_EBS_CSI_DriverRole 替换为之前创建的 IAM 角色。如果您的集群位于 AWS GovCloud(美国东部)或 AWS GovCloud(美国西部)AWS 区域,则将 arn:aws: 替换为 arn:aws-us-gov:。
eksctl create addon --name aws-ebs-csi-driver --cluster test --service-account-role-arn arn:aws:iam::111118437221:role/AmazonEKS_test_EBS_CSI_DriverRole
1,检查 Amazon EBS CSI 附加组件的当前版本。将 my-cluster 替换为您的集群名称
要使用 eksctl 移除 Amazon EBS CSI 附加组件
请将 my-cluster 替换为您的集群名称,然后运行以下命令。
eksctl delete addon --cluster my-cluster --name aws-ebs-csi-driver --preserve
按照如下步骤操作:
git clone https://github.com/kubernetes-sigs/aws-ebs-csi-driver.git
cd aws-ebs-csi-driver&&kubectl apply -k deploy/kubernetes/overlays/stable/ecr
这将在 Kubernetes 集群中创建一个名为 ebs-csi-controller
的 Deployment,并且将相关的服务和 RBAC(Role-Based Access Control)对象部署到默认的 kube-system
命名空间中。kubectl get pods -n kube-system
确保 ebs-csi-controller
的 Pod 和其他相关的 Pod 已经成功启动,并且处于运行状态kubectl get storageclasses.storage.k8s.io
如果看到ebs-csi的存储类,则说明已安装成功。参照文档部署示例应用程序
1,cd aws-ebs-csi-driver/examples/kubernetes/dynamic-provisioning/
2,kubectl apply -f manifests
3,kubectl describe storageclass ebs-sc
输出示例如下。
Name: ebs-sc
IsDefaultClass: No
Annotations: kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"storage.k8s.io/v1","kind":"StorageClass","metadata":{"annotations":{},"name":"ebs-sc"},"provisioner":"ebs.csi.aws.com","volumeBindingMode":"WaitForFirstConsumer"}
Provisioner: ebs.csi.aws.com
Parameters:
AllowVolumeExpansion:
MountOptions:
ReclaimPolicy: Delete
VolumeBindingMode: WaitForFirstConsumer
Events:
4,看默认命名空间中的 Pods。几分钟后,app Pod 的状态变为 Running。kubectl get pods --watch
5,列出默认命名空间中的持久性卷。查找具有 default/ebs-claim 声明的持久性卷。
kubectl get pv
输出示例如下。
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-37717cd6-d0dc-11e9-b17f-06fad4858a5a 4Gi RWO Delete Bound default
6,描述持久性卷。将 pvc-37717cd6-d0dc-11e9-b17f-06fad4858a5a 替换为上一步的输出值。
kubectl describe pv pvc-37717cd6-d0dc-11e9-b17f-06fad4858a5a
输出示例如下。
Name: pvc-37717cd6-d0dc-11e9-b17f-06fad4858a5a
Labels:
Annotations: pv.kubernetes.io/provisioned-by: ebs.csi.aws.com
Finalizers: [kubernetes.io/pv-protection external-attacher/ebs-csi-aws-com]
StorageClass: ebs-sc
Status: Bound
Claim: default/ebs-claim
Reclaim Policy: Delete
Access Modes: RWO
VolumeMode: Filesystem
Capacity: 4Gi
Node Affinity:
Required Terms:
Term 0: topology.ebs.csi.aws.com/zone in [region-code]
Message:
Source:
Type: CSI (a Container Storage Interface (CSI) volume source)
Driver: ebs.csi.aws.com
VolumeHandle: vol-0d651e157c6d93445
ReadOnly: false
VolumeAttributes: storage.kubernetes.io/csiProvisionerIdentity=1567792483192-8081-ebs.csi.aws.com
Events:
7,验证 Pod 是否将数据写入卷。
kubectl exec -it app -- cat /data/out.txt
输出示例如下。
Wed May 5 16:17:03 UTC 2021
Wed May 5 16:17:08 UTC 2021
Wed May 5 16:17:13 UTC 2021
Wed May 5 16:17:18 UTC 2021
[...]
解决方法:
describe pvc:kubectl describe pvc
1,如果角色无权限,查看服务帐户
kubectl get serviceaccount ebs-csi-controller-sa -n kube-system -oyaml
示例如下:
apiVersion: v1
automountServiceAccountToken: true
kind: ServiceAccount
metadata:
annotations:
eks.amazonaws.com/role-arn: arn:aws:iam:::role/AmazonEKS_EBS_CSI_DriverRole
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"v1","automountServiceAccountToken":true,"kind":"ServiceAccount","metadata":{"annotations":{},"labels":{"app.kubernetes.io/name":"aws-ebs-csi-driver"},"name":"ebs-csi-controller-sa","namespace":"kube-system"}}
creationTimestamp: "2023-05-19T09:11:15Z"
labels:
app.kubernetes.io/component: csi-driver
app.kubernetes.io/managed-by: EKS
app.kubernetes.io/name: aws-ebs-csi-driver
app.kubernetes.io/version: 1.18.0
name: ebs-csi-controller-sa
namespace: kube-system
resourceVersion: "37609755"
uid: 874ff4db-8c38-420a-87ad-79154213de46
注意查看annotations是否有role-arn,如果没有,执行命令,附加对应IAM角色到服务帐户
eksctl create addon --name aws-ebs-csi-driver --cluster eks-op-occ --service-account-role-arn arn:aws:iam::111118437221:role/AmazonEKS_EBS_CSI_DriverRole --force
2,如果caused by: AccessDenied: Not authorized to perform sts:AssumeRoleWithWebIdentity
查看IAM角色信任关系
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::oidc-provider/oidc.eks.us-east-1.amazonaws.com/id/"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"oidc.eks.us-east-1.amazonaws.com/id/:aud": "sts.amazonaws.com",
"oidc.eks.us-east-1.amazonaws.com/id/:sub": "system:serviceaccount:kube-system:ebs-csi-controller-sa"
}
}
}
]
}
二,待补充
Amazon EBS CSI 驱动程序
部署示例应用程序
创建 Amazon EBS CSI 驱动程序 IAM 角色
为集群创建 IAM OIDC 提供商