Persistent volumes (PVs) and persistent volume claims (PVCs) are created. For more
information about how to create a PV, see Use Alibaba Cloud disks as volumes, Use Alibaba Cloud NAS as volumes, and Use OSS volumes. For more information about how to create a PVC, see Create a Persistent Volume Claim. In this topic, an Alibaba Cloud disk is provisioned as a PV and mounted by using
a PVC. A PVC named WordPress-pv-claim and a PVC named WordPress-MySQL-pv-claim are
created. WordPress-pv-claim is used to mount a PV to the WordPress application. WordPress-MySQL-pv-claim
is used to mount a PV to the WordPress-MySQL application.
Background information
This example shows how to use custom orchestration templates to set up a WordPress
site with a MySQL database.
The created PVCs are used when you configure the YAML files of the applications. You
can specify WordPress-pvc in the YAML file of the WordPress application to mount the
corresponding PV. You can also specify WordPress-MySQL in the YAML file of the WordPress-MySQL
application to mount the corresponding PV.
A Secret is used to manage the usernames and passwords that are required to create
and access the MySQL database.
In this example, a Secret named mysql-pass is created to manage the MySQL root password.
The Secret type is set to Opaque. You can specify the created Secret in the YAML files of the applications.
In the left-side navigation pane, click Workload.
Click the Deployments tab. In the upper-right corner of the Deployments tab, click Create from Template.
Select the namespace to deploy the application. Use the following YAML template to
create a Deployment on which the WordPress application runs.
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress
labels:
app: wordpress
spec:
selector:
matchLabels:
app: wordpress
tier: frontend
strategy:
type: Recreate
template:
metadata:
labels:
app: wordpress
tier: frontend
spec:
containers:
- image: wordpress:4
name: wordpress
env:
- name: WORDPRESS_DB_HOST
value: wordpress-mysql # Specify the MySQL database that is accessed by the WordPress application. You must specify the value as the name of the Service that is created for the WordPress-MySQL application.
- name: WORDPRESS_DB_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-pass
key: password-mysql
ports:
- containerPort: 80
name: wordpress
volumeMounts:
- name: wordpress-pvc
mountPath: /var/www/html
volumes:
- name: wordpress-pvc
persistentVolumeClaim:
claimName: wordpress-pv-claim
Use the following YAML template to create a Deployment on which the MySQL application
runs.
To enable external access to the WordPress application, create a LoadBalancer type Service for the WordPress application. ACK will automatically create a Server
Load Balancer (SLB) instance for the created LoadBalancer type Service. This allows
external access to the WordPress application.
To enable WordPress to access the MySQL database internally, create a ClusterIP type
Service for the WordPress-MySQL application. The MySQL database does not require external
access. Therefore, you do not need to create a LoadBalancer type Service for the WordPress-MySQL application. For more information about how
to create a Service, see Create a service.
Use the following YAML templates to create a Service for the WordPress application
and a Service for the WordPress-MySQL application.
After the Services are created, go to the details page of the cluster. In the left-side
navigation pane, click Services.
On the Services page, you can find the Service that is created for the WordPress application
and obtain the external endpoint of the Service.
Enter <external endpoint IP address>/wp-admin/install.php into the address bar of your browser and press Enter to access WordPress.
What to do next
When you configure WordPress, you can use the created Secret as the credential for
logging on to WordPress. Data generated in the container that runs WordPress is stored
in the mounted PV.