设置Kubernetes Cronjob:无法获取资源;“水平足自动秤”;在API组中“;“自动缩放”;在名称空间中;登台;
我试图设置一个cron作业来修补水平pod autoscaler,但该作业返回设置Kubernetes Cronjob:无法获取资源;“水平足自动秤”;在API组中“;“自动缩放”;在名称空间中;登台;,kubernetes,roles,kubernetes-cronjob,Kubernetes,Roles,Kubernetes Cronjob,我试图设置一个cron作业来修补水平pod autoscaler,但该作业返回horizontalpodautoscalers。禁止自动缩放“我的web服务”:用户“system:serviceCount:staging:sa cron runner”无法获取命名空间“staging”中API组“autoscaling”中的资源“horizontalpodautoscalers” 我已尝试设置所有角色权限,如下所示: kind: Role apiVersion: rbac.authorizati
horizontalpodautoscalers。禁止自动缩放“我的web服务”:用户“system:serviceCount:staging:sa cron runner”无法获取命名空间“staging”中API组“autoscaling”中的资源“horizontalpodautoscalers”
我已尝试设置所有角色权限,如下所示:
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
namespace: staging
name: cron-runner
rules:
- apiGroups: ["autoscaling"]
resources: ["horizontalpodautoscalers"]
verbs: ["patch"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: cron-runner
namespace: staging
subjects:
- kind: ServiceAccount
name: sa-cron-runner
namespace: staging
roleRef:
kind: Role
name: cron-runner
apiGroup: rbac.authorization.k8s.io
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: sa-cron-runner
namespace: staging
---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: scale-up-job
namespace: staging
spec:
schedule: "16 20 * * 1-6"
concurrencyPolicy: Forbid
jobTemplate:
spec:
template:
spec:
serviceAccountName: sa-cron-runner
containers:
- name: scale-up-job
image: bitnami/kubectl:latest
command:
- /bin/sh
- -c
- kubectl patch hpa my-web-service --patch '{\"spec\":{\"minReplicas\":6}}'
restartPolicy: OnFailure
kubectl auth can-i patch horizontalpodautoscalers-n staging——由于sa cron runner也返回no
,因此无法正确设置权限
我如何调试这个?我看不出这是如何错误地设置的我认为问题在于cronjob需要首先获得资源,然后才能获得相同的补丁。因此,您需要在角色
规范中明确指定获取
的权限
该错误还提到获取资源时的身份验证问题:
horizontalpodautoscalers.autoscaling "my-web-service" is forbidden: User "system:serviceaccount:staging:sa-cron-runner" cannot get resource "horizontalpodautoscalers" in API group "autoscaling" in the namespace "staging"
尝试将角色的动词部分修改为:
verbs: ["patch", "get"]
根据cronjob内部运行的脚本中的要求,还可以包括list
和watch
。这是否回答了您的问题?嗯,这对上述情况有何影响?…我已经尽我所能做到了这就是你所要求的--as=system:serviceaccount:staging:sa cron runner。我想这就是你要找的。在动词上请求的另一种方法是获取整个列表,如kubectl auth can-i--list--as=system:servicecomport:staging:sa cron runner。但我现在在您的输出中看到它已经使用了正确的serviceaccount。这可能是个问题,但不要认为这是身份验证问题kubectl auth can-i patch horizontalpodautoscalers-n staging--as sa cron runner
仍然返回no。尝试使用如下命令:kubectl auth can-i patch horizontalpodautoscalers-n staging--as system:servicecomport:staging:sa cron runner
添加此命令后,并使用Krishan建议的命令,它成功了!