k8s常用命令详解

1.核心资源类型

  • po -> pods
  • svc -> services
  • deploy -> deployments
  • rs -> replicasets
  • nv -> namespaces
  • no -> nodes
  • ing -> ingresses(入口)
  • cm -> configmaps
  • secret -> secrets
  • pv -> persistentvolumes
  • pvc -> persistentvolumeclaims
  • sa -> serviceaccounts

2.语法格式

kubectl [command] [TYPE] [NAME] [flags]

3.集群和节点管理

3.1.查看集群信息

[root@k8s-master-001 ~]# kubectl cluster-info
Kubernetes control plane is running at https://10.201.82.136:6443
CoreDNS is running at https://10.201.82.136:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
Metrics-server is running at https://10.201.82.136:6443/api/v1/namespaces/kube-system/services/https:metrics-server:/proxy

作用:显示Master节点和核心服务(如DNS)的地址

3.2.查看节点状态

[root@k8s-master-001 ~]# kubectl get nodes
NAME            STATUS                     ROLES    AGE    VERSION
10.201.82.136   Ready,SchedulingDisabled   <none>   318d   v1.23.10
10.201.82.137   Ready                      <none>   318d   v1.23.10
10.201.82.138   Ready                      <none>   318d   v1.23.10
10.201.82.139   Ready                      <none>   318d   v1.23.10
10.201.82.140   Ready                      <none>   318d   v1.23.10
10.201.82.141   NotReady                   <none>   318d   v1.23.10
10.201.82.142   Ready                      <none>   318d   v1.23.10
10.201.82.143   Ready                      <none>   318d   v1.23.10
#查看更详细的信息(如内核版本,资源容量)
[root@k8s-master-001 ~]# kubectl get nodes -o wide
NAME            STATUS                     ROLES    AGE    VERSION    INTERNAL-IP     EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION                CONTAINER-RUNTIME
10.201.82.136   Ready,SchedulingDisabled   <none>   318d   v1.23.10   10.201.82.136   <none>        CentOS Linux 7 (Core)   4.19.12-1.el7.elrepo.x86_64   docker://18.9.9
10.201.82.137   Ready                      <none>   318d   v1.23.10   10.201.82.137   <none>        CentOS Linux 7 (Core)   4.19.12-1.el7.elrepo.x86_64   docker://18.9.9
10.201.82.138   Ready                      <none>   318d   v1.23.10   10.201.82.138   <none>        CentOS Linux 7 (Core)   4.19.12-1.el7.elrepo.x86_64   docker://18.9.9
10.201.82.139   Ready                      <none>   318d   v1.23.10   10.201.82.139   <none>        CentOS Linux 7 (Core)   4.19.12-1.el7.elrepo.x86_64   docker://18.9.9
10.201.82.140   Ready                      <none>   318d   v1.23.10   10.201.82.140   <none>        CentOS Linux 7 (Core)   4.19.12-1.el7.elrepo.x86_64   docker://18.9.9
10.201.82.141   NotReady                   <none>   318d   v1.23.10   10.201.82.141   <none>        CentOS Linux 7 (Core)   4.19.12-1.el7.elrepo.x86_64   docker://Unknown
10.201.82.142   Ready                      <none>   318d   v1.23.10   10.201.82.142   <none>        CentOS Linux 7 (Core)   4.19.12-1.el7.elrepo.x86_64   docker://18.9.9
10.201.82.143   Ready                      <none>   318d   v1.23.10   10.201.82.143   <none>        CentOS Linux 7 (Core)   4.19.12-1.el7.elrepo.x86_64   docker://18.9.9
#查看某个阶段的详细信息
[root@k8s-master-001 ~]# kubectl describe node 10.201.82.136
Name:               10.201.82.136
Roles:              <none>
Labels:             beta.kubernetes.io/arch=amd64
                    beta.kubernetes.io/os=linux
                    kubernetes.io/arch=amd64
                    kubernetes.io/hostname=10.201.82.136
                    kubernetes.io/os=linux
                    zone=public
Annotations:        node.alpha.kubernetes.io/ttl: 0
                    volumes.kubernetes.io/controller-managed-attach-detach: true
CreationTimestamp:  Fri, 25 Oct 2024 03:27:33 +0800
Taints:             node.kubernetes.io/unschedulable:NoSchedule
Unschedulable:      true
Lease:
  HolderIdentity:  10.201.82.136
  AcquireTime:     <unset>
  RenewTime:       Mon, 08 Sep 2025 09:54:58 +0800
