kubernetes 使用&常用命令

一、集群信息搜集

kubectl get [po/no/svc/rc/deploy/ns]
可选参数
-n [ns名称]
-o [wide/yaml]

#在master 查看 node状态
[root@k8s-master ~]# kubectl get nodes
NAME         STATUS   ROLES    AGE   VERSION
k8s-master   Ready    master   24h   v1.16.0
k8s-node1    Ready    <none>   24h   v1.16.0
k8s-node2    Ready    <none>   24h   v1.16.0

[root@k8s-master ~]# kubectl get no
NAME         STATUS   ROLES    AGE   VERSION
k8s-master   Ready    master   24h   v1.16.0
k8s-node1    Ready    <none>   24h   v1.16.0
k8s-node2    Ready    <none>   24h   v1.16.0

#删除节点:无效且显示的也可以删除
[root@k8s-master ~]# kubectl delete node 192.168.245.252
node "192.168.245.252" deleted

#使用 kubectl describe 命令,查看一个 API 对象的细节(信息很多截取部分)
[root@k8s-master ~]# kubectl describe node k8s-node1
Name:               k8s-node1
Roles:              <none>
...
Addresses:
  InternalIP:  192.168.1.22
  Hostname:    k8s-node1
...
Events:
...
...
注意:在 Kubernetes 执行的过程中,对 API 对象的所有重要操作,都会被记录在这个对象的 Events 里,并且显示在 kubectl describe 指令返回的结果中。

#查看集群信息
[root@k8s-master ~]# kubectl cluster-info
Kubernetes master is running at https://192.168.1.18:6443
KubeDNS is running at https://192.168.1.18:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

#查看service的信息
[root@k8s-master ~]# kubectl get service
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.1.0.1     <none>        443/TCP        24h
nginx        NodePort    10.1.60.49   <none>        80:31407/TCP   24h

[root@k8s-master ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.1.0.1     <none>        443/TCP        24h
nginx        NodePort    10.1.60.49   <none>        80:31407/TCP   24h

#查看所有名称空间内的资源
[root@k8s-master ~]# kubectl get pods --all-namespaces
NAMESPACE     NAME                                    READY   STATUS    RESTARTS   AGE
default       nginx-54c66f4bdc-4sp2p                  1/1     Running   1          24h
kube-system   coredns-58cc8c89f4-gssl8                1/1     Running   0          24h
kube-system   coredns-58cc8c89f4-hvvql                1/1     Running   0          24h
kube-system   etcd-k8s-master                         1/1     Running   0          24h
...
...

#同时查看多种资源信息
[root@k8s-master ~]# kubectl get pod,svc -n kube-system
NAME                                        READY   STATUS    RESTARTS   AGE
pod/coredns-58cc8c89f4-gssl8                1/1     Running   0          24h
pod/coredns-58cc8c89f4-hvvql                1/1     Running   0          24h
pod/etcd-k8s-master                         1/1     Running   0          24h
...
...
NAME                           TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)                  AGE
service/kube-dns               ClusterIP   10.1.0.10      <none>        53/UDP,53/TCP,9153/TCP   24h
service/kubernetes-dashboard   NodePort    10.1.177.151   <none>        443:30001/TCP            23h

二、常用命令

1. 查看资源信息

