kubectl常用命令(主题篇)

2024-01-08 21:54:50

上一篇是按照操作把全局性的命令给整理出来,但是在实际的使用中,经常需要对某一个主题进行操作,因此这一篇按照对应的主题进行一系列操作。

?集群

查看集群基本信息

kubectl cluster-info

namespace 命名空间

查看

kubectl? get namespace

kubectl get ns

kubectl get --all-namespaces

查看指定的命名空间

kubectl get ns default

查看特定命名空间的概况

kubectl describe ns default

创建命名空间

?kubectl create namespace dev-test

删除命名空间

kubectl delete? ns dev-test

根据配置文件创建、删除

kubectl create -f dev-test.yaml

pod

查看

查看指定空间中的pod,如果不指定则是default命名空间中的

kubectl get pod [-n kube-system]

查看所有命名空间中的pod

kubectl get pod -A

查看守护进程

查看某个命名空间中的特定pod,命名空间指定不能省略,不然会找不到

kubectl get pod kube-flannel-ds-554jc -n kube-flannel

查看指定pod的详细信息

kubectl describe pod kube-flannel-ds-554jc -n kube-flannel

增删

删除pod,删除之后集群会再重启拉起

kubectl delete pod nginx-64777cd554-zgfqj -n dev

通过文件创建|删除 pod

kubectl create|delete -f dev-pod.yaml

命令行方式创建pod,指定副本数,指定命名空间

kubectl run nginx? --image=nginx:1.17.1 --port=80? --replicas=3 -n dev

操作

进入pod

kubectl exec -it kube-apiserver-master01? -n kube-system bash

在pod外面执行任务

kubectl exec -it <podname> -c <container name> -n <namespace> -- touch /usr/local/test

部署deployment

创建deployment

kubectl create deployment my-dep --image=busybox

删除指定deployment

kubectl delete ?deployment my-dep

查看deploy

kubectl get deployments

服务service

查看服务

查看在运行中服务

kubectl get ing -A

查看服务

kubectl get services

创建服务

kubectl create service clusterip my-cs --tcp=5678:8080

删除服务

kubectl delete ?svc my-cs

node

查看节点的描述信息

kubectl describe ?node master01

Name:               master01
Roles:              control-plane,master
Labels:             beta.kubernetes.io/arch=amd64
                    beta.kubernetes.io/os=linux
                    kubernetes.io/arch=amd64
                    kubernetes.io/hostname=master01
                    kubernetes.io/os=linux
                    node-role.kubernetes.io/control-plane=
                    node-role.kubernetes.io/master=
                    node.kubernetes.io/exclude-from-external-load-balancers=
Annotations:        flannel.alpha.coreos.com/backend-data: {"VNI":1,"VtepMAC":"a6:4d:30:a1:32:aa"}
                    flannel.alpha.coreos.com/backend-type: vxlan
                    flannel.alpha.coreos.com/kube-subnet-manager: true
                    flannel.alpha.coreos.com/public-ip: 192.168.182.127
                    kubeadm.alpha.kubernetes.io/cri-socket: /var/run/dockershim.sock
                    management.cattle.io/pod-limits: {"memory":"340Mi"}
                    management.cattle.io/pod-requests: {"cpu":"950m","memory":"290Mi","pods":"9"}
                    node.alpha.kubernetes.io/ttl: 0
                    volumes.kubernetes.io/controller-managed-attach-detach: true
CreationTimestamp:  Fri, 22 Dec 2023 23:20:54 +0800
Taints:             node-role.kubernetes.io/master:NoSchedule
Unschedulable:      false
Lease:
  HolderIdentity:  master01
  AcquireTime:     <unset>
  RenewTime:       Sat, 06 Jan 2024 01:24:49 +0800
Conditions:
  Type                 Status  LastHeartbeatTime                 LastTransitionTime                Reason                       Message
  ----                 ------  -----------------                 ------------------                ------                       -------
  NetworkUnavailable   False   Fri, 05 Jan 2024 18:35:20 +0800   Fri, 05 Jan 2024 18:35:20 +0800   FlannelIsUp                  Flannel is running on this node
  MemoryPressure       False   Sat, 06 Jan 2024 01:24:10 +0800   Fri, 05 Jan 2024 23:14:28 +0800   KubeletHasSufficientMemory   kubelet has sufficient memory available
  DiskPressure         False   Sat, 06 Jan 2024 01:24:10 +0800   Fri, 05 Jan 2024 23:14:28 +0800   KubeletHasNoDiskPressure     kubelet has no disk pressure
  PIDPressure          False   Sat, 06 Jan 2024 01:24:10 +0800   Fri, 05 Jan 2024 23:14:28 +0800   KubeletHasSufficientPID      kubelet has sufficient PID available
  Ready                True    Sat, 06 Jan 2024 01:24:10 +0800   Fri, 05 Jan 2024 23:14:28 +0800   KubeletReady                 kubelet is posting ready status
