A guide for Docker users transitioning to the kubectl command line

Transitioning from Docker CLI to Kubernetes kubectl
Transitioning from Docker CLI to Kubernetes kubectl

For Docker users new to Kubernetes, learning the kubectl command efficiently is key.

This guide aims to bridge the gap between Docker CLI and Kubernetes CLI, explaining how kubectl interacts with Kubernetes’ API. While kubectl is made to feel intuitive for Docker users, there are notable distinctions. We’ll compare Docker commands to their kubectl counterparts, easing the transition.

In a Kubernetes environment, direct Docker commands are less common, typically reserved for debugging. The shift to Kubernetes involves primarily using kubectl for operations, moving away from some Docker-centric practices.

docker run

How to run an nginx Deployment and expose it? Check out kubectl run .

Use docker command:

$ docker run -d --restart=always -e DOMAIN=cluster --name nginx-app -p 80:80 nginx
a9ec34d9878748d2f33dc20cb25c714ff21da8d40558b45bfaec9955859075d0
$ docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                         NAMES
a9ec34d98787        nginx               "nginx -g 'daemon of   2 seconds ago       Up 2 seconds        0.0.0.0:80->80/tcp, 443/tcp   nginx-app 

Use kubectl command:

# start the pod running nginx
$ kubectl run --image=nginx nginx-app --port=80 --env="DOMAIN=cluster"
deployment "nginx-app" created


When operating on a Kubernetes cluster version 1.2 or higher, executing kubectl run will generate a Deployment named “nginx-app”. In older versions, a replication controller is generated instead. To maintain the previous behavior, include the --generation=run/v1 parameter, resulting in the creation of a replication controller. Refer to kubectl run for additional information.

# expose a port through with a service
$ kubectl expose deployment nginx-app --port=80 --name=nginx-http
service "nginx-http" exposed

With the kubectl command, we establish a Deployment, ensuring that a specified number of N pods are running nginx (where N represents the number of replicas defined in the spec, defaulting to 1). Additionally, we set up a service that employs a selector to pair Deployments with their respective selectors. Further details can be found in the Quick Start guide.

kubectl run [-i] [--tty] --attach <name> --image=<image>

Unlike the behavior of docker run, when using kubectl attach with the --attach flag, we connect to stdin, stdout, and stderr simultaneously, without the ability to select a specific output stream (similar to docker -a).

docker ps

How to list which ones are running? Check out kubectl get .

Use docker command:

$ docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                         NAMES
a9ec34d98787        nginx               "nginx -g 'daemon of   About an hour ago   Up About an hour    0.0.0.0:80->80/tcp, 443/tcp   nginx-app

Use kubectl command:

$ kubectl get po
NAME              READY     STATUS    RESTARTS   AGE
nginx-app-5jyvm   1/1       Running   0          1h

docker attach

How to connect to a process already running in a container? Check out kubectl attach .

Use docker command:

$ docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                         NAMES
a9ec34d98787        nginx               "nginx -g 'daemon of   8 minutes ago       Up 8 minutes        0.0.0.0:80->80/tcp, 443/tcp   nginx-app
$ docker attach a9ec34d98787
...

Use kubectl command:

$ kubectl get pods
NAME              READY     STATUS    RESTARTS   AGE
nginx-app-5jyvm   1/1       Running   0          10m
$ kubectl attach -it nginx-app-5jyvm
...

docker exec

How to execute command in container? Check out kubectl exec .

Use docker command:

$ docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                         NAMES
a9ec34d98787        nginx               "nginx -g 'daemon of   8 minutes ago       Up 8 minutes        0.0.0.0:80->80/tcp, 443/tcp   nginx-app
$ docker exec a9ec34d98787 cat /etc/hostname
a9ec34d98787

Use kubectl command:

$ kubectl get po
NAME              READY     STATUS    RESTARTS   AGE
nginx-app-5jyvm   1/1       Running   0          10m
$ kubectl exec nginx-app-5jyvm -- cat /etc/hostname
nginx-app-5jyvm

What to do when executing interactive commands?

Use docker command:

$ docker exec -ti a9ec34d98787 /bin/sh
# exit

Use kubectl command:

$ kubectl exec -ti nginx-app-5jyvm -- /bin/sh      
# exit

docker logs

How to view stdout/stderr of a running process? View kubectl logs .

Use docker command:

