Skip to content

Instantly share code, notes, and snippets.

@usrbinkat
Last active December 22, 2021 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save usrbinkat/a457c96a50e325200fd59ed681a0242d to your computer and use it in GitHub Desktop.
Save usrbinkat/a457c96a50e325200fd59ed681a0242d to your computer and use it in GitHub Desktop.
kind-kubevirt-testvm-ssh

How to test Kubevirt VM SSH via NodePort

Prerequisites:

1. Create Kind Cluster

cat <<EOF > /tmp/kind.yml && kind create cluster --config /tmp/kind.yml
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
name: kubevirt-ssh-testing
networking:
  apiServerAddress: "127.0.0.1"
  apiServerPort: 6443
nodes:
  - role: control-plane
    image: kindest/node:v1.23.0
    extraPortMappings:
    - containerPort: 30950
      hostPort: 30950
EOF

2. Deploy Kubevirt v0.48.1

kubectl create -f https://github.com/kubevirt/kubevirt/releases/download/v0.48.1/kubevirt-operator.yaml
kubectl create -f https://github.com/kubevirt/kubevirt/releases/download/v0.48.1/kubevirt-cr.yaml
  • Wait for deployment status Done (may take a few minutes)
watch kubectl get kubevirt.kubevirt.io/kubevirt -n kubevirt -o=jsonpath="{.status.phase}"

3. Deploy a Fedora VM & SSH Service with correct labeling

cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Service
metadata:
  name: testvm-ssh
spec:
  ports:
  - nodePort: 30950
    port: 30950
    protocol: TCP
    targetPort: 22
  selector:
    test: kmi
  type: NodePort
---
apiVersion: kubevirt.io/v1alpha3
kind: VirtualMachine
metadata:
  name: testvm
  labels:
    test: kmi
spec:
  running: true
  template:
    metadata:
      labels:
        test: kmi
    spec:
      domain:
        devices:
          autoattachPodInterface: true
          autoattachSerialConsole: true
          autoattachGraphicsDevice: true
          disks:
            - name: containerdisk
              bootOrder: 1
              disk:
                bus: virtio
            - name: cloudinitdisk
              disk:
                bus: virtio
        resources:
          limits:
            memory: 2G
          requests:
            memory: 2G
      volumes:
        - name: containerdisk
          containerDisk:
            image: quay.io/containerdisks/fedora:35
            imagePullPolicy: IfNotPresent
        - name: cloudinitdisk
          cloudInitNoCloud:
            userData: |
              #cloud-config
              chpasswd:
                expire: False
                list: |
                   fedora:fedora
              users:
                - name: fedora
                  shell: /bin/bash
                  lock_passwd: false
                  groups: sudo,wheel
                  sudo: ['ALL=(ALL) NOPASSWD:ALL']
                  ssh-authorized-keys:
                    - $(cat ~/.ssh/id_rsa.pub)
EOF
  • Wait for testvm state "running"
watch kubectl get vmi testvm -o=jsonpath="{.status.phase}"

4. Observe Fedora TestVM Serial and or VNC Console during boot

  • You can login to this vm with user:pass fedora:fedora
  • Exit the serial console with ctrl+shift+]
virtctl console testvm
virtctl vnc testvm

5. SSH into Fedora TestVM via NodePort exposed from Kind to 127.0.0.1

ssh -p 30950 fedora@127.0.0.1

Additional Notes:

  • Troubleshooting Commands
kubectl describe vm testvm
kubectl describe vmi testvm
kubectl get vmi testvm -owide
kubectl describe svc testvm-ssh
  • Run using emulation instead of hardware virtualization
kubectl create configmap kubevirt-config -n kubevirt --from-literal debug.useEmulation=true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment