ReplicaSet
ReplicaSet是新一代的Replication Controller,两者之间的区别在于,RS比RC在标签选择上更灵活,RC的选择器仅支持K:V,而RS的选择器不仅支持KV,还支持编写表达式来进行选择
spec:
# modify replicas according to your case
replicas: 3
selector:
matchLabels:
tier: frontend
matchExpressions:
- {key: tier, operator: In, values: [frontend]}
第一个RS清单文件
rs-demo.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: myapp
namespace: default
spec:
replicas: 2
selector:
matchLabels:
app: myapp
release: canary
template:
metadata:
name: myapp-pod
labels:
app: myapp
release: canary
environment: qa
spec:
containers:
- name: myapp-container
image: ikubernetes/myapp:v1
ports:
- name: http
containerPort: 80
创建RS
# kubectl apply -f rs-demo.yaml
replicaset.apps/myapp created
查看Pods信息
# kubectl get pods -o wide --show-labels -l app=myapp
NAME READY STATUS RESTARTS AGE IP NODE LABELS
myapp-g77rm 1/1 Running 0 2m 10.244.2.3 node003 app=myapp,environment=qa,release=canary
myapp-k5l75 1/1 Running 0 2m 10.244.1.13 node002 app=myapp,environment=qa,release=canary
查看rs信息
# kubectl get rs -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
myapp 2 2 2 6m myapp-container ikubernetes/myapp:v1 app=myapp,release=canary
RS中关于Pod的模版定义,其中有一个镜像的字段,该字段虽然是可以动态修改的,但是不会动态重建,只会在新建的Pods生效,以下是测试
修改镜像版本,可以看到RS镜像已经变了
# kubectl edit rs myapp
replicaset.extensions/myapp edited
# kubectl get rs -o wide |grep myapp
myapp 2 2 2 8m myapp-container ikubernetes/myapp:v2 app=myapp,release=canary
访问Pods试下
# kubectl get pods -o wide -l app=myapp
NAME READY STATUS RESTARTS AGE IP NODE
myapp-g77rm 1/1 Running 0 10m 10.244.2.3 node003
myapp-k5l75 1/1 Running 0 10m 10.244.1.13 node002
# curl 10.244.2.3
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
删除Pods测试是否重建时是否应用新镜像
# kubectl delete pods myapp-k5l75
pod "myapp-k5l75" deleted
# kubectl get pods -o wide -l app=myapp
NAME READY STATUS RESTARTS AGE IP NODE
myapp-5jx59 1/1 Running 0 3m 10.244.2.4 node003
myapp-d92bt 1/1 Running 0 19s 10.244.1.14 node002
# curl 10.244.1.14
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
可以看到新创建的容器已经应用新的镜像了
但是,这带来了一个问题,发布时,该怎么发布?
一个一个删除,肯定不行,因为太慢了,而且手动麻烦。 全部删除也不行,因为一定时间内用户肯定访问异常。
所以,最好是蓝绿发布,默认情况下,用户通过Service先请求rs1中的pods,开始部署后,建立rs2,所有pod建立完成后,将Service中标签选择使用rs2,rs2开始处理请求。