【问题标题】:Setup RBAC in Kubernetes for a Cronjob running kubectl在 Kubernetes 中为运行 kubectl 的 Cronjob 设置 RBAC
【发布时间】:2019-10-09 03:43:40
【问题描述】:
➜  ~ kubectl version
Client Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.3", GitCommit:"2d3c76f9091b6bec110a5e63777c332469e0cba2", GitTreeState:"clean", BuildDate:"2019-08-19T11:13:54Z", GoVersion:"go1.12.9", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"12", GitVersion:"v1.12.10", GitCommit:"e3c134023df5dea457638b614ee17ef234dc34a6", GitTreeState:"clean", BuildDate:"2019-07-08T03:40:54Z", GoVersion:"go1.10.8", Compiler:"gc", Platform:"linux/amd64"}

我正在尝试从 Cronjob 运行 kubectl 以更改部署中的 pod 数量。

我按照https://stackoverflow.com/a/54908449/3477266 中的建议创建了这样的 Cronjob 及其角色

apiVersion: v1
kind: ServiceAccount
metadata:
  name: scheduled-autoscaler-service-account
  namespace: default

---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: scheduler-autoscaler-role
rules:
- apiGroups:
  - extensions
  - apps
  resources:
  - deployments
  verbs:
  - patch
  - get
  - list

---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: schedule-autoscaler-role-binding
subjects:
- kind: ServiceAccount
  name: scheduled-autoscaler-service-account
  namespace: default
roleRef:
  kind: Role
  name: schedule-autoscaler-role
  apiGroup: ""

---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: adwords-api-scale-up-cron-job
spec:
  schedule: "*/2 * * * *"
  jobTemplate:
    spec:
      activeDeadlineSeconds: 100
      template:
        spec:
          serviceAccountName: scheduled-autoscaler-service-account
          containers:
          - name: adwords-api-scale-up-container
            image: bitnami/kubectl:1.15-debian-9
            command:
              - bash
            args:
              - "-xc"
              - |
                kubectl scale --replicas=2 --v=7 
deployment/adwords-api-deployment
          restartPolicy: OnFailure

但是,我在运行此作业的 pod 中收到以下错误:

来自服务器的错误(禁止):deployments.extensions“adwords-api-deployment”被禁止:用户“system:serviceaccount:default:scheduled-autoscaler-service-account”无法在 API 组“extensions”中获取资源“deployments” " 在命名空间“默认”中

如何调试问题所在?对我来说,似乎我已授予它在消息中抱怨的所有权限,但它仍然无法正常工作。

提前致谢

更新: 我解决了我的问题。在 RoleBinding 中定义角色名称时,它只是一个拼写错误。那里的名字是错误的。

但我只有在得知可以使用此命令检查权限后才发现这一点:

kubectl auth can-i list deployment --as=system:serviceaccount:default:scheduled-autoscaler-service-account -n default

我认为这是更复杂的事情,可能是由于缺乏 Kubernetes 经验。

【问题讨论】:

    标签: kubernetes kubectl


    【解决方案1】:

    我认为您不能在绑定中将apiGroup 留空。试试apiGroup: rbac.authorization.k8s.io

    【讨论】:

    • 感谢您的回答。我尝试了这个建议,但仍然遇到同样的错误:/
    • 尝试添加资源deployments/scale 或类似的,我忘记了子资源的名称。
    【解决方案2】:

    您可能需要在对应的Job 模板中提供kubectl 二进制文件以及来自源k8s 集群主机的特定kubeconfig 文件,以便从相关Pod 中建立到目标k8s 集群的连接,确定有关集群的足够信息,AuthenticationAuthorization 机制。

    我一直在对originCronJob配置进行一些调整,设法添加hostPath卷挂载,映射源k8s主机kubeconfig路径:$HOME/.kube在每个Pod下,由作业释放:

    apiVersion: batch/v1beta1
    kind: CronJob
    metadata:
      name: adwords-api-scale-up-cron-job
    spec:
      schedule: "*/2 * * * *"
      jobTemplate:
        spec:
          activeDeadlineSeconds: 100
          template:
            spec:
              serviceAccountName: scheduled-autoscaler-service-account
              containers:
              - name: adwords-api-scale-up-container
                image: bitnami/kubectl:1.15-debian-9
                command:
                  - bash
                args:
                  - "-xc"
                  - |
                    kubectl scale --replicas=2 --v=7 deployment/adwords-api-deployment
                volumeMounts:
                - name: kubectl-config
                  mountPath: /.kube/
                  readOnly: true
              volumes:
              - name: kubectl-config
                hostPath:
                  path: $HOME/.kube # Replace $HOME with an evident path location
              restartPolicy: OnFailure
    

    我已经检查了您授予的 RBAC 规则并且它们很好,同时在我的环境中的类似场景中重现了您的问题。

    【讨论】:

    • @luislhl 这能回答你的问题吗?你还有这个问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-19
    • 2021-09-19
    • 2019-06-19
    • 2020-12-22
    • 1970-01-01
    相关资源
    最近更新 更多