乐趣区

关于kubernetes:kubevirtVirtualMachineInstanceReplicaSetvmis扩缩容弹性伸缩

@TOC

概述 / 了解

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

应用场景

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

创立 vmis

  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
  2. 应用 kubectl 命令创立 vmis

    [root@master vm]# kubectl apply -f vmis.yaml
    virtualmachineinstancereplicaset.kubevirt.io/testreplicaset created
  3. 查看运行状态

    [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
  4. 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 testreplicaseth6zsl
  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
  2. 应用 scale 命令,设置正本数为 5

    [root@master vm]# kubectl scale vmirs testreplicaset --replicas 5
    virtualmachineinstancereplicaset.kubevirt.io/testreplicaset scaled
  3. 查看成果
[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
  2. 创立 vmis-hpa

    [root@master vm]# kubectl apply -f vmis-hpa.yaml
    horizontalpodautoscaler.autoscaling/testreplicaset created
  3. 查看状态
[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
退出移动版