Kubernetes/kops:将 EBS 卷附加到实例时出错。您无权执行此操作。错误 403

Kubernetes/kops: error attaching EBS volume to instance. You are not authorized to perform this operation. Error 403

我在 kops 提供的 AWS 集群上使用 EBS 卷安装测试了 kubernetes 部署。这是部署 yml 文件:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: helloworld-deployment-volume
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: helloworld
    spec:
      containers:
      - name: k8s-demo
        image: wardviaene/k8s-demo
        ports:
        - name: nodejs-port
          containerPort: 3000
        volumeMounts:
        - mountPath: /myvol
          name: myvolume
      volumes:
      - name: myvolume
        awsElasticBlockStore:
          volumeID: <volume_id>

kubectl create -f <path_to_this_yml> 之后,我在 pod 描述中收到以下消息:

Attach failed for volume "myvolume" : Error attaching EBS volume "XXX" to instance "YYY": "UnauthorizedOperation: You are not authorized to perform this operation. status code: 403

看起来这只是一个权限问题。好的,我检查了节点角色 IAM -> Roles -> nodes.<my_domain> 的策略,发现那里没有允许操作卷的操作,只有 ec2:DescribeInstances 操作默认。所以我添加了 AttachVolumeDetachVolume 操作:

    {
        "Sid": "kopsK8sEC2NodePerms",
        "Effect": "Allow",
        "Action": [
            "ec2:DescribeInstances",
            "ec2:AttachVolume",
            "ec2:DetachVolume"
        ],
        "Resource": [
            "*"
        ]
    },

但这并没有帮助。我仍然收到该错误:

Attach failed for volume "myvolume" : Error attaching EBS volume "XXX" to instance "YYY": "UnauthorizedOperation: You are not authorized to perform this operation.

我是不是漏掉了什么?

我找到了解决办法。它被描述为

在 kops 1.8.0-beta.1 中,主节点要求您标记 AWS 卷:

KubernetesCluster: <clustername-here>

因此有必要使用 awscli:

创建带有该标签的 EBS 卷
aws ec2 create-volume --size 10 --region eu-central-1 --availability-zone eu-central-1a --volume-type gp2 --tag-specifications 'ResourceType=volume,Tags=[{Key=KubernetesCluster,Value=<clustername-here>}]'

或者您可以在 EC2 -> Volumes -> Your volume -> Tags

中手动标记它

就是这样。

编辑:

可以在作为集群一部分的 EC2 实例标签中找到正确的集群名称。键是一样的:KubernetesCluster.