CentOS Stream 10
Sponsored Link

Kubernetes : 仮想マシン作成2025/01/27

 

KubeVirt で仮想マシンを作成します。

当例では以下のように 4 台のノードを使用して Kubernetes クラスターを構成しています。

+----------------------+   +----------------------+
|  [ ctrl.srv.world ]  |   |   [ dlp.srv.world ]  |
|     Manager Node     |   |     Control Plane    |
+-----------+----------+   +-----------+----------+
        eth0|10.0.0.25             eth0|10.0.0.30
            |                          |
------------+--------------------------+-----------
            |                          |
        eth0|10.0.0.51             eth0|10.0.0.52
+-----------+----------+   +-----------+----------+
| [ node01.srv.world ] |   | [ node02.srv.world ] |
|     Worker Node#1    |   |     Worker Node#2    |
+----------------------+   +----------------------+

[1]

OS イメージを保管するための外部ストレージが必要となります。
当例では、こちらの [1], [2], [3] の通り、Control Plane ノード上に NFS サーバーを起動し、[/home/nfsshare] ディレクトリを共有ディレクトリとして設定して、動的プロビジョニングできるよう NFS subdir external provisioner を設定して進めます。

[2] OS イメージを保管するために Containerized Data Importer をインストールします。
[cent@ctrl ~]$
export TAG=$(curl -s -w %{redirect_url} https://github.com/kubevirt/containerized-data-importer/releases/latest)

[cent@ctrl ~]$
export VERSION=$(echo ${TAG##*/})

[cent@ctrl ~]$
wget https://github.com/kubevirt/containerized-data-importer/releases/download/${VERSION}/cdi-operator.yaml

[cent@ctrl ~]$
wget https://github.com/kubevirt/containerized-data-importer/releases/download/${VERSION}/cdi-cr.yaml
[cent@ctrl ~]$
vi cdi-cr.yaml
apiVersion: cdi.kubevirt.io/v1beta1
kind: CDI
metadata:
  name: cdi
spec:
  config:
    # 下行を追記してメモリーリミットを適当に増やす
    podResourceRequirements:
      limits:
        cpu: '1'
        memory: 4Gi
    featureGates:
    - HonorWaitForFirstConsumer
  imagePullPolicy: IfNotPresent
  infra:
    nodeSelector:
      kubernetes.io/os: linux
    tolerations:
    - key: CriticalAddonsOnly
      operator: Exists
  workload:
    nodeSelector:
      kubernetes.io/os: linux

[cent@ctrl ~]$
kubectl apply -f cdi-operator.yaml

namespace/cdi created
customresourcedefinition.apiextensions.k8s.io/cdis.cdi.kubevirt.io created
clusterrole.rbac.authorization.k8s.io/cdi-operator-cluster created
clusterrolebinding.rbac.authorization.k8s.io/cdi-operator created
serviceaccount/cdi-operator created
role.rbac.authorization.k8s.io/cdi-operator created
rolebinding.rbac.authorization.k8s.io/cdi-operator created
deployment.apps/cdi-operator created

[cent@ctrl ~]$
kubectl apply -f cdi-cr.yaml

cdi.cdi.kubevirt.io/cdi created
# 一定時間経過すると 関連 pod が起動

[cent@ctrl ~]$
kubectl get pods -n cdi

NAME                               READY   STATUS    RESTARTS   AGE
cdi-apiserver-74dcb7f4c5-mvtwv     1/1     Running   0          63s
cdi-deployment-546cdb5fcd-r27xg    1/1     Running   0          62s
cdi-operator-59c7548cfb-bm2dr      1/1     Running   0          89s
cdi-uploadproxy-66448dc879-22n88   1/1     Running   0          63s
[3] 仮想マシンを作成します。当例では Fedora で作成します。
[cent@ctrl ~]$
kubectl get sc

NAME         PROVISIONER                                                RECLAIMPOLICY   VOLUMEBINDINGMODE   ALLOWVOLUMEEXPANSION   AGE
nfs-client   cluster.local/nfs-client-nfs-subdir-external-provisioner   Delete          Immediate           true                   97m

# PVC 作成

[cent@ctrl ~]$
vi fedora-pvc.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: "fedora-pvc"
  labels:
    app: containerized-data-importer
  annotations:
    cdi.kubevirt.io/storage.import.endpoint: "https://download.fedoraproject.org/pub/fedora/linux/releases/41/Cloud/x86_64/images/Fedora-Cloud-Base-AmazonEC2-41-1.4.x86_64.raw.xz"
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
  storageClassName: nfs-client

[cent@ctrl ~]$
kubectl apply -f fedora-pvc.yml

persistentvolumeclaim/fedora-pvc created
[cent@ctrl ~]$
kubectl get pvc

NAME         STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   VOLUMEATTRIBUTESCLASS   AGE
fedora-pvc   Bound    pvc-da7807ef-cfd7-4d4f-ae88-573d2139dd8f   10Gi       RWO            nfs-client     <unset>                 5s

[cent@ctrl ~]$
kubectl get pods

NAME                  READY   STATUS    RESTARTS   AGE
importer-fedora-pvc   1/1     Running   0          13s

# possible to see importing logs

[cent@ctrl ~]$
kubectl logs -f importer-fedora-pvc

.....
.....
I0127 04:03:56.748963       1 data-processor.go:341] Expanding image size to: 10146021376
E0127 04:03:56.761941       1 prlimit.go:156] failed to kill the process; os: process already finished
I0127 04:03:56.761953       1 data-processor.go:253] Validating image
E0127 04:03:56.766226       1 prlimit.go:156] failed to kill the process; os: process already finished
I0127 04:03:56.769261       1 data-processor.go:247] New phase: Complete
I0127 04:03:56.769498       1 importer.go:231] {"scratchSpaceRequired":false,"preallocationApplied":false,"message":"Import Complete"}

