一 Replication Controller的介绍
pod可能会由于各种原因消失和多出来,例如node节点去除集群或者人为的手工创建,所以为了方便和管理pod的数量,k8s里面
的另外一种资源就应运而生,Replication Controller,它能够管理特定的pod的标签,使其动态保持一个数量,少了就会去创建,多了就会删除集群中的pod
始终保持集群中pod数量是一个自己定义的数量
二 Replication Controller的组成(后面部分都简称RC)
RC由三部分组成
-
- label selector 用于确定RC能够控制的pod的标签的范围
- replica count 副本个数,指定运行容器的数量
- pod template pod的模板,用于创建新的pod副本
三 以yaml的形式创建一个RC
3.1 配置文件内容如下
[root@node01 Chapter04]# cat kubia-rc.yaml apiVersion: v1 kind: ReplicationController metadata: name: kubia spec: replicas: 3 selector: app: kubia template: metadata: labels: app: kubia spec: containers: - name: kubia image: luksa/kubia ports: - containerPort: 8080
3.2 创建RC
[root@node01 Chapter04]# k create -f kubia-rc.yaml replicationcontroller/kubia created
[root@node01 Chapter04]# k get rc NAME DESIRED CURRENT READY AGE kubia 3 3 2 9s
[root@node01 Chapter04]# k get po NAME READY STATUS RESTARTS AGE kubia-cc6dg 1/1 Running 0 27s kubia-dsc2x 1/1 Running 0 27s kubia-liveness 1/1 Running 24 94m kubia-liveness-delay 1/1 Running 17 64m kubia-vz52m 1/1 Running 0 27s
3.3 经由RC管控的pod在被删除某个副本之后,RC会感知到数量已经不符合预期,会重新去拉取镜像创建pod,红色的是刚刚由RC最新创建的
[root@node01 Chapter04]# k get po NAME READY STATUS RESTARTS AGE kubia-cc6dg 1/1 Running 0 6m55s kubia-dsc2x 1/1 Running 0 6m55s kubia-vz52m 1/1 Running 0 6m55s [root@node01 Chapter04]# k delete po kubia-cc6dg pod "kubia-cc6dg" deleted [root@node01 Chapter04]# k get po NAME READY STATUS RESTARTS AGE kubia-dsc2x 1/1 Running 0 8m22s kubia-qgxvw 1/1 Running 0 80s kubia-vz52m 1/1 Running 0 8m22s
3.4 RC完成通过标签来管控pod,也可以通过修改pod里面的标签,从而脱离RC的管控,甚至将pod从一个RC移动至另一个RC
[root@node01 Chapter04]# k get po --show-labels NAME READY STATUS RESTARTS AGE LABELS kubia-dsc2x 1/1 Running 0 11m app=kubia kubia-qgxvw 1/1 Running 0 4m49s app=kubia kubia-vz52m 1/1 Running 0 11m app=kubia
[root@node01 Chapter04]# k label po kubia-qgxvw type=docker pod/kubia-qgxvw labeled
[root@node01 Chapter04]# k get po --show-labels NAME READY STATUS RESTARTS AGE LABELS kubia-dsc2x 1/1 Running 0 12m app=kubia kubia-qgxvw 1/1 Running 0 5m39s app=kubia,type=docker kubia-vz52m 1/1 Running 0 12m app=kubia
[root@node01 Chapter04]# k label po kubia-vz52m app=k8s --overwrite pod/kubia-vz52m labeled
[root@node01 Chapter04]# k get po NAME READY STATUS RESTARTS AGE kubia-dsc2x 1/1 Running 0 13m kubia-jqr8m 0/1 ContainerCreating 0 5s kubia-qgxvw 1/1 Running 0 6m9s kubia-vz52m 1/1 Running 0 13m
注意: 可以使用k edit rc rc_name来编辑rc的配置
3.5 修改pod的模板之后,原来的pod将不受到管控,并且删除之后RC将会产生新的pod里面也会和修改过后的模板保持一致
[root@node01 Chapter04]# k delete po --all pod "kubia-dsc2x" deleted pod "kubia-jqr8m" deleted pod "kubia-qgxvw" deleted pod "kubia-vz52m" deleted
[root@node01 Chapter04]# k get po --show-labels NAME READY STATUS RESTARTS AGE LABELS kubia-27g4j 1/1 Running 0 99s app=kubia,version=v1.0 kubia-k7964 1/1 Running 0 99s app=kubia,version=v1.0 kubia-wn8wz 1/1 Running 0 99s app=kubia,version=v1.0