Let’s start by creating your first Pod.

apiVersion: v1
kind: Pod
metadata:
  name: hello-pod
  labels:
    app.kubernetes.io/name: hello
spec:
  containers:
    - name: hello-container
      image: busybox
      command: ["sh", "-c", "echo Hello from my container! && sleep 3600"]

Save the above YAML to pod.yaml and use the following command to create the Pod:

kubectl apply -f pod.yaml

If all goes well, you will get the following output:

pod/hello-pod created

Kubernetes responds with the resource type and the name it created. You can use kubectl get pods to get a list of all Pods running the default namespace of the cluster.

$ kubectl get pods
NAME        READY   STATUS    RESTARTS   AGE
hello-pod   1/1     Running   0          7s

You can use the logs command to see the output from the container running inside the Pod:

$ kubectl logs hello-podHello from my container!

When running multiple containers running inside the same Pod, you can use the -c flag to specify the container name whose logs you want to get. For example: kubectl logs hello-pod -c hello-container

If we delete this Pod using kubectl delete pod hello-pod, the Pod will be gone forever. There’s nothing that would automatically restart it.

If you run the kubectl get pods again, you will notice the Pod is gone:

$ kubectl get pods
No resources found in default namespace.

Not automatically recreating the Pod is the opposite of the behavior you want. If you have your containers running in a Pod, you would want Kubernetes to reschedule and restart them if something goes wrong automatically.