
ECR 자격 증명 갱신을 자동화하고 CronJob을 통해 비밀에 토큰을 저장하려고 합니다. CronJob을 실행할 때마다 결과 작업 로그에 다음 오류가 표시됩니다.
2023-09-14T20:11:20.326837046Z error: failed to create secret secrets is forbidden: User "system:serviceaccount:cfh:default" cannot create resource "secrets" in API group "" in the namespace "cfh"
흥미롭게도 이는 삭제 단계에서 실패하지 않는 것 같습니다. 문제가 kubectl create secret docker-registry
유형 이후 표준 비밀 동사 이외의 다른 역할 권한을 갖는 것과 관련이 있는지 궁금합니다 docker-registry
. 그러나 확실하지 않습니다.
내 CronJob YAML은 다음과 같습니다.
apiVersion: batch/v1
kind: CronJob
metadata:
name: ecr-registry-helper
creationTimestamp: '2023-09-11T00:06:03Z'
generation: 25
namespace: cfh
fields:
- ecr-registry-helper
- 0 */10 * * *
- 'False'
- 0
- 11m
- 3d20h
- ecr-registry-helper
- omarxs/awskctl:v1.0
- <none>
spec:
concurrencyPolicy: Allow
failedJobsHistoryLimit: 1
jobTemplate:
metadata:
creationTimestamp: null
namespace: cfh
spec:
template:
metadata:
creationTimestamp: null
spec:
containers:
- command:
- /bin/bash
- '-c'
- >-
ECR_TOKEN=`aws ecr get-login-password --region ${AWS_REGION}`
NAMESPACE_NAME=cfh
kubectl delete secret --ignore-not-found regcred -n
$NAMESPACE_NAME
echo "deleted secret"
kubectl create secret docker-registry regcred
--docker-server=https://${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com
--docker-username=AWS --docker-password="${ECR_TOKEN}" -n $NAMESPACE_NAME
echo "Secret was successfully updated at $(date)"
envFrom:
- secretRef:
name: ecr-registry-helper-secrets
- configMapRef:
name: ecr-registry-helper-cm
image: omarxs/awskctl:v1.0
imagePullPolicy: IfNotPresent
name: ecr-registry-helper
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
_init: false
__active: true
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Never
schedulerName: default-scheduler
serviceAccount: default
serviceAccountName: default
terminationGracePeriodSeconds: 30
schedule: 0 */10 * * *
successfulJobsHistoryLimit: 2
suspend: false
__clone: true
내 ServiceAccount도 마찬가지입니다.
apiVersion: v1
kind: ServiceAccount
metadata:
name: default
creationTimestamp: '2023-09-10T22:14:31Z'
namespace: cfh
fields:
- default
- 0
- 3d21h
automountServiceAccountToken: false
__clone: true
역할
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
creationTimestamp: '2023-09-11T00:06:03Z'
name: role-full-access-to-secrets
namespace: cfh
rules:
- apiGroups:
- ''
resourceNames:
- regcred
resources:
- secrets
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
그리고 역할 바인딩
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
creationTimestamp: '2023-09-11T00:06:03Z'
name: default-role-binding
namespace: cfh
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: role-full-access-to-secrets
subjects:
- kind: ServiceAccount
name: default
namespace: cfh
답변1
문제를 해결하는 데 도움을 준 @veera-nagireddy에게 감사드립니다. (추가 내용은 원본 게시물에 대한 그의 댓글을 참조하세요.)
문제는 의 역할을 맡았음에도 불구하고 가 네임스페이스 CronJob
에서 비밀을 생성/업데이트할 수 있는 권한이 없다는 것입니다 . 이 문제를 해결하기 위해 네임스페이스 의 비밀을 변경할 수 있는 권한을 부여하는 & 도 만들어야 했습니다 .cfh
ServiceAccount
ClusterRole
ClusterRoleBinding
ServiceAccount
cfh
클러스터역할:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
creationTimestamp: '2023-09-16T02:39:55Z'
name: ecr-registry-helper-cluster-role
rules:
- apiGroups:
- ''
resources:
- secrets
verbs:
- create
- delete
- update
ClusterRoleBinding:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
creationTimestamp: '2023-09-16T02:42:42Z'
name: ecr-registry-helper-cluster-role-binding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: ecr-registry-helper-cluster-role
subjects:
- kind: ServiceAccount
name: default
namespace: cfh
이 두 리소스를 생성한 후 성공적으로 비밀을 생성할 수 있었습니다.