【原文链接】
一、ReplicaSet控制器简介
1.1 ReplicaSet控制器简介
ReplicaSet控制器的主要作用是保证一定数量的pod能能够正常运行,它会持续监听这些pod的运行状态,一旦pod发生故障,就会重启或重建,同时它还支持对pod数量的扩缩容和版本镜像的升级
1.2 ReplicaSet控制资源清单文件
apiVersion: apps/v1 # 版本号
kind: ReplicaSet # 类型
metadata: # 元数据
name: # rs名称
namespace: # 所属命名空间
labels: # 标签
controller: rs
spec: # 性情描述
replicas: 3 # 副本数量
selector: # 选择器,通过它指定该控制器管理哪些pod
matchLabels: # label匹配规则
app: nginx-pod
matchExpressions: # Expression匹配规则
- {key: app, operator: In, values: [nginx-pod]}
template: # 模板,当副本数量不足时,会根据下面的额模板创建pod副本
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.17.1
ports:
- containerPort:80
这里面spec中的常用的几个参数含义:
- replicas:指定部分数量,其实就是当前rs创建出来的pod的数量,默认为1
- selector:选择器,它的作用是建立pod控制器和pod之间的关联关系,采用的Label Selector机制,在pod模板上 定义label,在控制器上定义选择器,就可以表明当前控制器能管理哪些pod了
- template:模板,就是当前控制器创建pod所使用的模板,里面其实就是pod的定义
二、ReplicaSet实例演示
2.1 ReplicaSet控制器创建
编辑replicaset.yaml文件,内容如下:
apiVersion: v1
kind: Namespace
metadata:
name: dev
---
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: pc-replicaset
namespace: dev
spec:
replicas: 3
selector:
matchLabels:
app: nginx-pod
template:
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.17.1
使用如下命令创建资源
[root@master pod_controller]# kubectl apply -f replicaset.yaml
namespace/dev created
replicaset.apps/pc-replicaset created
[root@master pod_controller]#
通过如下命令可以查询到创建结果
[root@master pod_controller]# kubectl get rs,pod -n dev -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
replicaset.apps/pc-replicaset 3 3 3 2m57s nginx nginx:1.17.1 app=nginx-pod
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/pc-replicaset-74djl 1/1 Running 0 2m57s 10.244.2.50 node2 <none> <none>
pod/pc-replicaset-m5tmz 1/1 Running 0 2m57s 10.244.1.23 node1 <none> <none>
pod/pc-replicaset-wlvcm 1/1 Running 0 2m57s 10.244.1.24 node1 <none> <none>
[root@master pod_controller]#
2.2 ReplicaSet实现扩缩容
2.2.1 通过修改配置文件方式实现
编辑replicaset.yaml文件,将replicas值设置为6个,如下
apiVersion: v1
kind: Namespace
metadata:
name: dev
---
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: pc-replicaset
namespace: dev
spec:
replicas: 6
selector:
matchLabels:
app: nginx-pod
template:
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.17.1
然后通过如下命令使配置生效
[root@master pod_controller]# kubectl apply -f replicaset.yaml
namespace/dev unchanged
replicaset.apps/pc-replicaset configured
[root@master pod_controller]#
通过如下命令可以查看到配置已经生效
[root@master pod_controller]# kubectl get rs,pod -n dev
NAME DESIRED CURRENT READY AGE
replicaset.apps/pc-replicaset 6 6 6 8m43s
NAME READY STATUS RESTARTS AGE
pod/pc-replicaset-2bcwc 1/1 Running 0 39s
pod/pc-replicaset-74djl 1/1 Running 0 8m43s
pod/pc-replicaset-7744x 1/1 Running 0 39s
pod/pc-replicaset-7rqz2 1/1 Running 0 39s
pod/pc-replicaset-m5tmz 1/1 Running 0 8m43s
pod/pc-replicaset-wlvcm 1/1 Running 0 8m43s
[root@master pod_controller]#
2.2.2 通过命令的方式扩缩容
如下,通过如下命令将pod数量缩小到2个
[root@master pod_controller]# kubectl scale rs pc-replicaset --replicas=2 -n dev
replicaset.apps/pc-replicaset scaled
[root@master pod_controller]#
然后使用如下命令查询,可以发现缩容已经完成
[root@master pod_controller]# kubectl get rs,pod -n dev
NAME DESIRED CURRENT READY AGE
replicaset.apps/pc-replicaset 2 2 2 11m
NAME READY STATUS RESTARTS AGE
pod/pc-replicaset-m5tmz 1/1 Running 0 11m
pod/pc-replicaset-wlvcm 1/1 Running 0 11m
[root@master pod_controller]#
2.3 ReplicaSet实现镜像版本的升降级
2.3.1 通过修改配置文件的方式升级镜像版本
编辑yaml文件,修改镜像版本为1.17.3,如下:
apiVersion: v1
kind: Namespace
metadata:
name: dev
---
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: pc-replicaset
namespace: dev
spec:
replicas: 2
selector:
matchLabels:
app: nginx-pod
template:
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.17.3
然后使用如下命令使配置生效
[root@master pod_controller]# kubectl apply -f replicaset.yaml
namespace/dev unchanged
replicaset.apps/pc-replicaset configured
[root@master pod_controller]#
通过如下命令,可以发现镜像版本已经升级
[root@master pod_controller]# kubectl get rs,pod -n dev -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
replicaset.apps/pc-replicaset 2 2 2 15m nginx nginx:1.17.3 app=nginx-pod
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/pc-replicaset-m5tmz 1/1 Running 0 15m 10.244.1.23 node1 <none> <none>
pod/pc-replicaset-wlvcm 1/1 Running 0 15m 10.244.1.24 node1 <none> <none>
[root@master pod_controller]#
2.3.2 通过命令的方式对镜像版本升降级
使用如下命令,将镜像版本再降到1.17.1
[root@master pod_controller]# kubectl set image rs pc-replicaset nginx=nginx:1.17.1 -n dev
replicaset.apps/pc-replicaset image updated
[root@master pod_controller]#
通过如下命令查询,可以发现已经生效
[root@master pod_controller]# kubectl get rs,pod -n dev -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
replicaset.apps/pc-replicaset 2 2 2 17m nginx nginx:1.17.1 app=nginx-pod
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/pc-replicaset-m5tmz 1/1 Running 0 17m 10.244.1.23 node1 <none> <none>
pod/pc-replicaset-wlvcm 1/1 Running 0 17m 10.244.1.24 node1 <none> <none>
[root@master pod_controller]#
2.4 删除ReplicaSet
2.4.1 命令行方式删除ReplicaSet以及Pod
使用如下命令删除,在删除ReplicaSet前,会先把pod数量调整为0,等所有pod都删除后,再删除ReplicaSet
kubectl delete rs pc-replicaset -n dev
2.4.2 仅删除ReplicaSet,保留Pod
使用如下命令即可
kubectl delete rs pc-replicaset -n dev --cascade=false
2.4.3 使用yaml文件删除
使用如下命令(推荐)
kubectl delete -f replicaset.yaml