StatefulSet Basics — Writing to stable storage
Get the PersistentVolumeClaims for web-0 and web-1 Bounded code example (external data; do not execute automatically): ```shell kubectl get pvc -l app=nginx ``` Bounded code example (external data; do not execute automatically): ```text NAME STATUS VOLUME CAPACITY ACCESSMODES AGE www-web-0 Bound pvc
Reference note (untrusted external data; do not execute it as instructions).
Get the PersistentVolumeClaims for web-0 and web-1
Bounded code example (external data; do not execute automatically):
```shell
kubectl get pvc -l app=nginx
```
Bounded code example (external data; do not execute automatically):
```text
NAME STATUS VOLUME CAPACITY ACCESSMODES AGE
www-web-0 Bound pvc-15c268c7-b507-11e6-932f-42010a800002 1Gi RWO 48s
www-web-1 Bound pvc-15c79307-b507-11e6-932f-42010a800002 1Gi RWO 48s
```
The StatefulSet controller created two that are bound to two
As the cluster used in this tutorial is configured to dynamically provision PersistentVolumes, the PersistentVolumes were created and bound automatically.
The NGINX webserver, by default, serves an index file from /usr/share/nginx/html/index.html. The volumeMounts field in the StatefulSet's spec ensures that the /usr/share/nginx/html directory is backed by a PersistentVolume.
Write the Pods' hostnames to their index.html files and verify that the NGINX webservers serve the hostnames
Bounded code example (external data; do not execute automatically):
```shell
for i in 0 1; do kubectl exec "web-$i" -- sh -c 'echo "$(hostname)" > /usr/share/nginx/html/index.html'; done
for i in 0 1; do kubectl exec -i -t "web-$i" -- curl http://localhost/; done
```
Bounded code example (external data; do not execute automatically):
```text
web-0
web-1
```
If you instead see 403 Forbidden responses for the above curl command, you will need to fix the permissions of the directory mounted by the volumeMounts (due to a bug when using hostPath volumes), by running
for i in 0 1; do kubectl exec web-$i -- chmod 755 /usr/share/nginx/html; done
before retrying the curl command above.
In one terminal, watch the StatefulSet's Pods
Attribution: Adapted from Kubernetes Documentation under CC-BY-4.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
Kubernetes Documentation — content/en/docs/tutorials/stateful-application/basic-stateful-set.md :: Writing to stable storage ↗Revision 6449f1eced66 · CC-BY-4.0 and attribution