仅用CronJob与ServiceAccount扩缩容K8s Deployment遇报错求助
解决kubectl scale报错
error: no objects passed to scale的问题 问题原因
这个报错的核心是kubectl未找到要操作的Deployment对象,根源在于命令参数顺序不符合kubectl语法规则——你把命名空间参数--namespace=myspace放在了资源名称之后,导致kubectl在默认命名空间(而非myspace)中查找mydeployment,自然找不到目标对象,从而抛出该错误。
解决方案
1. 修正CronJob的kubectl命令参数顺序
将命名空间参数作为全局选项移到scale命令之前,或者使用-n缩写放在资源名称前,两种写法均有效:
写法一(使用完整--namespace参数)
apiVersion: batch/v1 kind: CronJob metadata: name: scale-up-job namespace: myspace spec: schedule: "40 15 * * 1-5" concurrencyPolicy: Forbid failedJobsHistoryLimit: 1 successfulJobsHistoryLimit: 1 jobTemplate: spec: backoffLimit: 0 template: spec: containers: - name: kubectl image: bitnami/kubectl:latest imagePullPolicy: IfNotPresent args: - --namespace=myspace - scale - deployment - mydeployment - --replicas=3 serviceAccountName: scaling-service-account restartPolicy: Never
写法二(使用-n缩写)
仅修改args部分,其余配置保持不变:
args: - scale - deployment - mydeployment - -n - myspace - --replicas=3
2. 额外验证与优化
- 确认
mydeployment存在于myspace命名空间:执行kubectl get deployments mydeployment -n myspace验证资源是否存在。 - 优化ClusterRole配置:ClusterRole是集群级资源,无需指定
metadata.namespace,可删除该字段减少冗余:
apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: scaling-cluster-role rules: - apiGroups: ["apps"] resources: ["deployments/scale"] verbs: ["patch"]
内容的提问来源于stack exchange,提问作者Marian
相关产品推荐
相关产品推荐

