ReplicaSet属于apps群组v1版本,早期大多数控制器都位于extensions/v1beta1,v1beta2,…

ReplicaSet工作流程

在Controller Manager中有一段代码称为ReplicaSet的控制器代码(Controller loop)。而控制器代码需要真正工作起来需要创建出对应的ReplicaSet Object,ReplicaSet Object是用于向API Server请求管理Pod对象(标签选择器选定的对象),如果存在此标签选择器选定的对象,且足量则无需创建,若没有ReplicaSet会借助于控制循环中的代码向APIserver创建出新的Pod,Pod来自于模板定义。然后由Scheduler调度并绑定至某节点,而后pod由kubelet来负责运行。

ReplicaSet资源定义规范

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: …
namespace: …
spec:
minReadySeconds <integer> # Pod就绪后多少秒内,Pod任一容器无crash方可视为“就绪”
replicas <integer> # 期望的Pod副本数,默认为1
selector: # 标签选择器,必须匹配template字段中Pod模板中的标签;
matchExpressions <[]Object> # 标签选择器表达式列表,多个列表项之间为“与”关系
matchLabels <map[string]string> # map格式的标签选择器
template: # Pod模板对象
metadata: # Pod对象元数据
labels: # 由模板创建出的Pod对象所拥有的标签,必须要能够匹配前面定义的标签选择器
spec: # Pod规范,格式同自主式Pod
……

ReplicaSet示例

1.编写资源清单

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
root@k8s-master01:~/yaml/chapter08# vim replicaset-demo.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: replicaset-demo
spec:
minReadySeconds: 3
replicas: 2
selector:
matchLabels:
app: demoapp
release: stable
version: v1.0
template:
metadata:
labels:
app: demoapp
release: stable
version: v1.0
spec:
containers:
- name: demoapp
image: ikubernetes/demoapp:v1.0
ports:
- name: http
containerPort: 80
livenessProbe:
httpGet:
path: '/livez'
port: 80
initialDelaySeconds: 5
readinessProbe:
httpGet:
path: '/readyz'
port: 80
initialDelaySeconds: 15

2.应用配置清单

1
2
3
4
5
6
root@k8s-master01:~/yaml/chapter08# kubectl apply -f replicaset-demo.yaml
replicaset.apps/replicaset-demo created

root@k8s-master01:~/yaml/chapter08# kubectl get rs
NAME DESIRED CURRENT READY AGE
replicaset-demo 2 2 2 15m

3.查看rs的详细信息

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
root@k8s-master01:~/yaml/chapter08# kubectl describe rs replicaset-demo
Name: replicaset-demo
Namespace: default
Selector: app=demoapp,release=stable,version=v1.0
Labels: <none>
Annotations: <none>
Replicas: 2 current / 2 desired
Pods Status: 2 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
Labels: app=demoapp
release=stable
version=v1.0
Containers:
demoapp:
Image: ikubernetes/demoapp:v1.0
Port: 80/TCP
Host Port: 0/TCP
Liveness: http-get http://:80/livez delay=5s timeout=1s period=10s #success=1 #failure=3
Readiness: http-get http://:80/readyz delay=15s timeout=1s period=10s #success=1 #failure=3
Environment: <none>
Mounts: <none>
Volumes: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 89s replicaset-controller Created pod: replicaset-demo-62bxh
Normal SuccessfulCreate 89s replicaset-controller Created pod: replicaset-demo-kwl4r

# rs创建了2个pod

4.查看pod

1
2
3
4
root@k8s-master01:~/yaml/chapter08# kubectl get pods
NAME READY STATUS RESTARTS AGE
replicaset-demo-62bxh 1/1 Running 0 2m34s
replicaset-demo-kwl4r 1/1 Running 0 2m34s

5.删除pod

1
2
3
4
5
6
7
8
root@k8s-master01:~/yaml/chapter08# kubectl delete pods replicaset-demo-62bxh
pod "replicaset-demo-62bxh" deleted

# 一旦pod被删除,控制器会重新生成一个pod
root@k8s-master01:~/yaml/chapter08# kubectl get pods
NAME READY STATUS RESTARTS AGE
replicaset-demo-k2jnl 0/1 Running 0 51s
replicaset-demo-kwl4r 1/1 Running 0 18m

6.导出pod的yaml信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
root@k8s-master01:~# kubectl get pods replicaset-demo-k2jnl -o yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: "2021-07-21T06:46:44Z"
generateName: replicaset-demo-
labels:
app: demoapp
release: stable
version: v1.0
name: replicaset-demo-k2jnl
namespace: default
ownerReferences: # 此处可以看到Pod归谁所有。
- apiVersion: apps/v1
blockOwnerDeletion: true
controller: true
kind: ReplicaSet
name: replicaset-demo
uid: d16ab83c-c9a6-486f-b202-72e93f982750
resourceVersion: "1196532"
uid: b7478159-6cbd-4139-8954-7758e46679c3

ReplicaSet应用的更新和回滚

ReplicaSet更新机制

ReplicaSet支持两种更新

  • 删除式更新:在修改时只修改了APIServer中的pod模板信息,而ReplicaSet标签选择器所选定的pod数量足够时,其不会立即更新,需要删除当前的Pod后才会使用新的模板创建出pod.
  • 单批次删除所有Pod,一次完成所有更新;服务会中断一段时间;
  • 分批次删除,待一批次就绪之后,才删除下一批;滚动更新;
删除式更新示例

1.查看ReplicaSet信息

1
2
3
root@k8s-master01:~# kubectl get rs -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
replicaset-demo 2 2 2 41m demoapp ikubernetes/demoapp:v1.0 app=demoapp,release=stable,version=v1.0

2.现在将demoapp:v1.0升级到demoapp:v1.1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 命令式更新使用kubectl Set来进行设定
root@k8s-master01:~# kubectl set image replicasets/replicaset-demo demoapp=ikubernetes/demoapp:v1.1
replicaset.apps/replicaset-demo image updated

# 查看pod
root@k8s-master01:~# kubectl get pods -l 'app=demoapp' -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
replicaset-demo-k2jnl 1/1 Running 3 29m 10.244.2.24 k8s-node02 <none> <none>
replicaset-demo-kwl4r 1/1 Running 0 46m 10.244.3.27 k8s-node03 <none> <none>

# 查看pod是否被跟新
root@k8s-master01:~# curl 10.244.3.27
iKubernetes demoapp v1.0 !! ClientIP: 10.244.0.0, ServerName: replicaset-demo-kwl4r, ServerIP: 10.244.3.27!
# 目前依旧未被跟新,主要是因为使用命令方式更新为删除式更新,需要将pod删除后才会进行更新

3.删除pod,并再次查看

1
2
3
4
5
6
7
8
root@k8s-master01:~# kubectl delete pods replicaset-demo-kwl4r
pod "replicaset-demo-kwl4r" deleted

# 再次查看
root@k8s-master01:~# kubectl get pods -l 'app=demoapp' -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
replicaset-demo-5fmvs 0/1 Running 0 44s 10.244.3.28 k8s-node03 <none> <none> # 此为新创建的pod
replicaset-demo-k2jnl 1/1 Running 3 40m 10.244.2.24 k8s-node02 <none> <none>

4.访问新创建出的Pod

1
2
3
root@k8s-master01:~# curl 10.244.3.28
iKubernetes demoapp v1.1 !! ClientIP: 10.244.0.0, ServerName: replicaset-demo-5fmvs, ServerIP: 10.244.3.28!
# pod已经被更新