Executing multiple commands( or from a shell script) in a kubernetes pod

kubernetes run command in container
kubectl exec pod not found
kubernetes run multiple commands in container
kubectl run command in pod
kubernetes shell on pod
kubectl run entrypoint
container command
kubernetes stop pod

I'm writing a shell script which needs to login into the pod and execute a series of commands in a kubernetes pod.

below is my sample_script.sh

kubectl exec octavia-api-worker-pod-test -c octavia-api bash unset http_proxy https_proxy mv /usr/local/etc/octavia/octavia.conf /usr/local/etc/octavia/octavia.conf-orig /usr/local/bin/octavia-db-manage --config-file /usr/local/etc/octavia/octavia.conf upgrade head

After running this script, I'm not getting any output. Any help will be greatly appreciated

Are you running all these commands as a single line command? First of all, there's no ; or && between those commands. So if you paste it as a multi-line script to your terminal, likely it will get executed locally.

Second, to tell bash to execute something, you need: bash -c "command".

Try running this:

$ kubectl exec POD_NAME -- bash -c "date && echo 1"

Wed Apr 19 19:29:25 UTC 2017
1

You can make it multiline like this:

$ kubectl exec POD_NAME -- bash -c "date && \
      echo 1 && \
      echo 2"

Using multiple commands in a kubernetes yaml file, So I followed the kubernetes documentation to execute a command using a yaml file: http://kubernetes. run multiple commands. apiVersion: v1 kind: Pod metadata: name: hello-world spec: # specification of the pod's contents restartPolicy: Never Execute shell script file using kubernetes as a cron job. The args are then passed as commands to the shell. In shell scripting a semicolon separates commands, and && conditionally runs the following command if the first succeed. In the above example, it always runs command one followed by command two, and only runs command three if command two succeeded.

-t can solve your task

For example, I run here few cmd:

kubectl get pods |grep nginx|cut -f1 -d\  |\
while read pod; \
 do echo "$pod writing:";\
  kubectl exec -t $pod -- bash -c \
   "dd if=/dev/zero of=/feeds/test.bin bs=260K count=4 2>&1|\
    grep copi |cut -d, -f4; \
    a=$SECONDS; echo -ne 'reading:'; cat /feeds/test.bin >/dev/null ; \
    let a=SECONDS-a ; \
    echo $a sec"
done

p.s. your example will be:

kubectl exec -t octavia-api-worker-pod-test -c octavia-api -- bash -c "unset http_proxy https_proxy ; mv /usr/local/etc/octavia/octavia.conf /usr/local/etc/octavia/octavia.conf-orig ; /usr/local/bin/octavia-db-manage --config-file /usr/local/etc/octavia/octavia.conf ; upgrade ; head"

Executing a Command in a Container · The Kubectl Book, Commands may be a Shell with a tty. Exec Command. Run a command in a Container in the cluster by specifying the Pod name. kubectl exec  And inside Volumes I will put the configmap name (postgres-init-script-configmap) which is the name of the config defined inside the configmap.yaml file. There is no need to create the configmap using kubernetes, The pod will take the configuration from the configmap file as long as you place it in the same directory as the pod.yaml .

The following should work

kubectl -it exec podname -- bash -c "ls && ls"

bin   dev   etc   home  proc  root  run   sys   tmp   usr   var bin  
dev   etc   home  proc  root  run   sys   tmp   usr   var

If above command doesn't work then try too replace bash with one of the following /bin/bash, sh, bin/sh

Get a Shell to a Running Container, kubectl exec -it shell-demo -- /bin/bash. Note: The double dash symbol “--” is used to separate the arguments you want to pass to the command  Executing a Command in a Container Commands may be a Shell with a tty. Exec Command. Run a command in a Container in the cluster by specifying the Pod name.

Define a Command and Arguments for a Container, This is required for the variable to be expanded in the command or args field. Run a command in a shell. In some cases, you need your command  my problem is that the pod stop after the execution of the command, i want to execute the command and the image ( application ) continue working the command is just for doing some modification in the container. no to execute it only.

Executing commands in Pods using K8s API, As part of my exploration of Kubernetes, while working on a project I wanted to exception: The command parameter can be included multiple times. for a single command parameter, one that will simply execute /bin/bash in the pod: /api​/v1/namespaces/project-1/pods/pod-1-lmlzj/exec?command=/bin/  Get a Shell to a Running Container. This page shows how to use kubectl exec to get a shell to a running Container. Before you begin. Getting a shell to a Container. Writing the root page for nginx. Running individual commands in a Container. Opening a shell when a Pod has more than one Container.

Kubernetes, Using kubectl exec to open a bash terminal in a pod. After all, open a terminal in a pod is the most direct way to introspect the pod. Assuming the pod/storage is still  Define a command and arguments when you create a Pod. When you create a Pod, you can define a command and arguments for the containers that run in the Pod. To define a command, include the command field in the configuration file. To define arguments for the command, include the args field in the configuration file.

Comments
  • Not really descriptive: What output would you expect?
  • Please accept an answer if it solves your problem.
  • What does '--' do in this case? Why is it needed?
  • The double dash symbol "--" is used to separate the arguments you want to pass to the command from the kubectl arguments
  • This is not a good idea. -t has certain output characters like \r you should probably not use in your scripts. stackoverflow.com/questions/55034214/…