# データのインポートが終了するとimporter pod も終了

[cent@ctrl ~]$
kubectl get pods

No resources found in default namespace.
# 仮想マシン作成

[cent@ctrl ~]$
vi fedora-vm.yml
apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
  name: fedora41
  labels:
    kubevirt.io/os: linux
spec:
  runStrategy: Halted
  template:
    spec:
      domain:
        cpu:
          cores: 2
        devices:
          disks:
          - disk:
              bus: virtio
            name: disk0
          - cdrom:
              bus: sata
              readonly: true
            name: cloudinitdisk
          interfaces:
          - name: default
            masquerade: {}
        machine:
          type: q35
        resources:
          requests:
            memory: 4096M
      networks:
      - name: default
        pod: {}
      volumes:
      - name: disk0
        persistentVolumeClaim:
          claimName: fedora-pvc
      - cloudInitNoCloud:
          userData: |
            #cloud-config
            hostname: fedora41
            ssh_pwauth: true
            disable_root: false
            chpasswd:
              list: |
                root:myrootpassword
                fedora:userpassword
              expire: False
        name: cloudinitdisk

[cent@ctrl ~]$
kubectl apply -f fedora-vm.yml

virtualmachine.kubevirt.io/fedora41 created
[cent@ctrl ~]$
kubectl get vms

NAME       AGE   STATUS    READY
fedora41   33s   Stopped   False

[cent@ctrl ~]$ virtctl start fedora41 
VM fedora41 was scheduled to start

[cent@ctrl ~]$
kubectl get vmi

NAME       AGE   PHASE     IP               NODENAME           READY
fedora41   24s   Running   192.168.40.207   node01.srv.world   True

[cent@ctrl ~]$ virtctl console fedora41 
Successfully connected to fedora41 console. The escape sequence is ^]

fedora41 login: root
Password:
[root@fedora41 ~]#
# ホストのコンソールに戻るには Ctrl + ] キー
# * virsh コマンドと同じ操作


# ssh で接続
[cent@ctrl ~]$ kubectl get pods 
NAME                           READY   STATUS    RESTARTS   AGE
virt-launcher-fedora41-8bgpp   2/2     Running   0          106s

[cent@ctrl ~]$ kubectl port-forward pod/virt-launcher-fedora41-8bgpp 2220:22 & 

[cent@ctrl ~]$ ssh fedora@127.0.0.1 -p 2220 
Handling connection for 2220
The authenticity of host '[127.0.0.1]:2220 ([127.0.0.1]:2220)' can't be established.
ED25519 key fingerprint is SHA256:GLKGVtlYXlSk3YPsQF3wRaENGBmg4+mcC0g4A1oC8A4.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[127.0.0.1]:2220' (ED25519) to the list of known hosts.
fedora@127.0.0.1's password:
[fedora@fedora41 ~]$
関連コンテンツ