kubectl 常用操作命令#1
•创建deployment:nginx-deploy
kubectl run nginx-deploy --image=nginx:1.14-alpine --port=80 --replicas=1 --dry-run=true
#--dry-run =true:仅打印将要发送的对象,而不发送它。
kubectl run nginx-deploy --image=nginx:1.14-alpine --port=80 --replicas=1
•列出deployment: kubectl get deployment
kubectl get deployment -w #-w:watch,此时处于监控状态,直到available.
•列出所有 pod:kubectl get pods
•列出所有pod并显示详细信息:kubectl get pods -o wide
•输出pod信息为yaml格式:kubectl get pod myapp-xxxxxxxxx-xxxx -o yaml
•扩展/缩减pod数量:kubectl scale --replicas=5 deployment nginx
•删除pod: nginx-deploy:kubectl delete pods nginx-deploy-66ff98548d-zm2pq
★彻底删除pods:
•检查是否创建了deployments任务:kubectl get deployments
•检查是否创建了副本控制器ReplicationController:kubectl get rc
•检查死否创建了副本集replicasets:kubectl get rs
若有,先执行: kubectl delete deployment|rc|rs $name
再执行: kubectl delete pod $pod_name
•创建service/向外暴露deployment nginx-deploy: kubectl expose deployment nginx-deploy --name nginx --port=80 --target-port=80 --protocol=TCP #--name:指定服务名
•获取服务:kubectl get services #简写:svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d23h
nginx ClusterIP 10.96.64.218 <none> 80/TCP 7m1s
※此时可以通过nginx集群的IP:10.96.64.218访问nginx。
•查询coredns的IP地址:
kubectl get pods -n kube-system -o wide
•coredns的service名及IP
kubectl get svc -n kube-system
※使用此IP:10.96.0.10解析服务名,达到访问pod的目的。
◆确认dns解析
事前安装:yum install -y bind-utils
dig -t A nginx.default.svc.cluster.local @10.96.0.10 #默认在default名称空间的本地域名。
或者:创建测试pod,查看pod内部本地域名解析。
[root@k8smaster01 ~]# kubectl run client --image=busybox --replicas=1 -it --restart=Never
If you don't see a command prompt, try pressing enter.
/ # cat /etc/resolv.conf
nameserver 10.96.0.10
search default.svc.cluster.local svc.cluster.local cluster.local
options ndots:5
•显示deployment的信息
kubectl describe deployment nginx-deploy
•查看service关联的pod
kubectl get svc
kubectl describe svc nginx
•查看pods的label
kubectl get pods --show-labels
•修改service的信息
kubectl edit svc nginx
•删除service
kubectl delete svc nginx #删除后,需要重建。
重建service:kubectl expose deployment nginx-deploy --name nginx --port=80 --target-port=80 --protocol=TCP
•在一个pod中,动态测试另一个pod的负载平衡例:
while true; do wget -O - -q nginx; sleep 5 ; done
•迭代更新镜像版本
①查看pod名:kubectl get pods
仅输出pod名称:kubectl get pods -o name
②查看pod的镜像版本:kubectl describe pods myapp-f44986c9-qbh9l
… …
Containers:
client:
Container ID: docker://2844ed2948f984b3e710d16ef66aaf10a775781ee527c1dc895fde8d08bea438
Image: myapp:v1
… …
③升级镜像版本
kubectl set image deployment myapp myapp=myapp:v2
④查看更新过程
kubectl rollout status deployment myapp
⑤再执行②,查看镜像版本。
⑥回滚到前一个版本
kubectl rollout undo deployment myapp