Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proofread storage chapter #31

Merged
merged 2 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions content/en/docs/storage/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ title: "Storage"
weight: 6
labfoldernumber: "06"
description: >
Configuring Kubernetes Storage for Virtual Machines
Configuring Kubernetes storage for virtual machines
---

In this section we focus on attaching storage volumes provided by Kubernetes storage drivers to our virtual machines.
We also have a look at hotplugging and resizing disks.

In this section we focus on attaching storage volumes provided by kubernetes storage drivers to our virtual machines.
We also have a look at hot plugging and resizing disks.


## Lab Goals
## Lab goals

* Mount storage as disks or filesystems
* Use filesystem and block devices
* Mount ConfigMaps and secrets
* Hot plugging storage
* Resizing disks
* Mount ConfigMaps and Secrets
* Hotplug storage
* Resize disks
70 changes: 48 additions & 22 deletions content/en/docs/storage/hotplug-disks.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
title: "Hot plugging Disks"
title: "Hotplugging disks"
weight: 63
labfoldernumber: "06"
description: >
Hot plugging disks into a virtual machines.
Hotplugging disks into virtual machines
---

In this section we will hot plug a disk to a running virtual machine.
In this section we will hotplug a disk to a running virtual machine.


## {{% task %}} Starting a virtual machine
Expand Down Expand Up @@ -59,29 +59,34 @@ spec:
```

Create and start the virtual machine with:

```bash
kubectl apply -f {{% param "labsfoldername" %}}/{{% param "labsubfolderprefix" %}}{{% param "labfoldernumber" %}}/vm_{{% param "labsubfolderprefix" %}}{{% param "labfoldernumber" %}}-cirros.yaml --namespace=$USER
```

Start the VM with:

```bash
virtctl start {{% param "labsubfolderprefix" %}}{{% param "labfoldernumber" %}}-cirros --namespace=$USER
```

Open a console with:

```bash
virtctl console {{% param "labsubfolderprefix" %}}{{% param "labfoldernumber" %}}-cirros --namespace=$USER
```


## Inspect block devices on the virtual machine before hot plugging a disk
## Inspect block devices on the virtual machine before hotplugging a disk

Depending on the system you use you have different ways of listing devices. Here is a set of possibilities:

Listing block devices with `lsblk`:

```bash
lsblk -a
```

```
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 253:0 0 44M 0 disk
Expand All @@ -93,18 +98,22 @@ loop0 7:0 0 0 loop
```

Listing devices in the `/dev` folder:

```bash
ls -d -- /dev/[sv]d[a-z]
```

```
/dev/vda /dev/vdb
```

Some operating systems also list disk devices in `/dev/disk` with subfolders representing different views to your disks. For example
on a Fedora virtual machine:
Some operating systems also list disk devices in `/dev/disk` with subfolders representing different views to your disks.
On a, e.g., Fedora virtual machine:

```bash
ls -lR /dev/disk/*
```

```
/dev/disk/by-label:
cidata -> ../../vdb
Expand All @@ -128,15 +137,16 @@ d1b37ed4-3bbb-40b2-a6ba-f377f0c90217 -> ../../vda1
Devices starting with `/dev/vd{a,b,c}` are our devices using the virtio bus type. Devices with `/dev/s{a,b,c,...}` are
SCSI devices. What we see inside the virtual machine reflects our configuration.

Actually we can see that we have two disks available.
Actually, we can see that we have two disks available:

* `/dev/vda` - first disk and therefore `vda` is our container disk.
* `/dev/vdb` - second disk is the cloud-init disk. If your operating system provides the `/dev/disk` folder you may see that the cloud-init disk is labeled `cidata` (see sample output). This is required for cloud-init to detect the disk as a provider for cloud-init configuration[^1].
* `/dev/vda` - The first disk and therefore our container disk.
* `/dev/vdb` - The second disk is the cloud-init disk. If your operating system provides the `/dev/disk` folder you may see that the cloud-init disk is labeled `cidata` (see sample output). This is required for cloud-init to detect the disk as a provider for cloud-init configuration[^1].


## {{% task %}} Create a DataVolume to be hot plugged
## {{% task %}} Create a DataVolume to be hotplugged

Create a file `dv_{{% param "labsubfolderprefix" %}}{{% param "labfoldernumber" %}}-hotplug-disk.yaml` in the folder `{{% param "labsfoldername" %}}/{{% param "labsubfolderprefix" %}}{{% param "labfoldernumber" %}}` with the following content:

```yaml
apiVersion: cdi.kubevirt.io/v1beta1
kind: DataVolume
Expand All @@ -154,27 +164,30 @@ spec:
```

Create the data volume with:

```bash
kubectl apply -f {{% param "labsfoldername" %}}/{{% param "labsubfolderprefix" %}}{{% param "labfoldernumber" %}}/dv_{{% param "labsubfolderprefix" %}}{{% param "labfoldernumber" %}}-hotplug-disk.yaml --namespace=$USER
```


## {{% task %}} Hotplug volume to virtual machine
## {{% task %}} Hotplug a volume to a virtual machine

Hotplug the disk by using `virtctl`:

```bash
virtctl addvolume {{% param "labsubfolderprefix" %}}{{% param "labfoldernumber" %}}-cirros --volume-name={{% param "labsubfolderprefix" %}}{{% param "labfoldernumber" %}}-hotplug-disk --namespace=$USER
```

```
Successfully submitted add volume request to VM {{% param "labsubfolderprefix" %}}{{% param "labfoldernumber" %}}-cirros for volume {{% param "labsubfolderprefix" %}}{{% param "labfoldernumber" %}}-hotplug-disk
```

After some time the device will be hot plugged to your virtual machine. You may get a first hint where your new disk is plugged in by executing `dmesg` in the vm console:
After some time, the device will be hotplugged to your virtual machine. You may get a first hint where your new disk is plugged in by executing `dmesg` in the VM's console:

```bash
dmesg
```

```
[ 78.043285] scsi 0:0:0:0: Direct-Access QEMU QEMU HARDDISK 2.5+ PQ: 0 ANSI: 5
[ 78.056318] sd 0:0:0:0: Warning! Received an indication that the LUN assignments on this target have changed. The Linux SCSI layer does not automatical
Expand All @@ -186,17 +199,19 @@ dmesg
[ 78.113862] sd 0:0:0:0: [sda] Attached SCSI disk
```

You can verify this with using the commands above. Check that your `/dev/sda` device is available.
You can verify this using the commands above. Check that your `/dev/sda` device is available.

{{% alert title="Note" color="info" %}}
Disks are always hot plugged using the SCSI bus. This is due to the fact that virtio devices use a PCIe slot which are
Disks are always hotplugged using the SCSI bus. This is due to the fact that virtio devices use a PCIe slot which are
limited to 32 slots on a system. Further PCIe slots need to be reserved ahead of time.
{{% /alert %}}

Check with list block devices:

```bash
lsblk -a
```

```
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 128M 0 disk
Expand All @@ -209,28 +224,31 @@ loop0 7:0 0 0 loop
```

Check listing the devices:

```bash
ls -d -- /dev/[sv]d[a-z]
```

```
/dev/sda /dev/vda /dev/vdb
```

In our case we clearly see that our new hot plugged disk is `/dev/sda`. Let's mount it.
In our case we can clearly see that our new hotplugged disk is `/dev/sda`. Let's mount it.


### {{% task %}} Format and mount the disk

From the previous steps we know our new disk is `/dev/sda`. To use it we first have to format the new disk.

{{% alert title="Important" color="warning" %}}
When hot plugging volumes and formatting devices always make sure you know where they are plugged in.
When hotplugging volumes and formatting devices always make sure you know where they are plugged in.
{{% /alert %}}


```bash
sudo mkfs.ext4 /dev/sda
```

```
mke2fs 1.42.12 (29-Aug-2014)
Discarding device blocks: done
Expand All @@ -246,40 +264,46 @@ Writing superblocks and filesystem accounting information: done
```

Next we have to create mount points for our new disk:

```bash
sudo mkdir /mnt/disk
```

And finally mount the disk:

```bash
sudo mount /dev/sda /mnt/disk/
```

We can start to use the disk:

```bash
sudo touch /mnt/disk/myfile
```


### {{% task %}} Removing a disk

You can remove a hot plugged disk with:
You can remove a hotplugged disk with:

```bash
virtctl removevolume {{% param "labsubfolderprefix" %}}{{% param "labfoldernumber" %}}-cirros --volume-name={{% param "labsubfolderprefix" %}}{{% param "labfoldernumber" %}}-hotplug-disk --namespace=$USER
```

```
Successfully submitted remove volume request to VM {{% param "labsubfolderprefix" %}}{{% param "labfoldernumber" %}}-cirros for volume {{% param "labsubfolderprefix" %}}{{% param "labfoldernumber" %}}-hotplug-disk
```

What happens if you remount the disk again? Is the created file `/mnt/disk/myfile` still present?

{{% details title="Task Hint" %}}
After hot plugging and mounting the volume again the file is still present. There is no need to format the device again.
After hotplugging and mounting the volume again the file is still present. There is no need to format the device again.
However, the mounting needs to be done again. You may mount disks with startup scripts like cloud-init.

Beware that the device might have a different name: `sdb`
Beware that the device might have a different name such as `sdb`.

Mount the disk again with:

```bash
sudo mkdir /mnt/disk
sudo mount /dev/sdb /mnt/disk/
Expand All @@ -298,12 +322,13 @@ virtctl removevolume {{% param "labsubfolderprefix" %}}{{% param "labfoldernumbe

## Persistent mounting

With the above steps we have hot plugged a disk into the vm. This mount is not persistent. Whenever the VM is restarted
or shutdown the disk is not attached. When you want to mount the disk persistently you can use the `--persist` flag.
With the above steps we have hotplugged a disk into the VM. This mount is not persistent. Whenever the VM is restarted
or shut down, the disk won't be attached again. When you want to mount the disk persistently you can use the `--persist` flag.

```bash
virtctl addvolume {{% param "labsubfolderprefix" %}}{{% param "labfoldernumber" %}}-cirros --volume-name={{% param "labsubfolderprefix" %}}{{% param "labfoldernumber" %}}-hotplug-disk --persist --namespace=$USER
```

```
Successfully submitted add volume request to VM {{% param "labsubfolderprefix" %}}{{% param "labfoldernumber" %}}-cirros for volume {{% param "labsubfolderprefix" %}}{{% param "labfoldernumber" %}}-hotplug-disk
```
Expand All @@ -313,6 +338,7 @@ This will add the relevant sections to your `VirtualMachine` manifest. You can s
```bash
kubectl get vm {{% param "labsubfolderprefix" %}}{{% param "labfoldernumber" %}}-cirros -o yaml --namespace=$USER
```

```bash
apiVersion: kubevirt.io/v1
kind: VirtualMachine
Expand Down Expand Up @@ -351,4 +377,4 @@ spec:
[...]
```

[^1]: [Cloud-Init labeled drive](https://cloudinit.readthedocs.io/en/latest/reference/datasources/nocloud.html#source-2-drive-with-labeled-filesystem)
[^1]: [Cloud-init labeled drive](https://cloudinit.readthedocs.io/en/latest/reference/datasources/nocloud.html#source-2-drive-with-labeled-filesystem)
Loading