Conditions:
  Type             Status  LastHeartbeatTime                 LastTransitionTime                Reason                       Message
  ----             ------  -----------------                 ------------------                ------                       -------
  MemoryPressure   False   Mon, 08 Sep 2025 09:54:52 +0800   Fri, 25 Oct 2024 03:27:33 +0800   KubeletHasSufficientMemory   kubelet has sufficient memory available
  DiskPressure     False   Mon, 08 Sep 2025 09:54:52 +0800   Fri, 25 Oct 2024 03:27:33 +0800   KubeletHasNoDiskPressure     kubelet has no disk pressure
  PIDPressure      False   Mon, 08 Sep 2025 09:54:52 +0800   Fri, 25 Oct 2024 03:27:33 +0800   KubeletHasSufficientPID      kubelet has sufficient PID available
  Ready            True    Mon, 08 Sep 2025 09:54:52 +0800   Mon, 03 Mar 2025 05:16:57 +0800   KubeletReady                 kubelet is posting ready status
Addresses:
  InternalIP:  10.201.82.136
  Hostname:    10.201.82.136
Capacity:
  cpu:                8
  ephemeral-storage:  309500736Ki
  hugepages-2Mi:      0
  memory:             24671864Ki
  pods:               110
Allocatable:
  cpu:                7600m
  ephemeral-storage:  285235877826
  hugepages-2Mi:      0
  memory:             23647864Ki
  pods:               110
System Info:
  Machine ID:                 651506103bc742469c237f96ce0ce1d1
  System UUID:                b47ecc82-8f47-0546-bf15-0f2c0823c234
  Boot ID:                    2fca042d-b226-4bb2-9719-f53d044ab812
  Kernel Version:             4.19.12-1.el7.elrepo.x86_64
  OS Image:                   CentOS Linux 7 (Core)
  Operating System:           linux
  Architecture:               amd64
  Container Runtime Version:  docker://18.9.9
  Kubelet Version:            v1.23.10
  Kube-Proxy Version:         v1.23.10
PodCIDR:                      172.16.0.0/24
PodCIDRs:                     172.16.0.0/24
Non-terminated Pods:          (7 in total)
  Namespace                   Name                                     CPU Requests  CPU Limits  Memory Requests  Memory Limits  Age
  ---------                   ----                                     ------------  ----------  ---------------  -------------  ---
  default                     filebeat-cbc64                           0 (0%)        0 (0%)      0 (0%)           0 (0%)         318d
  kube-system                 etcd-10.201.82.136                       100m (1%)     0 (0%)      100Mi (0%)       0 (0%)         318d
  kube-system                 kube-apiserver-10.201.82.136             250m (3%)     0 (0%)      0 (0%)           0 (0%)         318d
  kube-system                 kube-controller-manager-10.201.82.136    200m (2%)     0 (0%)      0 (0%)           0 (0%)         318d
  kube-system                 kube-router-mvw7m                        250m (3%)     0 (0%)      250Mi (1%)       0 (0%)         318d
  kube-system                 kube-scheduler-10.201.82.136             100m (1%)     0 (0%)      0 (0%)           0 (0%)         318d
  kube-system                 traefik-ingress-controller-67v5c         0 (0%)        0 (0%)      0 (0%)           0 (0%)         317d
Allocated resources:
  (Total limits may be over 100 percent, i.e., overcommitted.)
  Resource           Requests    Limits
  --------           --------    ------
  cpu                900m (11%)  0 (0%)
  memory             350Mi (1%)  0 (0%)
  ephemeral-storage  0 (0%)      0 (0%)
  hugepages-2Mi      0 (0%)      0 (0%)
Events:              <none>

4.命名空间操作

4.1.查看所有命名空间

[root@k8s-master-001 ~]# kubectl get namespaces
NAME              STATUS   AGE
datastudio        Active   318d
default           Active   318d
dimmetrics        Active   318d
engage            Active   318d
gkp               Active   317d
gvp               Active   317d
jaeger            Active   170d
kube-node-lease   Active   318d
kube-public       Active   318d
kube-system       Active   318d
lakehouse         Active   318d
liveinsight       Active   318d
metaspace         Active   318d
minio             Active   178d
moebius-system    Active   318d
spark             Active   202d
spark-test        Active   204d
sre               Active   318d
zongguan          Active   318d
#获取使用缩写
kubectl get ns

4.2.切换当前操作的命名空间

# 为当前上下文设置默认命名空间
kubectl config set-context --current --namespace=<namespace-name>
# 例如,切换到 kube-system
kubectl config set-context --current --namespace=kube-system

