Replication Controller Replica Set

如果我们都人工的去解决遇到的pod重启问题,似乎又回到了以前刀耕火种的时代了是吧,如果有一种工具能够来帮助我们管理Pod就好了,Pod不够了自动帮我新增一个,Pod挂了自动帮我在合适的节点上重新启动一个Pod,这样是不是遇到重启问题我们都不需要手动去解决了。

幸运的是,Kubernetes就为我们提供了这样的资源对象:

  • Replication Controller:用来部署、升级Pod
  • Replica Set:下一代的Replication Controller
  • Deployment:可以更加方便的管理PodReplica Set

环境

192.168.48.101 master01
192.168.48.201 node01
192.168.48.202 node02

Replication Controller

Replication Controller简称RCRCKubernetes系统中的核心概念之一,简单来说,RC可以保证在任意时间运行Pod的副本数量,能够保证Pod总是可用的。如果实际Pod数量比指定的多那就结束掉多余的,如果实际数量比指定的少就新启动一些Pod,当Pod失败、被删除或者挂掉后,RC都会去自动创建新的Pod来保证副本数量,所以即使只有一个Pod,我们也应该使用RC来管理我们的Pod

编写myapp-rc.yaml

 vim myapp-rc.yaml

apiVersion: v1
kind: ReplicationController
metadata:
  name: myapp-rc
  labels:
    app: myapp
    type: rc
spec:
  replicas: 2
  selector:
      app: myapp
      type: rc
  template:
    metadata:
      name: myapp-pod
      labels:
        app: myapp
        type: rc
    spec:
      containers:
      - name: myapp
        image: ikubernetes/myapp:v1
        ports:
        - name: http
          containerPort: 80

上面的YAML文件:

  • kind:ReplicationController
  • spec.replicas: 指定Pod副本数量,默认为1
  • spec.selector: RC通过该属性来筛选要控制的Pod
  • spec.template: 这里就是我们之前的Pod的定义的模块,但是不需要apiVersionkind
  • spec.template.metadata.labels: 注意这里的Podlabels要和spec.selector相同,这样RC就可以来控制当前这个Pod了。

创建myapp-rc.yaml

[root@master01 deploy_yaml]# kubectl apply -f myapp-rc.yaml 
replicationcontroller/myapp-rc created

[root@master01 deploy_yaml]# kubectl get pod
NAME                           READY   STATUS    RESTARTS   AGE
demo-deploy-8675c97685-vhncn   1/1     Running   0          8d
demo-deploy-8675c97685-w7md2   1/1     Running   0          8d
demo-rc-l9c8r                  1/1     Running   0          4m49s
demo-rc-qvdd7                  1/1     Running   0          4m49s
myapp-rc-hzsxm                 1/1     Running   0          7s
myapp-rc-w5k7r                 1/1     Running   0          7s

查看rc

[root@master01 deploy_yaml]# kubectl get rc
NAME       DESIRED   CURRENT   READY   AGE
demo-rc    2         2         2       6m18s
myapp-rc   2         2         2       96s

查看具体信息

[root@master01 deploy_yaml]# kubectl describe rc myapp-rc 
Name:         myapp-rc
Namespace:    default
Selector:     app=myapp,type=rc
Labels:       app=myapp
              type=rc
