2016-02-10 20:29:07 +00:00
---
2018-05-21 15:55:51 +00:00
title: kubectl for Docker Users
2018-10-30 16:42:16 +00:00
content_template: templates/concept
2018-05-21 15:55:51 +00:00
reviewers:
2016-07-29 17:36:25 +00:00
- bgrant0607
- brendandburns
- thockin
2016-02-10 20:29:07 +00:00
---
2016-02-26 11:54:48 +00:00
2018-10-30 16:42:16 +00:00
{{% capture overview %}}
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.
{{% /capture %}}
2016-02-26 11:54:48 +00:00
2018-10-30 16:42:16 +00:00
{{% capture body %}}
## docker run
2016-02-26 11:54:48 +00:00
2018-04-27 22:02:19 +00:00
To run an nginx Deployment and expose the Deployment, see [kubectl run ](/docs/reference/generated/kubectl/kubectl-commands/#run ).
2016-02-26 11:54:48 +00:00
2018-03-22 17:18:04 +00:00
docker:
2016-02-26 11:54:48 +00:00
```shell
$ docker run -d --restart=always -e DOMAIN=cluster --name nginx-app -p 80:80 nginx
2017-11-08 17:24:33 +00:00
55c103fa129692154a7652490236fee9be47d70a8dd562281ae7d2f9a339a6db
2016-02-26 11:54:48 +00:00
$ docker ps
2017-11-08 17:24:33 +00:00
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
55c103fa1296 nginx "nginx -g 'daemon of…" 9 seconds ago Up 9 seconds 0.0.0.0:80->80/tcp nginx-app
2016-02-26 11:54:48 +00:00
```
2018-03-22 17:18:04 +00:00
kubectl:
2016-02-26 11:54:48 +00:00
```shell
# start the pod running nginx
$ kubectl run --image=nginx nginx-app --port=80 --env="DOMAIN=cluster"
2016-03-28 17:58:08 +00:00
deployment "nginx-app" created
2016-03-29 01:08:58 +00:00
```
2018-05-05 16:00:51 +00:00
{{< note > }}
2018-11-06 19:33:04 +00:00
`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.
2018-05-05 16:00:51 +00:00
{{< / note > }}
2016-03-29 01:08:58 +00:00
```shell
2016-02-26 11:54:48 +00:00
# expose a port through with a service
2016-03-28 17:58:08 +00:00
$ kubectl expose deployment nginx-app --port=80 --name=nginx-http
service "nginx-http" exposed
2016-02-26 11:54:48 +00:00
```
2018-03-22 17:18:04 +00:00
By using kubectl, you can create a [Deployment ](/docs/concepts/workloads/controllers/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 ](/docs/concepts/services-networking/service/ ) with a selector that matches the pod labels. For more information, see [Use a Service to Access an Application in a Cluster ](/docs/tasks/access-application-cluster/service-access-application-cluster ).
2016-02-26 11:54:48 +00:00
2018-03-22 17:18:04 +00:00
By default images run in the background, similar to `docker run -d ...` . To run things in the foreground, use:
2016-02-26 11:54:48 +00:00
```shell
kubectl run [-i] [--tty] --attach < name > --image=< image >
```
2018-03-22 17:18:04 +00:00
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.
2016-02-26 11:54:48 +00:00
2018-03-22 17:18:04 +00:00
Because the kubectl run command starts a Deployment for the container, the Deployment restarts if you terminate the attached process by using Ctrl+C, unlike `docker run -it` .
To destroy the Deployment and its pods you need to run `kubectl delete deployment <name>` .
2016-02-26 11:54:48 +00:00
2018-10-30 16:42:16 +00:00
## docker ps
2016-02-26 11:54:48 +00:00
2018-04-27 22:02:19 +00:00
To list what is currently running, see [kubectl get ](/docs/reference/generated/kubectl/kubectl-commands/#get ).
2016-02-26 11:54:48 +00:00
2018-03-22 17:18:04 +00:00
docker:
2016-02-26 11:54:48 +00:00
```shell
2017-11-08 17:24:33 +00:00
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
14636241935f ubuntu:16.04 "echo test" 5 seconds ago Exited (0) 5 seconds ago cocky_fermi
55c103fa1296 nginx "nginx -g 'daemon of…" About a minute ago Up About a minute 0.0.0.0:80->80/tcp nginx-app
2016-02-26 11:54:48 +00:00
```
2018-03-22 17:18:04 +00:00
kubectl:
2016-02-26 11:54:48 +00:00
```shell
2018-11-28 12:55:28 +00:00
$ kubectl get po
2017-11-08 17:24:33 +00:00
NAME READY STATUS RESTARTS AGE
nginx-app-8df569cb7-4gd89 1/1 Running 0 3m
ubuntu 0/1 Completed 0 20s
2016-02-26 11:54:48 +00:00
```
2018-10-30 16:42:16 +00:00
## docker attach
2016-02-26 11:54:48 +00:00
2018-04-27 22:02:19 +00:00
To attach a process that is already running in a container, see [kubectl attach ](/docs/reference/generated/kubectl/kubectl-commands/#attach ).
2016-02-26 11:54:48 +00:00
2018-03-22 17:18:04 +00:00
docker:
2016-02-26 11:54:48 +00:00
```shell
$ docker ps
2017-11-08 17:24:33 +00:00
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
55c103fa1296 nginx "nginx -g 'daemon of…" 5 minutes ago Up 5 minutes 0.0.0.0:80->80/tcp nginx-app
$ docker attach 55c103fa1296
2016-02-26 11:54:48 +00:00
...
```
2018-03-22 17:18:04 +00:00
kubectl:
2016-02-26 11:54:48 +00:00
```shell
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-app-5jyvm 1/1 Running 0 10m
2017-11-08 17:24:33 +00:00
2016-02-26 11:54:48 +00:00
$ kubectl attach -it nginx-app-5jyvm
...
```
2018-03-22 17:18:04 +00:00
To detach from the container, you can type the escape sequence Ctrl+P followed by Ctrl+Q.
2018-01-25 22:12:34 +00:00
2018-10-30 16:42:16 +00:00
## docker exec
2016-02-26 11:54:48 +00:00
2018-04-27 22:02:19 +00:00
To execute a command in a container, see [kubectl exec ](/docs/reference/generated/kubectl/kubectl-commands/#exec ).
2016-02-26 11:54:48 +00:00
2018-03-22 17:18:04 +00:00
docker:
2016-02-26 11:54:48 +00:00
```shell
$ docker ps
2017-11-08 17:24:33 +00:00
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
55c103fa1296 nginx "nginx -g 'daemon of…" 6 minutes ago Up 6 minutes 0.0.0.0:80->80/tcp nginx-app
$ docker exec 55c103fa1296 cat /etc/hostname
55c103fa1296
2016-02-26 11:54:48 +00:00
```
2018-03-22 17:18:04 +00:00
kubectl:
2016-02-26 11:54:48 +00:00
```shell
$ kubectl get po
NAME READY STATUS RESTARTS AGE
nginx-app-5jyvm 1/1 Running 0 10m
2017-11-08 17:24:33 +00:00
2016-02-26 11:54:48 +00:00
$ kubectl exec nginx-app-5jyvm -- cat /etc/hostname
nginx-app-5jyvm
```
2018-03-22 17:18:04 +00:00
To use interactive commands.
2016-02-26 11:54:48 +00:00
2018-03-22 17:18:04 +00:00
docker:
2016-02-26 11:54:48 +00:00
```shell
2017-11-08 17:24:33 +00:00
$ docker exec -ti 55c103fa1296 /bin/sh
2016-02-26 11:54:48 +00:00
# exit
```
2018-03-22 17:18:04 +00:00
kubectl:
2016-02-26 11:54:48 +00:00
```shell
$ kubectl exec -ti nginx-app-5jyvm -- /bin/sh
# exit
```
2018-03-22 17:18:04 +00:00
For more information, see [Get a Shell to a Running Container ](/docs/tasks/debug-application-cluster/get-shell-running-container/ ).
2016-02-26 11:54:48 +00:00
2018-10-30 16:42:16 +00:00
## docker logs
2016-02-26 11:54:48 +00:00
2018-04-27 22:02:19 +00:00
To follow stdout/stderr of a process that is running, see [kubectl logs ](/docs/reference/generated/kubectl/kubectl-commands/#logs ).
2016-02-26 11:54:48 +00:00
2018-03-22 17:18:04 +00:00
docker:
2016-02-26 11:54:48 +00:00
```shell
$ 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" "-"
```
2018-03-22 17:18:04 +00:00
kubectl:
2016-02-26 11:54:48 +00:00
```shell
$ 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" "-"
```
2018-03-22 17:18:04 +00:00
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:
2016-02-26 11:54:48 +00:00
```shell
$ 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" "-"
```
2018-04-09 14:21:10 +00:00
For more information, see [Logging Architecture ](/docs/concepts/cluster-administration/logging/ ).
2016-02-26 11:54:48 +00:00
2018-10-30 16:42:16 +00:00
## docker stop and docker rm
2016-02-26 11:54:48 +00:00
2018-04-27 22:02:19 +00:00
To stop and delete a running process, see [kubectl delete ](/docs/reference/generated/kubectl/kubectl-commands/#delete ).
2016-02-26 11:54:48 +00:00
2018-03-22 17:18:04 +00:00
docker:
2016-02-26 11:54:48 +00:00
```shell
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2017-11-08 17:24:33 +00:00
a9ec34d98787 nginx "nginx -g 'daemon of" 22 hours ago Up 22 hours 0.0.0.0:80->80/tcp, 443/tcp nginx-app
2016-02-26 11:54:48 +00:00
$ docker stop a9ec34d98787
a9ec34d98787
2017-11-08 17:24:33 +00:00
2016-02-26 11:54:48 +00:00
$ docker rm a9ec34d98787
a9ec34d98787
```
2018-03-22 17:18:04 +00:00
kubectl:
2016-02-26 11:54:48 +00:00
```shell
2016-03-28 17:58:08 +00:00
$ kubectl get deployment nginx-app
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx-app 1 1 1 1 2m
2017-11-08 17:24:33 +00:00
2016-03-28 17:58:08 +00:00
$ kubectl get po -l run=nginx-app
NAME READY STATUS RESTARTS AGE
nginx-app-2883164633-aklf7 1/1 Running 0 2m
2017-11-08 17:24:33 +00:00
2016-03-28 17:58:08 +00:00
$ kubectl delete deployment nginx-app
deployment "nginx-app" deleted
2017-11-08 17:24:33 +00:00
2016-03-28 17:58:08 +00:00
$ kubectl get po -l run=nginx-app
# Return nothing
2016-02-26 11:54:48 +00:00
```
2018-05-05 16:00:51 +00:00
{{< note > }}
2018-11-06 19:33:04 +00:00
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.
2018-05-05 16:00:51 +00:00
{{< / note > }}
2016-02-26 11:54:48 +00:00
2018-10-30 16:42:16 +00:00
## docker login
2016-02-26 11:54:48 +00:00
2017-03-20 22:35:51 +00:00
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 ](/docs/concepts/containers/images/#using-a-private-registry ).
2016-02-26 11:54:48 +00:00
2018-10-30 16:42:16 +00:00
## docker version
2016-02-26 11:54:48 +00:00
2018-04-27 22:02:19 +00:00
To get the version of client and server, see [kubectl version ](/docs/reference/generated/kubectl/kubectl-commands/#version ).
2016-02-26 11:54:48 +00:00
2018-03-22 17:18:04 +00:00
docker:
2016-02-26 11:54:48 +00:00
```shell
$ 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
```
2018-03-22 17:18:04 +00:00
kubectl:
2016-02-26 11:54:48 +00:00
```shell
$ kubectl version
2017-09-02 02:24:35 +00:00
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"}
2016-02-26 11:54:48 +00:00
```
2018-10-30 16:42:16 +00:00
## docker info
2016-02-26 11:54:48 +00:00
2018-04-27 22:02:19 +00:00
To get miscellaneous information about the environment and configuration, see [kubectl cluster-info ](/docs/reference/generated/kubectl/kubectl-commands/#cluster-info ).
2016-02-26 11:54:48 +00:00
2018-03-22 17:18:04 +00:00
docker:
2016-02-26 11:54:48 +00:00
```shell
$ 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
```
2018-03-22 17:18:04 +00:00
kubectl:
2016-02-26 11:54:48 +00:00
```shell
$ kubectl cluster-info
Kubernetes master is running at https://108.59.85.141
2017-03-09 18:12:17 +00:00
KubeDNS is running at https://108.59.85.141/api/v1/namespaces/kube-system/services/kube-dns/proxy
2017-12-24 04:51:03 +00:00
kubernetes-dashboard is running at https://108.59.85.141/api/v1/namespaces/kube-system/services/kubernetes-dashboard/proxy
2017-03-09 18:12:17 +00:00
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
2016-03-28 17:58:08 +00:00
```
2018-11-28 12:55:28 +00:00
{{% /capture %}}