Nodes
# kubectl get no
# kubectl get no -o wide
# kubectl describe no
# kubectl get no -o yaml
# kubectl get node --selector=[label_name]
# kubectl get nodes -o jsonpath=' {.items[*].status.addresses [?(@.type=="ExternalIP")].address}'
# kubectl top node [node_name]
Pods
# kubectl get po -o wide
# kubectl describe po
# kubectl get po
# kubectl get po --show-labels
# kubectl get po -l app=nginx
# kubectl get po -o yaml
# kubectl get pod [pod_name] - o yaml --export
# kubect l get pod [ pod_name] - o yaml --export > nameoffile.yaml
# kubectl get pods -- field-selector status.phase=Running
Namespaces
# kubectl get ns
# kubectl get ns - o yaml
# kubectl describe ns
Deployments
# kubectl get deploy
# kubectl describe deploy
# kubectl get deploy - o wide
# kubectl get deploy - o yaml
Services
# kubectl get svc
# kubectl describe svc
# kubectl get svc - o wide
# kubectl get svc - o yaml
# kubectl get svc --show-labels
DaemonSets
# kubectl get ds
# kubectl get ds --all-namespaces
# kubectl describe ds [daemonset_name] - n [namespce_name]
# kubectl get ds [ds_name] -n [ns_name] -o yaml
Events
# kubectl get events
# kubectl get events -n kube-system
# kubectl get events -w
logs
# kubectl logs [pod_name]
# kubectl logs --since=1h [pod_name]
# kubectl logs --tail =20 [pod_name]
# kubectl logs -f -c [container_name] [pod_name]
# kubectl logs [pod_name] > pod.log
Service Accounts
# kubectl get sa
# kubectl get sa -o yaml
# kubectl get serviceaccounts default -o yaml >./sa.yaml
# kubectl replace serviceaccount default -f ./sa.yaml
ReplicaSets
# kubectl get rs
# kubectl describe rs
# kubectl get rs -o wide
# kubectl get rs -o yaml
Roles
# kubectl get roles --all -namespaces
# kubectl get roles --all -namespaces -o yaml
Secrets
# kubectl get secrets
# kubectl get secrets --all -namespaces
# kubectl get secrets -o yaml
ConfigMaps
# kubectl get cm
# kubectl get cm --all-namespaces
# kubectl get cm --all-namespaces -o yaml
Ingress
# kubectl get ing
# kubectl get ing --all-namespaces
PersistentVolume
# kubectl get pv
# kubectl describe pv
PersistentVolumeClaim
# kubectl get pvc
# kubectl describe pvc
StorageClass
# kubectl get sc
# kubectl get sc -o yaml
Multiple Resources
# kubectl get svc,po
# kubectl get deploy,no
# kubectl get all
# kubectl get all --all -namespaces

2. 改变资源属性

Taint
# kubectl taint [node_name] [taint_name] 
Labels
# kubectl label [node_name] disktype=ssd
# kubectl label [pod_name] env=prod
Cordon/Uncordon
# kubectl cordon [node_name]
# kunectl uncordon [node_name]
Drain
# kubectl drain [node_name]
Nodes/Pods
# kubectl delet enode [node_name]
# kubectl delet epod [pod_name]
# kubectl edit node [node_name]
# kubectl edit pod [pod_name] 
Deployments/Namespaces
# kubectl edit deploy [deploy_name]
# kubectl delete deploy [deploy_name]
# kubectl expse deploy [deploy_name] --por=80  -type=NodePort
# kubectl scale deploy [deploy_name] --repicas=5
# kubectl delete ns 
# kubectl edit ns [ns_name]
Services
# kubectl edit svc [svc_name]
# kubectl delete svc [svc_name]
DaemonSets
# kubectl edit ds [ds_name] -n kube-system
# kubectl delete ds [ds_name]
Service Accounts
# kubectl edit sa [sa_name]
# kubectl delete sa [sa_name]
Annotate
# kubectl annotate po [pod_name] [annotation]
# kubectl annotate no [node_name]

3. 添加资源

Taint
# kubectl taint [node_name] [taint_name] 
Labels
# kubectl label [node_name] disktype=ssd
# kubectl label [pod_name] env=prod
Cordon/Uncordon
# kubectl cordon [node_name]
# kunectl uncordon [node_name]
Drain
# kubectl drain [node_name]
Nodes/Pods
# kubectl delet enode [node_name]
# kubectl delet epod [pod_name]
# kubectl edit node [node_name]
# kubectl edit pod [pod_name] 
Deployments/Namespaces
# kubectl edit deploy [deploy_name]
# kubectl delete deploy [deploy_name]
# kubectl expse deploy [deploy_name] --por=80  -type=NodePort
# kubectl scale deploy [deploy_name] --repicas=5
# kubectl delete ns 
# kubectl edit ns [ns_name]
Services
# kubectl edit svc [svc_name]
# kubectl delete svc [svc_name]
DaemonSets
# kubectl edit ds [ds_name] -n kube-system
# kubectl delete ds [ds_name]
Service Accounts
# kubectl edit sa [sa_name]
# kubectl delete sa [sa_name]
Annotate
# kubectl annotate po [pod_name] [annotation]
# kubectl annotate no [node_name]

4. 请求

API Call
# kubectl get --raw /apis/metrics.k8s.io/
Cluster Info
# kubectl config 
# kubectl cluster-info
# kubectl get componentstatuses
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。