kubectl命令使用
create
kubectl create deployment NAME --image=image -- [COMMAND] [args...] //格式
[root@master ~]# kubectl create deployment test1 --image busybox
deployment.apps/test1 created //使用busybox镜像创建一个test1的pod
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
test1-78d64fd9b9-4ihbm 0/1 CrashLoopBackOff 3 114s //可以看到处于退出状态,因为busybox使用的是sh,没有任务就会退出
[root@master ~]# kubectl create deployment test2 --image busybox -- sleep 60
deployment.apps/test2 created
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
test2-7c95bf5bcb-wdf2 1/1 Running 0 17s //正在运行
[root@master ~]# kubectl create deployment web --image nginx --replicas 3 // 创建使用nginx镜像创建三个pod,名字为web
deployment.apps/web created
[root@master ~]# kubectl get pod
web-96d5df5c8-2kqfx 1/1 Running 0 57s
web-96d5df5c8-ld842 1/1 Running 0 57s
web-96d5df5c8-vtwks 1/1 Running 0 57s
[root@master ~]# kubectl get pods -o wide //查看pod运行的节点位置
web-96d5df5c8-2kqfx 1/1 Running 0 2m2s 10.244.2.3 node2.example.com <none> <none>
web-96d5df5c8-ld842 1/1 Running 0 2m2s 10.244.1.6 node1.example.com <none> <none>
web-96d5df5c8-vtwks 1/1 Running 0 2m2s 10.244.2.4 node2.example.com <none> <none>
[root@master ~]# kubectl create deployment web01 --image nginx --port=80 //暴露80端口号
run
// 启动一个 nginx pod
[root@master ~]# kubectl run nginx --image nginx
pod/nginx created
[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx 1/1 Running 0 36s 10.244.1.7 node1.example.com <none> <none>
[root@master ~]# kubectl delete pods nginx //删除nginx的pod
pod "nginx" deleted
[root@master ~]# kubectl run nginx --images nginx --port 80 // 暴露容器的80端口号
// 在容器中设置标签“app=nginx”和“env=prod”
[root@master ~]# kubectl run nginx --image nginx --labels "app=nginx,env=prod"
pod/nginx created
[root@master ~]# kubectl describe pod nginx //描述nginx信息
// 测试运行
[root@master ~]# kubectl run nginx --image nginx --dry-run server //不会真正运行
W1219 01:41:21.786495 157488 helpers.go:553] --dry-run is deprecated and can be replaced with --dry-run=client.
pod/nginx created (dry run)
delete
[root@master ~]# kubectl delete deployment test1 // 删除test1,使用deployment类型,因为我们当时创建的时候使用的是deployment类型
deployment.apps "test1" deleted
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-6799fc88d8-thr6q 1/1 Running 0 4h2m
test2-7c95bf5bcb-tqgn5 0/1 Terminating 8 32m
web-96d5df5c8-2kqfx 1/1 Running 0 30m
web-96d5df5c8-ld842 1/1 Running 0 30m
web-96d5df5c8-vtwks 1/1 Running 0 30m
[root@master ~]# kubectl delete deployment test2
deployment.apps "test2" deleted
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-6799fc88d8-thr6q 1/1 Running 0 4h2m
web-96d5df5c8-2kqfx 1/1 Running 0 31m
web-96d5df5c8-ld842 1/1 Running 0 31m
web-96d5df5c8-vtwks 1/1 Running 0 31m
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 29h
nginx NodePort 10.99.74.11 <none> 80:31173/TCP 28h
web ClusterIP 10.109.101.12 <none> 8080/TCP 12m
// 删除service类型的pod
[root@master ~]# kubectl delete svc nginx
'service "nginx" deleted
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none