管理名称空间资源

查看名称空间

1
2
3
4
5
6
7
[root@ceshi-130 conf]# kubectl get ns
[root@ceshi-130 conf]# kubectl get namespace
NAME STATUS AGE
default Active 7d17h
kube-node-lease Active 7d17h
kube-public Active 7d17h
kube-system Active 7d17h

查询default空间所有资源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@ceshi-130 conf]# kubectl get all -n default
[root@ceshi-130 conf]# kubectl get all #默认default
pod资源
NAME READY STATUS RESTARTS AGE
pod/nginx-ds-c7bnr 1/1 Running 1 18h
pod/nginx-ds-lkznc 1/1 Running 1 18h

service资源
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 192.168.0.1 <none> 443/TCP 7d17h

daemonset控制器资源
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
daemonset.apps/nginx-ds 2 2 2 2 2 <none> 18h

创建名称空间

1
2
3
4
5
[root@ceshi-130 conf]# kubectl create ns ceshi
namespace/ceshi created
[root@ceshi-130 conf]# kubectl get ns
NAME STATUS AGE
ceshi Active 44s

删除名称空间

1
2
[root@ceshi-130 conf]# kubectl delete ns ceshi
namespace "ceshi" deleted

管理Deployment资源

创建deployment

1
2
3
4
5
6
7
[root@ceshi-130 conf]# kubectl create deployment nginx-test --image=harbor.od.com/public/nginx:v1.7.9 -n kube-public
[root@ceshi-130 conf]# kubectl get deployment -n kube-public
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-test 1/1 1 1 17s
[root@ceshi-130 conf]# kubectl get pods -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-test-88bc69dd6-cgh8k 1/1 Running 0 41s

查看

1
2
3
4
5
6
[root@ceshi-130 conf]# kubectl get deployment -n kube-public
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-test 1/1 1 1 9m13s
[root@ceshi-130 conf]# kubectl get deployment -n kube-public -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
nginx-test 1/1 1 1 10m nginx harbor.od.com/public/nginx:v1.7.9 app=nginx-test

详细查看

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
[root@ceshi-130 conf]# kubectl describe deployment nginx-test -n kube-public
Name: nginx-test #名称
Namespace: kube-public #名称空间
CreationTimestamp: Tue, 03 Aug 2021 10:34:12 +0800 #创建时间
Labels: app=nginx-test #标签
Annotations: deployment.kubernetes.io/revision: 1 #注解
Selector: app=nginx-test #标签选择器
Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable
状态副本集 预期一个 最新状态 总共一个 1个存在 0个不存在
StrategyType: RollingUpdate #更新策略,默认滚动发布(蓝绿发布,灰度发布,滚动发布,金丝雀发布)
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=nginx-test
Containers:
nginx:
Image: harbor.od.com/public/nginx:v1.7.9
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-test-88bc69dd6 (1/1 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 12m deployment-controller Scaled up replica set nginx-test-88bc69dd6 to 1

pod资源

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
进入pod资源
[root@ceshi-130 conf]# kubectl exec -it nginx-test-88bc69dd6 bash -n kube-public

配置端口
[root@ceshi-130 conf]# kubectl expose deployment nginx-test --port=80 -n kube-public
service/nginx-test exposed
[root@ceshi-130 conf]# kubectl get all -n kube-public
NAME READY STATUS RESTARTS AGE
pod/nginx-test-88bc69dd6-cgh8k 1/1 Running 2 5h16m

对外提供的唯一出口
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/nginx-test ClusterIP 192.168.37.172 <none> 80/TCP 25s


NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx-test 1/1 1 1 5h16m

NAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-test-88bc69dd6 1 1 1 5h16m

查看LVS规则,上面的CLUSTER-IP代理172.7.200.1节点
[root@ceshi-131 certs]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.0.1:443 nq
-> 10.1.74.22:6443 Masq 1 0 0
-> 10.1.74.23:6443 Masq 1 0 0
TCP 192.168.37.172:80 nq
-> 172.7.200.1:80 Masq 1 0 1

扩容节点
[root@ceshi-130 conf]# kubectl scale deployment nginx-test --replicas=2 -n kube-public
deployment.extensions/nginx-test scaled

[root@ceshi-130 conf]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.0.1:443 nq
-> 10.1.74.22:6443 Masq 1 0 0
-> 10.1.74.23:6443 Masq 1 0 0
TCP 192.168.37.172:80 nq
-> 172.7.200.1:80 Masq 1 0 0
-> 172.7.200.3:80 Masq 1 0 0

删除deployment

1
2
3
4
5
6
7
8
9
10
11
12
13
查看
[root@ceshi-131 certs]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-ljx 1/1 1 1 42m
删除
[root@ceshi-131 certs]# kubectl delete deploy nginx-ljx
deployment.extensions "nginx-ljx" deleted

[root@ceshi-131 certs]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-ds-f82d2 1/1 Running 0 2m43s
nginx-ds-lkznc 1/1 Running 3 24h
nginx-ljx-669cc7d666-hj9qh 0/1 Terminating 0 73s

通过和本机8080 apiserver进行通信,将用户再命令行输入的命令,组织并转换为apiserver识别的信息,实现管理各种资源的一种有限途径


以yaml格式显示pod

1
[root@ceshi-130 conf]# kubectl get pods nginx-test-88bc69dd6-4vk58 -o yaml -n kube-public

以yaml格式显示service

1
[root@ceshi-130 conf]# kubectl get svc nginx-test -o yaml -n kube-public

创建资源配置清单

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
[root@ceshi-130 ~]# cat nginx-ljx.yaml 
apiVersion: v1
kind: Service
metadata:
name: nginx-ljx
namespace: default
labels:
app: nginx-ljx
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
type: ClusterIP
[root@ceshi-130 ~]# kubectl create -f nginx-ljx.yaml
service/nginx-ljx created
查看service
[root@ceshi-130 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 192.168.0.1 <none> 443/TCP 8d
nginx-ljx ClusterIP 192.168.134.19 <none> 80/TCP 35s

离线修改并应用
[root@ceshi-130 ~]# kubectl apply -f nginx-ljx.yaml

在线修改并应用
[root@ceshi-130 ~]# kubectl edit svc nginx-ljx
service/nginx-ljx edited
[root@ceshi-130 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 192.168.0.1 <none> 443/TCP 8d
nginx-ljx ClusterIP 192.168.129.159 <none> 888/TCP 3m5s

陈述式删除(命令行)
[root@ceshi-130 ~]# kubectl delete svc nginx-ljx
service "nginx-ljx" deleted
声明式删除(yaml/json)
[root@ceshi-130 ~]# kubectl delete -f nginx-ljx.yaml