kubectl命令学习
常用命令参考:https://juejin.cn/post/7031201391553019911
查看集群全部信息
# 查看全部信息
kubectl get all
#---- 示例 ----
[liqian120@k8s-autoqa0620ep-test ~]$ kubectl get all
NAME READY STATUS RESTARTS AGE
pod/ng-demo-ds1-2h2zl 1/1 Terminating 0 9d
pod/ng-demo-ds1-bg62j 0/1 Terminating 0 8d
pod/ng-demo-ds1-d9rwh 0/1 Terminating 0 9d
pod/ng-demo-ds1-hl2gl 0/1 Terminating 0 9d
pod/ng-demo-ds1-njzr5 0/1 Terminating 0 9d
pod/ng-demo-ds1-ptfmq 0/1 Terminating 0 9d
pod/ng-demo-ds1-s2tg9 0/1 Terminating 0 9d
pod/ng-demo-ds1-tgfm7 1/1 Running 0 8d
pod/ng-demo-ds1-v9r88 1/1 Running 0 8d
pod/ng-demo-ds1-xspmw 0/1 Terminating 0 9d
pod/ng-demo-ds1-znnnm 1/1 Running 0 8d
pod/test-2 1/1 Running 0 35d
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 172.17.0.1 <none> 443/TCP 35d
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
daemonset.apps/ng-demo-ds1 4 4 3 3 3 <none> 8d
查看集群服务
# 查看服务
kubectl get svc
查看集群状态
# 查看集群状态
kubectl cluster-info
#----示例-------
[liqian120@k8s-autoqa0620ep-test ~]$ kubectl cluster-info
Kubernetes control plane is running at https://autoqa0620ep-k8s.dev.vip.sankuai.com
查看节点具体信息
kubectl get node --all-namespaces -o wide
# 不想展示详细信息时
kubectl get node
查看节点创建时间
# 通过kubectl命令来获取节点的详细信息,其中包含节点的创建时间;过滤并查看节点创建时间,使用jq命令来过滤JSON输出中的创建时间信息
[liqian120@k8s-autoqa0620ep-test ~]$ kubectl get nodes -o json | jq '.items[] | {name: .metadata.name, creationTimestamp: .metadata.creationTimestamp}'
{
"name": "hh-hulk-k8s-ep-dev44.mt",
"creationTimestamp": "2024-07-05T04:27:59Z"
}
{
"name": "yg-hulk-k8s-ep-dev23.mt",
"creationTimestamp": "2024-06-21T03:49:34Z"
}
{
"name": "yg-hulk-k8s-ep-dev28.mt",
"creationTimestamp": "2024-07-05T04:43:18Z"
}
{
"name": "yg-hulk-k8s-ep-dev29.mt",
"creationTimestamp": "2024-07-05T04:41:43Z"
}
查看节点上pod的数量
kubectl get pods --all-namespaces -o wide | grep <node-name> | wc -l
# eg
kubectl get pods --all-namespaces -o wide | grep hh-hulk-k8s-ep-dev44.mt | wc -l
查看节点具体信息,例如cpu\内存等
kubectl describe node <node-name>
#----例子---
kubectl describe node yg-hulk-k8s-ep-dev28.mt
[liqian120@k8s-autoqa0620ep-test ~]$ kubectl describe node yg-hulk-k8s-ep-dev28.mt
Name: yg-hulk-k8s-ep-dev28.mt
Roles: <none>
Labels: beta.kubernetes.io/arch=amd64
beta.kubernetes.io/os=linux
cpu.busy.tp99=10
cpu_model=Intel-R-Xeon-R-Gold5118CPU-2.30GH
cpu_model_std=Intel_R_Xeon_R_Gold_5118_CPU_2.30GHz
disktype=hdd
ebs=support
ebs-version=v5.3.34_3827d0a
efs=unsupport
gray_area=ebs_gray
hostStatus=online
idc=yg
kernel=Linux4.18.0-147.mt20221104.486.el8_1.x86_6
kernel_std=Linux_4.18.0-147.mt20221104.486.el8_1.x86_64
kubernetes.io/hostname=yg-hulk-k8s-ep-dev28.mt
mem_size=384
mke.hulk.sankuai.com/appkey=com.sankuai.hulknode.kubelet.autoqa0620ep
netType=50g
node.alpha.sankuai.com/kubelet-version=v1.11.3-mt20230912-dirty
non-limit-frequency=support
oversell=1.0
page-cache=support
shield.hulk.sankuai.com/building=YG-M2
shield.hulk.sankuai.com/compute-level=429900215
shield.hulk.sankuai.com/net_pod=YG-2
shield.hulk.sankuai.com/rack_info=YG-M2F301-B-4-19
shield.hulk.sankuai.com/switch=LF-YG-DC4F301-B03-RG6510-A-EP-INT
smart-nic=support
system.disk.io.rank=1
vendors=intel
Annotations: node.alpha.kubernetes.io/node-runtime-metrics-cpu: -1
node.alpha.kubernetes.io/ttl: 0
numa.kubernetes.io/numacpu:
0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46|1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47
numa.kubernetes.io/numamem: 201115742208|201791021056
CreationTimestamp: Fri, 05 Jul 2024 04:43:18 +0000
Taints: <none>
Unschedulable: false
Lease: Failed to get lease: the server could not find the requested resource (get leases.coordination.k8s.io yg-hulk-k8s-ep-dev28.mt)
Conditions:
Type Status LastHeartbeatTime LastTransitionTime Reason Message
---- ------ ----------------- ------------------ ------ -------
OutOfDisk False Fri, 26 Jul 2024 06:24:19 +0000 Fri, 05 Jul 2024 04:43:18 +0000 KubeletHasSufficientDisk kubelet has sufficient disk space available
MemoryPressure False Fri, 26 Jul 2024 06:24:19 +0000 Fri, 05 Jul 2024 04:43:18 +0000 KubeletHasSufficientMemory kubelet has sufficient memory available
DiskPressure False Fri, 26 Jul 2024 06:24:19 +0000 Fri, 05 Jul 2024 04:43:18 +0000 KubeletHasNoDiskPressure kubelet has no disk pressure
PIDPressure False Fri, 26 Jul 2024 06:24:19 +0000 Fri, 05 Jul 2024 04:43:18 +0000 KubeletHasSufficientPID kubelet has sufficient PID available
Ready True Fri, 26 Jul 2024 06:24:19 +0000 Mon, 15 Jul 2024 11:53:51 +0000 KubeletReady kubelet is posting ready status
Addresses:
InternalIP: 10.15.3.47
Hostname: yg-hulk-k8s-ep-dev28.mt
Capacity:
cpu: 48
ephemeral-storage: 104841732Ki
hugepages-1Gi: 0
hugepages-2Mi: 0
memory: 393463636Ki
pod.alpha.kubernetes.io/opaque-int-resource-HULK_DISK: 12256
pod.alpha.kubernetes.io/opaque-int-resource-IMAGE_DISK: 1812
pods: 70
Allocatable:
cpu: 48
ephemeral-storage: 96622140052
hugepages-1Gi: 0
hugepages-2Mi: 0
memory: 387069780Ki
pod.alpha.kubernetes.io/opaque-int-resource-HULK_DISK: 12256
pod.alpha.kubernetes.io/opaque-int-resource-IMAGE_DISK: 1812
pods: 70
System Info:
Machine ID: 1324ca4d8099476d99a8633e7cb952b7
System UUID: 4c4c4544-004c-4c10-8054-b5c04f315332
Boot ID: 92450b37-67ef-4c2e-a83b-4be1b8e4eb23
Kernel Version: 4.18.0-147.mt20221104.486.el8_1.x86_64
OS Image: CentOS Linux 7 (Core)
Operating System: linux
Architecture: amd64
Container Runtime Version: docker://1.13.1
Kubelet Version: v1.11.3-mt20230912-dirty
Kube-Proxy Version: v1.11.3-mt20230912-dirty
Non-terminated Pods: (2 in total)
Namespace Name CPU Requests CPU Limits Memory Requests Memory Limits Age
--------- ---- ------------ ---------- --------------- ------------- ---
default ng-demo-ds1-znnnm 0 (0%) 0 (0%) 0 (0%) 0 (0%) 8d
meituan-hulk shield-test-kata-dev-e410100761725ccb27d01fcf318ffc9f-ivz61 100m (0%) 1 (2%) 1Gi (0%) 1Gi (0%) 3d2h
Allocated resources:
(Total limits may be over 100 percent, i.e., overcommitted.)
Resource Requests Limits
-------- -------- ------
cpu 100m (0%) 1 (2%)
memory 1Gi (0%) 1Gi (0%)
ephemeral-storage 0 (0%) 0 (0%)
hugepages-1Gi 0 (0%) 0 (0%)
hugepages-2Mi 0 (0%) 0 (0%)
pod.alpha.kubernetes.io/opaque-int-resource-HULK_DISK 0 0
pod.alpha.kubernetes.io/opaque-int-resource-IMAGE_DISK 20 0
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Warning MissingClusterDNS 97s (x11584 over 10d) kubelet kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. Falling back to "Default" policy.
[liqian120@k8s-autoqa0620ep-test ~]$
查看Kubernetes pod相关信息
# 查看Kubernetes pod信息
kubectl get pod
kubectl get pod -o wide #[可以得到pod详细信息]
# 查看pod日志信息
kubectl logs pod-name
# 伸缩扩展副本
kubectl scale deployment 集群名称 --replicas=5(数量)
#---- 示例 ----
[liqian120@k8s-autoqal03ep-test ~]$ kubectl get pod --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
density-test-7837 cadvisor 1/1 Running 0 2d3h
kube-monitor mke-kube-state-metrics-597b6fbb4b-rxgt2 0/1 CrashLoopBackOff 2261 5d4h
kube-monitor mke-node-exporter-vhgp7 1/1 Running 7 68d
[liqian120@k8s-autoqal03ep-test ~]$ kubectl get pod --all-namespaces -o wide
NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
density-test-7837 cadvisor 1/1 Running 0 2d3h 10.233.111.58 yg-hulk-k8s-ep-dev11.mt <none> <none>
kube-monitor mke-kube-state-metrics-597b6fbb4b-rxgt2 0/1 CrashLoopBackOff 2261 5d4h 10.233.236.61 yg-hulk-k8s-ep-dev11.mt <none> <none>
kube-monitor mke-node-exporter-vhgp7 1/1 Running 7 68d 10.233.111.58 yg-hulk-k8s-ep-dev11.mt <none> <none>
[liqian120@k8s-autoqal03ep-test ~]$