微分享-kubectl get常用命令

查询节点

kubectl get nodes
NAME                STATUS   ROLES           AGE   VERSION
dev-control-plane   Ready    control-plane   66d   v1.25.2

获得更加详细的信息

kubectl get nodes -o wide
AME                STATUS   ROLES           AGE   VERSION   INTERNAL-IP   EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION      CONTAINER-RUNTIME
dev-control-plane   Ready    control-plane   66d   v1.25.2   172.18.0.2    <none>        Ubuntu 22.04.1 LTS   5.10.104-linuxkit   containerd://1.6.8

查询pod信息

获取pod信息,默认是default名称空间

 kubectl get pods
 NAME                       READY   STATUS    RESTARTS        AGE
application-sample-0       1/1     Running   2 (7m32s ago)   65d
application-sample-1       1/1     Running   2 (7m32s ago)   65d
application-sample-2       1/1     Running   2 (7m32s ago)   65d
traefik-6b4f56955f-krwx6   1/1     Running   1 (7m32s ago)   47d

获取pod信息,默认是default名称空间,并查看附加信息【如:pod的IP及在哪个节点运行】

 kubectl get pods -o wide
 NAME                       READY   STATUS    RESTARTS        AGE   IP           NODE                NOMINATED NODE   READINESS GATES
application-sample-0       1/1     Running   2 (8m34s ago)   65d   10.244.0.6   dev-control-plane   <none>           <none>
application-sample-1       1/1     Running   2 (8m34s ago)   65d   10.244.0.2   dev-control-plane   <none>           <none>
application-sample-2       1/1     Running   2 (8m34s ago)   65d   10.244.0.9   dev-control-plane   <none>           <none>
traefik-6b4f56955f-krwx6   1/1     Running   1 (8m34s ago)   47d   10.244.0.7   dev-control-plane   <none>           <none>

获取指定名称空间的pod

kubectl get pods -n kube-system
NAME                                        READY   STATUS    RESTARTS        AGE
coredns-565d847f94-4s84z                    1/1     Running   3 (9m27s ago)   66d
coredns-565d847f94-8hcxv                    1/1     Running   3 (9m27s ago)   66d
etcd-dev-control-plane                      1/1     Running   3 (9m27s ago)   66d
kindnet-wdhvg                               1/1     Running   3 (9m27s ago)   66d

获取所有名称空间的pod

kubectl get pods -A 

查看pod的标签信息

kubectl get pods --show-labels
AME                       READY   STATUS    RESTARTS      AGE   LABELS
application-sample-0       1/1     Running   2 (14m ago)   65d   app=nginx

根据Selector(label query)来查询pod

kubectl get pod -A --selector="app=nginx" --show-labels
# 简化写法
kubectl get pods -l app=nginx --show-labels

NAMESPACE   NAME                   READY   STATUS    RESTARTS      AGE
default     application-sample-0   1/1     Running   2 (17m ago)   65d
default     application-sample-1   1/1     Running   2 (17m ago)   65d
default     application-sample-2   1/1     Running   2 (17m ago)   65d

获取指定名称空间中的指定pod

kubectl get pod -n kube-system coredns-565d847f94-4s84z
NAME                       READY   STATUS    RESTARTS      AGE
coredns-565d847f94-4s84z   1/1     Running   3 (10m ago)   66d

查看pod的详细信息,以yaml格式显示

kubectl get pod -n kube-system coredns-565d847f94-4s84z -o yaml

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: "2023-06-12T06:47:56Z"
  generateName: coredns-565d847f94-
  labels:
    k8s-app: kube-dns
    pod-template-hash: 565d847f94
  name: coredns-565d847f94-4s84z
  namespace: kube-system
  ownerReferences:
 - apiVersion: apps/v1
    blockOwnerDeletion: true
    controller: true
    kind: ReplicaSet
    name: coredns-565d847f94
spec:
  affinity:
    podAntiAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - podAffinityTerm:
          labelSelector:
          .......
  phase: Running
  podIP: 10.244.0.5
  podIPs:
 - ip: 10.244.0.5
  qosClass: Burstable
  startTime: "2023-06-12T06:48:03Z"

总结

kubectl get

  • node 代表节点
  • pods 代表pod
    • -n 命名空间
    • -o
      • wide 更详细信息
    • 具体pod名称 -o
      • yaml 查看对应pod的yaml文件信息
    • –show-labels 显示标签
    • -l +具体标签信息

举个例子:查询一个label为k8s-app=kube-dns的pod,并显示出标签和其详细信息

kubectl get pods -A -l k8s-app=kube-dns --show-labels -o wide
NAMESPACE     NAME                       READY   STATUS    RESTARTS      AGE   IP           NODE                NOMINATED NODE   READINESS GATES   LABELS
kube-system   coredns-565d847f94-4s84z   1/1     Running   3 (35m ago)   66d   10.244.0.5   dev-control-plane   <none>           <none>            k8s-app=kube-dns,pod-template-hash=565d847f94
kube-system   coredns-565d847f94-8hcxv   1/1     Running   3 (35m ago)   66d   10.244.0.8   dev-control-plane   <none>           <none>            k8s-app=kube-dns,pod-template-hash=565d847f94

参考
Kubernetes K8S之kubectl命令详解及常用示例

你可能感兴趣的:(微分享,kubernetes,容器)