命令格式 | 命令说明 |
---|---|
kubectl run 资源名称 -参数 --image=镜像名称:标签 | 创建资源对象,常用参数-i交互,-t终端 |
kubectl get 查询资源 可选参数 -o wide 显示主机信息 | 常用查询的资源 node|deployment|pod |
kubectl exec -it 容器id 执行的命令 | 同 docker exec 指令,进入容器内 |
kubectl describe 资源类型 资源名称 | 查询资源的详细信息 |
kubectl attach | 同 docker attach 指令,连接容器 |
kubectl logs 容器id | 查看容器控制台的标准输出 |
kubectl delete 资源类型 资源名称 | 删除指定的资源 |
kubectl create|apply -f 资源文件 | 执行指定的资源文件 |
[root@master ~]# kubectl get node
NAME STATUS ROLES AGE VERSION
master Ready master 18h v1.17.6
node-0001 Ready <none> 17h v1.17.6
node-0002 Ready <none> 17h v1.17.6
node-0003 Ready <none> 17h v1.17.6
[root@master ~]# kubectl get node master
NAME STATUS ROLES AGE VERSION
master Ready master 18h v1.17.6
[root@master ~]# kubectl get node master -o wide #显示版本更多的系统信息
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
master Ready master 18h v1.17.6 192.168.1.21 <none> CentOS Linux 7 (Core) 3.10.0-1160.53.1.el7.x86_64 docker://18.6.3
[root@master ~]# kubectl run myhttpd --image=192.168.1.100:5000/myos:httpd
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/myhttpd created #运行程序放在后台了
[root@master ~]# kubectl run testos -it --image=192.168.1.100:5000/myos:v1804
[root@testos-79778b4895-s8mxl /]# 进入前台里了
# 进入容器
[root@master ~]# kubectl exec -it testos-79778b4895-s8mxl -- /bin/bash
[root@testos-79778b4895-s8mxl /]#
[root@master ~]# kubectl attach -it testos-79778b4895-s8mxl
[root@testos-79778b4895-s8mxl /]#
# describe 查询详细信息
[root@master ~]# kubectl -n kube-system describe pod kube-flannel-ds-amd64-rtl4l
Name: kube-proxy-4tbp6
Namespace: kube-system
... ...
# 删除资源,直接删除POD会自动重建
[root@master ~]# kubectl delete pod testos-79778b4895-s8mxl
pod "testos-79778b4895-s8mxl" deleted
# 查看终端日志
[root@master ~]# kubectl logs testweb-7bf98b9576-v566c
AH00558: httpd: Could not reliably determine the server fully qualified domain name, using 10.244.4.2. Set the 'ServerName' directive globally to suppress this message
# 执行指定的资源文件
[root@master flannel]# kubectl apply -f kube-flannel.yml