Sling Academy
Home/DevOps/Kubernetes: Setting multiple commands in a YAML file

Kubernetes: Setting multiple commands in a YAML file

Last updated: January 31, 2024

Introduction

Kubernetes has become the go-to solution for container orchestration, allowing developers to manage containerized applications at scale efficiently. Part of working with Kubernetes involves writing YAML files that describe the desired state of your system. YAML files define your Kubernetes objects such as Pods, Deployments, Services, etc. Often, you need to specify commands to run when your containers start. This tutorial guides you through setting multiple commands in a Kubernetes YAML file, with code examples ranging from basic to advanced.

Understanding the Command and Arguments

In Kubernetes, the terms command and args correspond to the Docker entrypoint and command, respectively. You can override the default entrypoint by specifying the command field, and you can override the default command by specifying the args field.

apiVersion: v1
kind: Pod
metadata:
  name: example-pod
spec:
  containers:
  - name: hello
    image: ubuntu
    command: ["/bin/echo"]
    args: ["Hello, Kubernetes!"]

This will result in the execution of “/bin/echo Hello, Kubernetes!” when the Pod starts.

Passing Multiple Commands

Sometimes, you need to execute multiple commands when your container starts. Kubernetes doesn’t natively allow you to specify multiple commands in a list, but you can execute multiple commands using shell syntax.

apiVersion: v1
kind: Pod
metadata:
  name: multi-command-pod
spec:
  containers:
  - name: hello-multi
    image: ubuntu
    command: ["/bin/sh", "-c"]
    args: ["echo 'Hello, Kubernetes!' && echo 'This is a second command.'"]

This Pod will execute two echo commands sequentially when it starts. Note that we’re using /bin/sh as the command and passing a string of commands to -c.

Using Scripts to Run Multiple Commands

If you have several commands to run, you might consider using a script. You can add a script to your Docker image and then call that script in your YAML file.

FROM ubuntu
ADD start.sh /start.sh
RUN chmod +x /start.sh
ENTRYPOINT ["/start.sh"]

start.sh could be a script that runs multiple commands:

#!/bin/bash
echo "Starting the first task."
sleep 2
echo "Starting the second task."

Your Kubernetes YAML would look like this:

apiVersion: v1
kind: Pod
metadata:
  name: multi-script-pod
spec:
  containers:
  - name: custom-script-container
    image: custom-image-with-script

The custom script inside your container image will handle command execution.

Advanced Command Execution

In some cases, you might need more complex command execution such as loops or conditional execution. You can also manage this through the combination of the command and args.

apiVersion: v1
kind: Pod
metadata:
  name: advanced-command-pod
spec:
  containers:
  - name: advanced-commands-container
    image: ubuntu
    command: ["/bin/sh", "-c"]
    args: ["for i in $(seq 1 5); do echo \"Loop $i\"; done"]

This example uses a simple for-loop in shell script syntax, contained within the args array, to execute a series of echo commands.

Conclusion

Setting multiple commands in Kubernetes YAML files can be achieved through shell syntax or by using scripts embedded in your container images. Understanding how to structure these commands allows you to have fine-grained control over the startup behavior of containers within your Pods.

Next Article: Kubernetes Error: Pod has unbound immediate PersistentVolumeClaims

Previous Article: Minikube: How to expose a service externally to the outside world (external IP)

Series: Kubernetes Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide