K8s常用命令

2022-10-27 13:45:29 浏览数 (1)

梳理常用的K8s命令

功能说明

命令

node

查看服务器节点

kubectl get nodes

查看服务器节点详情

kubectl get nodes -o wide

节点打标签

kubectl label nodes <节点名称> labelName=<标签名称>

查看节点标签

kubectl get node --show-labels

删除节点标签

kubectl label node <节点名称> labelName-

pod

查看pod节点

kubectl get pod

查看pod节点详情

kubectl get pod -o wide

查看所有名称空间下的pod

kubectl get pod --all-namespaces

根据yaml文件创建pod

kubectl apply -f <文件名称>

根据yaml文件删除pod

kubectl delete -f <文件名称>

删除pod节点

kubectl delete pod <pod名称> -n <名称空间>

查看异常的pod节点

kubectl get pods -n <名称空间>

查看异常pod节点的日志

kubectl describe pod <pod名称> -n <名称空间>

svc

查看服务

kubectl get svc

查看服务详情

kubectl get svc -o wide

查看所有名称空间下的服务

kubectl get svc --all-namespaces

# 查看K8s版本

点击查看

代码语言:javascript复制
[root@master home]# kubectl version
Client Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.0", GitCommit:"9e991415386e4cf155a24b1da15becaa390438d8", GitTreeState:"clean", BuildDate:"2020-03-25T14:58:59Z", GoVersion:"go1.13.8", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.0", GitCommit:"9e991415386e4cf155a24b1da15becaa390438d8", GitTreeState:"clean", BuildDate:"2020-03-25T14:50:46Z", GoVersion:"go1.13.8", Compiler:"gc", Platform:"linux/amd64"}


[root@master home]# kubectl api-versions
admissionregistration.k8s.io/v1
admissionregistration.k8s.io/v1beta1
apiextensions.k8s.io/v1
apiextensions.k8s.io/v1beta1
apiregistration.k8s.io/v1
apiregistration.k8s.io/v1beta1
app.k8s.io/v1beta1
apps/v1
authentication.k8s.io/v1
authentication.k8s.io/v1beta1
authorization.k8s.io/v1
authorization.k8s.io/v1beta1
autoscaling/v1
autoscaling/v2beta1
autoscaling/v2beta2
batch/v1
batch/v1beta1
certificates.k8s.io/v1beta1
cluster.kubesphere.io/v1alpha1
coordination.k8s.io/v1
coordination.k8s.io/v1beta1
crd.projectcalico.org/v1
devops.kubesphere.io/v1alpha1
devops.kubesphere.io/v1alpha3
discovery.k8s.io/v1beta1
events.k8s.io/v1beta1
extensions/v1beta1
iam.kubesphere.io/v1alpha2
installer.kubesphere.io/v1alpha1
kubeapps.com/v1alpha1
monitoring.coreos.com/v1
monitoring.kubesphere.io/v1alpha1
network.kubesphere.io/v1alpha1
networking.k8s.io/v1
networking.k8s.io/v1beta1
node.k8s.io/v1beta1
notification.kubesphere.io/v1alpha1
policy/v1beta1
rbac.authorization.k8s.io/v1
rbac.authorization.k8s.io/v1beta1
scheduling.k8s.io/v1
scheduling.k8s.io/v1beta1
servicemesh.kubesphere.io/v1alpha2
snapshot.storage.k8s.io/v1beta1
storage.k8s.io/v1
storage.k8s.io/v1beta1
storage.kubesphere.io/v1alpha1
tenant.kubesphere.io/v1alpha1
tenant.kubesphere.io/v1alpha2
v1
[root@master home]# 

# 查看K8s集群信息

点击查看

代码语言:javascript复制
[root@master home]# kubectl cluster-info
Kubernetes master is running at https://apiserver.cluster.local:6443
KubeDNS is running at https://apiserver.cluster.local:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
[root@master home]# kubectl -s https://apiserver.cluster.local:6443 get componentstatuses
NAME                 STATUS    MESSAGE             ERROR
scheduler            Healthy   ok                  
controller-manager   Healthy   ok                  
etcd-0               Healthy   {"health":"true"}   
[root@master home]# 

# kubetl get

点击查看

  • 确认pod
代码语言:javascript复制
[root@master ~]# kubectl get pods
如果希望得到更加详细一点的信息,可以加上-o wide参数
[root@master ~]# kubectl get pods -o wide
  • 确认deployment
代码语言:javascript复制
[root@master ~]# kubectl get deployment
  • 确认node信息
代码语言:javascript复制
[root@master ~]# kubectl get nodes -o wide
  • 确认namespace信息
代码语言:javascript复制
[root@master ~]# kubectl get ns
  • 确认服务
代码语言:javascript复制
[root@master ~]# kubectl get svc

提示

待补充。。。

点击查看

# kubectl describe

# kubectl logs

# kubectl exec

# kubectl cp

# kubectl attach

# kubectl edit

# kubectl replace

# kubectl patch

# kubectl apply

# kubectl scale

# kube autoscale

# kubectl cordon/uncordon

# kubectl drain

0 人点赞