kubectl常用命令

1 查看类命令

---

# 查看集群信息

kubectl cluster-info

# 查看各组件信息
kubectl -s http://localhost:8080 get componentstatuses

# 查看pods所在的运行节点
kubectl get pods -o wide

# 查看pods定义的详细信息
kubectl get pods -o yaml

# 查看Replication Controller信息
kubectl get rc

# 查看service的信息
kubectl get service

# 查看节点信息
kubectl get nodes

# 按selector名来查找pod
kubectl get pod --selector name=redis

#查看所有命名空间的pod

kubectl get pod --all-namespaces

#查看pod的运行日志

kubectl log -f mir2-handler-deployment-3490314515-2vn0c --namespace=dev


# 查看运行的pod的环境变量
kubectl exec pod名 env
#进入pod对应的容器内部
[root@dev-master hzb]# kubectl exec -it hzb-mongo1-ceph bash
root@hzb-mongo1-ceph:/#   

2 操作类命令

---

# 创建
kubectl create -f 文件名

# 重建
kubectl replace -f 文件名  [--force]

# 删除

kubectl delete -f 文件名
kubectl delete pod pod名
kubectl delete rc rc名
kubectl delete service service名
kubectl delete pod --all
 
#patch
如果一个容器已经在运行,这时需要对一些容器属性进行修改,又不想删除容器,或不方便通过replace的方式进行更新。kubernetes还提供了一种在容器运行时,直接对容器进行修改的方式,就是patch命令。
[root@autodeploy mysql]# cat mysql-pvc.yml 
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: mysql-nfs-claim-hzb
  namespace: common
  annotations:
    volume.beta.kubernetes.io/storage-class: "nfs-storage-class"
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 2048Mi
[root@autodeploy mysql]# 
[root@autodeploy mysql]# 
[root@autodeploy mysql]# kubectl patch pvc test-pvc-hzb -p '{"spec":{"resources":{"requests":{"storage":"2048Mi"}}}}' -n common
 
#停用节点172.16.80.37
kubectl cordon 172.16.80.37
 
#启用节点172.16.80.37
kubectl uncordon 172.16.80.37
 
#将172.16.80.37上的资源迁移到其他节点
[root@master bin]# kubectl get pod --all-namespaces -o wide
NAMESPACE     NAME                                       READY     STATUS    RESTARTS   AGE       IP             NODE
ceshi1        tomcat-ee3247f81d-1847955224-qnlx9         1/1       Running   0          40m       10.2.18.2      172.16.80.37
kube-system   kube-apiserver-172.16.80.35                1/1       Running   0          10d       172.16.80.35   172.16.80.35
kube-system   kube-controller-manager-172.16.80.35       1/1       Running   0          10d       172.16.80.35   172.16.80.35
kube-system   kube-dns-1120462606-vt9jm                  3/3       Running   0          10d       10.2.8.5       172.16.80.36
kube-system   kube-proxy-172.16.80.35                    1/1       Running   0          10d       172.16.80.35   172.16.80.35
kube-system   kube-scheduler-172.16.80.35                1/1       Running   0          10d       172.16.80.35   172.16.80.35
kube-system   tiller-deploy-3688326028-bqjxs             1/1       Running   0          10d       10.2.8.6       172.16.80.36
mir2          mir2-handler-deployment-3733720023-gww07   1/1       Running   0          10d       10.2.8.10      172.16.80.36
mir2          mir2-manager-deployment-801894099-hqsnh    1/1       Running   0          10d       10.2.8.4       172.16.80.36
mir2          mir2-ui-deployment-312936261-bhhc2         1/1       Running   0          4m        10.2.18.3      172.16.80.37
mir2          mongodb-1377461491-7h3lr                   1/1       Running   0          10d       10.2.8.2       172.16.80.36
[root@master bin]# 
[root@master bin]# 
[root@master bin]# 
[root@master bin]# 
[root@master bin]# 
[root@master bin]# kubectl drain 172.16.80.37
node "172.16.80.37" cordoned
pod "tomcat-ee3247f81d-1847955224-qnlx9" evicted
pod "mir2-ui-deployment-312936261-bhhc2" evicted
node "172.16.80.37" drained
[root@master bin]# 
[root@master bin]# 
[root@master bin]# 
[root@master bin]# 
[root@master bin]# kubectl get pod --all-namespaces -o wide
NAMESPACE     NAME                                       READY     STATUS    RESTARTS   AGE       IP             NODE
ceshi1        tomcat-ee3247f81d-1847955224-665f5         1/1       Running   0          46s       10.2.8.7       172.16.80.36
kube-system   kube-apiserver-172.16.80.35                1/1       Running   0          10d       172.16.80.35   172.16.80.35
kube-system   kube-controller-manager-172.16.80.35       1/1       Running   0          10d       172.16.80.35   172.16.80.35
kube-system   kube-dns-1120462606-vt9jm                  3/3       Running   0          10d       10.2.8.5       172.16.80.36
kube-system   kube-proxy-172.16.80.35                    1/1       Running   0          10d       172.16.80.35   172.16.80.35
kube-system   kube-scheduler-172.16.80.35                1/1       Running   0          10d       172.16.80.35   172.16.80.35
kube-system   tiller-deploy-3688326028-bqjxs             1/1       Running   0          10d       10.2.8.6       172.16.80.36
mir2          mir2-handler-deployment-3733720023-gww07   1/1       Running   0          10d       10.2.8.10      172.16.80.36
mir2          mir2-manager-deployment-801894099-hqsnh    1/1       Running   0          10d       10.2.8.4       172.16.80.36
mir2          mir2-ui-deployment-312936261-pmzq9         1/1       Running   0          46s       10.2.8.3       172.16.80.36
mir2          mongodb-1377461491-7h3lr                   1/1       Running   0          10d       10.2.8.2       172.16.80.36
[root@master bin]# 
原文地址:https://www.cnblogs.com/boshen-hzb/p/6626676.html