Kubernetes 之 ReplicaSet
ReplicaSet 定义
ReplicaSet 是 Kubernetes 中的一种副本控制器,其主要作用是控制其管理的 Pod 的预设副本数量。它会持续监听这些 Pod 的运行状态,在Pod发生故障时执行重启策略,当 Pod 数量减少时会重新启动新的 Pod 副本。但官方推荐不要直接使用 ReplicaSet,推荐用Deployment 取而代之,Deployment 是 ReplicaSet 的父集。ReplicaSet 无法灵活性升级回退和扩缩容,但 Deployment 支持了这两大功能。
ReplicaSet 使用
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: rs-nginx
namespace: default
labels:
app: rs-nginx
spec:
replicas: 2
selector:
matchLabels:
app: pod-nginx
template:
metadata:
labels:
app: pod-nginx
spec:
containers:
- name: nginx
image: docker.io/k8s-test:v1.0
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
root@k8s-master1:~# kubectl get pods -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
rs-nginx-hljsk 1/1 Running 0 9s 10.244.194.120 k8s-worker1 <none> <none>
rs-nginx-t85wr 1/1 Running 0 9s 10.244.126.22 k8s-worker2 <none> <none>
root@k8s-master1:~# kubectl delete pods rs-nginx-hljsk
pod "rs-nginx-hljsk" deleted
root@k8s-master1:~# kubectl get pods -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
rs-nginx-t85wr 1/1 Running 0 41s 10.244.126.22 k8s-worker2 <none> <none>
rs-nginx-xsmdm 1/1 Running 0 4s 10.244.194.121 k8s-worker1 <none> <none>
root@k8s-master1:~# kubectl get rs
NAME DESIRED CURRENT READY AGE
rs-nginx 2 2 2 64s