Home » DevOps » Kubernetes: Simple example of pod running

About Mark Needham

Mark Needham

Kubernetes: Simple example of pod running

I recently needed to create a Kubernetes pod that would ‘just sit there’ while I used kube cp to copy some files to a persistent volume to which it was bound.

I started out with this naive pod spec:

pod_no_while.yaml

kind: Pod
apiVersion: v1
metadata:
  name: marks-dummy-pod
spec:
  containers:
    - name: marks-dummy-pod
      image: ubuntu
  restartPolicy: Never

Let’s apply that template:

$ kubectl apply -f pod_no_while.yaml 
pod "marks-dummy-pod" created

And let’s check if we have any running pods:

$ kubectl get pods
No resources found, use --show-all to see completed objects.

We won’t see anything here because unsurprisingly the pod has run to completion as there’s nothing to keep it running! We can confirm that by running this command:

$ kubectl get pods --show-all
NAME              READY     STATUS      RESTARTS   AGE
marks-dummy-pod   0/1       Completed   0          1m

Now let’s create a pod that has an infinite bash while loop:

pod.yaml

kind: Pod
apiVersion: v1
metadata:
  name: marks-dummy-pod
spec:
  containers:
    - name: marks-dummy-pod
      image: ubuntu
      command: ["/bin/bash", "-ec", "while :; do echo '.'; sleep 5 ; done"]
  restartPolicy: Never

Let’s apply that one instead:

$ kubectl apply -f pod.yaml 
The Pod "marks-dummy-pod" is invalid: spec: Forbidden: pod updates may not change fields other than `spec.containers[*].image`, `spec.initContainers[*].image`, `spec.activeDeadlineSeconds` or `spec.tolerations` (only additions to existing tolerations)

Oops, we need to delete it first so let’s do that:

$ kubectl delete pod marks-dummy-pod
pod "marks-dummy-pod" deleted

Attempt #2:

$ kubectl apply -f pod.yaml 
pod "marks-dummy-pod" created

And let’s checkup on our pod:

$ kubectl get pods
NAME              READY     STATUS    RESTARTS   AGE
marks-dummy-pod   1/1       Running   0          14s

Looks better already. Let’s check the logs

$ kubectl logs marks-dummy-pod 
.
.

Great! We can now kubectl cp to our heart’s content and then delete the pod aftewards.

Published on System Code Geeks with permission by Mark Needham, partner at our SCG program. See the original article here: Kubernetes: Simple example of pod running

Opinions expressed by System Code Geeks contributors are their own.

Do you want to know how to develop your skillset to become a sysadmin Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. Introduction to NGINX

2. Apache HTTP Server Cookbook

3. VirtualBox Essentials

4. Nagios Monitoring Cookbook

5. Linux BASH Programming Cookbook

6. Postgresql Database Tutorial

 

and many more ....

 

 

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz