本节,我们通过一些例子来演示ReplicaSet的创建、查看、更新、删除等操作。

创建

首先我们做准备一个名为replicaset.yaml的配置文件,其内容如下所示:

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: replicaset-runs-pod
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.19.0

ReplicaSet配置保证同时有3个Pod副本在运行。

使用kubectl create命令创建ReplicaSet控制器,如下所示:

[root@ecs-d8b6 manifests]# kubectl create -f replicaset.yaml 
replicaset.apps/replicaset-runs-pod created

命令行输出显示,一个名为replicaset-runs-podReplicaSet控制器已经创建完成。

查看

我们先查看刚创建的ReplicaSet控制器:

[root@ecs-d8b6 manifests]# kubectl get replicaset 
NAME                  DESIRED   CURRENT   READY   AGE
replicaset-runs-pod   3         3         3       90s

可以看到控制器replicaset-runs-pod期望的Pod数量(DESIRED)为3,当前已创建的Pod数量(CURRENT)也为3,并且处于READY状态的Pod数量(READY)同样是3,正是我们期望的状态。

接着再查看实际运行的Pod:

[root@ecs-d8b6 manifests]# kubectl get pods 
NAME                        READY   STATUS    RESTARTS   AGE
replicaset-runs-pod-dzhqp   1/1     Running   0          5m
replicaset-runs-pod-hpbs8   1/1     Running   0          5m
replicaset-runs-pod-xhx9j   1/1     Running   0          5m

可以看到有3个Pod在运行,Pod名字统一使用其控制器名字为前缀,再加一个随机字符串。

使用kubectl describe命令查看控制器,可以看到其创建Pod的记录:

[root@ecs-d8b6 manifests]# kubectl describe replicaset replicaset-runs-pod 
Name:         replicaset-runs-pod
Namespace:    default
Selector:     app=nginx
Labels:       <none>
Annotations:  <none>
Replicas:     3 current / 3 desired
Pods Status:  3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        nginx:1.19.0
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age   From                   Message
  ----    ------            ----  ----                   -------
  Normal  SuccessfulCreate  8m5s  replicaset-controller  Created pod: replicaset-runs-pod-xhx9j
  Normal  SuccessfulCreate  8m5s  replicaset-controller  Created pod: replicaset-runs-pod-dzhqp
  Normal  SuccessfulCreate  8m5s  replicaset-controller  Created pod: replicaset-runs-pod-hpbs8

通过最后的Events信息可知,控制器replicaset-runs-pod运行过程中分别创建了上面的3个Pod。

更新

改变副本个数

我们使用kubectl edit命令修改控制器replicaset-runs-pod,把spec.replicas 数量由3调整为5:

[root@ecs-d8b6 manifests]# kubectl edit replicaset replicaset-runs-pod 
replicaset.apps/replicaset-runs-pod edited

然后再次查看Pod:

[root@ecs-d8b6 manifests]# kubectl get pods 
NAME                        READY   STATUS    RESTARTS   AGE
replicaset-runs-pod-5mtdk   1/1     Running   0          37s
replicaset-runs-pod-6rqqp   1/1     Running   0          37s
replicaset-runs-pod-dzhqp   1/1     Running   0          21m
replicaset-runs-pod-hpbs8   1/1     Running   0          21m
replicaset-runs-pod-xhx9j   1/1     Running   0          21m

可以看到两个新的Pod被创建了出来,Pod副本数与配置中spec.replicas设定置一致。

类似地,当我们再次修改控制器replicaset-runs-pod配置,将spec.replicas 数量由5调整为3,多出的Pod数量会被自动删除。

模拟Pod异常

ReplicaSet控制器将始终确保集群中运行的Pod副本数满足期望值,当Pod被删除时,控制器将马上创建新的Pod填补空缺:

[root@ecs-d8b6 manifests]# kubectl delete pods replicaset-runs-pod-dzhqp
pod "replicaset-runs-pod-dzhqp" deleted
[root@ecs-d8b6 manifests]# kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
replicaset-runs-pod-hpbs8   1/1     Running   0          31m
replicaset-runs-pod-tbccg   1/1     Running   0          9s
replicaset-runs-pod-xhx9j   1/1     Running   0          31m

上面的例子中,当手动删除某个ReplicaSet控制器管理的Pod时,新的Pod马上被创建了出来,通过Pod运行时间或者控制器事件信息都可以看出。

删除

当删除ReplicaSet控制器时,其管理的Pod默认也会相应的删除,如下所示:

[root@ecs-d8b6 manifests]# kubectl delete -f replicaset.yaml 
replicaset.apps "replicaset-runs-pod" deleted
[root@ecs-d8b6 manifests]# kubectl get pods
No resources found in default namespace.

小结

ReplicaSet控制器的能力侧重于对副本数量的把控,它能保证集群中时刻运行指定数量的Pod副本,当配置中Pod副本数量变化时,可以动态地调整Pod数量。

最后编辑: kuteng  文档更新时间: 2021-09-18 14:58   作者:kuteng