Annotations:  kubectl.kubernetes.io/last-applied-configuration:
                {"apiVersion":"v1","kind":"ReplicationController","metadata":{"annotations":{},"labels":{"app":"myapp","type":"rc"},"name":"myapp-rc","nam...
Replicas:     2 current / 2 desired
Pods Status:  2 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=myapp
           type=rc
  Containers:
   myapp:
    Image:        ikubernetes/myapp:v1
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age   From                    Message
  ----    ------            ----  ----                    -------
  Normal  SuccessfulCreate  2m8s  replication-controller  Created pod: myapp-rc-hzsxm
  Normal  SuccessfulCreate  2m8s  replication-controller  Created pod: myapp-rc-w5k7r

修改yaml下Pod的副本数量为3:

vim myapp-rc.yaml
....
replicas: 3

[root@master01 deploy_yaml]# kubectl apply -f myapp-rc.yaml 
replicationcontroller/myapp-rc configured
[root@master01 deploy_yaml]# kubectl get rc
NAME       DESIRED   CURRENT   READY   AGE
demo-rc    2         2         2       8m48s
myapp-rc   3         3         3       4m6s
[root@master01 deploy_yaml]# 

滚动升级

[root@master01 deploy_yaml]# kubectl rolling-update myapp-rc --image=ikubernetes/myapp:v2
Command "rolling-update" is deprecated, use "rollout" instead
Created myapp-rc-df879e42cfe882821bb7b680abe0f890
Scaling up myapp-rc-df879e42cfe882821bb7b680abe0f890 from 0 to 3, scaling down myapp-rc from 3 to 0 (keep 3 pods available, don't exceed 4 pods)
Scaling myapp-rc-df879e42cfe882821bb7b680abe0f890 up to 1
Scaling myapp-rc down to 2
Scaling myapp-rc-df879e42cfe882821bb7b680abe0f890 up to 2
Scaling myapp-rc down to 1
Scaling myapp-rc-df879e42cfe882821bb7b680abe0f890 up to 3
Scaling myapp-rc down to 0
Update succeeded. Deleting old controller: myapp-rc
Renaming myapp-rc-df879e42cfe882821bb7b680abe0f890 to myapp-rc
replicationcontroller/myapp-rc rolling updated

Replication Set

Replication Set简称RS,随着Kubernetes的高速发展,官方已经推荐我们使用RSDeployment来代替RC了,实际上RSRC的功能基本一致,目前唯一的一个区别就是RC只支持基于等式的selector(env=dev或environment!=qa),但RS还支持基于集合的selector(version in (v1.0, v2.0)),这对复杂的运维管理就非常方便了。

kubectl命令行工具中关于RC的大部分命令同样适用于我们的RS资源对象。不过我们也很少会去单独使用RS,它主要被Deployment这个更加高层的资源对象使用,除非用户需要自定义升级功能或根本不需要升级Pod,在一般情况下,我们推荐使用Deployment而不直接使用Replica Set

最后我们总结下关于RC/RS的一些特性和作用吧:

  • 大部分情况下,我们可以通过定义一个RC实现的Pod的创建和副本数量的控制
  • RC中包含一个完整的Pod定义模块(不包含apiversionkind
  • RC是通过label selector机制来实现对Pod副本的控制的
  • 通过改变RC里面的Pod副本数量,可以实现Pod的扩缩容功能
  • 通过改变RC里面的Pod模板中镜像版本,可以实现Pod的滚动升级功能(但是不支持一键回滚,需要用相同的方法去修改镜像地址)

编写myapp-rs.yaml

vim myapp-rs.yaml

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: myapp-replicaset
  namespace: default
  labels:
    app: myapp
    type: replicaset
spec:
  replicas: 2
  selector:
    matchLabels:
      app: myapp
      type: replicaset
  template:
    metadata:
      name: myapp-pod
      labels:
        app: myapp
        type: replicaset
    spec:
      containers:
      - name: myapp
        image: ikubernetes/myapp:v1
        ports:
        - name: http
          containerPort: 80

创建myapp-rs.yaml

[root@master01 deploy_yaml]# kubectl apply -f myapp-rs.yaml 
replicaset.apps/myapp-replicaset created
[root@master01 deploy_yaml]# kubectl get pod
NAME                           READY   STATUS    RESTARTS   AGE
demo-deploy-8675c97685-vhncn   1/1     Running   0          8d
demo-deploy-8675c97685-w7md2   1/1     Running   0          8d
demo-rc-l9c8r                  1/1     Running   0          23m
demo-rc-qvdd7                  1/1     Running   0          23m
myapp-rc-brbm2                 1/1     Running   0          4m14s
myapp-rc-c78nt                 1/1     Running   0          4m14s
myapp-rc-lkbxw                 1/1     Running   0          4m14s
myapp-replicaset-h4g92         1/1     Running   0          8s
myapp-replicaset-l24ff         1/1     Running   0          8s


查看rs

[root@master01 deploy_yaml]# kubectl get rs
NAME                     DESIRED   CURRENT   READY   AGE
demo-deploy-8675c97685   2         2         2       8d
myapp-replicaset         2         2         2       48s


查看具体信息

[root@master01 deploy_yaml]# kubectl describe rs myapp-replicaset 
Name:         myapp-replicaset
Namespace:    default
Selector:     app=myapp,type=replicaset
Labels:       app=myapp
              type=replicaset
Annotations:  kubectl.kubernetes.io/last-applied-configuration:
                {"apiVersion":"apps/v1","kind":"ReplicaSet","metadata":{"annotations":{},"labels":{"app":"myapp","type":"replicaset"},"name":"myapp-replic...
Replicas:     2 current / 2 desired
Pods Status:  2 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=myapp
           type=replicaset
  Containers:
   myapp:
    Image:        ikubernetes/myapp:v1
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age    From                   Message
  ----    ------            ----   ----                   -------
  Normal  SuccessfulCreate  5m45s  replicaset-controller  Created pod: myapp-replicaset-h4g92
  Normal  SuccessfulCreate  5m45s  replicaset-controller  Created pod: myapp-replicaset-l24ff


修改yaml下Pod的副本数量为3:

vim myapp-rs.yaml
.....
replicas: 3


[root@master01 deploy_yaml]# kubectl apply -f myapp-rs.yaml 
replicaset.apps/myapp-replicaset configured
[root@master01 deploy_yaml]# kubectl get rs
NAME                     DESIRED   CURRENT   READY   AGE
demo-deploy-8675c97685   2         2         2       8d
myapp-replicaset         3         3         3       6m11s
[root@master01 deploy_yaml]# 


Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