Sergej Sergeenko
Sergej Sergeenko
626 0
11.12.2016
  • Run WordPress and MySQL on Kubernetes

Today we look how to run WordPress and MySQL with / on Kubernetes. In the last article we looked at an example of how to run WordPress in Docker. It was simple – we have created a docker compose file and defined image, ports, depends and volumes.

In Kubernetes we must create two files – Pod and Service for each . We’ll use the mysql and wordpress (wordpress image include Apache web-server) Docker images for this installation.

In our example MySQL and WordPress will run on two Pods and then we set up a Kubernetes service to front each pod.

1. Create and run MySQL on a single pod.

You can run MySQL in two ways – with dashboard wizard, command line or yaml file. I’ll show you how to run it with the help of YAML file.

apiVersion: v1
kind: Pod
metadata:
name: mysql-pod
labels:
name: mysql-pod
spec:
containers:
-
name: mysql
image: mysql:latest
env:
-
name: "MYSQL_USER"
value: "mysql"
-
name: "MYSQL_PASSWORD"
value: "mysql"
-
name: "MYSQL_DATABASE"
value: "sample"
-
name: "MYSQL_ROOT_PASSWORD"
value: "supersecret"
ports:
-
containerPort: 3306

2. Create MySQL Kubernetes Service.

After creating Pod set up MySQL Service

apiVersion: v1
kind: Service
metadata:
name: mysql-service
labels:
name: mysql-pod
context: docker-k8s-lab
spec:
ports:
- port: 3306
selector:
name: mysql-pod

3. Create and run WordPress container on a single Pod.

I made it through Kubernetes Dashboard. As you see on screen we use mysql-service defined in previous step for WORDPRESS_DB_HOST.

wordpress-pod

We can use command line for this operation, for example:

$ kubectl run wordpress --image=wordpress --port=80

By default, the pod is only accessible by its internal IP within the cluster, so in next step we create WordPress Service.

4. Create WordPress Service for external traffic in Kubernetes

apiVersion: v1
kind: Service
metadata:
name: wordpress
labels:
app: wordpress
spec:
ports:
- nodePort: 30338
port: 80
protocol: TCP
targetPort: 80
selector:
app: wordpress
sessionAffinity: None
type: LoadBalancer
status:
loadBalancer: {}

or you can simply run the command of kubectl:

$ kubectl expose deployment wordpress --type=LoadBalancer

After you run this command Kubernetes automatically create wordpress service yaml file. The –type=LoadBalancer flag creates an external IP on which the pod accepts traffic.

So, Service was created -check it:

$ kubectl describe services wordpress
Name: wordpress
Namespace: default
Labels: app=wordpress
Selector: app=wordpress
Type: LoadBalancer
IP: 10.0.0.20
Port: 80/TCP
NodePort: 30338/TCP
Endpoints: 172.17.0.4:80
Session Affinity: None

So, now after expose our depoyment we having:

  • A service
  • The forwarding rules and target pool that comprise the external load balancer.
  • A firewall rule that allows traffic to port 80 on the external IP.

5. Check out our WordPress website

To see our work in action, visit the external IP address created in the previous step. Use Node port 30338 wich we defined in previous step.

wordpress-pod

Export Yaml from Kubernetes

If you want export you deployment into yaml or json files, siply use this command:

kubectl get -o yaml pod wordpress-pod // export pod in yaml format
kubectl get -o json pod wordpress-pod // export pod in json format



Comments are closed.