前言: Kubectl 是管理k8s集群的命令行工具,通过生成的 json 格式传递给 apiserver 进行创建、查看、管理的操作 。
kubectl --help
kubectl run 命令
常用语法:
kubectl run NAME --image=image [--env="key=value"参数] [--port=port端口] [--replicas=replicas副本集] [--dry-run=bool状态] [--overrides=inline-json] [--command命令] -- [COMMAND] [args...] [options]`
例:
创建一个名字为nginx-deployment 的资源Pod,镜像是nginx,端口是80,有三个副本数量。
[root@master01 ~]# kubectl run nginx-deployment --image=nginx --port=80 --replicas=3
[root@master01 ~]# kubectl get pods ‘查看刚创建的Pod资源’
[root@master01 ~]# kubectl get pods -o wide
[root@master01 ~]# kubectl get all ‘可看到副本数、控制器数都是3个’
[root@master01 ~]# kubectl delete deploy/nginx-deployment
[root@master01 ~]# kubectl delete deploy/nginx
[root@master01 ~]# kubectl get pods ‘全部被删除了’
No resources found.
创建 —>发布 —>更新 —>回滚 —>删除
(在master01上配置
)
[root@master01 ~]# kubectl run nginx --image=nginx:latest --port=80 --replicas=3
[root@master01 ~]# kubectl get pods,deployment,replicaset
NAME READY STATUS RESTARTS AGE
pod/nginx-7697996758-fgs9m 1/1 Running 0 7m36s
pod/nginx-7697996758-jqhqx 1/1 Running 0 7m36s
pod/nginx-7697996758-tt59h 1/1 Running 0 7m36s
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deployment.extensions/nginx 3 3 3 3 7m36s
NAME DESIRED CURRENT READY AGE
replicaset.extensions/nginx-7697996758 3 3 3 7m36s
[root@master01 ~]# kubectl expose deployment nginx --port=80 --target-port=80 --name=nginx-service --type=NodePort
[root@master01 ~]# kubectl get service
‘46971 : NodePort对外提供的端口’
[root@master01 ~]# kubectl get pods -o wide ‘查看网络状态信息’
[root@master01 ~]# kubectl get endpoints
(在node01、node02节点上
)
[root@node01 ~]# yum install ipvsadm -y
[root@node01 ~]# ipvsadm -L -n
[root@master01 ~]# kubectl get pods
[root@master01 ~]# kubectl logs nginx-7697996758-fgs9m
[root@master01 ~]# kubectl logs nginx-7697996758-jqhqx
[root@master01 ~]# kubectl logs nginx-7697996758-tt59h
[root@master01 ~]# kubectl logs nginx-7697996758-fgs9m ‘再次访问就可看到对应的日志信息了’
更新版本为1.14版本
打开谷歌浏览器,点击右上角三个点—选择更多工具—点击开发者选项—>找到network—>刷新访问 —>找到headers头部信息,就可以看到nginx版本信息。
(在master01节点
)
[root@master01 ~]# kubectl set --help
[root@master01 ~]# kubectl set image deployment/nginx nginx=nginx:1.14 ‘更新nginx为1.14版本’
[root@master01 ~]# kubectl get pods -w ‘-w :开启动态监听状态’
‘ctrl+c结束进程’
如业务更新完,大量用户访问出现error问题,那么就要业务回滚到原来的版本。
[root@master01 ~]# kubectl rollout history deployment/nginx ‘查看历史版本,有两个版本状态’
[root@master01 ~]# kubectl rollout undo deployment/nginx ‘执行回滚到上一次版本’
[root@master01 ~]# kubectl rollout status deployment/nginx ‘检查回滚状态’
[root@master01 ~]# kubectl rollout history deployment/nginx ‘再次查看版本状态’
[root@master01 ~]# kubectl get deployment ‘查看deployment资源名称’
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx 3 3 3 3 3h19m
[root@master01 ~]# kubectl delete deployment/nginx ‘删除nginx项目的deployment资源’
deployment.extensions "nginx" deleted
[root@master01 ~]# kubectl get deployment
No resources found. ‘删除完成’
[root@master01 ~]# kubectl get svc ‘查看service资源名称’
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 443/TCP 22h
nginx-service NodePort 10.0.0.100 80:46971/TCP 3h12m
[root@master01 ~]# kubectl delete svc/nginx-service ‘删除nginx项目的service资源’
service "nginx-service" deleted
[root@master01 ~]# kubectl run nginx --image=nginx:latest --port=80 --replicas=3
[root@master01 ~]# kubectl get pods
[root@master01 ~]# kubectl describe pod nginx-7697996758-l8xnw
[root@master01 ~]# kubectl describe deployment/nginx
[root@master01 ~]# kubectl exec -it nginx-7697996758-p7c7h bash
root@nginx-7697996758-p7c7h:/#