CentOS Stream 9
Sponsored Link

Kubernetes : 仮想マシン作成2025/05/20

 

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-6c76687b66-fx6jq    1/1     Running   1 (25s ago)   55s
cdi-deployment-5f6ff949d7-pjx8c   1/1     Running   0             55s
cdi-operator-ccb895984-hnnls      1/1     Running   0             76s
cdi-uploadproxy-b499c7956-wcfrg   1/1     Running   0             55s
[3] 仮想マシンを作成します。当例では Ubuntu 24.04 で作成します。
[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 ubuntu-pvc.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: "ubuntu-pvc"
  labels:
    app: containerized-data-importer
  annotations:
    cdi.kubevirt.io/storage.import.endpoint: "http://cloud-images.ubuntu.com/releases/24.04/release/ubuntu-24.04-server-cloudimg-amd64.img"
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 15Gi
  storageClassName: nfs-client

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

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

NAME         STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   VOLUMEATTRIBUTESCLASS   AGE
ubuntu-pvc   Bound    pvc-91489452-5712-4a35-986a-7385e130a0dc   15Gi       RWO            nfs-client     <unset>                 5s

[cent@ctrl ~]$
kubectl get pods

NAME                  READY   STATUS    RESTARTS   AGE
importer-ubuntu-pvc   1/1     Running   0          3s

# possible to see importing logs

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

.....
.....
I0520 05:27:44.437641       1 data-processor.go:341] Expanding image size to: 15220080640
E0520 05:27:44.445445       1 prlimit.go:156] failed to kill the process; os: process already finished
I0520 05:27:44.445500       1 data-processor.go:253] Validating image
E0520 05:27:44.449675       1 prlimit.go:156] failed to kill the process; os: process already finished
I0520 05:27:44.452912       1 data-processor.go:247] New phase: Complete
I0520 05:27:44.453080       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 ubuntu-vm.yml
apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
  name: ubuntu2404
  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: ubuntu-pvc
      - cloudInitNoCloud:
          userData: |
            #cloud-config
            hostname: ubuntu2404
            ssh_pwauth: true
            disable_root: false
            chpasswd:
              list: |
                root:myrootpassword
                ubuntu:userpassword
              expire: False
        name: cloudinitdisk

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

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

NAME         AGE   STATUS    READY
ubuntu2404   10s   Stopped   False

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

[cent@ctrl ~]$
kubectl get vmi

NAME         AGE   PHASE     IP               NODENAME           READY
ubuntu2404   18s   Running   192.168.40.215   node01.srv.world   True

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

ubuntu2404 login: root
Password:
Welcome to Ubuntu 24.04.2 LTS (GNU/Linux 6.8.0-60-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/pro

.....
.....

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


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

[cent@ctrl ~]$ kubectl port-forward pod/virt-launcher-ubuntu2404-vssn2 2220:22 & 

[cent@ctrl ~]$ ssh ubuntu@127.0.0.1 -p 2220 
Handling connection for 2220
The authenticity of host '[127.0.0.1]:222 ([127.0.0.1]:222)' can't be established.
ED25519 key fingerprint is SHA256:W/jCnIP+PoghhP3FOrVDa9HBGjOzip+QVBqGEep5roM.
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]:222' (ED25519) to the list of known hosts.
ubuntu@127.0.0.1's password:
Welcome to Ubuntu 24.04.2 LTS (GNU/Linux 6.8.0-60-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/pro

.....
.....

ubuntu@ubuntu2404:~$
関連コンテンツ