k8s常用命令
# k8s常用命令
# 1.只在 master 节点执行
- 执行如下命令,等待 3-10 分钟,直到所有的容器组处于 Running 状态
[root@master mapper]# watch kubectl get pod -n kube-system -o wide
Every 2.0s: kubectl get pod -n kube-system -o wide Fri Oct 16 18:29:36 2020
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
calico-kube-controllers-5d7686f694-6h6zp 1/1 Running 1 2d1h 100.89.161.134 master <none> <none>
calico-node-jnwpr 1/1 Running 1 2d 192.168.0.191 node1 <none> <none>
calico-node-qqxxj 1/1 Running 1 2d1h 192.168.0.190 master <none> <none>
calico-node-v67rj 1/1 Running 1 2d 192.168.0.192 node2 <none> <none>
coredns-f9fd979d6-h79pn 1/1 Running 1 2d1h 100.89.161.135 master <none> <none>
coredns-f9fd979d6-hfkkr 1/1 Running 1 2d1h 100.89.161.133 master <none> <none>
etcd-master 1/1 Running 1 2d1h 192.168.0.190 master <none> <none>
kube-apiserver-master 1/1 Running 1 2d1h 192.168.0.190 master <none> <none>
kube-controller-manager-master 1/1 Running 1 2d1h 192.168.0.190 master <none> <none>
kube-proxy-cgw59 1/1 Running 1 2d1h 192.168.0.190 master <none> <none>
kube-proxy-k947z 1/1 Running 1 2d 192.168.0.192 node2 <none> <none>
kube-proxy-ksxm5 1/1 Running 1 2d 192.168.0.191 node1 <none> <none>
kube-scheduler-master 1/1 Running 1 2d1h 192.168.0.190 master <none> <none>
kuboard-7b9c48495-xc8zw 1/1 Running 1 2d 100.108.11.196 node2 <none> <none>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 2.查看 master 节点初始化结果
[root@master mapper]# kubectl get nodes -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
master Ready master 2d1h v1.19.0 192.168.0.190 <none> CentOS Linux 7 (Core) 3.10.0-1127.19.1.el7.x86_64 docker://19.3.0
node1 Ready <none> 2d v1.19.0 192.168.0.191 <none> CentOS Linux 7 (Core) 3.10.0-1127.19.1.el7.x86_64 docker://19.3.0
node2 Ready <none> 2d v1.19.0 192.168.0.192 <none> CentOS Linux 7 (Core) 3.10.0-1127.19.1.el7.x86_64 docker://19.3.0
1
2
3
4
5
2
3
4
5
# 3.查看所有名称空间总的容器
[root@master mapper]# kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system calico-kube-controllers-5d7686f694-6h6zp 1/1 Running 1 2d1h
kube-system calico-node-jnwpr 1/1 Running 1 2d
kube-system calico-node-qqxxj 1/1 Running 1 2d1h
kube-system calico-node-v67rj 1/1 Running 1 2d
kube-system coredns-f9fd979d6-h79pn 1/1 Running 1 2d1h
kube-system coredns-f9fd979d6-hfkkr 1/1 Running 1 2d1h
kube-system etcd-master 1/1 Running 1 2d1h
kube-system kube-apiserver-master 1/1 Running 1 2d1h
kube-system kube-controller-manager-master 1/1 Running 1 2d1h
kube-system kube-proxy-cgw59 1/1 Running 1 2d1h
kube-system kube-proxy-k947z 1/1 Running 1 2d
kube-system kube-proxy-ksxm5 1/1 Running 1 2d
kube-system kube-scheduler-master 1/1 Running 1 2d1h
kube-system kuboard-7b9c48495-xc8zw 1/1 Running 1 2d
ocp cloud-eureka-0 1/1 Running 1 2d
ocp cloud-eureka-1 1/1 Running 1 2d
ocp cloud-eureka-2 1/1 Running 1 2d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 4.查看所有名称空间总的容器
kubectl get pods --all-namespaces -o jsonpath="{..image}" |\
tr -s '[[:space:]]' '\n' |\
sort |\
uniq -c
使用 tr 将空格替换为新的行
使用 sort 对结果排序
使用 uniq 对镜像使用计数
1
2
3
4
5
6
7
2
3
4
5
6
7
# 5.查看指定节点状态以及节点详细信息
[root@master mapper]# kubectl describe node node1
Name: node1
Roles: <none>
Labels: beta.kubernetes.io/arch=amd64
beta.kubernetes.io/os=linux
kubernetes.io/arch=amd64
kubernetes.io/hostname=node1
kubernetes.io/os=linux
Annotations: kubeadm.alpha.kubernetes.io/cri-socket: /var/run/dockershim.sock
node.alpha.kubernetes.io/ttl: 0
projectcalico.org/IPv4Address: 192.168.0.191/24
projectcalico.org/IPv4IPIPTunnelAddr: 100.66.209.192
volumes.kubernetes.io/controller-managed-attach-detach: true
CreationTimestamp: Wed, 14 Oct 2020 17:29:52 +0800
Taints: <none>
Unschedulable: false
Lease:
HolderIdentity: node1
AcquireTime: <unset>
RenewTime: Fri, 16 Oct 2020 18:30:43 +0800
Conditions:
Type Status LastHeartbeatTime LastTransitionTime Reason Message
---- ------ ----------------- ------------------ ------ -------
NetworkUnavailable False Fri, 16 Oct 2020 17:47:28 +0800 Fri, 16 Oct 2020 17:47:28 +0800 CalicoIsUp Calico is running on this node
MemoryPressure False Fri, 16 Oct 2020 18:26:59 +0800 Wed, 14 Oct 2020 17:29:52 +0800 KubeletHasSufficientMemory kubelet has sufficient memory available
DiskPressure False Fri, 16 Oct 2020 18:26:59 +0800 Wed, 14 Oct 2020 17:29:52 +0800 KubeletHasNoDiskPressure kubelet has no disk pressure
PIDPressure False Fri, 16 Oct 2020 18:26:59 +0800 Wed, 14 Oct 2020 17:29:52 +0800 KubeletHasSufficientPID kubelet has sufficient PID available
Ready True Fri, 16 Oct 2020 18:26:59 +0800 Wed, 14 Oct 2020 17:30:04 +0800 KubeletReady kubelet is posting ready status
Addresses:
InternalIP: 192.168.0.191
Hostname: node1
Capacity:
cpu: 2
ephemeral-storage: 38815216Ki
hugepages-1Gi: 0
hugepages-2Mi: 0
memory: 3871096Ki
pods: 110
Allocatable:
cpu: 2
ephemeral-storage: 35772103007
hugepages-1Gi: 0
hugepages-2Mi: 0
memory: 3768696Ki
pods: 110
System Info:
Machine ID: f3390ae2e4ff47198055e00581443f92
System UUID: 564D5B6D-9D02-9A99-266B-69248EC47C65
Boot ID: c41e1ee9-42d7-4d22-9c30-d4b8d2383bb7
Kernel Version: 3.10.0-1127.19.1.el7.x86_64
OS Image: CentOS Linux 7 (Core)
Operating System: linux
Architecture: amd64
Container Runtime Version: docker://19.3.0
Kubelet Version: v1.19.0
Kube-Proxy Version: v1.19.0
PodCIDR: 100.64.2.0/24
PodCIDRs: 100.64.2.0/24
Non-terminated Pods: (3 in total)
Namespace Name CPU Requests CPU Limits Memory Requests Memory Limits AGE
--------- ---- ------------ ---------- --------------- ------------- ---
kube-system calico-node-jnwpr 250m (12%) 0 (0%) 0 (0%) 0 (0%) 2d1h
kube-system kube-proxy-ksxm5 0 (0%) 0 (0%) 0 (0%) 0 (0%) 2d1h
ocp cloud-eureka-0 0 (0%) 0 (0%) 0 (0%) 0 (0%) 2d
Allocated resources:
(Total limits may be over 100 percent, i.e., overcommitted.)
Resource Requests Limits
-------- -------- ------
cpu 250m (12%) 0 (0%)
memory 0 (0%) 0 (0%)
ephemeral-storage 0 (0%) 0 (0%)
hugepages-1Gi 0 (0%) 0 (0%)
hugepages-2Mi 0 (0%) 0 (0%)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Starting 44m kubelet, node1 Starting kubelet.
Normal NodeAllocatableEnforced 44m kubelet, node1 Updated Node Allocatable limit across pods
Normal NodeHasSufficientPID 44m (x7 over 44m) kubelet, node1 Node node1 status is now: NodeHasSufficientPID
Normal NodeHasSufficientMemory 44m (x8 over 44m) kubelet, node1 Node node1 status is now: NodeHasSufficientMemory
Normal NodeHasNoDiskPressure 44m (x8 over 44m) kubelet, node1 Node node1 status is now: NodeHasNoDiskPressure
Normal Starting 43m kube-proxy, node1 Starting kube-proxy.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 6.查看命名空间
[root@master ~]# kubectl get namespace
# kubectl get ns 也可以
NAME STATUS AGE
default Active 5d6h
kube-node-lease Active 5d6h
kube-public Active 5d6h
kube-system Active 5d6h
ocp Active 5d5h
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 7.创建命名空间
[root@master ~]# kubectl create namespace summer
namespace/summer created
[root@master ~]# kubectl get namespace
NAME STATUS AGE
default Active 5d6h
kube-node-lease Active 5d6h
kube-public Active 5d6h
kube-system Active 5d6h
ocp Active 5d5h
summer Active 2s
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 8.删除命名空间
[root@master ~]# kubectl delete namespace summer
namespace "summer" deleted
1
2
2