上一篇是按照操作把全局性的命令给整理出来,但是在实际的使用中,经常需要对某一个主题进行操作,因此这一篇按照对应的主题进行一系列操作。
查看集群基本信息
kubectl cluster-info
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,如果不指定则是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
kubectl create deployment my-dep --image=busybox
kubectl delete ?deployment my-dep
kubectl get deployments
查看在运行中服务
kubectl get ing -A
查看服务
kubectl get services
kubectl create service clusterip my-cs --tcp=5678:8080
kubectl delete ?svc my-cs
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>
kubectl get nodes
查看节点的资源使用情况
kubectl top node?
kubectl get endpoints
--show-labels
kubectl get pod -A -o wide --show-labels
kubectl label pod nginx version=2.0 -n dev [--overwrite]
kubectl get pod -n dev -l version=2.0 --show-labels
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常用操作按照主题进行的梳理,还有不少命令还没整理好,也有一些命令自己使用过程中还有问题,需要进一步探索,本文先整理这些内容,后续内容有待进一步更新。