Deploying WordPress and MySQL with Persistent Volumes | WordPress Deployment on Kubernetes
HTML-код
- Опубликовано: 25 авг 2024
- #kubernetes #devops #wordpress
Deploying WordPress and MySQL with Persistent Volumes | WordPress Deployment on Kubernetes
This video shows you how to deploy a WordPress site and a MySQL database. Both applications use PersistentVolumes and PersistentVolumeClaims to store data.
Setup MySQL on Kubernetes
1. Create Secret for MySQL
echo -n 'mysql1234' | base64
apiVersion: v1
kind: Secret
metadata:
name: wp-db-secrets
type: Opaque
data:
MYSQL_ROOT_PASSWORD: bXlzcWwxMjM0
kubectl apply -f mysql-secret.yaml
kubectl get secrets
2. Create PV and PVC for MySQL
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv
spec:
storageClassName: local-storage
claimRef:
name: mysql-volume
namespace: default
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
local:
path: /mnt
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- node01
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-volume
namespace: default
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: local-storage
kubectl apply -f mysql-volume.yaml
kubectl get pv
kubectl get pvc
3. Create Deployment for MySQL
---
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: mysql
labels:
app: mysql
spec:
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: database
image: mysql:5.7
args:
mount volume
- "--ignore-db-dir=lost+found"
add root password
envFrom:
- secretRef:
name: wp-db-secrets
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
volumes:
- name: mysql-data
persistentVolumeClaim:
claimName: mysql-volume
kubectl apply -f mysql.yaml
kubectl describe pod
4. Create Service for MySQL
---
apiVersion: v1
kind: Service
metadata:
name: mysql-service
spec:
ports:
- port: 3306
protocol: TCP
selector:
app: mysql
kubectl apply -f mysql-service.yaml
kubectl get svc
kubectl exec -it pod_name -- bash
mysql -u root -p
CREATE DATABASE wordpress;
show databases;
exit
Deploy the WordPress on Kubernetes and connect with MySQL Pod.
Create PV PVC for WordPress
apiVersion: v1
kind: PersistentVolume
metadata:
name: wordpress-pv
spec:
storageClassName: local-storage
claimRef:
name: wordpress-volume
namespace: default
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
local:
path: /data
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- node01
Kubectl apply -f wordpress-pv.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: wordpress-volume
namespace: default
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: local-storage
kubectl apply -f wp-volume.yaml
2. Create Deployment for WordPress
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress
spec:
replicas: 1
selector:
matchLabels:
app: wordpress
template:
metadata:
labels:
app: wordpress
spec:
containers:
- name: wordpress
image: wordpress:5.8.3-php7.4-apache
ports:
- containerPort: 80
name: wordpress
volumeMounts:
- name: wordpress-data
mountPath: /var/www
env:
- name: WORDPRESS_DB_HOST
value: mysql-service.default.svc.cluster.local
- name: WORDPRESS_DB_PASSWORD
valueFrom:
secretKeyRef:
name: wp-db-secrets
key: MYSQL_ROOT_PASSWORD
- name: WORDPRESS_DB_USER
value: root
- name: WORDPRESS_DB_NAME
value: wordpress
volumes:
- name: wordpress-data
persistentVolumeClaim:
claimName: wordpress-volume
kubectl apply -f wp.yaml
3. Create Service for WordPress
---
kind: Service
apiVersion: v1
metadata:
name: wordpress-service
spec:
type: LoadBalancer
selector:
app: wordpress
ports:
- name: http
protocol: TCP
port: 80
targetPort: 80
kubectl apply -f wp-service.yaml
kubectl get svc
kubectl get secret --namespace default wordpress -o jsonpath="{.data.wordpress-password}" | base64 -d
Have specifies storage class, nfs pv and pvc for mysql and wordpress. Pv 10 gi, pvc 5 gi for wordpress and 5gi for mysql. 5 10 gi bound to mysql and wordpress pending..
Great Saleem !!!
Thank you
Great
thanks
thank you but i face isssue Error establishing a database connection, help me please !
You are welcome
Please send the logs
Hi! which IP and port are you connecting to?
I get a 500 error: Error establishing a database connection. I haven't been able to troubleshoot it :(
Master node ip and port is assigned by Kubernetes lb @@yesitasnow