4.3.指定命名空间执行命令

大多数命令都可以通过-n或-namespace 来指定命名空间

[root@k8s-master-001 ~]# kubectl get pods -n kube-system
NAME                                    READY   STATUS    RESTARTS        AGE
coredns-5c988b955c-6wrgl                1/1     Running   0               113d
coredns-5c988b955c-nbb9t                1/1     Running   0               139d
coredns-5c988b955c-tlh5x                1/1     Running   0               139d
etcd-10.201.82.136                      1/1     Running   1 (189d ago)    318d
kube-apiserver-10.201.82.136            1/1     Running   3 (113d ago)    318d
kube-controller-manager-10.201.82.136   1/1     Running   35 (113d ago)   318d
kube-router-4t5s5                       1/1     Running   0               318d
kube-router-5mn7r                       1/1     Running   15 (22d ago)    318d
kube-router-7h227                       1/1     Running   11 (29h ago)    318d
kube-router-94vlh                       1/1     Running   0               318d
kube-router-jxjkx                       1/1     Running   0               318d
kube-router-kgqlh                       1/1     Running   3 (69d ago)     318d
kube-router-mvw7m                       1/1     Running   1 (189d ago)    318d
kube-router-zgd88                       1/1     Running   15 (17d ago)    318d
kube-scheduler-10.201.82.136            1/1     Running   36 (113d ago)   318d
metrics-server-645bf7fbbb-kds4g         1/1     Running   0               113d
traefik-ingress-controller-2dsqj        1/1     Running   1 (312d ago)    317d
traefik-ingress-controller-44h8x        1/1     Running   0               317d
traefik-ingress-controller-5jrjt        1/1     Running   0               317d
traefik-ingress-controller-67v5c        1/1     Running   1 (189d ago)    317d
traefik-ingress-controller-7d57n        1/1     Running   0               317d
traefik-ingress-controller-9zzq4        1/1     Running   0               317d
traefik-ingress-controller-cztdr        1/1     Running   0               317d
traefik-ingress-controller-vcjz4        1/1     Running   0               317d
[root@k8s-master-001 ~]# kubectl describe pod/dsm-api-64dd7bb99-2x2tk -n gkp
Name:         dsm-api-64dd7bb99-2x2tk
Namespace:    gkp
Priority:     0
Node:         10.201.82.139/10.201.82.139
Start Time:   Mon, 25 Aug 2025 09:53:46 +0800
Labels:       name=dsm-api
              pod-template-hash=64dd7bb99
Annotations:  kubectl.kubernetes.io/restartedAt: 2025-05-14T00:57:41Z
Status:       Running
IP:           172.16.6.43
IPs:
  IP:           172.16.6.43