$ docker logs -f a9e
192.168.9.1 - - [14/Jul/2015:01:04:02 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.35.0" "-"
192.168.9.1 - - [14/Jul/2015:01:04:03 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.35.0" "-"

Use kubectl command:

$ kubectl logs -f nginx-app-zibvs
10.240.63.110 - - [14/Jul/2015:01:09:01 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.26.0" "-"
10.240.63.110 - - [14/Jul/2015:01:09:02 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.26.0" "-"


It’s important to note a subtle distinction between pods and containers: in Kubernetes, by default, if a process within a pod exits, the pod itself will not terminate; instead, the process will restart. This behavior mirrors the docker run --restart=always option, which is a key distinction. Additionally, in Docker, the output of each process call is concatenated, whereas in Kubernetes, each call produces separate outputs. To view the output of a previous execution in Kubernetes:

$ kubectl logs --previous nginx-app-zibvs
10.240.63.110 - - [14/Jul/2015:01:09:01 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.26.0" "-"
10.240.63.110 - - [14/Jul/2015:01:09:02 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.26.0" "-"

View logs and monitor cluster activity for more information.

docker stop and docker rm

How to stop and delete running processes? See kubectl delete .

Use docker command:

$ docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                         NAMES
a9ec34d98787        nginx               "nginx -g 'daemon of   22 hours ago        Up 22 hours         0.0.0.0:80->80/tcp, 443/tcp   nginx-app
$ docker stop a9ec34d98787
a9ec34d98787
$ docker rm a9ec34d98787
a9ec34d98787

Use kubectl command:

$ kubectl get deployment nginx-app
NAME        DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx-app   1         1         1            1           2m
$ kubectl get po -l run=nginx-app
NAME                         READY     STATUS    RESTARTS   AGE
nginx-app-2883164633-aklf7   1/1       Running   0          2m
$ kubectl delete deployment nginx-app
deployment "nginx-app" deleted
$ kubectl get po -l run=nginx-app
# Return nothing

Please note that we do not delete pods directly. Using the kubectl command, we want to delete the Deployment that owns the pod. If we delete the pod directly, Deployment will recreate the pod.

docker login

There is no direct emulation of in kubectl docker login. If you are interested in using Kubernetes in a private registry, see Using a Private Registry .

docker version

How to check the client and server versions? Check kubectl version .

Use docker command:

$ docker version
Client version: 1.7.0
Client API version: 1.19
Go version (client): go1.4.2
Git commit (client): 0baf609
OS/Arch (client): linux/amd64
Server version: 1.7.0
Server API version: 1.19
Go version (server): go1.4.2
Git commit (server): 0baf609
OS/Arch (server): linux/amd64

Use kubectl command:

$ kubectl version
Client Version: version.Info{Major:"1", Minor:"6", GitVersion:"v1.6.9+a3d1dfa6f4335", GitCommit:"9b77fed11a9843ce3780f70dd251e92901c43072", GitTreeState:"dirty", BuildDate:"2017-08-29T20:32:58Z", OpenPaasKubernetesVersion:"v1.03.02", GoVersion:"go1.7.5", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"6", GitVersion:"v1.6.9+a3d1dfa6f4335", GitCommit:"9b77fed11a9843ce3780f70dd251e92901c43072", GitTreeState:"dirty", BuildDate:"2017-08-29T20:32:58Z", OpenPaasKubernetesVersion:"v1.03.02", GoVersion:"go1.7.5", Compiler:"gc", Platform:"linux/amd64"}

docker info

How can I get various information about the environment and configuration? Check kubectl cluster-info .

Use docker command:

$ docker info
Containers: 40
Images: 168
Storage Driver: aufs
 Root Dir: /usr/local/google/docker/aufs
 Backing Filesystem: extfs
 Dirs: 248
 Dirperm1 Supported: false
Execution Driver: native-0.2
Logging Driver: json-file
Kernel Version: 3.13.0-53-generic
Operating System: Ubuntu 14.04.2 LTS
CPUs: 12
Total Memory: 31.32 GiB
Name: k8s-is-fun.mtv.corp.google.com
ID: ADUV:GCYR:B3VJ:HMPO:LNPQ:KD5S:YKFQ:76VN:IANZ:7TFV:ZBF4:BYJO
WARNING: No swap limit support

Use kubectl command:

$ kubectl cluster-info
Kubernetes master is running at https://108.59.85.141
KubeDNS is running at https://108.59.85.141/api/v1/namespaces/kube-system/services/kube-dns/proxy
KubeUI is running at https://108.59.85.141/api/v1/namespaces/kube-system/services/kube-ui/proxy
Grafana is running at https://108.59.85.141/api/v1/namespaces/kube-system/services/monitoring-grafana/proxy
Heapster is running at https://108.59.85.141/api/v1/namespaces/kube-system/services/monitoring-heapster/proxy
InfluxDB is running at https://108.59.85.141/api/v1/namespaces/kube-system/services/monitoring-influxdb/proxy

Author

0 Shares:
You May Also Like
Make Kubernetes simpler! 8 AI Tools You Must Know
Read More

Make Kubernetes simpler! 8 AI Tools You Must Know

Table of Contents Show OverviewK8sGPTInstallPrerequisiteskubectl-aiInstall via Homebrew:Install via Krew:DemoKoPylotFunctionOperating principleKopilotInstallKubectl-GPTInstallPrerequisitesKube-CopilotInstallSet operationKubernetes ChatGPT botDemoAppilotAuthor Overview Kubernetes users inevitably face…