一、Kubectl命令行说明
类型 | 命令 | 描述 |
基础命令 | create | 通过文件名或标准输入创建资源 |
expose | 将一个资源公开为一个新的kubernetes服务 | |
run |
创建并运行一个特定的镜像,可能是副本 创建一个deployment或job管理创建的容器 |
|
set |
配置应用资源 修改现有应用程序资源 |
|
get | 显示一个或多个资源 | |
explain | 文档参考资料 | |
edit | 使用默认的编辑器编辑一个资源 | |
delete | 通过文件名、标准输入、资源名称或标签选择器来删除资源 | |
部署命令 | rollout | 管理资源的发布 |
rolling-update | 执行指定复制控制器的滚动更新 | |
scale | 扩容或缩容Pod数量,Deployment、ReplicasSet、RC或Job | |
autoscale | 创建一个自动选择扩容或者缩容并设置Pod数量 | |
集群管理命令 | certificate | 修改证书资源 |
cluster-info | 显示集群信息 | |
top | 显示资源(CPUMemory/Storage) | |
cordon | 标记节点为不可调度 | |
uncordon | 标记节点可调度 | |
drain | 维护期间排除节点 | |
taint | 更新一个或多个节点上的污点 | |
排错/调试 | describe | 显示特定资源或资源组的详细信息 |
logs | 打印pod中容器的日志 | |
attach | 连接到一个运行的容器,既可以查看output stream,也可以与容器(stdin)进行交互 | |
exec | 在容器中执行命令 | |
port-forward | 将一个或多个本地端口转发到pod中 | |
proxy | 运行Kubernetes API服务器的代理 | |
cp | 从容器中复制文件或目录 | |
auth | 检查授权 | |
高级命令 | apply | 通过文件名或标准输入将配置应用于资源 |
patch | 使用(patch)补丁修改、更新资源的字段 | |
replace | 用文件名或标准输入替换资源 | |
convert | 在不同的API版本之间转换配置文件 | |
设置命令 | label | 更新资源的标签 |
annotate | 更新资源上的注释 | |
completion | 输出指定shell的代码(bash or zsh) | |
其他命令 | api-versions | 在服务器上打印支持的API版本,格式为“group / version” |
config | 修改Kubernetes的文件 | |
help | help命令 | |
plugin | 显示安装的插件 | |
version | 显示版本信息 |
具体可以参考:https://kubernetes.io/docs/reference/kubectl/kubectl/
二、kubectl事例
(1)创建一个deployment(deployment用来管理Pod和RS)
# kubectl run hello-world --replicas=3 --labels="app=example_nginx" --image=nginx:1.10 --port=80 deployment.apps "hello-world" created # 备注 hello-world : deployment的名称 --replicas:副本数 --labels:标签(非唯一,用于识别用途等特点) --image:使用的镜像 --port:暴露的端口
(2)查看pod
[root@master-01 ~]# kubectl get pod NAME READY STATUS RESTARTS AGE hello-world-76c54b84b-b5bcz 1/1 Running 0 11m hello-world-76c54b84b-bmhsd 1/1 Running 0 11m hello-world-76c54b84b-rgn2q 1/1 Running 0 11m [root@master-01 ~]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE hello-world-76c54b84b-b5bcz 1/1 Running 0 11m 10.20.184.81 master-01 hello-world-76c54b84b-bmhsd 1/1 Running 0 11m 10.20.254.104 node-03 hello-world-76c54b84b-rgn2q 1/1 Running 0 11m 10.20.190.57 node-01
(3)查看deployment
[root@master-01 ~]# kubectl get deployment NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE hello-world 3 3 3 3 12
(4)查看pod的描述信息
[root@master-01 ~]# kubectl describe pod hello-world-76c54b84b-b5bcz Name: hello-world-76c54b84b-b5bcz Namespace: default Node: master-01/172.16.60.95 Start Time: Wed, 20 Jun 2018 09:53:07 +0800 Labels: app=example_nginx pod-template-hash=327106406 Annotations: <none> Status: Running IP: 10.20.184.81 Controlled By: ReplicaSet/hello-world-76c54b84 . . .
(5)描述deployment信息
[root@master-01 ~]# kubectl describe deploy/hello-world Name: hello-world Namespace: default CreationTimestamp: Wed, 20 Jun 2018 09:53:07 +0800 Labels: app=example_nginx Annotations: deployment.kubernetes.io/revision=1 Selector: app=example_nginx Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable StrategyType: RollingUpdate MinReadySeconds: 0 RollingUpdateStrategy: 1 max unavailable, 1 max surge Pod Template: Labels: app=example_nginx Containers: hello-world: Image: nginx:1.10 Port: 80/TCP Host Port: 0/TCP Environment: <none> Mounts: <none> Volumes: <none> Conditions: Type Status Reason ---- ------ ------ Available True MinimumReplicasAvailable Progressing True NewReplicaSetAvailable OldReplicaSets: <none> NewReplicaSet: hello-world-76c54b84b (3/3 replicas created) Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 18m deployment-controller Scaled up replica set hello-world-76c54b84b to 3
(6)查看其它命名空间的资源(--namespace xxx / -n xxx)
[root@master-01 ~]# kubectl get pod -o wide -n kube-system NAME READY STATUS RESTARTS AGE IP NODE calico-kube-controllers-98989846-gjk55 1/1 Running 1 8d 172.16.60.98 node-02 calico-node-4mxvv 1/1 Running 1 8d 172.16.60.98 node-02 calico-node-ftg9v 1/1 Running 1 8d 172.16.60.96 master-02 calico-node-hctvm 1/1 Running 1 8d 172.16.60.97 node-01 calico-node-rbv5b 1/1 Running 1 8d 172.16.60.99 node-0
(7)edit修改配置
# 将刚才port的80 改为8088 [root@master-01 ~]# kubectl edit deploy hello-world ... ... ... ports: - containerPort: 8088 ... ...
describe检查
[root@master-01 ~]# kubectl describe deploy/hello-world ... ... ... Pod Template: Labels: app=example_nginx Containers: hello-world: Image: nginx:1.10 Port: 8088/TCP Host Port: 0/TCP
(8)创建一个Service对象,暴露Deployment端口
[root@master-01 ~]# kubectl expose deploy/hello-world --port=88 --type=NodePort --target-port=80 --name=example-nginx-service service "example-nginx-service" exposed # 备注 --port=88:Service服务的端口 --target-port=80: 容器暴露的端口 --type=NodePort:会随机开放一个宿主机端口(端口范围在apiserver中定义)
查看/描述 服务
[root@master-01 ~]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE example-nginx-service NodePort 10.254.43.65 <none> 88:40591/TCP 5m [root@master-01 ~]# kubectl describe svc example-nginx-service Name: example-nginx-service Namespace: default Labels: app=example_nginx Annotations: <none> Selector: app=example_nginx Type: NodePort IP: 10.254.43.65 Port: <unset> 88/TCP TargetPort: 80/TCP NodePort: <unset> 40591/TCP Endpoints: 10.20.184.82:80,10.20.190.59:80,10.20.254.106:80 Session Affinity: None External Traffic Policy: Cluster Events: <none>
访问nginx
# 可以通过Service 的cluster-ip:88 / ndoe-ip:40591 [root@master-01 ~]# curl 10.254.43.65:88 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
# 任意node-ip [root@master-01 ~]# curl 172.16.60.95:40591 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
(9)创建一个Service对象,暴露UDP端口
# kubectl expose deploy/hello-world --port=4123 --type=NodePort --protocol=udp --target-port=80 --name=example-upd-service
(10)查看Pod日志
[root@master-01 ~]# kubectl logs pods/hello-world-76c54b84b-rx4vz 172.16.60.95 - - [20/Jun/2018:02:35:20 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-" 172.16.60.95 - - [20/Jun/2018:02:36:06 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-" 172.16.60.95 - - [20/Jun/2018:02:36:43 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36" "-"
(11)使用标签查询Pod
[root@master-01 ~]# kubectl get pod --selector="app=example_nginx" -o wide NAME READY STATUS RESTARTS AGE IP NODE hello-world-76c54b84b-8zn5j 1/1 Running 0 18m 10.20.190.59 node-01 hello-world-76c54b84b-pbp9h 1/1 Running 0 18m 10.20.254.106 node-03 hello-world-76c54b84b-rx4vz 1/1 Running 0 18m 10.20.184.82 master-01