Using Commands and Arguments in a Kubernetes Pod
Kubernetes is open-source software that is used for the deployment and automation of software applications. It is a container orchestration platform. It means that we can manage microservices using containers.
What is a Kubernetes pod?
Kubernetes Pod is the smallest unit in the structure of Kubernetes. It basically consists of one or more containers. Pod can also be referred to as a single-running instance in the Kubernetes cluster. It has a single IP address. It is to be noted that if more than one container is present inside the pod, then they have to share the resources.
Lifecycle of a Pod
When we execute pods, we can see several states of a pod. The different states are as follows:
- Pending: It means the pods that have been created have been accepted by the Kubernetes, but they do not have any containers yet.
- Running: Running step means that the containers are present in the pod and that the containers are in running state. It also means that the pod has been assigned in a particular node.
- Succeeded: It means that the containers have performed their job successfully and has terminated after the execution.
- Failed: It means at least one container present in that particular pod has failed.
- Unknown: It means Kubernetes encountered an unexpected error with the node in which that pod was running.
Using Commands and Arguments in a Kubernetes Pod
There are many commands that can be used in a Kubernetes Pod. First we need to ensure that Docker Desktop and minikube are installed. Before performing operations on Kubernetes Pod, we need to execute minikube start. Minikube is used to Kubernetes cluster on local machine.
Commands
During the creation of Pod YAML file we give a field named command. This field is used to override the default value or the ENTRYPOINT that is given by Docker Image. This is basically used for the customization of commands as per our requirements. We cannot dynamically change the values of commands during the run time creation of pod.
Arguments
Arguments or Args are values that are specified to the command that has been mentioned in the command field.The arguments are basically used to display additional values that are required along with the commands that has been mentioned. To get the value of arguments we use logs command.
Below is the sample example regarding the use of commands and arguments
Creation of Pod
We need to create a pod. But before that we need to define a YAML file that comprises of the state of pods. It is an instruction file that defines the structure of pod. It also includes configuration and behavior of the pod. Basically Arguments are used in the YAML file. Below is the sample YAML file.
apiVersion: v1
kind: Pod
metadata:
name: demo-pod
spec:
containers:
- name: demo-container
image: alpine
command: ["echo"]
args: ["Hello, Kubernetes!"]
restartPolicy: Never
Let us go through line by line
- apiVersion is used to specify Kubernetes API version that is used to create objects.
- kind is used to specify Kubernetes object.
- metadata contains extra information about pod
- Inside spec we provide details about the containers. The image used is Alpine
- command is used when we want to execute something inside container. Here "echo" is the command to be used. This is used when we want to override the commands provided by the Docker image.
- args are parameters that are supplied to command. Here as we have used echo, it requires a message which is to be executed. It is to be noted that number of command fields and number of args should be same.
- restartPolicy never means once the pod gets terminated it should never restart.
To create a pod we use apply command followed by the name of pod yaml file.
kubectl apply -f pod_1.yaml
Get list of pods
To get list of pods we have two commands. The first one is
kubectl get pods
Using this command we will get names of pods along with their status.
To get into details like getting the status, Age, State,IP, Node etc we use the command
kubectl get pods -o wide
Describe pod
To get description of a single pod, we use describe command. Describe command provides information about the pod like status, IP, Port, Image, Image ID, Args, Command etc. This is particularly useful when we want to check the properties of any particular pod.
kubectl describe pod <podname>
Getting Logs
This command is used when we want to display the output or the logs of any particular pod. We can also use logs to get output of any container present on a particular pod.
kubectl logs <pod name>
As we can see from the above the arguments gets displayed when we use the logs command.
Usecases and best practices for specifying commands and arguments
Usecases
Some usecase where we cam use Commands and Arguments
- Overriding the Default values of Docker Images: Commands and Arguments are useful when we want to influence the container behavior without changing the specifications mentioned in the Docker Image. This is particularly useful when we want to customize the containers.
- Debugging: Commands and Arguments are useful when we want to debug our container. For instance if our container is facing some particular issue then we can use commands and arguments for diagnostic checks.
Best Practices
- It is always recommended not to use passwords in Commands and Arguments. Since the YAML file is accessible by everyone, it is always recommended to use Secrets to store passwords.
- To utilize the Docker images in different scenarios, it is best recommended to utilize the commands and arguments.
- There can be scenarios where multiple containers can utilize the same Docker image but the cases will be different. In that case, a lot of commands and arguments will be utilized. The list of commands and arguments should always be documented for easy understanding.
Common issues and debugging tips
- When we use Commands and Arguments, we can face same common issues . These issues can result in failed execution of container.
- It can be seen that the number of commands and arguments might not be same. This is a common error and this is one of the major reasons of execution failure. It is always recommended to use kubectl describe command as it provides details about commands and arguments.
- Incorrect syntax like extra commas, misplaced quotes can cause the container to fail to start. It is always recommended to check for the YAML files.
- In commands and arguments we often use file paths. Incorrect definition of file paths can cause the failure of container. Always check for correct file paths or use kubectl exec command to check for the same.
- While the execution of commands and arguments, it can be seen that the container does not have access to a particular file. To overcome this issue we can specify chmod command in the commands and arguments section or use kubectl command to get details about the error logs.