Merge pull request #49130 from network-charles/network-charles-patch-2

Improve PV Storage Task
pull/50509/head
Kubernetes Prow Robot 2025-04-15 19:57:05 -07:00 committed by GitHub
commit 51588d5f21
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 195 additions and 14 deletions

View File

@ -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
@ -226,10 +226,168 @@ 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`, an `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
needs 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
The `index.html` file mentioned here refers to the one created in the "[Create an index.html file on your Node](#create-an-index-html-file-on-your-node)" section.
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
sudo 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 create 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 these 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
```
@ -242,21 +400,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
@ -299,4 +449,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)

View File

@ -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;
}

View File

@ -19,4 +19,4 @@ spec:
volumes:
- name: config
persistentVolumeClaim:
claimName: test-nfs-claim
claimName: task-pv-storage