##概述/理解VirtualMachineInstanceReplicaSet(vmis)确保指定数量的 VirtualMachineInstance(vmi) 副本在任何时候都在运行。我们可以这样理解,vmis就是kubernetes(k8s)里面的控制器(DeployMent,ReplicaSet)管理我们pod的副本数,实现扩缩容、回滚等。也可以借助HorizontalPodAutoscaler(hpa)实现弹性伸缩。这里我们就说vmis控制器,在这里的vmis控制器,管理我们vmi虚拟机实例的副本数,也可以实现扩缩容,借助hpa实现弹性伸缩。所有我们的yaml文件写法原理都类似。

使用场景

当需要许多相同的虚拟机,并且不关心在虚拟机终止后任何磁盘状态时。

创建

  1. 编写vmis的yaml文件
[root@master vm]# cat vmis.yaml
apiVersion: kubevirt.io/v1alpha3
kind: VirtualMachineInstanceReplicaSet
metadata:
  name: testreplicaset
spec:
  replicas: 2
  selector:
    matchLabels:
      myvmi: myvmi  # 保持一致,选择
  template:
    metadata:
      labels:
        myvmi: myvmi # 保持一致,匹配
    spec:
      domain:
        devices:
          disks:
          - name: containerdisk
            disk:
              bus: virtio
        resources:
          requests:
            memory: 1024M
      volumes:
      - name: containerdisk
        containerDisk:
          image: centos7
          imagePullPolicy: IfNotPresent
  1. 使用kubectl命令创建vmis
[root@master vm]# kubectl apply -f vmis.yaml
virtualmachineinstancereplicaset.kubevirt.io/testreplicaset created
  1. 查看运行状态
[root@master vm]# kubectl get vmis
NAME                  AGE   PHASE        IP             NODENAME   READY
testreplicaset6vm9s   42s   Running      10.244.0.139   master     False
testreplicaset8dshm   22s   Scheduling                             False
testreplicasetbqxnb   22s   Scheduling                             False
[root@master vm]# kubectl get vmis
NAME                  AGE   PHASE     IP             NODENAME   READY
testreplicaset8dshm   46s   Running   10.244.0.141   master     False
testreplicasetbqxnb   46s   Running   10.244.0.140   master     False
[root@master vm]# kubectl get pod
NAME                                      READY   STATUS    RESTARTS   AGE
virt-launcher-testreplicaset8dshm-nz7x2   2/2     Running   0          69s
virt-launcher-testreplicasetbqxnb-ljp2f   2/2     Running   0          70s
  1. describe 查看详细信息
[root@master vm]# kubectl describe vmirs testreplicaset
Name:         testreplicaset
Namespace:    default
Labels:       <none>
Annotations:  kubevirt.io/latest-observed-api-version: v1
              kubevirt.io/storage-observed-api-version: v1alpha3
API Version:  kubevirt.io/v1
Kind:         VirtualMachineInstanceReplicaSet
Metadata:
  Creation Timestamp:  2022-05-02T13:50:05Z
  Generation:          2
  Managed Fields:
    API Version:  kubevirt.io/v1alpha3
    Fields Type:  FieldsV1
    fieldsV1:
      f:metadata:
        f:annotations:
          f:kubevirt.io/latest-observed-api-version:
          f:kubevirt.io/storage-observed-api-version:
      f:spec:
        f:template:
          f:metadata:
            f:creationTimestamp:
      f:status:
        .:
        f:labelSelector:
        f:replicas:
    Manager:      Go-http-client
    Operation:    Update
    Time:         2022-05-02T13:50:05Z
    API Version:  kubevirt.io/v1alpha3
    Fields Type:  FieldsV1
    fieldsV1:
      f:metadata:
        f:annotations:
 ...
 ...
                  .:
                  f:memory:
            f:volumes:
    Manager:         kubectl
    Operation:       Update
    Time:            2022-05-02T13:50:05Z
  Resource Version:  267261
  Self Link:         /apis/kubevirt.io/v1/namespaces/default/virtualmachineinstancereplicasets/testreplicaset
  UID:               96d17d12-17b5-4df7-940a-fac7c6b820d2
