Declaratively create a kubernetes pod using kubectl (for docker administrators)

2 min read | by Jordi Prats

The first thing one want to try once we have access to a Kubernetes cluster is to run a pod in it. Let's try to create our first pod on kubernetes, declaratively: meaning that we will be writing a manifest to do it.

For the ones that are familiarized with docker what we are trying to do is equivalent to the following docker run command:

docker run -dt nginx 

To do this on a kubernetes cluster we will have create a pod manifest using yaml:

apiVersion: v1 kind: Pod metadata:  name: demo spec:  containers:  - name: container  image: nginx 

Using this manifest we are telling the kubernetes cluster that a kind: Pod resource needs to be created, it's name is name: demo and we want it to be composed of containers:, which in this case is just one container named - name: container using the image image: nginx.

To be able to apply this manifest we will have to use kubectl apply with the file containing this manifest like so:

$ kubectl apply -f /tmp/pod.yaml  pod/demo created 

On docker, to check whether the container have been created we would use docker ps:

$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bbf4ccca3dc0 nginx "/docker-entrypoint.…" 7 seconds ago Up 2 seconds 80/tcp xenodochial_khayyam 

On a kubernetes cluster it's most close equivalent would be kubectl get pods. Once the the kubectl apply have been issues it's going to take a while to get the pod up and runnig, here is what it looks like when the pod is still being created:

$ kubectl get pods NAME READY STATUS RESTARTS AGE demo 0/1 ContainerCreating 0 9s 

Once it's reade we will see the STATUS column that changes to Running:

$ kubectl get pods NAME READY STATUS RESTARTS AGE demo 1/1 Running 0 27s 

Posted on 31/03/2021