Creating the first pod in Kubernetes

To create the pod let's first install the Minikube from here.

Let's start with the basic pod.

vi first-pod.yml
kind: Pod
apiVersion: v1
metadata:
  name: my-pod
spec:
  containers:
    - name: container1
      image: ubuntu
      command: ["/bin/bash","-c","while true; do echo I-am first-pod; sleep 5; done"]
  restartPolicy: Never
  1. kind: This field specifies the type of resources being used. In this case, the resource is a 'Pod', which is the most basic unit of deployment in Kubernetes.

  2. apiVersion: The version of the Kubernetes API that the manifest file is utilizing is specified in this field.

  3. metadata: Information about the resource being defined is contained in this field. In this case, the name field gives the pod a name.

  4. spec: The description of the pod, including the desired state and operating instructions, are contained in this field.

  5. containers: This field specifies the containers that should be run as part of the pod. In this case, there is a single container defined, with the name of container1, an image of ubuntu, and a command to execute when the container starts.

  6. command: This field specifies the command that should be run when the container starts.

  7. restartPolicy: This field specifies the restart policy for the pod. In this case, the restart policy is set to 'Never', which means that the pod will not be restarted if it exists of crashes

To execute the manifest file, use the command

kubectl apply -f <podname>

kubectl apply -f first-pod.yml

The above command is used to create or update resources in the Kubernetes cluster.

The 'apply' subcommand tells 'kubectl' to apply the configuration specified in the specified file in our case which is first-pod.yml to the cluster. If the resources defined in the file do not already exist in the cluster, 'kubectl apply' will create then. If the resources already exist 'kubectl apply' will update them with the new configuration.

The '-f' flag specifies the path to the file containing the resources configuration to apply.

Now to see the pod apply

kubectl get pods

If you want to see, where or in which node pod is running

kubectl get pods -o wide

To see more detailed information about pods

kubectl describe pod <podname> OR

kubectl describe pod/<podname>

kubectl describe my-pod

To view the log generated by the pod in the Kubernetes cluster

kubectl logs -f <podname>

kubectl logs -f my-pod

To delete the pod from the Kubernetes cluster

kubectl delete pod <podname> OR

kubectl delete -f <manifestfile_name.yml>

kubectl delete pod my-pod

Create a multi-container pod

vi multi-container.yml
kind: Pod
apiVersion: v1
metadata:
  name: second-pod
spec:
  containers:
    - name: container1
      image: ubuntu
      command: ["/bin/bash","-c","while true; do echo I-am-container-1; sleep 5; done"]
    - name: container2
      image: ubuntu
      command: ["/bin/bash","-c","while true; do echo I-am-container-1; sleep 5; done"]
kubectl apply -f mulit-container-yml
kubectl logs -f container1

cltrl + c

kubectl logs -f container2

To execute a command in a container

kubectl exec <pod-name> -it -c <container-name> -- /bin/bash

kubectl exec second-pod -it -c container1 -- /bin/bash

In Kubernetes, we can define environment variables for a container in a pod using the 'env' field in the container specification

For example

vi env-pod.yml
kind: Pod
apiVersion: v1
metadata:
  name: env-pod
spec:
  containers:
    - name: container1
      image: ubuntu
      command: ["/bin/bash","-c","while true; do echo It-is-environment demo; sleep 5; done"]
      env:
        - name: MY_NAME
          value: SANDEEP_DHUNGANA
kubectl apply -f env-pod.yml

Now execute a command inside container1 to see the environment variable.

kubectl exec env-pod -it -c container1 --/bin/bash
echo $MY_NAME

To specify the pod with ports

vi pod-port.yml
kind: Pod
apiVersion: v1
metadata:
  name: port-pod
spec:
  containers:
    - name: container1
      image: httpd
      ports:
        - containerPort: 80
kubectl apply -f port-pod.yml

kubectl describe pod <pod-name> for more detail.