Ubuntu 24.04
Sponsored Link

Kubernetes : 仮想マシン作成2024/11/18

 

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], [4] の通り、Control Plane ノード上に NFS サーバーを起動し、[/home/nfsshare] ディレクトリを共有ディレクトリとして設定して、動的プロビジョニングできるよう NFS subdir external provisioner を設定して進めます。

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

root@ctrl:~#
export VERSION=$(echo ${TAG##*/})

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

root@ctrl:~#
wget https://github.com/kubevirt/containerized-data-importer/releases/download/${VERSION}/cdi-cr.yaml
root@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

root@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

root@ctrl:~#
kubectl apply -f cdi-cr.yaml

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

root@ctrl:~#
kubectl get pods -n cdi

NAME                               READY   STATUS    RESTARTS   AGE
cdi-apiserver-555ccd5f7b-nkf96     1/1     Running   0          30s
cdi-deployment-8bf6546cc-xhhrg     1/1     Running   0          30s
cdi-operator-659fd5d79-g49j6       1/1     Running   0          42s
cdi-uploadproxy-6dcd6d454b-t985r   1/1     Running   0          30s
[3] 仮想マシンを作成します。当例では Ubuntu 24.04 で作成します。
root@ctrl:~#
kubectl get sc

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

# PVC 作成

root@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

root@ctrl:~#
kubectl apply -f ubuntu-pvc.yml

persistentvolumeclaim/ubuntu-pvc created
root@ctrl:~#
kubectl get pvc

NAME         STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   VOLUMEATTRIBUTESCLASS   AGE
ubuntu-pvc   Bound    pvc-7eb289d4-d9c9-4f94-8043-5b6e29c421a4   15Gi       RWO            nfs-client     <unset>                 5s

root@ctrl:~#
kubectl get pods

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

# 進行状況をログで確認可

root@ctrl:~#
kubectl logs -f importer-ubuntu-pvc

.....
.....
I1118 02:34:00.808524       1 data-processor.go:341] Expanding image size to: 15220080640
E1118 02:34:00.840043       1 prlimit.go:156] failed to kill the process; os: process already finished
I1118 02:34:00.840066       1 data-processor.go:253] Validating image
E1118 02:34:00.844044       1 prlimit.go:156] failed to kill the process; os: process already finished
I1118 02:34:00.853015       1 data-processor.go:247] New phase: Complete
I1118 02:34:00.853219       1 importer.go:231] {"scratchSpaceRequired":false,"preallocationApplied":false,"message":"Import Complete"}

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

root@ctrl:~#
kubectl get pods

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

root@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

root@ctrl:~#
kubectl apply -f ubuntu-vm.yml

virtualmachine.kubevirt.io/ubuntu2404 created
root@ctrl:~#
kubectl get vms

NAME         AGE   STATUS    READY
ubuntu2404   19s   Stopped   False

root@ctrl:~# virtctl start ubuntu2404 
VM ubuntu2404 was scheduled to start

root@ctrl:~#
kubectl get vmi

NAME         AGE   PHASE     IP               NODENAME           READY
ubuntu2404   22s   Running   192.168.40.232   node01.srv.world   True

root@ctrl:~# virtctl console ubuntu2404 
Successfully connected to ubuntu2404 console. The escape sequence is ^]

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

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

 System information as of Mon Nov 18 02:44:16 UTC 2024

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


# ssh で接続
root@ctrl:~# kubectl get pods 
NAME                             READY   STATUS    RESTARTS   AGE
virt-launcher-ubuntu2404-mg4gs   2/2     Running   0          101s

root@ctrl:~# kubectl port-forward pod/virt-launcher-ubuntu2404-mg4gs 222:22 & 

root@ctrl:~# ssh ubuntu@127.0.0.1 -p 222 
Handling connection for 222
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.1 LTS (GNU/Linux 6.8.0-48-generic x86_64)

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

 System information as of Mon Nov 18 05:25:15 UTC 2024

.....
.....

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