replica_set_example.yaml配置如下:
apiVersion: extensions/v1beta1
kind: ReplicaSet #控制器类型为ReplicaSet
metadata:
name: nginx-relica #ReplicaSet控制器名字为nginx-relica
spec: #ReplicaSet控制器的描述
replicas: 3 #pod的副本数
selector: #ReplicaSet控制器的选择器
matchLabels: #ReplicaSet控制器定义标签
app: nginx-label #ReplicaSet控制器管理标签为app=nginx-label的pod
template: #pod的模板
metadata: #pod的元数据
labels: #定义pod的标签
app: nginx-label #定义pod的标签为app=nginx-label
spec: #pod的描述
containers: #定义pod中容器
- name: nginx-pod #容器的名字为nginx-pod
image: nginx #容器的镜像为nginx
imagePullPolicy: IfNotPresent #镜像策略,如果本地有nginx镜像,就用本地的镜像启动容器,如果本地没有默认从docker hub上获取latest版本nginx镜像
ports: #定义容器端口
- containerPort: 80 #暴露容器中80端口
command: ["/bin/sh", "-c", "echo hello world > /usr/share/nginx/html/hello.html; sleep 3600"] #容器启动后,执行command命令
用create创建ReplicaSet, kubectl create -f replica_set_example.yaml
,执行如下
[root@k8s-master01 sc_work]# kubectl create -f replica_set_example.yaml
replicaset.extensions/nginx-relica created
[root@k8s-master01 sc_work]# kubectl get replicaset
NAME DESIRED CURRENT READY AGE
nginx-relica 3 3 3 12s
[root@k8s-master01 sc_work]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-relica-8b26w 1/1 Running 0 20s
nginx-relica-grs84 1/1 Running 0 20s
nginx-relica-ts6n4 1/1 Running 0 20s
ReplicaSet管理了3个副本的pod,删除其中一个pod后,ReplicaSet会根据副本数重新启动一个pod,保持集群中有3个副本,如命令所示
[root@k8s-master01 sc_work]# kubectl delete pod nginx-relica-grs84
pod "nginx-relica-grs84" deleted
[root@k8s-master01 sc_work]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-relica-kpq7k 1/1 Running 0 33s
nginx-relica-rrsph 1/1 Running 0 2m13s
nginx-relica-ts6n4 1/1 Running 0 3m5s
通过ReplicaSet创建pod后,如果修改了pod的标签,ReplicaSet检查管理的pod数量少于副本数,会重新启动pod维持副本数量的pod。为修改pod标签前如下
[root@k8s-master01 sc_work]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-relica-kpq7k 1/1 Running 47 47h app=nginx-label
nginx-relica-rrsph 1/1 Running 47 47h app=nginx-label
nginx-relica-ts6n4 1/1 Running 47 47h app=nginx-label
修改其中一个pod的标签app=nginx-label修改为app=nginx-debug
[root@k8s-master01 sc_work]# kubectl label pod nginx-relica-kpq7k app=nginx-debug --overwrite
pod/nginx-relica-kpq7k labeled
然后再查询pod数量
[root@k8s-master01 sc_work]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-relica-hpk5f 1/1 Running 0 15s app=nginx-label
nginx-relica-kpq7k 1/1 Running 47 47h app=nginx-debug
nginx-relica-rrsph 1/1 Running 47 47h app=nginx-label
nginx-relica-ts6n4 1/1 Running 47 47h app=nginx-label
发现又重新启动了一个app=nginx-label的pod,始终维持在副本数量的pod。
前面示例已经用过了标签选择器,标签选择器主要是用来控制pod的
spec: #ReplicaSet控制器的描述
replicas: 3 #pod的副本数
selector: #ReplicaSet控制器的选择器
matchLabels: #ReplicaSet控制器定义标签
app: nginx-label #ReplicaSet控制器管理标签为app=nginx-label的pod
selector选择器下的matchLabels用来匹配单个标签键值对形式的,相当于k8s老版本中ReplicationController控制器的作用。另外selector选择器下的matchExpressions拥有更强大的选择功能。例如
spec:
replicas: 3
selector:
matchExpressions:
- key: app #matchExpressions为list类型
operator: In
values:
- nginx-prod #values为List类型
该标签选择器会选择标签app在nginx-prod标签的pod进行管理。
matchExpressions选择模式强大,有如下几种选择模式:
例如,只要创建的pod含有便签app,不管标签的value值如何,都会被ReplicaSet进行管理。
spec:
replicas: 3
selector:
matchExpressions:
- key: app
operator: Exists
注意:上面示例中,只指定了matchExpressions中的一个标签表达式,因为matchExpressions是list类型,可以指定多个选择器表达式,如果指定多个表达式,多有选择器必须与Pod匹配才能选中并管理pod。另外,如果同时指定了matchExpressions和matchLabels两种选择模式,则两种选择模式必须都匹配,pod才会被选中并进行管理。
replicaset管理pod后,如果修改了ReplicaSet标签选择器,会使ReplicaSet放弃正在管理的pod,转而去管理修改选择器后的指定的pod或者重新创建指定标签的pod。
比如用replica_set_example.yaml创建replicaset,并创建管理4个pod
apiVersion: extensions/v1beta1
kind: ReplicaSet
metadata:
name: nginx-relica
spec:
replicas: 4
selector:
matchLabels:
app: nginx-prod
template:
metadata:
labels:
app: nginx-prod
spec:
containers:
- name: nginx-pod
image: nginx
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
command: ["/bin/sh", "-c", "echo hello world > /usr/share/nginx/html/hello.html; sleep 3600"]
[root@k8s-master01 sc_work]# kubectl create -f replica_set_example.yaml
replicaset.extensions/nginx-relica created
[root@k8s-master01 sc_work]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-relica-4wcvq 1/1 Running 0 2m1s
nginx-relica-5q59d 1/1 Running 0 2m1s
nginx-relica-x694n 1/1 Running 0 2m1s
nginx-relica-ztp6p 1/1 Running 0 2m1s
执行上述命令后,nginx-relica管理着4个标签为nginx-prod的pod,下面修改一下nginx-relica的选择器,使其管理4个标签为nginx-debug的pod
kubectl edit rs nginx-relica #编辑nginx-relica
……省略
spec:
replicas: 4
selector:
matchLabels:
app: nginx-debug #标签选择器从nginx-prod修改为nginx-debug
template:
metadata:
creationTimestamp: null
labels:
app: nginx-debug #pod的模板标签从nginx-prod修改为nginx-debug
spec:
……省略
编辑完replicaset后,重新查看pod情况,发现replicaset重新管理了4个标签为app=nginx-debug的pod,但原来的app=nginx-prod的pod并没有自动删除,如果手动删除后,不会再自动重建,如果手动删除标签为app=nginx-debug的pod,replicaset会自动进行重新重建标签为app=nginx-debug的pod,使其数量达到副本数。
[root@k8s-master01 sc_work]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-relica-4wcvq 1/1 Running 0 4m26s app=nginx-prod
nginx-relica-5q59d 1/1 Running 0 4m26s app=nginx-prod
nginx-relica-5wspn 1/1 Running 0 21s app=nginx-debug
nginx-relica-795dg 1/1 Running 0 21s app=nginx-debug
nginx-relica-cp2fx 1/1 Running 0 21s app=nginx-debug
nginx-relica-nx8d6 1/1 Running 0 21s app=nginx-debug
nginx-relica-x694n 1/1 Running 0 4m26s app=nginx-prod
nginx-relica-ztp6p 1/1 Running 0 4m26s app=nginx-prod
集群中,有时访问量增加,需要水平扩展pod数量,或者晚上访问量小,缩小pod数量,节省资源。
下面通过ReplicaSet创建3个pod,然后再水平扩展到4个pod
#通过replicaset创建管理pod
[root@k8s-master01 sc_work]# kubectl create -f replica_set_example.yaml
replicaset.extensions/nginx-relica created
#成功创建3个pod
[root@k8s-master01 sc_work]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-relica-dnmhs 1/1 Running 0 5s
nginx-relica-g98nc 1/1 Running 0 5s
nginx-relica-w88mm 1/1 Running 0 5s
#ReplicaSet的副本数扩展到4个
[root@k8s-master01 sc_work]# kubectl scale replicaset nginx-relica --replicas=4
replicaset.extensions/nginx-relica scaled
#再次查询pod数量为4
[root@k8s-master01 sc_work]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-relica-88r59 1/1 Running 0 5s
nginx-relica-dnmhs 1/1 Running 0 63s
nginx-relica-g98nc 1/1 Running 0 63s
nginx-relica-w88mm 1/1 Running 0 63s
kubectl delete replicaset nginx-relica #执行命令后,nginx-relica管理的pod也会被删除
kubectl delete rs nginx-relica --cascade=false #删除nginx-relica后,nginx-relica管理的pod继续运行
注:ReplicaSet可以简写为rs,比如
kubectl get rs
参考《kubernetes in action》