Spec:
  Replicas:  2
  Selector:
    Match Labels:
      Myvmi:  myvmi
  Template:
    Metadata:
      Creation Timestamp:  <nil>
      Labels:
        Myvmi:  myvmi
    Spec:
      Domain:
        Devices:
          Disks:
            Disk:
              Bus:  virtio
            Name:   containerdisk
        Resources:
          Requests:
            Memory:  1024M
      Volumes:
        Container Disk:
          Image:              kubevirt/cirros-container-disk-demo
          Image Pull Policy:  IfNotPresent
        Name:                 containerdisk
Status:
  Label Selector:  myvmi=myvmi
  Replicas:        2
Events:
  Type    Reason            Age    From                                 Message
  ----    ------            ----   ----                                 -------
  Normal  SuccessfulCreate  5m21s  virtualmachinereplicaset-controller  Started the virtual machine by creating the new virtual machine instance testreplicaseth4zsl
  Normal  SuccessfulCreate  5m21s  virtualmachinereplicaset-controller  Started the virtual machine by creating the new virtual machine instance testreplicasetw75s4

扩缩容

  1. 查看vmis
[root@master vm]# kubectl get -f vmis.yaml
NAME             DESIRED   CURRENT   READY   AGE
testreplicaset   3         3                 2m52s
  1. 使用scale命令,设置副本数为5
[root@master vm]# kubectl scale vmirs testreplicaset --replicas 5
virtualmachineinstancereplicaset.kubevirt.io/testreplicaset scaled
  1. 查看效果
[root@master vm]# kubectl get vmis
NAME                  AGE     PHASE     IP             NODENAME   READY
testreplicaset98x8d   5m29s   Running   10.244.0.146   master     False
testreplicasetddqc9   2m24s   Running   10.244.0.148   master     False
testreplicasetdss8l   5m29s   Running   10.244.0.144   master     False
testreplicasetmhm6x   5m29s   Running   10.244.0.145   master     False
testreplicasetv4dzs   2m24s   Running   10.244.0.147   master     False

[root@master vm]# kubectl get pod
NAME                                      READY   STATUS    RESTARTS   AGE
virt-launcher-testreplicaset98x8d-5p99p   2/2     Running   0          3m15s
virt-launcher-testreplicasetddqc9-6c2m4   2/2     Running   0          10s
virt-launcher-testreplicasetdss8l-9mv56   2/2     Running   0          3m15s
virt-launcher-testreplicasetmhm6x-r76wt   2/2     Running   0          3m15s
virt-launcher-testreplicasetv4dzs-bm4s8   2/2     Running   0          10s

弹性伸缩

方法1

  1. 使用Horizo​​ntal Pod Autoscaler(hpa),创建yaml文件
[root@master vm]# cat vmis-hpa.yaml
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  creationTimestamp: null
  name: testreplicaset
spec:
  maxReplicas: 5
  minReplicas: 2
  scaleTargetRef:
    apiVersion: kubevirt.io/v1
    kind: VirtualMachineInstanceReplicaSet
    name: testreplicaset
  1. 创建vmis-hpa
[root@master vm]# kubectl apply -f vmis-hpa.yaml
horizontalpodautoscaler.autoscaling/testreplicaset created
  1. 查看状态
[root@master vm]# kubectl get hpa
NAME             REFERENCE                                         TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
testreplicaset   VirtualMachineInstanceReplicaSet/testreplicaset   <unknown>/80%   2         5         0          7s

方法2

  1. 使用命令kubectl autoscale
[root@master vm]# kubectl autoscale vmirs testreplicaset --min=2 --max=5
作者:|Cloud叶子翻面er|,原文链接: http://www.imooc.com/article/325263

文章推荐

Go中rune类型浅析

Spark: 单词计数(Word Count)的MapReduce实现(Java/Python)

一次搞定 vue3的 路由 + 菜单 + tabs

一文学完Linux常用命令

自动微分原理

Nginx分片限流

函数计算 HTTP 触发器支持异步,解放双手搭建 Web 服务

【Java 数据结构及算法实战】系列 013:Java队列07——双端

怎么用python做一个解压缩小工具,以后再也不用下载各种格式...

给linux(centos)操作系统设置主机名的几种方式

一文带你了解 「图数据库」Nebula 的存储设计和思考

【Java分享客栈】未来迈向高级工程师绕不过的技能:JMeter压...