kubectl run NAME - clarifying

1/9/2019

According to the docs:

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#run

kubectl run NAME --image=image

will run an image.

Some questions:

  • I assume this is a pod rather than a container?

  • And I assume NAME is associated with the pod?

-- Snowcrash
kubectl
kubernetes

2 Answers

1/9/2019

Snowcrash, you are correct. This is basically the same as docker run command. So using kubectl run NAME --image=image will exactly run a pod named NAME from docker image called image. You can check what exactly is happening by using kubectl describe pod NAME Here is an example of kubectl run nginx --image=nginx

Events:
  Type     Reason            Age                From                    Message
  ----     ------            ----               ----                    -------
  Warning  FailedScheduling  89s (x2 over 89s)  default-scheduler       0/2 nodes are available: 2 node(s) had taints that the pod didn't tolerate.
  Normal   Scheduled         19s                default-scheduler       Successfully assigned default/nginx-7cdbd8cdc9-glkxq to centos-master
  Normal   Pulling           18s                kubelet, centos-master  pulling image "nginx"
  Normal   Pulled            14s                kubelet, centos-master  Successfully pulled image "nginx"
  Normal   Created           14s                kubelet, centos-master  Created container
  Normal   Started           14s                kubelet, centos-master  Started container

So what happened after kubectl run is:

  • Scheduler was trying to pick a node to launch the container (at first it failed due to a taints, because my node is in NotReady state (not important at the moment, but you can read more about it here)

  • Scheduler successfully assigned the pod to the node (centos-master).

  • The kubelet checks if the docker image is available and pulls it if necessary.

  • Then the container is created and started.

*here you can find an interesting article which explains this in a little more detailed way.

The name is associated with pod, because Pod is the smallest unit of work in Kubernetes. Each pod can contain one or more containers. All the containers in the Pod have the same IP address and port space, can access shared storage on the Node hosting that pod.

Basically the kubectl command-line tool supports several different ways to create and manage Kubernetes objects:

  • Imperative commands
  • Imperative object configuration
  • Declarative object configuration

*you can find more about them in this StackOverflow answer or this Medium article.

run command is an example of imperative approach. Is the simplest to start

[...] Because this technique operates directly on live objects, it provides no history of previous configurations.

-- aurelius
Source: StackOverflow

1/9/2019

The kubectl run NAME --image=image command, will raise a pod (by creating a corresponding deployment and replication set for given image) with name NAME using the image named image. Think of name here as the tag name in Docker.

Then, when you run kubectl get pods, you will see a pod with name NAME in list.

Using kubectl get all will fetch all resources in default name space (which will also list the deployment and replication set object)

Example :

$ kubectl run nginx --image=nginx

$ kubectl get all
NAME                         READY   STATUS              RESTARTS   AGE
pod/nginx-6db489d4b7-cs748   0/1     ContainerCreating   0          6s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   31d

NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx   0/1     1            0           6s

NAME                               DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-6db489d4b7   1         1         0       6s

If you are looking to create just a pod with no additional deployment and replication controller objects use below imerative command

kubectl run NAME --image=image --restart=Never

when you list result of above command (refer below output) you should see only the corresponding pod (no deployment and replication set object are created)

$ kubectl run nginx --image=nginx --restart=Never
pod/nginx created

$ kubectl get all
NAME                      READY   STATUS              RESTARTS   AGE
pod/nginx                 0/1     ContainerCreating   0          3s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   31d
-- Mauro Baraldi
Source: StackOverflow