今天来跟大家聊聊如何使用kubectl的命令
1.创建命令和docker run命令很像。我们通过–help来看看怎么用。
kubectl run --help
Create and run a particular image, possibly replicated.
Creates a deployment or job to manage the created container(s).
Examples:
#基于一个镜像创建
# Start a single instance of nginx.
kubectl run nginx --image=nginx
#端口号
# Start a single instance of hazelcast and let the container expose port 5701 .
kubectl run hazelcast --image=hazelcast --port=5701
# Start a single instance of hazelcast and set environment variables "DNS_DOMAIN=cluster" and "POD_NAMESPACE=default"
in the container.
kubectl run hazelcast --image=hazelcast --env="DNS_DOMAIN=cluster" --env="POD_NAMESPACE=default"
# Start a single instance of hazelcast and set labels "app=hazelcast" and "env=prod" in the container.
kubectl run hazelcast --image=hazelcast --labels="app=hazelcast,env=prod"
#创建副本数
# Start a replicated instance of nginx.
kubectl run nginx --image=nginx --replicas=5
#用法
kubectl run NAME --image=image [--env="key=value"] [--port=port] [--replicas=replicas] [--dry-run=bool]
[--overrides=inline-json] [--command] -- [COMMAND] [args...] [options]
所以我们可以根据用法总结出:
kubectl run 名称 --image=镜像名称 --port=端口号 --replicas=副本数量
#我创建nginxwebpod并且生成3个副本
kubectl run nginxweb --image=nginx:latest --port=80 --replicas=3
kubectl get pods,deployment,replicaset
#首先查看用法
kubectl expose --help
Usage:
#--port是指:pod内部通信端口。--target-port:暴露在外部的端口
kubectl expose (-f FILENAME | TYPE NAME) [--port=port] [--protocol=TCP|UDP|SCTP] [--target-port=number-or-name]
[--name=name] [--external-ip=external-ip-of-service] [--type=type] [options]
--type默认是ClusterIP(集群ip)
--type='': Type for this service: ClusterIP, NodePort, LoadBalancer, or ExternalName. Default is 'ClusterIP'.
将之前创建的pod对外开放
kubectl expose deployment nginxweb --port=80 --target-port=80 --name=nginx-port --type=NodePort
之后使用命令查看端口号
kubectl get pods,svc
由于node节点中的kube-proxy做了负载均衡(lvs)所以访问任意节点都可以访问到nginx的页面。
4.查看节点地址
kubectl get endpoints
5.更新版本
首先查看nginx现有的版本为1.17.10
查看set的用法:
[root@master_01 dashboard]# kubectl set --help
Configure application resources
These commands help you make changes to existing application resources.
Available Commands:
env Update environment variables on a pod template
image 更新一个 pod template 的镜像
resources 在对象的 pod templates 上更新资源的 requests/limits
selector 设置 resource 的 selector
serviceaccount Update ServiceAccount of a resource
subject Update User, Group or ServiceAccount in a RoleBinding/ClusterRoleBinding
Usage:
kubectl set SUBCOMMAND [options]
创建新版本的pod
kubectl set image deployment/nginxweb nginxweb=nginx:1.14
k8s是通过滚动跟新的方式进行版本更新,即创建一个新的pod再删除一个旧的pod,直到更新完毕。
6.版本回滚
[root@master_01 dashboard]# kubectl rollout --help
Manage the rollout of a resource.
Valid resource types include:
* deployments
* daemonsets
* statefulsets
Examples:
# Rollback to the previous deployment
kubectl rollout undo deployment/abc
# Check the rollout status of a daemonset
kubectl rollout status daemonset/foo
Available Commands:
history 显示 rollout 历史
pause 标记提供的 resource 为中止状态
resume 继续一个停止的 resource
status 显示 rollout 的状态
undo 撤销上一次的 rollout
Usage:
kubectl rollout SUBCOMMAND [options]
所以由上面可知可以用undo撤销之前的更新。
kubectl rollout undo deployment/nginxweb
#查看更新记录
[root@master_01 dashboard]# kubectl rollout history deployment/nginxweb
deployment.extensions/nginxweb
REVISION CHANGE-CAUSE
2
3