Tweak line wrapings and fix indentations in mysql-wordpress-persistent-volume.md

This commit is contained in:
Michael 2023-04-04 22:36:29 +08:00
parent 8c90131eaf
commit 61a91632dc
1 changed files with 123 additions and 104 deletions

View File

@ -11,20 +11,31 @@ card:
--- ---
<!-- overview --> <!-- overview -->
This tutorial shows you how to deploy a WordPress site and a MySQL database using Minikube. Both applications use PersistentVolumes and PersistentVolumeClaims to store data. This tutorial shows you how to deploy a WordPress site and a MySQL database using
Minikube. Both applications use PersistentVolumes and PersistentVolumeClaims to store data.
A [PersistentVolume](/docs/concepts/storage/persistent-volumes/) (PV) is a piece of storage in the cluster that has been manually provisioned by an administrator, or dynamically provisioned by Kubernetes using a [StorageClass](/docs/concepts/storage/storage-classes). A [PersistentVolumeClaim](/docs/concepts/storage/persistent-volumes/#persistentvolumeclaims) (PVC) is a request for storage by a user that can be fulfilled by a PV. PersistentVolumes and PersistentVolumeClaims are independent from Pod lifecycles and preserve data through restarting, rescheduling, and even deleting Pods. A [PersistentVolume](/docs/concepts/storage/persistent-volumes/) (PV) is a piece
of storage in the cluster that has been manually provisioned by an administrator,
or dynamically provisioned by Kubernetes using a [StorageClass](/docs/concepts/storage/storage-classes).
A [PersistentVolumeClaim](/docs/concepts/storage/persistent-volumes/#persistentvolumeclaims) (PVC)
is a request for storage by a user that can be fulfilled by a PV. PersistentVolumes and
PersistentVolumeClaims are independent from Pod lifecycles and preserve data through
restarting, rescheduling, and even deleting Pods.
{{< warning >}} {{< warning >}}
This deployment is not suitable for production use cases, as it uses single instance WordPress and MySQL Pods. Consider using [WordPress Helm Chart](https://github.com/bitnami/charts/tree/master/bitnami/wordpress) to deploy WordPress in production. This deployment is not suitable for production use cases, as it uses single instance
WordPress and MySQL Pods. Consider using
[WordPress Helm Chart](https://github.com/bitnami/charts/tree/master/bitnami/wordpress)
to deploy WordPress in production.
{{< /warning >}} {{< /warning >}}
{{< note >}} {{< note >}}
The files provided in this tutorial are using GA Deployment APIs and are specific to kubernetes version 1.9 and later. If you wish to use this tutorial with an earlier version of Kubernetes, please update the API version appropriately, or reference earlier versions of this tutorial. The files provided in this tutorial are using GA Deployment APIs and are specific
to kubernetes version 1.9 and later. If you wish to use this tutorial with an earlier
version of Kubernetes, please update the API version appropriately, or reference
earlier versions of this tutorial.
{{< /note >}} {{< /note >}}
## {{% heading "objectives" %}} ## {{% heading "objectives" %}}
* Create PersistentVolumeClaims and PersistentVolumes * Create PersistentVolumeClaims and PersistentVolumes
@ -35,12 +46,10 @@ The files provided in this tutorial are using GA Deployment APIs and are specifi
* Apply the kustomization directory by `kubectl apply -k ./` * Apply the kustomization directory by `kubectl apply -k ./`
* Clean up * Clean up
## {{% heading "prerequisites" %}} ## {{% heading "prerequisites" %}}
{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}} {{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
The example shown on this page works with `kubectl` 1.14 and above. The example shown on this page works with `kubectl` 1.14 and above.
Download the following configuration files: Download the following configuration files:
@ -49,36 +58,49 @@ Download the following configuration files:
1. [wordpress-deployment.yaml](/examples/application/wordpress/wordpress-deployment.yaml) 1. [wordpress-deployment.yaml](/examples/application/wordpress/wordpress-deployment.yaml)
<!-- lessoncontent -->
<!-- lessoncontent -->
## Create PersistentVolumeClaims and PersistentVolumes ## Create PersistentVolumeClaims and PersistentVolumes
MySQL and Wordpress each require a PersistentVolume to store data. Their PersistentVolumeClaims will be created at the deployment step. MySQL and Wordpress each require a PersistentVolume to store data.
Their PersistentVolumeClaims will be created at the deployment step.
Many cluster environments have a default StorageClass installed. When a StorageClass is not specified in the PersistentVolumeClaim, the cluster's default StorageClass is used instead. Many cluster environments have a default StorageClass installed.
When a StorageClass is not specified in the PersistentVolumeClaim,
the cluster's default StorageClass is used instead.
When a PersistentVolumeClaim is created, a PersistentVolume is dynamically provisioned based on the StorageClass configuration. When a PersistentVolumeClaim is created, a PersistentVolume is dynamically
provisioned based on the StorageClass configuration.
{{< warning >}} {{< warning >}}
In local clusters, the default StorageClass uses the `hostPath` provisioner. `hostPath` volumes are only suitable for development and testing. With `hostPath` volumes, your data lives in `/tmp` on the node the Pod is scheduled onto and does not move between nodes. If a Pod dies and gets scheduled to another node in the cluster, or the node is rebooted, the data is lost. In local clusters, the default StorageClass uses the `hostPath` provisioner.
`hostPath` volumes are only suitable for development and testing. With `hostPath`
volumes, your data lives in `/tmp` on the node the Pod is scheduled onto and does
not move between nodes. If a Pod dies and gets scheduled to another node in the
cluster, or the node is rebooted, the data is lost.
{{< /warning >}} {{< /warning >}}
{{< note >}} {{< note >}}
If you are bringing up a cluster that needs to use the `hostPath` provisioner, the `--enable-hostpath-provisioner` flag must be set in the `controller-manager` component. If you are bringing up a cluster that needs to use the `hostPath` provisioner,
the `--enable-hostpath-provisioner` flag must be set in the `controller-manager` component.
{{< /note >}} {{< /note >}}
{{< note >}} {{< note >}}
If you have a Kubernetes cluster running on Google Kubernetes Engine, please follow [this guide](https://cloud.google.com/kubernetes-engine/docs/tutorials/persistent-disk). If you have a Kubernetes cluster running on Google Kubernetes Engine, please
follow [this guide](https://cloud.google.com/kubernetes-engine/docs/tutorials/persistent-disk).
{{< /note >}} {{< /note >}}
## Create a kustomization.yaml ## Create a kustomization.yaml
### Add a Secret generator ### Add a Secret generator
A [Secret](/docs/concepts/configuration/secret/) is an object that stores a piece of sensitive data like a password or key. Since 1.14, `kubectl` supports the management of Kubernetes objects using a kustomization file. You can create a Secret by generators in `kustomization.yaml`.
Add a Secret generator in `kustomization.yaml` from the following command. You will need to replace `YOUR_PASSWORD` with the password you want to use. A [Secret](/docs/concepts/configuration/secret/) is an object that stores a piece
of sensitive data like a password or key. Since 1.14, `kubectl` supports the
management of Kubernetes objects using a kustomization file. You can create a Secret
by generators in `kustomization.yaml`.
Add a Secret generator in `kustomization.yaml` from the following command.
You will need to replace `YOUR_PASSWORD` with the password you want to use.
```shell ```shell
cat <<EOF >./kustomization.yaml cat <<EOF >./kustomization.yaml
@ -91,42 +113,46 @@ EOF
## Add resource configs for MySQL and WordPress ## Add resource configs for MySQL and WordPress
The following manifest describes a single-instance MySQL Deployment. The MySQL container mounts the PersistentVolume at /var/lib/mysql. The `MYSQL_ROOT_PASSWORD` environment variable sets the database password from the Secret. The following manifest describes a single-instance MySQL Deployment. The MySQL
container mounts the PersistentVolume at /var/lib/mysql. The `MYSQL_ROOT_PASSWORD`
environment variable sets the database password from the Secret.
{{< codenew file="application/wordpress/mysql-deployment.yaml" >}} {{< codenew file="application/wordpress/mysql-deployment.yaml" >}}
The following manifest describes a single-instance WordPress Deployment. The WordPress container mounts the The following manifest describes a single-instance WordPress Deployment. The WordPress container mounts the
PersistentVolume at `/var/www/html` for website data files. The `WORDPRESS_DB_HOST` environment variable sets PersistentVolume at `/var/www/html` for website data files. The `WORDPRESS_DB_HOST` environment variable sets
the name of the MySQL Service defined above, and WordPress will access the database by Service. The the name of the MySQL Service defined above, and WordPress will access the database by Service. The
`WORDPRESS_DB_PASSWORD` environment variable sets the database password from the Secret kustomize generated. `WORDPRESS_DB_PASSWORD` environment variable sets the database password from the Secret kustomize generated.
{{< codenew file="application/wordpress/wordpress-deployment.yaml" >}} {{< codenew file="application/wordpress/wordpress-deployment.yaml" >}}
1. Download the MySQL deployment configuration file. 1. Download the MySQL deployment configuration file.
```shell ```shell
curl -LO https://k8s.io/examples/application/wordpress/mysql-deployment.yaml curl -LO https://k8s.io/examples/application/wordpress/mysql-deployment.yaml
``` ```
2. Download the WordPress configuration file. 2. Download the WordPress configuration file.
```shell ```shell
curl -LO https://k8s.io/examples/application/wordpress/wordpress-deployment.yaml curl -LO https://k8s.io/examples/application/wordpress/wordpress-deployment.yaml
``` ```
3. Add them to `kustomization.yaml` file. 3. Add them to `kustomization.yaml` file.
```shell ```shell
cat <<EOF >>./kustomization.yaml cat <<EOF >>./kustomization.yaml
resources: resources:
- mysql-deployment.yaml - mysql-deployment.yaml
- wordpress-deployment.yaml - wordpress-deployment.yaml
EOF EOF
``` ```
## Apply and Verify ## Apply and Verify
The `kustomization.yaml` contains all the resources for deploying a WordPress site and a
The `kustomization.yaml` contains all the resources for deploying a WordPress site and a
MySQL database. You can apply the directory by MySQL database. You can apply the directory by
```shell ```shell
kubectl apply -k ./ kubectl apply -k ./
``` ```
@ -135,80 +161,80 @@ Now you can verify that all objects exist.
1. Verify that the Secret exists by running the following command: 1. Verify that the Secret exists by running the following command:
```shell ```shell
kubectl get secrets kubectl get secrets
``` ```
The response should be like this: The response should be like this:
```shell ```
NAME TYPE DATA AGE NAME TYPE DATA AGE
mysql-pass-c57bb4t7mf Opaque 1 9s mysql-pass-c57bb4t7mf Opaque 1 9s
``` ```
2. Verify that a PersistentVolume got dynamically provisioned. 2. Verify that a PersistentVolume got dynamically provisioned.
```shell
kubectl get pvc
```
{{< note >}}
It can take up to a few minutes for the PVs to be provisioned and bound.
{{< /note >}}
The response should be like this: ```shell
kubectl get pvc
```
```shell {{< note >}}
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE It can take up to a few minutes for the PVs to be provisioned and bound.
mysql-pv-claim Bound pvc-8cbd7b2e-4044-11e9-b2bb-42010a800002 20Gi RWO standard 77s {{< /note >}}
wp-pv-claim Bound pvc-8cd0df54-4044-11e9-b2bb-42010a800002 20Gi RWO standard 77s
``` The response should be like this:
```
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
mysql-pv-claim Bound pvc-8cbd7b2e-4044-11e9-b2bb-42010a800002 20Gi RWO standard 77s
wp-pv-claim Bound pvc-8cd0df54-4044-11e9-b2bb-42010a800002 20Gi RWO standard 77s
```
3. Verify that the Pod is running by running the following command: 3. Verify that the Pod is running by running the following command:
```shell ```shell
kubectl get pods kubectl get pods
``` ```
{{< note >}} {{< note >}}
It can take up to a few minutes for the Pod's Status to be `RUNNING`. It can take up to a few minutes for the Pod's Status to be `RUNNING`.
{{< /note >}} {{< /note >}}
The response should be like this: The response should be like this:
``` ```
NAME READY STATUS RESTARTS AGE NAME READY STATUS RESTARTS AGE
wordpress-mysql-1894417608-x5dzt 1/1 Running 0 40s wordpress-mysql-1894417608-x5dzt 1/1 Running 0 40s
``` ```
4. Verify that the Service is running by running the following command: 4. Verify that the Service is running by running the following command:
```shell ```shell
kubectl get services wordpress kubectl get services wordpress
``` ```
The response should be like this: The response should be like this:
``` ```
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
wordpress LoadBalancer 10.0.0.89 <pending> 80:32406/TCP 4m wordpress LoadBalancer 10.0.0.89 <pending> 80:32406/TCP 4m
``` ```
{{< note >}} {{< note >}}
Minikube can only expose Services through `NodePort`. The EXTERNAL-IP is always pending. Minikube can only expose Services through `NodePort`. The EXTERNAL-IP is always pending.
{{< /note >}} {{< /note >}}
5. Run the following command to get the IP Address for the WordPress Service: 5. Run the following command to get the IP Address for the WordPress Service:
```shell ```shell
minikube service wordpress --url minikube service wordpress --url
``` ```
The response should be like this: The response should be like this:
``` ```
http://1.2.3.4:32406 http://1.2.3.4:32406
``` ```
6. Copy the IP address, and load the page in your browser to view your site. 6. Copy the IP address, and load the page in your browser to view your site.
@ -216,30 +242,23 @@ Now you can verify that all objects exist.
![wordpress-init](https://raw.githubusercontent.com/kubernetes/examples/master/mysql-wordpress-pd/WordPress.png) ![wordpress-init](https://raw.githubusercontent.com/kubernetes/examples/master/mysql-wordpress-pd/WordPress.png)
{{< warning >}} {{< warning >}}
Do not leave your WordPress installation on this page. If another user finds it, they can set up a website on your instance and use it to serve malicious content. <br/><br/>Either install WordPress by creating a username and password or delete your instance. Do not leave your WordPress installation on this page. If another user finds it,
{{< /warning >}} they can set up a website on your instance and use it to serve malicious content.<br/><br/>
Either install WordPress by creating a username and password or delete your instance.
{{< /warning >}}
## {{% heading "cleanup" %}} ## {{% heading "cleanup" %}}
1. Run the following command to delete your Secret, Deployments, Services and PersistentVolumeClaims: 1. Run the following command to delete your Secret, Deployments, Services and PersistentVolumeClaims:
```shell ```shell
kubectl delete -k ./ kubectl delete -k ./
``` ```
## {{% heading "whatsnext" %}} ## {{% heading "whatsnext" %}}
* Learn more about [Introspection and Debugging](/docs/tasks/debug/debug-application/debug-running-pod/) * Learn more about [Introspection and Debugging](/docs/tasks/debug/debug-application/debug-running-pod/)
* Learn more about [Jobs](/docs/concepts/workloads/controllers/job/) * Learn more about [Jobs](/docs/concepts/workloads/controllers/job/)
* Learn more about [Port Forwarding](/docs/tasks/access-application-cluster/port-forward-access-application-cluster/) * Learn more about [Port Forwarding](/docs/tasks/access-application-cluster/port-forward-access-application-cluster/)
* Learn how to [Get a Shell to a Container](/docs/tasks/debug/debug-application/get-shell-running-container/) * Learn how to [Get a Shell to a Container](/docs/tasks/debug/debug-application/get-shell-running-container/)