Controlled By:  ReplicaSet/dsm-api-64dd7bb99
Containers:
  dsm-api:
    Container ID:   docker://194820294ba9cdb0b4ca183353377c0525e5a4622e1de5af8ff637496a5a2687
    Image:          docker.gridsumdissector.com/gai/dsm-api:2.1.0-630-214
    Image ID:       docker-pullable://docker.gridsumdissector.com/gai/dsm-api@sha256:396d843f1c0f577b96ab3b2088ac5c3d4df84a6fde3f1e976d4c73321347eef3
    Port:           8080/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Mon, 25 Aug 2025 09:53:57 +0800
    Ready:          True
    Restart Count:  0
    Environment:
      PROFILE:                master
      APP_NAME:               dsm-api
      CLOSE_TRACING:          true
      APPLICATION_NAMESPACE:  gkp (v1:metadata.namespace)
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-wb8vq (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  kube-api-access-wb8vq:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              gaisuanfakaifatai=gaisuanfakaifatai
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:                      <none>

5.Pod管理

Pod是kubernetes中最小的部署单元

5.1.查看pod

#查看当前命名空间下所有的pod
[root@k8s-master-001 ~]# kubectl get pods
NAME                                 READY   STATUS             RESTARTS            AGE
agentd-5dfcc57d6b-52kqp              1/1     Running            0                   113d
filebeat-4bnc6                       1/1     Running            0                   318d
filebeat-cbc64                       1/1     Running            1 (189d ago)        318d
filebeat-fr5v4                       1/1     Running            0                   318d
filebeat-mp64p                       1/1     Running            1 (312d ago)        318d
filebeat-mtqzk                       1/1     Running            0                   318d
filebeat-nzp8b                       1/1     Running            0                   318d
filebeat-xtx5b                       1/1     Running            0                   318d
filebeat-zbprf                       1/1     Running            0                   318d
ibex-678d5bf5f-nz5h9                 0/1     CrashLoopBackOff   43247 (5m10s ago)   153d
milvus-attu-6c495cdc57-knjlt         1/1     Running            0                   31d
milvus-datacoord-87b5549d5-xhjwl     1/1     Running            0                   31d
milvus-datanode-6b4ddbff8f-4trrg     1/1     Running            3297 (9m10s ago)    31d
milvus-indexcoord-9647fdb89-fpd5t    1/1     Running            0                   31d
milvus-indexnode-6b569cc474-zdvcj    1/1     Running            0                   30h
milvus-proxy-7c45c9b897-8zvk4        1/1     Running            0                   31d
milvus-querycoord-5d647d6778-mddzf   1/1     Running            3297 (9m8s ago)     31d
milvus-querynode-6c7677f4c6-74c72    1/1     Running            3296 (10m ago)      31d
milvus-rootcoord-6c6f4946b6-qbgvr    1/1     Running            133 (7m36s ago)     30h
nightingale-init-jqmbs               0/1     Completed          0                   317d
nserver-78474dd64d-5kmfh             1/1     Running            5268 (94d ago)      113d
nwebapi-64b799db56-sm24z             0/1     CrashLoopBackOff   31840 (3m43s ago)   113d
prometheus-76ff7864ff-df8hm          1/1     Running            0                   317d
redis-yeying-6f5c5f8b8f-lxf8f        1/1     Running            0                   317d
tarzan-nacos-7c76d9d97-9dfmv         1/1     Running            0                   318d
#查看所有命名空间的pod
[root@k8s-master-001 ~]# kubectl get pods -A
NAMESPACE        NAME                                                       READY   STATUS             RESTARTS            AGE
datastudio       datastudio-backend-1-5-6-6d57859476-ggbnn                  1/1     Running            0                   69d
datastudio       datastudio-frontend-1-5-6-6446764fd5-6k7j6                 1/1     Running            0                   317d
default          agentd-5dfcc57d6b-52kqp                                    1/1     Running            0                   113d
default          filebeat-4bnc6                                             1/1     Running            0                   318d
default          filebeat-cbc64                                             1/1     Running            1 (189d ago)        318d
default          filebeat-fr5v4                                             1/1     Running            0                   318d
default          filebeat-mp64p                                             1/1     Running            1 (312d ago)        318d
default          filebeat-mtqzk                                             1/1     Running            0                   318d
default          filebeat-nzp8b                                             1/1     Running            0                   318d
default          filebeat-xtx5b                                             1/1     Running            0                   318d
default          filebeat-zbprf                                             1/1     Running            0                   318d
default          ibex-678d5bf5f-nz5h9                                       0/1     CrashLoopBackOff   43248 (105s ago)    153d
default          milvus-attu-6c495cdc57-knjlt                               1/1     Running            0                   31d
default          milvus-datacoord-87b5549d5-xhjwl                           1/1     Running            0                   31d
default          milvus-datanode-6b4ddbff8f-4trrg                           1/1     Running            3297 (10m ago)      31d
default          milvus-indexcoord-9647fdb89-fpd5t                          1/1     Running            0                   31d
default          milvus-indexnode-6b569cc474-zdvcj                          1/1     Running            0                   30h
default          milvus-proxy-7c45c9b897-8zvk4                              1/1     Running            0                   31d
default          milvus-querycoord-5d647d6778-mddzf                         1/1     Running            3297 (10m ago)      31d
default          milvus-querynode-6c7677f4c6-74c72                          1/1     Running            3296 (12m ago)      31d
default          milvus-rootcoord-6c6f4946b6-qbgvr                          1/1     Running            133 (9m23s ago)     30h
default          nightingale-init-jqmbs                                     0/1     Completed          0                   317d
default          nserver-78474dd64d-5kmfh                                   1/1     Running            5268 (94d ago)      113d
default          nwebapi-64b799db56-sm24z                                   0/1     CrashLoopBackOff   31841 (24s ago)     113d
default          prometheus-76ff7864ff-df8hm                                1/1     Running            0                   317d
default          redis-yeying-6f5c5f8b8f-lxf8f                              1/1     Running            0                   317d
default          tarzan-nacos-7c76d9d97-9dfmv                               1/1     Running            0                   318d
dimmetrics       dimmetrics3-backend-3-4-2-5f9478c66-sbvmc                  1/1     Running            0                   178d
dimmetrics       dimmetrics3-frontend-3-4-2-765fd6d8f4-nd6db                1/1     Running            0                   317d
engage           flowable-svc-6999fdb4b-j6gpf                               0/1     CrashLoopBackOff   310 (5m8s ago)      30h
engage           gep-api-market-svc-6984846bbb-dsppn                        1/1     Running            0                   48d
engage           gep-api-market-svc-6984846bbb-vpfvm                        1/1     Terminating        0                   103d
engage           gep-app-svc-5fcfcffc8-9lwpp                                1/1     Running            0                   30h
engage           gep-config-center-svc-65cdd59cb5-2pt76                     1/1     Terminating        0                   103d
engage           gep-config-center-svc-65cdd59cb5-fz755                     1/1     Running            0                   48d
engage           gep-development-fe-667864f98d-wvxng                        1/1     Running            0                   30h
#查看更加详细的信息
[root@k8s-master-001 ~]# kubectl get pods -o wide
NAME                                 READY   STATUS             RESTARTS            AGE    IP             NODE            NOMINATED NODE   READINESS GATES
agentd-5dfcc57d6b-52kqp              1/1     Running            0                   113d   172.16.4.137   10.201.82.138   <none>           <none>
filebeat-4bnc6                       1/1     Running            0                   318d   172.16.3.3     10.201.82.143   <none>           <none>
filebeat-cbc64                       1/1     Running            1 (189d ago)        318d   172.16.0.3     10.201.82.136   <none>           <none>
filebeat-fr5v4                       1/1     Running            0                   318d   172.16.1.5     10.201.82.141   <none>           <none>
filebeat-mp64p                       1/1     Running            1 (312d ago)        318d   172.16.5.78    10.201.82.140   <none>           <none>
filebeat-mtqzk                       1/1     Running            0                   318d   172.16.7.3     10.201.82.137   <none>           <none>
filebeat-nzp8b                       1/1     Running            0                   318d   172.16.6.3     10.201.82.139   <none>           <none>
filebeat-xtx5b                       1/1     Running            0                   318d   172.16.4.3     10.201.82.138   <none>           <none>
filebeat-zbprf                       1/1     Running            0                   318d   172.16.2.2     10.201.82.142   <none>           <none>
ibex-678d5bf5f-nz5h9                 0/1     CrashLoopBackOff   43248 (3m33s ago)   153d   172.16.3.248   10.201.82.143   <none>           <none>
milvus-attu-6c495cdc57-knjlt         1/1     Running            0                   31d    172.16.7.212   10.201.82.137   <none>           <none>
milvus-datacoord-87b5549d5-xhjwl     1/1     Running            0                   31d    172.16.7.211   10.201.82.137   <none>           <none>
milvus-datanode-6b4ddbff8f-4trrg     1/1     Running            3297 (12m ago)      31d    172.16.7.213   10.201.82.137   <none>           <none>
milvus-indexcoord-9647fdb89-fpd5t    1/1     Running            0                   31d    172.16.7.208   10.201.82.137   <none>           <none>
milvus-indexnode-6b569cc474-zdvcj    1/1     Running            0                   30h    172.16.2.49    10.201.82.142   <none>           <none>
milvus-proxy-7c45c9b897-8zvk4        1/1     Running            0                   31d    172.16.7.209   10.201.82.137   <none>           <none>
milvus-querycoord-5d647d6778-mddzf   1/1     Running            3297 (12m ago)      31d    172.16.7.210   10.201.82.137   <none>           <none>
milvus-querynode-6c7677f4c6-74c72    1/1     Running            3297 (35s ago)      31d    172.16.4.146   10.201.82.138   <none>           <none>
milvus-rootcoord-6c6f4946b6-qbgvr    1/1     Running            133 (11m ago)       30h    172.16.2.67    10.201.82.142   <none>           <none>
nightingale-init-jqmbs               0/1     Completed          0                   317d   172.16.4.25    10.201.82.138   <none>           <none>
nserver-78474dd64d-5kmfh             1/1     Running            5268 (94d ago)      113d   172.16.6.168   10.201.82.139   <none>           <none>
nwebapi-64b799db56-sm24z             0/1     CrashLoopBackOff   31841 (2m12s ago)   113d   172.16.6.163   10.201.82.139   <none>           <none>
prometheus-76ff7864ff-df8hm          1/1     Running            0                   317d   172.16.3.25    10.201.82.143   <none>           <none>
redis-yeying-6f5c5f8b8f-lxf8f        1/1     Running            0                   317d   172.16.7.25    10.201.82.137   <none>           <none>
tarzan-nacos-7c76d9d97-9dfmv         1/1     Running            0                   318d   172.16.4.2     10.201.82.138   <none>           <none>
#实时监控pod状态变化
[root@k8s-master-001 ~]# kubectl get pods -w
NAME                                 READY   STATUS             RESTARTS            AGE
agentd-5dfcc57d6b-52kqp              1/1     Running            0                   113d
filebeat-4bnc6                       1/1     Running            0                   318d
filebeat-cbc64                       1/1     Running            1 (189d ago)        318d
filebeat-fr5v4                       1/1     Running            0                   318d
filebeat-mp64p                       1/1     Running            1 (312d ago)        318d
filebeat-mtqzk                       1/1     Running            0                   318d
filebeat-nzp8b                       1/1     Running            0                   318d
filebeat-xtx5b                       1/1     Running            0                   318d
filebeat-zbprf                       1/1     Running            0                   318d
ibex-678d5bf5f-nz5h9                 0/1     CrashLoopBackOff   43248 (4m3s ago)    153d
milvus-attu-6c495cdc57-knjlt         1/1     Running            0                   31d
milvus-datacoord-87b5549d5-xhjwl     1/1     Running            0                   31d
milvus-datanode-6b4ddbff8f-4trrg     1/1     Running            3297 (13m ago)      31d
milvus-indexcoord-9647fdb89-fpd5t    1/1     Running            0                   31d
milvus-indexnode-6b569cc474-zdvcj    1/1     Running            0                   30h
milvus-proxy-7c45c9b897-8zvk4        1/1     Running            0                   31d
milvus-querycoord-5d647d6778-mddzf   1/1     Running            3297 (13m ago)      31d
milvus-querynode-6c7677f4c6-74c72    1/1     Running            3297 (65s ago)      31d
milvus-rootcoord-6c6f4946b6-qbgvr    1/1     Running            133 (11m ago)       30h
nightingale-init-jqmbs               0/1     Completed          0                   317d
nserver-78474dd64d-5kmfh             1/1     Running            5268 (94d ago)      113d
nwebapi-64b799db56-sm24z             0/1     CrashLoopBackOff   31841 (2m42s ago)   113d
prometheus-76ff7864ff-df8hm          1/1     Running            0                   317d
redis-yeying-6f5c5f8b8f-lxf8f        1/1     Running            0                   317d
tarzan-nacos-7c76d9d97-9dfmv         1/1     Running            0                   318d

5.2.查看pod详情

[root@k8s-master-001 ~]# kubectl describe pod dsm-api-64dd7bb99-2x2tk -n gkp
Name:         dsm-api-64dd7bb99-2x2tk
Namespace:    gkp
Priority:     0
Node:         10.201.82.139/10.201.82.139
Start Time:   Mon, 25 Aug 2025 09:53:46 +0800
Labels:       name=dsm-api
              pod-template-hash=64dd7bb99
Annotations:  kubectl.kubernetes.io/restartedAt: 2025-05-14T00:57:41Z
Status:       Running
IP:           172.16.6.43
IPs:
  IP:           172.16.6.43
Controlled By:  ReplicaSet/dsm-api-64dd7bb99
Containers:
  dsm-api:
    Container ID:   docker://194820294ba9cdb0b4ca183353377c0525e5a4622e1de5af8ff637496a5a2687
    Image:          docker.gridsumdissector.com/gai/dsm-api:2.1.0-630-214
    Image ID:       docker-pullable://docker.gridsumdissector.com/gai/dsm-api@sha256:396d843f1c0f577b96ab3b2088ac5c3d4df84a6fde3f1e976d4c73321347eef3
    Port:           8080/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Mon, 25 Aug 2025 09:53:57 +0800
    Ready:          True
    Restart Count:  0
    Environment:
      PROFILE:                master
      APP_NAME:               dsm-api
      CLOSE_TRACING:          true
      APPLICATION_NAMESPACE:  gkp (v1:metadata.namespace)
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-wb8vq (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  kube-api-access-wb8vq:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              gaisuanfakaifatai=gaisuanfakaifatai
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:                      <none>
[root@k8s-master-001 ~]# kubectl describe pod dsm-api-64dd7bb99-2x2tk
Error from server (NotFound): pods "dsm-api-64dd7bb99-2x2tk" not found
[root@k8s-master-001 ~]# kubectl describe pod dsm-api-64dd7bb99-2x2tk -n gkp
Name:         dsm-api-64dd7bb99-2x2tk
Namespace:    gkp
Priority:     0
Node:         10.201.82.139/10.201.82.139
Start Time:   Mon, 25 Aug 2025 09:53:46 +0800
Labels:       name=dsm-api
              pod-template-hash=64dd7bb99
Annotations:  kubectl.kubernetes.io/restartedAt: 2025-05-14T00:57:41Z
Status:       Running
IP:           172.16.6.43
IPs:
  IP:           172.16.6.43
Controlled By:  ReplicaSet/dsm-api-64dd7bb99
Containers:
  dsm-api:
    Container ID:   docker://194820294ba9cdb0b4ca183353377c0525e5a4622e1de5af8ff637496a5a2687
    Image:          docker.gridsumdissector.com/gai/dsm-api:2.1.0-630-214
    Image ID:       docker-pullable://docker.gridsumdissector.com/gai/dsm-api@sha256:396d843f1c0f577b96ab3b2088ac5c3d4df84a6fde3f1e976d4c73321347eef3
    Port:           8080/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Mon, 25 Aug 2025 09:53:57 +0800
    Ready:          True
    Restart Count:  0
    Environment:
      PROFILE:                master
      APP_NAME:               dsm-api
      CLOSE_TRACING:          true
      APPLICATION_NAMESPACE:  gkp (v1:metadata.namespace)
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-wb8vq (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  kube-api-access-wb8vq:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              gaisuanfakaifatai=gaisuanfakaifatai
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:                      <none>

5.3.查看pod日志

#查看一个pod的日志,如果是默认命名空间下pod可以直接查看,其他命名空间需要带命名空间,否则会找不到命名空间
kubectl logs <pod-name> -n <namespace-name>
kubectl logs dsm-api-64dd7bb99-2x2tk -n gkp
# 如果 Pod 有多个容器,需指定容器名
kubectl logs <pod-name> -c <container-name>
# 实时查看日志流(类似 tail -f)
kubectl logs -f <pod-name>
# 查看之前崩溃的 Pod 的日志(例如用于 CrashLoopBackOff 状态)
kubectl logs -f --previous <pod-name>

5.4.进入pod容器

kubectl exec -it <pod-name> -- /bin/bash
[root@k8s-master-001 ~]# kubectl exec -it dsm-api-64dd7bb99-2x2tk -n gkp -- /bin/bash
root@dsm-api-64dd7bb99-2x2tk:/workdir# ls
data  entrypoint.sh  src
root@dsm-api-64dd7bb99-2x2tk:/workdir# 
# 如果 Pod 有多个容器
kubectl exec -it <pod-name> -c <container-name> -- /bin/sh

5.5.删除pod

kubectl delete pod <pod-name>
# 强制立即删除(不等待优雅终止)
kubectl delete pod <pod-name> --grace-period=0 --force

6.部署与扩缩容

管理Deployment、ReplicaSet、Statefulset等wordload

6.1.查看部署

#获取默认命名空间下所有deployment
[root@k8s-master-001 ~]# kubectl get deployments
NAME                READY   UP-TO-DATE   AVAILABLE   AGE
agentd              1/1     1            1           317d
ibex                0/1     1            0           317d
milvus-attu         1/1     1            1           318d
milvus-datacoord    1/1     1            1           318d
milvus-datanode     1/1     1            1           318d
milvus-indexcoord   1/1     1            1           318d
milvus-indexnode    1/1     1            1           318d
milvus-proxy        1/1     1            1           318d
milvus-querycoord   1/1     1            1           318d
milvus-querynode    1/1     1            1           318d
milvus-rootcoord    1/1     1            1           318d
nserver             1/1     1            1           317d
nwebapi             0/1     1            0           317d
prometheus          1/1     1            1           317d
redis-yeying        1/1     1            1           317d
tarzan-nacos        1/1     1            1           318d
#获取指定命名空间下deployment
[root@k8s-master-001 ~]# kubectl get deployments -n gkp
NAME                                 READY   UP-TO-DATE   AVAILABLE   AGE
dsm-api                              1/1     1            1           317d
dsm-apiregister                      1/1     1            1           317d
dsm-commonj                          1/1     1            1           317d
dsm-datadocking                      1/1     1            1           317d
dsm-gateway                          1/1     1            1           317d
dsm-helpcenter                       1/1     1            1           317d
dsm-model                            1/1     1            1           317d
dsm-runtime                          1/1     1            1           317d
dsm-runtimelong                      1/1     1            1           317d
dsm-scheduler                        1/1     1            1           317d
dsm-spark                            1/1     1            1           317d
dsm-storage                          1/1     1            1           317d
dsm-task                             1/1     1            1           317d
#获取缩写
kubectl get deploy -n <namespace-name>

6.2.创建部署

# 使用 kubectl create 命令
kubectl create deployment nginx --image=nginx:1.23
# 更常用的方式是通过 YAML 文件
kubectl apply -f deployment.yaml

6.3.扩缩容

# 将名为 'nginx' 的 Deployment 扩展到 3 个副本
kubectl scale deployment/nginx --replicas=3
# 另一种方式,通过修改定义文件并应用
kubectl apply -f deployment.yaml

6.4.滚动更新与回归

# 更新镜像(触发滚动更新)
kubectl set image deployment/nginx nginx=nginx:1.24
# 查看滚动更新状态
kubectl rollout status deployment/nginx
# 查看更新历史
kubectl rollout history deployment/nginx
# 回滚到上一个版本
kubectl rollout undo deployment/nginx
# 回滚到指定版本
kubectl rollout undo deployment/nginx --to-revision=2

6.5.删除部署

kubectl delete deployment nginx
# 通过文件删除
kubectl delete -f deployment.yaml

7.服务与入口

管理Service和Ingress,用于暴露应用

7.1.查看服务

#查看默认命名空间defalut下服务信息
kubectl get services
[root@k8s-master-001 ~]# kubectl get services
NAME                  TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                           AGE
ibex                  NodePort    10.242.172.224   <none>        10090:10090/TCP,20090:20090/TCP   317d
kubernetes            ClusterIP   10.240.0.1       <none>        443/TCP                           318d
milvus                ClusterIP   10.246.2.137     <none>        19530/TCP,9091/TCP                318d
milvus-attu           NodePort    10.254.89.202    <none>        3000:28476/TCP                    318d
milvus-datacoord      ClusterIP   10.242.201.99    <none>        13333/TCP,9091/TCP                318d
milvus-datanode       ClusterIP   None             <none>        9091/TCP                          318d
milvus-indexcoord     ClusterIP   10.251.33.91     <none>        31000/TCP,9091/TCP                318d
milvus-indexnode      ClusterIP   None             <none>        9091/TCP                          318d
milvus-querycoord     ClusterIP   10.245.78.62     <none>        19531/TCP,9091/TCP                318d

# 或缩写
kubectl get svc

7.2创建服务

# 快速暴露一个 Deployment(创建 ClusterIP 类型的 Service)
kubectl expose deployment/nginx --port=80 --target-port=80
# 更常用的方式是通过 YAML 文件
kubectl apply -f service.yaml

7.3.查看入口

kubectl get ingress
# 或缩写
kubectl get ing

8.配置与存储

8.1.从文件创建ConfigMap/Secret

# 从目录创建 ConfigMap
kubectl create configmap app-config --from-file=./config/
# 从文件创建
kubectl create configmap app-config --from-file=./config/app.properties
# 从字面值创建
kubectl create secret generic db-secret --from-literal=username=admin --from-literal=password=secret

8.2.查看配置

kubectl get configmaps
kubectl describe configmap app-config
kubectl get secrets
kubectl describe secret db-secret

9.高级命令与技巧

9.1.kubectl apply vs kubectl create

kubectl apply vs kubectl create

  • create命令式管理,直接告诉 API 服务器要做什么。

  • apply声明式管理,通过 YAML 文件定义期望状态,k8s 会努力协调至该状态。这是推荐的最佳实践

9.2.使用 -o yaml--dry-run=client 导出和生成模板

# 获取一个资源的 YAML 定义(非常有用!)
kubectl get pod <pod-name> -o yaml
kubectl get deploy nginx -o yaml

# 生成一个 Deployment 的 YAML 模板,但并不真正创建它
kubectl create deployment nginx --image=nginx:1.23 --dry-run=client -o yaml > nginx-deployment.yaml

# 生成一个 Service 的 YAML 模板
kubectl create service clusterip nginx --tcp=80:80 --dry-run=client -o yaml > nginx-service.yaml

10.实际

kubectl get pods
# 发现 Pod 状态是 CrashLoopBackOff
kubectl logs my-pod
kubectl logs --previous my-pod # 查看上次崩溃的日志
# 从日志中发现是配置文件错误

kubectl describe configmap app-config

kubectl apply -f configmap.yaml

kubectl rollout restart deployment/my-app
# 或者直接删除 Pod,Deployment 会重建它
kubectl delete pod my-pod-xxxxx

kubectl get pods -w # 等待 Pod 变为 Running
kubectl logs -f my-new-pod-xxxxx # 查看新日志确认无误

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿小木的愤怒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值