在本文中,我们将介绍Kubernetes命令行与docker-cli之间的一些关系。kubectl工具,对于使用过docker-cli 用户来说,kubectl的设计感觉会很熟悉,但是也有一些明显的区别。本文的每个小结都讲解了一个docker子命令,并且列出与kubectl相对应命令。

docker run

如果运行nginx 并且暴露?使用kubectl run

使用docker:

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

使用kubectl:

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

kubectl run 在Kubernetes集群> = v1.2上将创建名是 “nginx-app”的 Deployment。如果运行的是低的版本,则会创建一个 replication controllers,如果要了解低版本方式,请使用—generator=run/v1,将会创建 replication controllers。查看kubectl run更多详情。现在,我们可以使用上面创建的Deployment来暴露一个新的服务:

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

使用kubectl创建一个Deployment,他能保证任何情况下有N个运行nginx 的pods(其中N是默认定义声明的副本数,默认为1个)。它同时会创建一个Services,使用选择器匹配Deployment’s selector。有关详细信息,请参阅快速入门

默认情况下镜像在后台运行,类似于docker run -d …如果要在前台运行,请使用:

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

要删除Deployment (及其pod),使用 kubectl delete deployment <name>

docker ps

如何列出当前运行的内容?参考kubectl get

使用docker:

  1. $ docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 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

与kubectl:

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

docker attach

如何连接已经运行在容器中的进程?参考kubectl附件

使用docker:

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

使用kubectl:

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

docker exec

如何在容器中执行命令?参考kubectl exec

使用docker:

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

使用kubectl:

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

交互式命令呢?

使用docker:

  1. $ docker exec -ti a9ec34d98787 /bin/sh
  2. # exit

使用kubectl:

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

有关更多信息,请参阅获取运行容器中的Shell

docker logs

如何查看进程打印stdout / stderr?参考kubectl logs

使用docker:

  1. $ docker logs -f a9e
  2. 192.168.9.1 - - [14/Jul/2015:01:04:02 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.35.0" "-"
  3. 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
  2. 10.240.63.110 - - [14/Jul/2015:01:09:01 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.26.0" "-"
  3. 10.240.63.110 - - [14/Jul/2015:01:09:02 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.26.0" "-"

感觉是时候普及下pods和containers之间的微小差异了; 默认情况下,如果进程退出,pods是不会终止,相反,它会重新启动该进程。这与docker run 配置—restart=always 选项有一个主要区别。要查看以前在Kubernetes中运行的输出,请运行如下:

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

有关详细信息,请参阅日志和群集监视

docker stop 和 docker rm

如何停止和删除正在运行的进程?参考kubectl delete

使用docker

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

使用kubectl:

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

注意,不要直接删除pod,使用kubectl请删除拥有该pod的Deployment。如果直接删除pod,则Deployment将会重新创建该pod。

docker login

与docker login相比,在kubectl中没有直接相识的命令。如果对于Kubernetes在私有registry中使用感兴趣,请参阅使用私有Registry

docker version

如何获客户端和服务器的版本号?参考kubectl version

使用docker:

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

使用kubectl:

  1. $ kubectl version
  2. Client Version: version.Info{Major:"0", Minor:"20.1", GitVersion:"v0.20.1", GitCommit:"", GitTreeState:"not a git tree"}
  3. Server Version: version.Info{Major:"0", Minor:"21+", GitVersion:"v0.21.1-411-g32699e873ae1ca-dirty", GitCommit:"32699e873ae1caa01812e41de7eab28df4358ee4", GitTreeState:"dirty"}

docker info

如何获取有关配置和环境信息?参考kubectl cluster-info

使用docker:

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

使用kubectl:

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

K8S中文社区微信公众号

原文: http://docs.kubernetes.org.cn/70.html