Kubectl管理工具常用命令
阿新 • • 發佈:2018-12-26
刪除容器與映象:
首先:
kubectl get all 查到之前建立的nginx容器和服務名字,然後刪掉
kubectl delete deployment.apps/nginx
kubectl delete service/nginx
1、建立
kubectl run nginx --replicas=3 --labels="app=nginx-example" --image=nginx:1.10 --port=80
2、檢視
#檢視副本
[root@master ~]# kubectl get deploy
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx 3 3 3 3 18h
#檢視容器與標籤
[root@master ~]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-5b8c7bbc4c-6zt6w 1/1 Running 0 18h app=nginx-example,pod-template-hash=1647366707
nginx-5b8c7bbc4c-jmzf9 1/1 Running 0 18h app=nginx-example,pod-template-hash=1647366707
nginx-5b8c7bbc4c-r7ljf 1/1 Running 0 18h app=nginx-example,pod-template-hash=1647366707
#根據標籤名字檢視服務
[root@master ~]# kubectl get pods -l app=nginx-example
NAME READY STATUS RESTARTS AGE
nginx-5b8c7bbc4c-6zt6w 1/1 Running 0 18h
nginx-5b8c7bbc4c-jmzf9 1/1 Running 0 18h
nginx-5b8c7bbc4c-r7ljf 1/1 Running 0 18h
#檢視資源的詳細資訊
[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
nginx-5b8c7bbc4c-6zt6w 1/1 Running 0 18h 172.17.13.2 192.168.1.42
nginx-5b8c7bbc4c-jmzf9 1/1 Running 0 18h 172.17.94.5 192.168.1.40
nginx-5b8c7bbc4c-r7ljf 1/1 Running 0 18h 172.17.94.3 192.168.1.40
--
3、釋出
根據kubectl get deploy 找到name 名字
#然後更加name執行,
kubectl expose deployment nginx --port=88 --type=NodePort --target-port=80 --name=nginx-service
#檢視建立的服務:
[[email protected] ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.10.10.1 <none> 443/TCP 7d
nginx-service NodePort 10.10.10.107 <none> 88:42784/TCP 24s
現在就可以在節點上面測試了:
curl 10.10.10.107:88 或者頁面訪問:http://192.168.1.40:42784
具體資訊檢視:
kubectl describe service nginx-service
4、故障排查
#查服務狀態
kubectl describe TYPE NAME_PREFIX
例如:
replicaset.apps/nginx-5b8c7bbc4c
#查服務日誌
kubectl logs nginx-xxx
例如:
kubectl logs pod/nginx-5b8c7bbc4c-6zt6w
#進入容器內部檢視
kubectl exec –it nginx-xxx bash
例如:
kubectl exec -it nginx-5b8c7bbc4c-jmzf9 bash
5、更新專案
#將nginx版本升級為1.11,--record代表記錄
kubectl set image deployment/nginx nginx=nginx:1.11 --record
升級完成檢查:
kubectl describe pod nginx-69fcd8b884-blnkg
或者直接編輯檔案進行更新:
kubectl edit deployment/nginx
直接更改映象版本即可:
升級完成檢查:
kubectl describe pod nginx-86b8d5d87c-4fwvr
檢查更新狀態
[root@master ~]# kubectl rollout status deployment/nginx
deployment "nginx" successfully rolled out
檢查更新版本:
[root@master ~]# kubectl rollout history deployment/nginx
deployments "nginx"
REVISION CHANGE-CAUSE
2 <none>
3 <none>
4 kubectl set image deployment/nginx nginx=nginx:1.13 --record=true
檢查 版本3 的資訊:
[root@master ~]# kubectl rollout history deployment/nginx --revision=3
6、回滾專案
#預設回滾到上個版本
kubectl rollout undo deployment/nginx
#回滾到指定版本
kubectl rollout undo deployment/nginx --to-revision=2
#查看回滾版本
kubectl describe pod nginx-86b8d5d87c-7c24f
7、擴容專案副本
#將副本數擴容到5個
kubectl scale deploy/nginx --replicas=5
#將副本縮到4個
kubectl scale deploy/nginx --replicas=4
或者:
kubectl scale deployment nginx --replicas=4
8、刪除專案
kubectl delete deploy/nginx
kubectl delete service/nginx-service