Skip to content

Commit

Permalink
improve pv storage task
Browse files Browse the repository at this point in the history
Co-authored-by: Tim Bannister <[email protected]>
  • Loading branch information
network-charles and sftim committed Dec 17, 2024
1 parent ce8a338 commit be8da21
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ Create the Pod:
kubectl apply -f https://k8s.io/examples/pods/storage/pv-pod.yaml
```

Verify that the container in the Pod is running;
Verify that the container in the Pod is running:

```shell
kubectl get pod task-pv-pod
Expand Down Expand Up @@ -226,10 +226,166 @@ use storage from a PersistentVolumeClaim.

## Clean up

Delete the Pod, the PersistentVolumeClaim and the PersistentVolume:
Delete the Pod:

```shell
kubectl delete pod task-pv-pod
```

## Mounting the same PersistentVolume in two places

You have understood how to create a PersistentVolume & PersistentVolumeClaim, and how to mount
the volume to a single location in a container. Let's explore how you can mount the same PersistentVolume
at two different locations in a container. Below is an example:

{{% code_sample file="pods/storage/pv-duplicate.yaml" %}}

Here:

- `subPath`: This field allows specific files or directories from the mounted PersistentVolume to be exposed at
different locations within the container. In this example:
- `subPath: html` mounts the html directory.
- `subPath: nginx.conf` mounts a specific file, nginx.conf.

Since the first subPath is `html`, this means that a `html` directory has to be created within `/mnt/data/`
on the node.

The second subPath `nginx.conf` means that a file within the `/mnt/data/` directory will be used. No other directory
need to be created.

Two volume mounts will be made on your nginx container:

- `/usr/share/nginx/html` for the static website
- `/etc/nginx/nginx.conf` for the default config

### Move the index.html file on your Node to a new folder

Open a shell to the single Node in your cluster. How you open a shell depends on how you set up your cluster.
For example, if you are using Minikube, you can open a shell to your Node by entering `minikube ssh`.

Create a `/mnt/data/html` directory:

```shell
# This assumes that your Node uses "sudo" to run commands
# as the superuser
sudo mkdir /mnt/data/html
```

Move index.html into the directory:

```shell
# Move index.html from its current location to the html sub-directory
mv /mnt/data/index.html html
```

### Create a new nginx.conf file

{{% code_sample file="pods/storage/nginx.conf" %}}

This is a modified version of the default `nginx.conf` file. Here, the default `keepalive_timeout` has been
modified to `60`

Create the nginx.conf file:

```shell
cat <<EOF > /mnt/data/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '\$remote_addr - \$remote_user [\$time_local] "\$request" '
'\$status \$body_bytes_sent "\$http_referer" '
'"\$http_user_agent" "\$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 60;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
EOF
```

### Create a Pod

Here we will be creating a pod that uses the existing persistentVolume and persistentVolumeClaim.
However, the pod mounts only a specific file, `nginx.conf`, and directory, `html`, to the container.

Create the Pod:

```shell
kubectl apply -f https://k8s.io/examples/pods/storage/pv-duplicate.yaml
```

Verify that the container in the Pod is running:

```shell
kubectl get pod test
```

Get a shell to the container running in your Pod:

```shell
kubectl exec -it test -- /bin/bash
```

In your shell, verify that nginx is serving the `index.html` file from the
hostPath volume:

```shell
# Be sure to run these 3 commands inside the root shell that comes from
# running "kubectl exec" in the previous step
apt update
apt install curl
curl http://localhost/
```

The output shows the text that you wrote to the `index.html` file on the
hostPath volume:

```
Hello from Kubernetes storage
```

In your shell, also verify that nginx is serving the `nginx.conf` file from the
hostPath volume:

```shell
# Be sure to run this commands inside the root shell that comes from
# running "kubectl exec" in the previous step
cat /etc/nginx/nginx.conf | grep keepalive_timeout
```

The output shows the modified text that you wrote to the `nginx.conf` file on the
hostPath volume:

```
keepalive_timeout 60;
```

If you see these messages, you have successfully configured a Pod to
use a specific file and directory in a storage from a PersistentVolumeClaim.

## Clean up

Delete the Pod:

```shell
kubectl delete pod test
kubectl delete pvc task-pv-claim
kubectl delete pv task-pv-volume
```
Expand All @@ -242,21 +398,13 @@ In the shell on your Node, remove the file and directory that you created:
```shell
# This assumes that your Node uses "sudo" to run commands
# as the superuser
sudo rm /mnt/data/index.html
sudo rm /mnt/data/html/index.html
sudo rm /mnt/data/nginx.conf
sudo rmdir /mnt/data
```

You can now close the shell to your Node.

## Mounting the same persistentVolume in two places

{{% code_sample file="pods/storage/pv-duplicate.yaml" %}}

You can perform 2 volume mounts on your nginx container:

- `/usr/share/nginx/html` for the static website
- `/etc/nginx/nginx.conf` for the default config

<!-- discussion -->

## Access control
Expand Down Expand Up @@ -299,4 +447,4 @@ PersistentVolume are not present on the Pod resource itself.
* [PersistentVolume](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#persistentvolume-v1-core)
* [PersistentVolumeSpec](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#persistentvolumespec-v1-core)
* [PersistentVolumeClaim](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#persistentvolumeclaim-v1-core)
* [PersistentVolumeClaimSpec](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#persistentvolumeclaimspec-v1-core)
* [PersistentVolumeClaimSpec](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#persistentvolumeclaimspec-v1-core)
31 changes: 31 additions & 0 deletions content/en/examples/pods/storage/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
user nginx;
worker_processes auto;

error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;


events {
worker_connections 1024;
}


http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;
#tcp_nopush on;

keepalive_timeout 60;

#gzip on;

include /etc/nginx/conf.d/*.conf;
}
2 changes: 1 addition & 1 deletion content/en/examples/pods/storage/pv-duplicate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ spec:
volumes:
- name: config
persistentVolumeClaim:
claimName: test-nfs-claim
claimName: task-pv-storage

0 comments on commit be8da21

Please sign in to comment.