首先介绍一些有关设置的信息:
EKS 版本:1.21
eksctl 版本:0.77.0
AWS Go SDK 版本:v1.44.28
使用部署kubectl
我在 AWS EKS 上有一个 k8s 集群,我正在为我的应用程序部署一个自定义的 k8s 控制器。使用来自eksworkshop.com,我使用 创建了具有适当 IAM 角色的服务账户eksctl
。我在我的账户中分配角色,deployment.yaml
如下所示。我还设置了securityContext
,这似乎可以解决某些情况下的问题,如下所述这里。
apiVersion: apps/v1
kind: Deployment
metadata:
name: tel-controller
namespace: tel
spec:
replicas: 2
selector:
matchLabels:
app: tel-controller
strategy:
rollingUpdate:
maxSurge: 50%
maxUnavailable: 50%
type: RollingUpdate
template:
metadata:
labels:
app: tel-controller
spec:
serviceAccountName: tel-controller-serviceaccount
securityContext:
fsGroup: 65534
containers:
- image: <image name>
imagePullPolicy: Always
name: tel-controller
args:
- --metrics-bind-address=:8080
- --health-probe-bind-address=:8081
- --leader-elect=true
ports:
- name: webhook-server
containerPort: 9443
protocol: TCP
- name: metrics-port
containerPort: 8080
protocol: TCP
- name: health-port
containerPort: 8081
protocol: TCP
securityContext:
readOnlyRootFilesystem: true
runAsNonRoot: true
allowPrivilegeEscalation: false
但这似乎不起作用。如果我描述 pod,我就会看到正确的角色。
AWS_DEFAULT_REGION: us-east-1
AWS_REGION: us-east-1
AWS_ROLE_ARN: arn:aws:iam::xxxxxxxxx:role/eksctl-eks-tel-addon-iamserviceaccount-tel-t-Role1-3APV5KCV33U8
AWS_WEB_IDENTITY_TOKEN_FILE: /var/run/secrets/eks.amazonaws.com/serviceaccount/token
Mounts:
/var/run/secrets/eks.amazonaws.com/serviceaccount from aws-iam-token (ro)
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-6ngsr (ro)
sts.GetCallerIdentityInput()
但是如果我从控制器应用程序内部执行操作,我会看到节点角色。显然,我得到了一个access denied
错误。
调用者身份:(go string){帐户:“xxxxxxxxxxxx”,Arn:“arn:aws:sts :: xxxxxxxxxxx:assumed-role / eksctl-eks-tel-nodegroup-voice-NodeInstanceRole-BJNYF5YC2CE3 / i-0694a2766c5d70901”,用户ID:“AROAZUYK7F2GRLKRGGNXZ:i-0694a2766c5d70901”}
这就是我通过服务帐户创建的方式
eksctl create iamserviceaccount --cluster ${EKS_CLUSTER_NAME} \
--namespace tel \
--name tel-controller-serviceaccount \
--attach-policy-arn arn:aws:iam::xxxxxxxxxx:policy/telcontrollerRoute53Policy \
--override-existing-serviceaccounts --approve
我以前曾成功完成过此操作。这次的不同之处在于,我还将角色和角色绑定附加到此服务帐户。我的rbac.yaml
SA。
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: tel-controller-role
labels:
app: tel-controller
rules:
- apiGroups: [""]
resources: [events]
verbs: [create, delete, get, list, update, watch]
- apiGroups: ["networking.k8s.io"]
resources: [ingressclasses]
verbs: [get, list]
- apiGroups: ["", "networking.k8s.io"]
resources: [services, ingresses]
verbs: [create, get, list, patch, update, delete, watch]
- apiGroups: [""]
resources: [configmaps]
verbs: [create, delete, get, update]
- apiGroups: ["coordination.k8s.io"]
resources: ["leases"]
verbs: [get, create, update]
- apiGroups: [""]
resources: [pods]
verbs: [get, list, watch, update]
- apiGroups: ["", "networking.k8s.io"]
resources: [services/status, ingresses/status]
verbs: [update, patch]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: tel-controller-rolebinding
labels:
app: tel-controller
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: tel-controller-role
subjects:
- kind: ServiceAccount
name: tel-controller-serviceaccount
namespace: tel
我在这里做错了什么?谢谢。
答案1
事实证明,这是一个编程问题(因此不确定它是否属于这里)。
因此,Go SDK 有两种方法来创建会话。显然,我使用的是已弃用的方法来创建会话(session.New
)。使用推荐的方法创建新会话session.NewSession
解决了我的问题。