kubectl for Docker Users

You can use the Kubernetes command line tool kubectl to interact with the API Server. Using kubectl is straightforward if you are familiar with the Docker command line tool. However, there are a few differences between the docker commands and the kubectl commands. The following sections show a docker sub-command and describe the equivalent kubectl command.

docker run

To run an nginx Deployment and expose the Deployment, see kubectl create deployment. docker:

  1. docker run -d --restart=always -e DOMAIN=cluster --name nginx-app -p 80:80 nginx
  1. 55c103fa129692154a7652490236fee9be47d70a8dd562281ae7d2f9a339a6db
  1. docker ps
  1. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  2. 55c103fa1296 nginx "nginx -g 'daemon of…" 9 seconds ago Up 9 seconds 0.0.0.0:80->80/tcp nginx-app

kubectl:

  1. # start the pod running nginx
  2. kubectl create deployment --image=nginx nginx-app
  1. # add env to nginx-app
  2. kubectl set env deployment/nginx-app DOMAIN=cluster
  1. deployment.apps/nginx-app created
  1. # add env to nginx-app
  2. kubectl set env deployment/nginx-app DOMAIN=cluster
  1. deployment.apps/nginx-app env updated

Note: kubectl commands print the type and name of the resource created or mutated, which can then be used in subsequent commands. You can expose a new Service after a Deployment is created.

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

By using kubectl, you can create a Deployment to ensure that N pods are running nginx, where N is the number of replicas stated in the spec and defaults to 1. You can also create a service with a selector that matches the pod labels. For more information, see Use a Service to Access an Application in a Cluster.

By default images run in the background, similar to docker run -d .... To run things in the foreground, use kubectl run to create pod:

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

Unlike docker run ..., if you specify --attach, then you attach stdin, stdout and stderr. You cannot control which streams are attached (docker -a ...). To detach from the container, you can type the escape sequence Ctrl+P followed by Ctrl+Q.

docker ps

To list what is currently running, see kubectl get.

docker:

  1. docker ps -a
  1. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  2. 14636241935f ubuntu:16.04 "echo test" 5 seconds ago Exited (0) 5 seconds ago cocky_fermi
  3. 55c103fa1296 nginx "nginx -g 'daemon of…" About a minute ago Up About a minute 0.0.0.0:80->80/tcp nginx-app

kubectl:

  1. kubectl get po
  1. NAME READY STATUS RESTARTS AGE
  2. nginx-app-8df569cb7-4gd89 1/1 Running 0 3m
  3. ubuntu 0/1 Completed 0 20s

docker attach

To attach a process that is already running in a container, see kubectl attach.

docker:

  1. docker ps
  1. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  2. 55c103fa1296 nginx "nginx -g 'daemon of…" 5 minutes ago Up 5 minutes 0.0.0.0:80->80/tcp nginx-app
  1. docker attach 55c103fa1296
  2. ...

kubectl:

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

To detach from the container, you can type the escape sequence Ctrl+P followed by Ctrl+Q.

docker exec

To execute a command in a container, see kubectl exec.

docker:

  1. docker ps
  1. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  2. 55c103fa1296 nginx "nginx -g 'daemon of…" 6 minutes ago Up 6 minutes 0.0.0.0:80->80/tcp nginx-app
  1. docker exec 55c103fa1296 cat /etc/hostname
  1. 55c103fa1296

kubectl:

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

To use interactive commands.

docker:

  1. docker exec -ti 55c103fa1296 /bin/sh
  2. # exit

kubectl:

  1. kubectl exec -ti nginx-app-5jyvm -- /bin/sh
  2. # exit

For more information, see Get a Shell to a Running Container.

docker logs

To follow stdout/stderr of a process that is running, see kubectl logs.

docker:

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

kubectl:

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

There is a slight difference between pods and containers; by default pods do not terminate if their processes exit. Instead the pods restart the process. This is similar to the docker run option --restart=always with one major difference. In docker, the output for each invocation of the process is concatenated, but for Kubernetes, each invocation is separate. To see the output from a previous run in Kubernetes, do this:

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

For more information, see Logging Architecture.

docker stop and docker rm

To stop and delete a running process, see kubectl delete.

docker:

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

kubectl:

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

Note: When you use kubectl, you don’t delete the pod directly. You have to first delete the Deployment that owns the pod. If you delete the pod directly, the Deployment recreates the pod.

docker login

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

docker version

To get the version of client and server, see kubectl version.

docker:

  1. docker version
  1. Client version: 1.7.0
  2. Client API version: 1.19
  3. Go version (client): go1.4.2
  4. Git commit (client): 0baf609
  5. OS/Arch (client): linux/amd64
  6. Server version: 1.7.0
  7. Server API version: 1.19
  8. Go version (server): go1.4.2
  9. Git commit (server): 0baf609
  10. OS/Arch (server): linux/amd64

kubectl:

  1. kubectl version
  1. 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"}
  2. 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

To get miscellaneous information about the environment and configuration, see kubectl cluster-info.

docker:

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

kubectl:

  1. kubectl cluster-info
  1. Kubernetes master is running at https://203.0.113.141
  2. KubeDNS is running at https://203.0.113.141/api/v1/namespaces/kube-system/services/kube-dns/proxy
  3. kubernetes-dashboard is running at https://203.0.113.141/api/v1/namespaces/kube-system/services/kubernetes-dashboard/proxy
  4. Grafana is running at https://203.0.113.141/api/v1/namespaces/kube-system/services/monitoring-grafana/proxy
  5. Heapster is running at https://203.0.113.141/api/v1/namespaces/kube-system/services/monitoring-heapster/proxy
  6. InfluxDB is running at https://203.0.113.141/api/v1/namespaces/kube-system/services/monitoring-influxdb/proxy