Pod的冗余保护

k8s使用ReplicationController和ReplicaSet对pod进行冗余保护控制,ReplicationController逐渐被淘汰。另外还有DaemonSets,Job,CronJob几种特殊需求的冗余控制,使用配置文件定制好了冗余Pod数量之后,k8s自动进行Pod的冗余监控以及停启操作。

创建RC和RS等的方式与创建Pod等一样,使用指令,

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@master ~]# kubectl create -f myweb_replicationset.yaml
replicaset.apps/mywebrs created
[root@master ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
my-nginx-86459cfc9f 2 2 2 59s
mywebrs 3 3 3 8s
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
my-nginx-86459cfc9f-48c2s 1/1 Running 0 68s
my-nginx-86459cfc9f-wz6ng 1/1 Running 0 68s
mywebrs-bddcv 1/1 Running 0 17s
mywebrs-jczxq 1/1 Running 0 17s
mywebrs-ksk66 1/1 Running 0 17s

其中myweb_replicationset.yaml文件内容为,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
apiVersion: apps/v1beta2
kind: ReplicaSet
metadata:
name: mywebrs
spec:
replicas: 3
selector:
matchLabels:
app: mywebv1
template:
metadata:
labels:
app: mywebv1
spec:
restartPolicy: Always
containers:
- name: myweb
image: mywebv1
imagePullPolicy: Never
ports:
- containerPort: 80
protocol: TCP

此时删除手动删除Pod,RS会自动创建符合配置文件定义的Pod数量及内容。

1
2
3
4
5
6
7
8
9
10
[root@master ~]# kubectl delete pod mywebrs-ksk66
pod "mywebrs-ksk66" deleted

[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
my-nginx-86459cfc9f-48c2s 1/1 Running 0 8m28s
my-nginx-86459cfc9f-wz6ng 1/1 Running 0 8m28s
mywebrs-bddcv 1/1 Running 0 7m37s
mywebrs-h9hvz 1/1 Running 0 6s
mywebrs-jczxq 1/1 Running 0 7m37s