是否有kubernetes角色定义允许命令`kubectl rollout restart deploy< deployment>`?

lg40wkob  于 2023-08-03  发布在  Kubernetes
关注(0)|答案(3)|浏览(97)

我希望kubernetes中的部署能够从集群内重新启动自己。
我知道我可以创建一个serviceaccount并将其绑定到pod,但是我缺少最具体的权限的名称(即而不仅仅是允许'*')允许命令

kubectl rollout restart deploy <deployment>

字符串
这是我有的,然后呢?是我所缺少的

apiVersion: v1
kind: ServiceAccount
metadata:
  name: restart-sa
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  namespace: default
  name: restarter
rules:
  - apiGroups: ["apps"]
    resources: ["deployments"]
    verbs: ["list", "???"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: testrolebinding
  namespace: default
subjects:
  - kind: ServiceAccount
    name: restart-sa
    namespace: default
roleRef:
  kind: Role
  name: restarter
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: v1
kind: Pod
metadata:
  name: example
spec:
  containers:
  - image: nginx
    name: nginx
  serviceAccountName: restart-sa

23c0lvtd

23c0lvtd1#

我认为以下是重新启动部署所需的最低权限:

rules:
 - apiGroups: ["apps", "extensions"]
   resources: ["deployments"]
   resourceNames: [$DEPLOYMENT]
   verbs: ["get", "patch"]

字符串

b1zrtrql

b1zrtrql2#

如果你想要在集群中重启kubernetes部署本身,你需要在rbac authorisation上设置权限。
在yaml文件中,您在Role:rules下遗漏了一些特定的权限,您需要以以下格式添加 predicate :[“get”,“watch”,“list”]
你需要在yaml文件中添加“deployment”而不是“Pod”。
请确保添加“serviceAccountName:restart-sa”在部署yaml文件的“spec:containers”下。如下所述:

apiVersion: apps/v1  
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
      serviceAccountName: restart-sa

字符串
然后,您可以使用以下命令重新启动部署:

**$**kubectl rollout restart部署[deployment_name]

sc4hvdpw

sc4hvdpw3#

如果有人也面临这个问题,我遇到了一个类似的问题,其中服务帐户无法执行推出重新启动
最后,我通过将复制集添加到资源列表中来修复它

kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  namespace: default
  name: restarter
rules:
  - apiGroups: ["apps"]
    resources: ["deployments", "replicasets", "pods"]
    verbs: ["get", "patch"]

字符串
希望这对你也有帮助:)

相关问题