kubectl: create pod

Let’s try creating a pod.

  1. create a pod.ymlwith your favourite image:
apiVersion: v1
kind: Pod
metadata:
    name: hello-pod
    labels:
        zone: prod
        version: v1
spec:
    containers:
    - name: hello-ctr
      image: some/image:latest
      ports:
      - containerPort: 8080

 

Then:

kubectl create -f pod.yml

returns

pod/hello-pod created

but we need to check what’s going on behind the scenes with:

kubectl get pods

Or if you have loads of pods, you can use:

kubectl get pods/hello-pod

or all pods in all namespaces:

kubectl get pods --all-namespaces

“kubectl get pods” warnings

kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-pod 0/1 ContainerCreating 0 1m

kubectl describe pods

This:

Reason: ContainerCreating

is because it’s still creating the container.

kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-pod 0/1 ImagePullBackOff 0 10m

Again:

kubectl describe pods

Reason: ImagePullBackOff

means there’s a problem with the image.

Check with:

docker inspect --type=image <name of image>:latest

I was getting:

Error: No such image:

So, let’s try another image. i.e. in pod.ymllet’s use:

image: hello-world:latest

Now create hits:

Error from server (AlreadyExists): error when creating "pod.yml": pods "hello-pod" already exists

 

Let’s try deleting this pod with:

kubectl delete hello-pod error: resource(s) were provided, but no name, label selector, or --all flag specified

https://kubernetes.io/docs/tasks/run-application/force-delete-stateful-set-pod/

 

Let’s try --all as mentioned:

kubectl delete hello-pod --all
error: the server doesn't have a resource type "hello-pod"

Solution is:

kubectl delete pod hello-pod
pod "hello-pod" deleted

Note: this can take a few minutes.

https://kubernetes.io/docs/tasks/run-application/force-delete-stateful-set-pod/

 

Let’s try create again:

kubectl get pods
NAME        READY     STATUS             RESTARTS   AGE
hello-pod   0/1       CrashLoopBackOff   1          11s

What is a CrashLoopBackOff? How to alert, debug / troubleshoot, and fix Kubernetes CrashLoopBackOff events.

Why am I getting this?

‘cos the hello-world image exits after printing out its introductory message.

Let’s try:

image: yeasy/simple-web:latest

We can keep track of events with:

kubectl get events

After around 5 mins it comes up. Let’s interact with the web server. First what IP does it have?

kubectl get pods -l zone=prod -o yaml | grep IP

kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-pod 1/1 Running 0 12m

kubectl exec -it hello-pod — /bin/bash

and

curl -i localhost:80

shows it’s working.

 

Note: you’d normally deploy a pod using a ReplicationController.

Delete existing pod with:

kubectl delete pods hello-pod

and create a ReplicationController with:

apiVersion: v1
kind: ReplicationController
metadata:
    name: hello-rc
spec:
    replicas: 10
    selector:
        app: hello-world
    template:
        metadata:
            labels:
              app: hello-world
        spec:
            containers:
            - name: hello-pod
              image: yeasy/simple-web:latest
              ports:
              - containerPort: 8080

Note: 4 top-level objects are:

  • apiVersion
  • kind
  • metadata
  • spec

then

kubectl create -f rc.yml

And inspect with:

kubectl get rc
NAME       DESIRED   CURRENT   READY     AGE
hello-rc   10        10        10        2m
 firstpod   master  kubectl describe rc
Name:         hello-rc
Namespace:    default
Selector:     app=hello-world
Labels:       app=hello-world
Annotations:  <none>
Replicas:     10 current / 10 desired
Pods Status:  10 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=hello-world
  Containers:
   hello-pod:
    Image:        yeasy/simple-web:latest
    Port:         8080/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age   From                    Message
  ----    ------            ----  ----                    -------
  Normal  SuccessfulCreate  2m    replication-controller  Created pod: hello-rc-tp5vf
  Normal  SuccessfulCreate  2m    replication-controller  Created pod: hello-rc-wvqlt
  Normal  SuccessfulCreate  2m    replication-controller  Created pod: hello-rc-vkq6r
  Normal  SuccessfulCreate  2m    replication-controller  Created pod: hello-rc-7j5qt
  Normal  SuccessfulCreate  2m    replication-controller  Created pod: hello-rc-rpbx9
  Normal  SuccessfulCreate  2m    replication-controller  Created pod: hello-rc-hl4zw
  Normal  SuccessfulCreate  2m    replication-controller  Created pod: hello-rc-7c52w
  Normal  SuccessfulCreate  2m    replication-controller  Created pod: hello-rc-k7wjg
  Normal  SuccessfulCreate  2m    replication-controller  Created pod: hello-rc-plgbf
  Normal  SuccessfulCreate  2m    replication-controller  (combined from similar events): Created pod: hello-rc-dsj2r

To increase replicas, update rc.yml and apply with:

kubectl apply -f rc.yml

 

Other errors

Unable to connect to the server

Unable to connect to the server: dial tcp 192.168.64.5:8443: i/o timeout

  • check minikube is started – minikube start
kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-pod 0/1 ContainerCreating 0 1m

kubectl describe pods

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *