Kubernetes常用命令

命令说明

-A 表示所有信息
-o wide 表示详细信息
–show-labels 表示查询标签详细信息

Node 相关

 获取节点信息

[root@k8s-master01 ~]# kubectl get node
NAME           STATUS   ROLES                  AGE    VERSION
k8s-master01   Ready    control-plane,master   5d1h   v1.23.6
k8s-node01     Ready                     5d1h   v1.23.1
k8s-node02     Ready                     5d1h   v1.23.1

获取节点详细信息

[root@k8s-master01 ~]# kubectl get node -o wide
NAME           STATUS   ROLES                  AGE    VERSION   INTERNAL-IP       EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION                CONTAINER-RUNTIME
k8s-master01   Ready    control-plane,master   5d1h   v1.23.6   192.168.160.100           CentOS Linux 7 (Core)   5.4.228-1.el7.elrepo.x86_64   docker://20.10.21
k8s-node01     Ready                     5d1h   v1.23.1   192.168.160.101           CentOS Linux 7 (Core)   5.4.228-1.el7.elrepo.x86_64   docker://20.10.21
k8s-node02     Ready                     5d1h   v1.23.1   192.168.160.102           CentOS Linux 7 (Core)   5.4.228-1.el7.elrepo.x86_64   docker://20.10.21

获取节点信息标签信息

[root@k8s-master01 ~]# kubectl get node --show-labels
NAME           STATUS   ROLES                  AGE    VERSION   LABELS
k8s-master01   Ready    control-plane,master   5d1h   v1.23.6   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master01,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node-role.kubernetes.io/master=,node.kubernetes.io/exclude-from-external-load-balancers=
k8s-node01     Ready                     5d1h   v1.23.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node01,kubernetes.io/os=linux
k8s-node02     Ready                     5d1h   v1.23.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node02,kubernetes.io/os=linux

给节点增加标签 

[root@k8s-master01 ~]# kubectl label nodes k8s-master01 type=xiaomi
node/k8s-master01 labeled

 给节点去除标签

[root@k8s-master01 ~]# kubectl label nodes k8s-master01 type-
node/k8s-master01 unlabeled

pod 相关

run pod(run docker一样)

[root@k8s-master01 ~]# kubectl run ng-test --image=nginx:1.14.2
pod/ng-test created

查看所有pod列表

[root@k8s-master01 ~]# kubectl get po -A
NAMESPACE      NAME                                   READY   STATUS              RESTARTS        AGE
default        ng-test                                0/1     ContainerCreating   0               27s
default        nginx-deployment-85b98978db-ksvqb      1/1     Running             2 (4d16h ago)   5d1h
default        nginx-deployment-85b98978db-wvnxr      1/1     Running             2 (4d16h ago)   5d1h
default        nginx-deployment-85b98978db-zckmw      1/1     Running             2 (4d16h ago)   5d1h
kube-flannel   kube-flannel-ds-562d5                  1/1     Running             2 (4d16h ago)   5d1h
kube-flannel   kube-flannel-ds-mpvlr                  1/1     Running             3 (4d16h ago)   5d1h
kube-flannel   kube-flannel-ds-rdzff                  1/1     Running             2 (4d16h ago)   5d1h
kube-system    coredns-6d8c4cb4d-rzlbl                1/1     Running             2 (4d16h ago)   5d1h
kube-system    coredns-6d8c4cb4d-w9hxs                1/1     Running             2 (4d16h ago)   5d1h
kube-system    etcd-k8s-master01                      1/1     Running             4 (4d16h ago)   5d1h
kube-system    kube-apiserver-k8s-master01            1/1     Running             2 (4d16h ago)   5d1h
kube-system    kube-controller-manager-k8s-master01   1/1     Running             2 (4d16h ago)   5d1h
kube-system    kube-proxy-97hqw                       1/1     Running             2 (4d16h ago)   5d1h
kube-system    kube-proxy-98frt                       1/1     Running             2 (4d16h ago)   5d1h
kube-system    kube-proxy-zhzjh                       1/1     Running             2 (4d16h ago)   5d1h
kube-system    kube-scheduler-k8s-master01            1/1     Running             2 (4d16h ago)   5d1h

查看指定命名空间下的pod

[root@k8s-master01 ~]# kubectl get po -n kube-system
NAME                                   READY   STATUS    RESTARTS        AGE
coredns-6d8c4cb4d-rzlbl                1/1     Running   2 (4d16h ago)   5d1h
coredns-6d8c4cb4d-w9hxs                1/1     Running   2 (4d16h ago)   5d1h
etcd-k8s-master01                      1/1     Running   4 (4d16h ago)   5d1h
kube-apiserver-k8s-master01            1/1     Running   2 (4d16h ago)   5d1h
kube-controller-manager-k8s-master01   1/1     Running   2 (4d16h ago)   5d1h
kube-proxy-97hqw                       1/1     Running   2 (4d16h ago)   5d1h
kube-proxy-98frt                       1/1     Running   2 (4d16h ago)   5d1h
kube-proxy-zhzjh                       1/1     Running   2 (4d16h ago)   5d1h
kube-scheduler-k8s-master01            1/1     Running   2 (4d16h ago)   5d1h

查看某个pod详细信息

[root@k8s-master01 ~]# kubectl get po -A -o wide | grep kube-apiserver-k8s-master01
kube-system    kube-apiserver-k8s-master01            1/1     Running   2 (4d16h ago)   5d1h    192.168.160.100   k8s-master01              

查看某个pod描述信息

[root@k8s-master01 ~]# kubectl describe pod kube-apiserver-k8s-master01 -n kube-system
Name:                 kube-apiserver-k8s-master01
Namespace:            kube-system
Priority:             2000001000
Priority Class Name:  system-node-critical
Node:                 k8s-master01/192.168.160.100
Start Time:           Thu, 23 Mar 2023 15:14:00 +0800
Labels:               component=kube-apiserver
                      tier=control-plane
Annotations:          kubeadm.kubernetes.io/kube-apiserver.advertise-address.endpoint: 192.168.160.100:6443
                      kubernetes.io/config.hash: d009473a9d4a2af6c51645f89f3b4c99
                      kubernetes.io/config.mirror: d009473a9d4a2af6c51645f89f3b4c99
                      kubernetes.io/config.seen: 2023-03-23T15:13:59.077626894+08:00
                      kubernetes.io/config.source: file
                      seccomp.security.alpha.kubernetes.io/pod: runtime/default
Status:               Running
IP:                   192.168.160.100
IPs:
  IP:           192.168.160.100
Controlled By:  Node/k8s-master01
Containers:
  kube-apiserver:
    Container ID:  docker://99384715a4e1a295106a66a863c4dae14d4975ea1b41ed2a2ff823924b5ec610
    Image:         registry.aliyuncs.com/google_containers/kube-apiserver:v1.23.17
    Image ID:      docker-pullable://registry.aliyuncs.com/google_containers/kube-apiserver@sha256:46953dc9bc74e9a5dc86a994b3f7717805f9d0c690bafe78ab7af631b757decf
    Port:          
    Host Port:     
    Command:
      kube-apiserver
      --advertise-address=192.168.160.100
      --allow-privileged=true
      --authorization-mode=Node,RBAC
      --client-ca-file=/etc/kubernetes/pki/ca.crt
      --enable-admission-plugins=NodeRestriction
      --enable-bootstrap-token-auth=true
      --etcd-cafile=/etc/kubernetes/pki/etcd/ca.crt
      --etcd-certfile=/etc/kubernetes/pki/apiserver-etcd-client.crt
      --etcd-keyfile=/etc/kubernetes/pki/apiserver-etcd-client.key
      --etcd-servers=https://127.0.0.1:2379
      --kubelet-client-certificate=/etc/kubernetes/pki/apiserver-kubelet-client.crt
      --kubelet-client-key=/etc/kubernetes/pki/apiserver-kubelet-client.key
      --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname
      --proxy-client-cert-file=/etc/kubernetes/pki/front-proxy-client.crt
      --proxy-client-key-file=/etc/kubernetes/pki/front-proxy-client.key
      --requestheader-allowed-names=front-proxy-client
      --requestheader-client-ca-file=/etc/kubernetes/pki/front-proxy-ca.crt
      --requestheader-extra-headers-prefix=X-Remote-Extra-
      --requestheader-group-headers=X-Remote-Group
      --requestheader-username-headers=X-Remote-User
      --secure-port=6443
      --service-account-issuer=https://kubernetes.default.svc.cluster.local
      --service-account-key-file=/etc/kubernetes/pki/sa.pub
      --service-account-signing-key-file=/etc/kubernetes/pki/sa.key
      --service-cluster-ip-range=10.1.0.0/16
      --tls-cert-file=/etc/kubernetes/pki/apiserver.crt
      --tls-private-key-file=/etc/kubernetes/pki/apiserver.key
    State:          Running
      Started:      Tue, 28 Mar 2023 15:11:36 +0800
    Last State:     Terminated
      Reason:       Error
      Exit Code:    1
      Started:      Thu, 23 Mar 2023 23:03:14 +0800
      Finished:     Fri, 24 Mar 2023 00:13:40 +0800
    Ready:          True
    Restart Count:  2
    Requests:
      cpu:        250m
    Liveness:     http-get https://192.168.160.100:6443/livez delay=10s timeout=15s period=10s #success=1 #failure=8
    Readiness:    http-get https://192.168.160.100:6443/readyz delay=0s timeout=15s period=1s #success=1 #failure=3
    Startup:      http-get https://192.168.160.100:6443/livez delay=10s timeout=15s period=10s #success=1 #failure=24
    Environment:  
    Mounts:
      /etc/kubernetes/pki from k8s-certs (ro)
      /etc/pki from etc-pki (ro)
      /etc/ssl/certs from ca-certs (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  ca-certs:
    Type:          HostPath (bare host directory volume)
    Path:          /etc/ssl/certs
    HostPathType:  DirectoryOrCreate
  etc-pki:
    Type:          HostPath (bare host directory volume)
    Path:          /etc/pki
    HostPathType:  DirectoryOrCreate
  k8s-certs:
    Type:          HostPath (bare host directory volume)
    Path:          /etc/kubernetes/pki
    HostPathType:  DirectoryOrCreate
QoS Class:         Burstable
Node-Selectors:    
Tolerations:       :NoExecute op=Exists
Events:
  Type     Reason            Age                 From     Message
  ----     ------            ----                ----     -------
  Warning  DNSConfigForming  16s (x23 over 27m)  kubelet  Nameserver limits were exceeded, some nameservers have been omitted, the applied nameserver line is: 10.159.65.1 10.159.65.2 223.5.5.5

强制删除pod

[root@k8s-master01 ~]# kubectl delete po ng-test --force --grace-period=0 -n csw
待验证

namespace 相关

切换默认的命名空间

[root@k8s-master01 ~]# kubectl config set-context --current --namespace=kube-system
Context "kubernetes-admin@kubernetes" modified.

获取集群命名空间列表

[root@k8s-master01 ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   5d1h
kube-flannel      Active   5d1h
kube-node-lease   Active   5d1h
kube-public       Active   5d1h
kube-system       Active   5d1h

创建命名空间

[root@k8s-master01 ~]# kubectl create ns demos
namespace/demos created
[root@k8s-master01 ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   5d1h
demos             Active   14s
kube-flannel      Active   5d1h
kube-node-lease   Active   5d1h
kube-public       Active   5d1h
kube-system       Active   5d1h

删除命名空间

[root@k8s-master01 ~]# kubectl delete ns demos
namespace "demos" deleted
[root@k8s-master01 ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   5d1h
kube-flannel      Active   5d1h
kube-node-lease   Active   5d1h
kube-public       Active   5d1h
kube-system       Active   5d1h

查看位于命名空间的资源

[root@k8s-master01 ~]# kubectl api-resources --namespaced=true
NAME                        SHORTNAMES   APIVERSION                     NAMESPACED   KIND
bindings                                 v1                             true         Binding
configmaps                  cm           v1                             true         ConfigMap
endpoints                   ep           v1                             true         Endpoints
events                      ev           v1                             true         Event
limitranges                 limits       v1                             true         LimitRange
persistentvolumeclaims      pvc          v1                             true         PersistentVolumeClaim
pods                        po           v1                             true         Pod
podtemplates                             v1                             true         PodTemplate
replicationcontrollers      rc           v1                             true         ReplicationController
resourcequotas              quota        v1                             true         ResourceQuota
secrets                                  v1                             true         Secret
serviceaccounts             sa           v1                             true         ServiceAccount
services                    svc          v1                             true         Service
controllerrevisions                      apps/v1                        true         ControllerRevision
daemonsets                  ds           apps/v1                        true         DaemonSet
deployments                 deploy       apps/v1                        true         Deployment
replicasets                 rs           apps/v1                        true         ReplicaSet
statefulsets                sts          apps/v1                        true         StatefulSet
localsubjectaccessreviews                authorization.k8s.io/v1        true         LocalSubjectAccessReview
horizontalpodautoscalers    hpa          autoscaling/v2                 true         HorizontalPodAutoscaler
cronjobs                    cj           batch/v1                       true         CronJob
jobs                                     batch/v1                       true         Job
leases                                   coordination.k8s.io/v1         true         Lease
endpointslices                           discovery.k8s.io/v1            true         EndpointSlice
events                      ev           events.k8s.io/v1               true         Event
ingresses                   ing          networking.k8s.io/v1           true         Ingress
networkpolicies             netpol       networking.k8s.io/v1           true         NetworkPolicy
poddisruptionbudgets        pdb          policy/v1                      true         PodDisruptionBudget
rolebindings                             rbac.authorization.k8s.io/v1   true         RoleBinding
roles                                    rbac.authorization.k8s.io/v1   true         Role
csistoragecapacities                     storage.k8s.io/v1beta1         true         CSIStorageCapacity

查看不在命名空间内的资源

[root@k8s-master01 ~]# kubectl api-resources --namespaced=false
NAME                              SHORTNAMES   APIVERSION                             NAMESPACED   KIND
componentstatuses                 cs           v1                                     false        ComponentStatus
namespaces                        ns           v1                                     false        Namespace
nodes                             no           v1                                     false        Node
persistentvolumes                 pv           v1                                     false        PersistentVolume
mutatingwebhookconfigurations                  admissionregistration.k8s.io/v1        false        MutatingWebhookConfiguration
validatingwebhookconfigurations                admissionregistration.k8s.io/v1        false        ValidatingWebhookConfiguration
customresourcedefinitions         crd,crds     apiextensions.k8s.io/v1                false        CustomResourceDefinition
apiservices                                    apiregistration.k8s.io/v1              false        APIService
tokenreviews                                   authentication.k8s.io/v1               false        TokenReview
selfsubjectaccessreviews                       authorization.k8s.io/v1                false        SelfSubjectAccessReview
selfsubjectrulesreviews                        authorization.k8s.io/v1                false        SelfSubjectRulesReview
subjectaccessreviews                           authorization.k8s.io/v1                false        SubjectAccessReview
certificatesigningrequests        csr          certificates.k8s.io/v1                 false        CertificateSigningRequest
flowschemas                                    flowcontrol.apiserver.k8s.io/v1beta2   false        FlowSchema
prioritylevelconfigurations                    flowcontrol.apiserver.k8s.io/v1beta2   false        PriorityLevelConfiguration
ingressclasses                                 networking.k8s.io/v1                   false        IngressClass
runtimeclasses                                 node.k8s.io/v1                         false        RuntimeClass
podsecuritypolicies               psp          policy/v1beta1                         false        PodSecurityPolicy
clusterrolebindings                            rbac.authorization.k8s.io/v1           false        ClusterRoleBinding
clusterroles                                   rbac.authorization.k8s.io/v1           false        ClusterRole
priorityclasses                   pc           scheduling.k8s.io/v1                   false        PriorityClass
csidrivers                                     storage.k8s.io/v1                      false        CSIDriver
csinodes                                       storage.k8s.io/v1                      false        CSINode
storageclasses                    sc           storage.k8s.io/v1                      false        StorageClass
volumeattachments                              storage.k8s.io/v1                      false        VolumeAttachment

service 相关

获取service列表

[root@k8s-master01 ~]# kubectl get svc
NAME       TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)                  AGE
kube-dns   ClusterIP   10.1.0.10            53/UDP,53/TCP,9153/TCP   5d1h
[root@k8s-master01 ~]# kubectl config set-context --current --namespace=default
Context "kubernetes-admin@kubernetes" modified.
[root@k8s-master01 ~]# kubectl get svc
NAME            TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes      ClusterIP   10.1.0.1               443/TCP        5d1h
nginx-service   NodePort    10.1.223.156           80:32613/TCP   5d1h

deployment

获取deployment

待研究

卷相关

获取PV

[root@k8s-master01 ~]# kubectl get pv
No resources found

获取多种资源

[root@k8s-master01 ~]# kubectl get ns,svc,po -A -o wide
NAME                        STATUS   AGE
namespace/default           Active   5d1h
namespace/kube-flannel      Active   5d1h
namespace/kube-node-lease   Active   5d1h
namespace/kube-public       Active   5d1h
namespace/kube-system       Active   5d1h

NAMESPACE     NAME                    TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)                  AGE    SELECTOR
default       service/kubernetes      ClusterIP   10.1.0.1               443/TCP                  5d1h   
default       service/nginx-service   NodePort    10.1.223.156           80:32613/TCP             5d1h   app=nginx
kube-system   service/kube-dns        ClusterIP   10.1.0.10              53/UDP,53/TCP,9153/TCP   5d1h   k8s-app=kube-dns

NAMESPACE      NAME                                       READY   STATUS    RESTARTS        AGE    IP                NODE           NOMINATED NODE   READINESS GATES
default        pod/ng-test                                1/1     Running   0               15m    10.244.2.8        k8s-node01                
default        pod/nginx-deployment-85b98978db-ksvqb      1/1     Running   2 (4d16h ago)   5d1h   10.244.2.6        k8s-node01                
default        pod/nginx-deployment-85b98978db-wvnxr      1/1     Running   2 (4d16h ago)   5d1h   10.244.1.9        k8s-node02                
default        pod/nginx-deployment-85b98978db-zckmw      1/1     Running   2 (4d16h ago)   5d1h   10.244.1.8        k8s-node02                
kube-flannel   pod/kube-flannel-ds-562d5                  1/1     Running   2 (4d16h ago)   5d1h   192.168.160.102   k8s-node02                
kube-flannel   pod/kube-flannel-ds-mpvlr                  1/1     Running   3 (4d16h ago)   5d1h   192.168.160.101   k8s-node01                
kube-flannel   pod/kube-flannel-ds-rdzff                  1/1     Running   2 (4d16h ago)   5d1h   192.168.160.100   k8s-master01              
kube-system    pod/coredns-6d8c4cb4d-rzlbl                1/1     Running   2 (4d16h ago)   5d1h   10.244.1.10       k8s-node02                
kube-system    pod/coredns-6d8c4cb4d-w9hxs                1/1     Running   2 (4d16h ago)   5d1h   10.244.2.7        k8s-node01                
kube-system    pod/etcd-k8s-master01                      1/1     Running   4 (4d16h ago)   5d1h   192.168.160.100   k8s-master01              
kube-system    pod/kube-apiserver-k8s-master01            1/1     Running   2 (4d16h ago)   5d1h   192.168.160.100   k8s-master01              
kube-system    pod/kube-controller-manager-k8s-master01   1/1     Running   2 (4d16h ago)   5d1h   192.168.160.100   k8s-master01              
kube-system    pod/kube-proxy-97hqw                       1/1     Running   2 (4d16h ago)   5d1h   192.168.160.102   k8s-node02                
kube-system    pod/kube-proxy-98frt                       1/1     Running   2 (4d16h ago)   5d1h   192.168.160.100   k8s-master01              
kube-system    pod/kube-proxy-zhzjh                       1/1     Running   2 (4d16h ago)   5d1h   192.168.160.101   k8s-node01                
kube-system    pod/kube-scheduler-k8s-master01            1/1     Running   2 (4d16h ago)   5d1h   192.168.160.100   k8s-master01              

你可能感兴趣的:(kubernetes,docker,容器)