Addresses:
  InternalIP:  192.168.182.127
  Hostname:    master01
Capacity:
  cpu:                2
  ephemeral-storage:  17394Mi
  hugepages-1Gi:      0
  hugepages-2Mi:      0
  memory:             3990120Ki
  pods:               110
Allocatable:
  cpu:                2
  ephemeral-storage:  16415037823
  hugepages-1Gi:      0
  hugepages-2Mi:      0
  memory:             3887720Ki
  pods:               110
System Info:
  Machine ID:                 b5c63e049a3344f9a3bb0d4147e8237d
  System UUID:                45da4d56-daf6-0055-e7a7-b414b6f8c4ac
  Boot ID:                    960d557f-4c01-48fd-b67d-ed3ce46a2935
  Kernel Version:             5.4.265-1.el7.elrepo.x86_64
  OS Image:                   CentOS Linux 7 (Core)
  Operating System:           linux
  Architecture:               amd64
  Container Runtime Version:  docker://24.0.7
  Kubelet Version:            v1.23.1
  Kube-Proxy Version:         v1.23.1
PodCIDR:                      10.244.0.0/24
PodCIDRs:                     10.244.0.0/24
Non-terminated Pods:          (9 in total)
  Namespace                   Name                                     CPU Requests  CPU Limits  Memory Requests  Memory Limits  Age
  ---------                   ----                                     ------------  ----------  ---------------  -------------  ---
  cattle-system               cattle-cluster-agent-694c664f67-m26qw    0 (0%)        0 (0%)      0 (0%)           0 (0%)         8d
  kube-flannel                kube-flannel-ds-554jc                    100m (5%)     0 (0%)      50Mi (1%)        0 (0%)         11d
  kube-system                 coredns-6d8c4cb4d-8qkwj                  100m (5%)     0 (0%)      70Mi (1%)        170Mi (4%)     14d
  kube-system                 coredns-6d8c4cb4d-kbj92                  100m (5%)     0 (0%)      70Mi (1%)        170Mi (4%)     14d
  kube-system                 etcd-master01                            100m (5%)     0 (0%)      100Mi (2%)       0 (0%)         14d
  kube-system                 kube-apiserver-master01                  250m (12%)    0 (0%)      0 (0%)           0 (0%)         14d
  kube-system                 kube-controller-manager-master01         200m (10%)    0 (0%)      0 (0%)           0 (0%)         14d
  kube-system                 kube-proxy-r2l4s                         0 (0%)        0 (0%)      0 (0%)           0 (0%)         14d
  kube-system                 kube-scheduler-master01                  100m (5%)     0 (0%)      0 (0%)           0 (0%)         14d
Allocated resources:
  (Total limits may be over 100 percent, i.e., overcommitted.)
  Resource           Requests    Limits
  --------           --------    ------
  cpu                950m (47%)  0 (0%)
  memory             290Mi (7%)  340Mi (8%)
  ephemeral-storage  0 (0%)      0 (0%)
  hugepages-1Gi      0 (0%)      0 (0%)
  hugepages-2Mi      0 (0%)      0 (0%)
Events:              <none>

查看所有node节点

kubectl get nodes

查看节点的资源使用情况

kubectl top node?

查看后端节点

kubectl get endpoints

label

查看标签

--show-labels

kubectl get pod -A -o wide --show-labels

打标签或更新标签

kubectl label pod nginx version=2.0 -n dev [--overwrite]

筛选指定标签的pod

kubectl get pod -n dev -l version=2.0 --show-labels

通过配置文件更新label

apiVersion: v1
kind: Pod
metadata:
  name: demo
  labels:
    app: nginx
    version: "1.0"
spec:
  containers:
  - name: nginx
    image: nginx

kubectl apply -f demo.yaml

?以上是将kubectl常用操作按照主题进行的梳理,还有不少命令还没整理好,也有一些命令自己使用过程中还有问题,需要进一步探索,本文先整理这些内容,后续内容有待进一步更新。

文章来源:https://blog.csdn.net/shanshan3003/article/details/135400533
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。