首先,我们需要说明一下replicasets 和replicacontroller的区别
replicasets 是replicacontroller的升级版, 并且,replicasets 支持selector , 这个在replicacontroller 是不支持的
ReplicaSets 和 replicacontroller 的功能是相同的: 监控pod 的状态, 并维持某个状态(例如 一直保持3个running nginx pod ,如果多了,就删掉一个,如果有一个失败了,就重新创建一个)
帮我们解决的问题就是:
高可用:我们不用时刻监视我们的pod状态, replicasets会自动帮我们重启,新建,删除,确保我们的Pod一直是正常工作的,如果没有replicasets ,如果我们的pod因为别的原因挂掉了,就无法提供服务了
可扩展: replicasets 让我们有能力快速扩展我们的pod的数量,根据我们提供的模板,可以快速的把我们的pod 的数量从3个扩展到10个,100个,然后将流量平均分配到各个pod,这样做到了负载均衡
我们看一下replicacontroller的定义文件,注意apiVersion:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
apiVersion: v1 kind: ReplicationController metadata: name: myapp-rc labels: app: myapp type: front-end spec: template: metadata: name: myapp-pod labels: app: myapp type: front-end spec: containers: - name: nginx-container image: nginx replicas: 3 |
对应的,我们看一下replicaSets的定义文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
apiVersion: apps/v1 kind: ReplicaSet metadata: name: myapp-replicaset labels: app: myapp type: front-end spec: template: metadata: name: myapp-pod labels: app: myapp type: front-end spec: containers: - name: nginx-container image: nginx replicas: 3 selector: matchLabels: type: front-end |
可以看到replicaSets 用的apiVersion 是 apps/v1, 而不是 v1 了,并且,spec中多了 selector的部分
以replicaSets的文件定义为例,我们这个文件,定义了一个模板 template :里边和我们pod定义部分一模一样,删掉了部分内容而已,主要就是name ,image ,也就是说,如果replicasets 发现我们的pod少于预定值,决定要创建一个的话,template就是它参考的标准,它会按照你给出的template来创建一个新的pod
replicas 就是我们给出的我们希望维持的pod 数量
我们来说一下label :
为什么要有label呢?如果所有的pod都是通过replicasets的模板创建的,label早就写死了,为啥还要再selector里加呢? 原因是,replicasets可以管理原来就有的pod,例如: 在我们创建replicasets之前,已经有3个pod 有标签type” front-end了, 那么我们的replicaset就会直接监控旧的pod数量和状态,除非旧的挂掉了,它才会根据模板创建新的pod
创建replicasets的命令:
1 |
kubectl apply -f replicaset-defination.yaml |
修改replicas数量:
1 |
kubectl scale --replicas=6 -f replicaset-defination.yaml |
或者:
1 |
kubectl scale --replicas=6 replicaset myapp-replicaset |
Latest posts by Zhiming Zhang (see all)
- aws eks node 自动化扩展工具 Karpenter - 8月 10, 2022
- ReplicationController and ReplicaSet in Kubernetes - 12月 20, 2021
- public key fingerprint - 5月 27, 2021