Reputation: 1
I don't understand about kubectl.
I'm trying to scale a pod using Cronjob in Huawei Cloud. I've resolved how to scale then using Linux Terminal and it works, but when i try to use the kubectl, it brings me the following error: `error:
I have the following YAML code from a kubectl in Huawei Cloud:
kind: CronJob
apiVersion: batch/v1
metadata:
name: resize-app01-test
namespace: my-namespace-hmg
uid: <my-private-uid>
resourceVersion: '<xxx>'
generation: 19
creationTimestamp: '2024-07-30T13:38:49Z'
annotations:
description: ''
managedFields:
- manager: cfe-apiserver
operation: Update
apiVersion: batch/v1
time: '2024-07-30T16:41:37Z'
fieldsType: FieldsV1
fieldsV1:
f:metadata:
f:annotations:
.: {}
f:description: {}
f:spec:
f:concurrencyPolicy: {}
f:failedJobsHistoryLimit: {}
f:jobTemplate:
f:spec:
f:template:
f:metadata:
f:labels:
.: {}
f:app: {}
f:version: {}
f:spec:
f:containers:
k:{"name":"container-teste"}:
.: {}
f:args: {}
f:command: {}
f:env:
.: {}
k:{"name":"PAAS_APP_NAME"}:
.: {}
f:name: {}
f:value: {}
k:{"name":"PAAS_NAMESPACE"}:
.: {}
f:name: {}
f:value: {}
k:{"name":"PAAS_PROJECT_ID"}:
.: {}
f:name: {}
f:value: {}
f:image: {}
f:imagePullPolicy: {}
f:name: {}
f:resources:
.: {}
f:limits:
.: {}
f:cpu: {}
f:memory: {}
f:requests:
.: {}
f:cpu: {}
f:memory: {}
f:terminationMessagePath: {}
f:terminationMessagePolicy: {}
f:dnsConfig:
.: {}
f:options: {}
f:dnsPolicy: {}
f:imagePullSecrets:
.: {}
k:{"name":"default-secret"}: {}
f:restartPolicy: {}
f:schedulerName: {}
f:securityContext: {}
f:terminationGracePeriodSeconds: {}
f:schedule: {}
f:successfulJobsHistoryLimit: {}
f:suspend: {}
f:timeZone: {}
- manager: kube-controller-manager
operation: Update
apiVersion: batch/v1
time: '2024-07-30T16:43:38Z'
fieldsType: FieldsV1
fieldsV1:
f:status:
f:lastScheduleTime: {}
f:lastSuccessfulTime: {}
subresource: status
spec:
schedule: "43 13 * * *"
timeZone: America/Sao_Paulo
concurrencyPolicy: Forbid
suspend: false
jobTemplate:
metadata:
creationTimestamp: null
spec:
template:
metadata:
creationTimestamp: null
labels:
app: resize-app01-teste
version: v1
spec:
containers:
- name: container-teste
image: bitnami/kubectl:latest
command:
- /bin/sh
args:
- '-c'
- kubectl scale --replicas=3 deployment/workload-v2-app01-hmg -n app01-hmg
env:
- name: PAAS_APP_NAME
value: resize-app01-teste
- name: PAAS_NAMESPACE
value: app01-hmg
- name: PAAS_PROJECT_ID
value: <my-project-id>
resources:
limits:
cpu: 250m
memory: 512Mi
requests:
cpu: 250m
memory: 512Mi
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
imagePullPolicy: IfNotPresent
restartPolicy: OnFailure
terminationGracePeriodSeconds: 60
dnsPolicy: ClusterFirst
securityContext: {}
imagePullSecrets:
- name: default-secret
schedulerName: default-scheduler
dnsConfig:
options:
- name: single-request-reopen
successfulJobsHistoryLimit: 3
failedJobsHistoryLimit: 1
status:
lastScheduleTime: '2024-07-30T16:43:00Z'
lastSuccessfulTime: '2024-07-30T13:40:03Z'
when the Cronjob is executed, the console brings me this error:
Error - no objects passed to scale
How can i resolve this?
I've tried to split the command: and args: like that ways:
1° try:
command:
- /bin/sh
- '-c'
- kubectl
- scale --replicas=3
- deployment/workload-v2-app01-hmg -n app01-hmg
2° try:
command:
- /bin/sh
args:
- '-c'
- kubectl
- scale --replicas=3
- deployment/workload-v2-app01-hmg -n app01-hmg
3° try:
command:
- /bin/sh
args:
- '-c'
- kubectl scale --replicas=3 deployment/workload-v2-app01-hmg -n app01-hmg
Upvotes: -1
Views: 83
Reputation: 312600
This isn't directly an answer, but your 1st and 2nd tries are doomed to failure because of how you've structured them. When you're using sh -c '...some script...'
, note that the -c
option only takes a single argument. That means that when you do this:
command:
- /bin/sh
- '-c'
- kubectl
- scale --replicas=3
- deployment/workload-v2-app01-hmg -n app01-hmg
Or this:
command:
- /bin/sh
args:
- '-c'
- kubectl
- scale --replicas=3
- deployment/workload-v2-app01-hmg -n app01-hmg
In both cases the argument to sh -c
is kubectl
, and nothing else. The remaining arguments become positional parameters to the script passed to -c
.
It's critical to understand this, because you're going to run into similar situations many times as you work with kubernetes.
The canonical way of embedding a shell script in a pod/deployment is to do something like this is:
command:
- /bin/sh
- -c
- |
echo your shell script
echo goes here
Using the |
literal block quote operator allows you to format a shell script as you normally would.
Your third try appears correct, assuming that your code is running under a serviceaccount with appropriate privileges.
Upvotes: 0