Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Wordpress in Kubernetes K3s on Raspberry Pi
# This manifest assumes 'wordpress' namespace is already present:
#
# kubectl create namespace wordpress
#
# Apply the manifest with:
#
# kubectl apply -f mariadb.yml
---
apiVersion: v1
kind: Secret
metadata:
name: mariadb-pass
namespace: wordpress
labels:
app: wordpress
data:
# This value is base64-encoded. Do not use this password in production!
password: MjJzaXl3dVpBNFdmeHNZcg==
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mariadb-pv-claim
namespace: wordpress
labels:
app: wordpress
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress-mariadb
namespace: wordpress
labels:
app: wordpress
spec:
selector:
matchLabels:
app: wordpress
tier: mariadb
strategy:
type: Recreate
template:
metadata:
labels:
app: wordpress
tier: mariadb
spec:
containers:
- image: tobi312/rpi-mariadb:10.3
name: mariadb
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mariadb-pass
key: password
ports:
- containerPort: 3306
name: mariadb
volumeMounts:
- name: mariadb-persistent-storage
mountPath: /var/lib/mysql
resources:
limits:
cpu: '1'
memory: '512Mi'
requests:
cpu: '500m'
memory: '256Mi'
volumes:
- name: mariadb-persistent-storage
persistentVolumeClaim:
claimName: mariadb-pv-claim
---
apiVersion: v1
kind: Service
metadata:
name: wordpress-mariadb
namespace: wordpress
labels:
app: wordpress
spec:
ports:
- port: 3306
selector:
app: wordpress
tier: mariadb
clusterIP: None
# This manifest assumes 'wordpress' namespace is already present:
#
# kubectl create namespace wordpress
#
# Apply the manifest with:
#
# kubectl apply -f wordpress.yml
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: wp-pv-claim
namespace: wordpress
labels:
app: wordpress
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress
namespace: 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:5.4-apache
name: wordpress
env:
- name: WORDPRESS_DB_HOST
value: wordpress-mariadb
- name: WORDPRESS_DB_PASSWORD
valueFrom:
secretKeyRef:
name: mariadb-pass
key: password
ports:
- containerPort: 80
name: wordpress
volumeMounts:
- name: wordpress-persistent-storage
mountPath: /var/www/html
resources:
limits:
cpu: '1'
memory: '512Mi'
requests:
cpu: '500m'
memory: '256Mi'
volumes:
- name: wordpress-persistent-storage
persistentVolumeClaim:
claimName: wp-pv-claim
---
apiVersion: v1
kind: Service
metadata:
name: wordpress
namespace: wordpress
labels:
app: wordpress
spec:
ports:
- port: 80
selector:
app: wordpress
tier: frontend
type: NodePort
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: wordpress
namespace: wordpress
spec:
rules:
- host: wordpress.10.0.100.99.nip.io
http:
paths:
- path: /
backend:
serviceName: wordpress
servicePort: 80
@mohsinonxrm

This comment has been minimized.

Copy link

@mohsinonxrm mohsinonxrm commented Jun 7, 2020

@geerlingguy, I'm getting Error establishing a database connection when trying to open the Wordpress site... can you please point me where to look to debug/fix?

@geerlingguy

This comment has been minimized.

Copy link
Owner Author

@geerlingguy geerlingguy commented Jun 7, 2020

Check to make sure both pods are running: kubectl get pods -n wordpress

@mohsinonxrm

This comment has been minimized.

Copy link

@mohsinonxrm mohsinonxrm commented Jun 7, 2020

@geerlingguy, did that. even deleted the WordPress namespace and started over, still the same error. BTW, excellent job on the series really enjoyed it. I hope you'll do another part and show some Kubernetes features, HA, failover, replicas, etc.
and... I'm looking forward to your Kubernetes 101!

@kimus

This comment has been minimized.

Copy link

@kimus kimus commented Jun 7, 2020

Not sure if it's the same problem. Not using raspberry pi, and so, I'm using mariadb:10.5 image.

All seams running fine:

$ kubectl get all -n wordpress

NAME                                     READY   STATUS    RESTARTS   AGE
pod/wordpress-mariadb-5cb547674f-58djk   1/1     Running   0          10m
pod/wordpress-5c6b9f6dd9-tcm5k           1/1     Running   0          10m

NAME                        TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE
service/wordpress-mariadb   ClusterIP   None          <none>        3306/TCP       10m
service/wordpress           NodePort    10.43.100.1   <none>        80:30775/TCP   10m

NAME                                READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/wordpress-mariadb   1/1     1            1           10m
deployment.apps/wordpress           1/1     1            1           10m

NAME                                           DESIRED   CURRENT   READY   AGE
replicaset.apps/wordpress-mariadb-5cb547674f   1         1         1       10m
replicaset.apps/wordpress-5c6b9f6dd9           1         1         1       10m

In the log of the wordpress I can see that didn't connect to database:

$ kubectl logs -n wordpress deployment.apps/wordpress

WordPress not found in /var/www/html - copying now...
Complete! WordPress has been successfully copied to /var/www/html
[07-Jun-2020 22:32:36 UTC] PHP Warning:  mysqli::__construct(): (HY000/2002): Connection refused in Standard input code on line 22

MySQL Connection Error: (2002) Connection refused

MySQL Connection Error: (2002) Connection refused

MySQL Connection Error: (2002) Connection refused

MySQL Connection Error: (2002) Connection refused

MySQL Connection Error: (2002) Connection refused
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 10.42.0.20. Set the 'ServerName' directive globally to suppress this message
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 10.42.0.20. Set the 'ServerName' directive globally to suppress this message
[Sun Jun 07 22:32:51.570388 2020] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.38 (Debian) PHP/7.3.18 configured -- resuming normal operations
[Sun Jun 07 22:32:51.570443 2020] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'
@madqb

This comment has been minimized.

Copy link

@madqb madqb commented Jun 8, 2020

I had the same problem. In mariadb.yml at the very bottom after port: 3306 add targetPort: 3306 on the next line.

@geerlingguy

This comment has been minimized.

Copy link
Owner Author

@geerlingguy geerlingguy commented Jun 9, 2020

Strange; maybe it's a Kubernetes version issue? Are you running on K3s 1.17 or something else? This matches the config that I've been deploying over and over again inside various Pi clusters here: https://github.com/geerlingguy/turing-pi-cluster/blob/master/roles/wordpress/templates/mariadb.yml#L85

And I haven't had any connection issues. The K8s Service docs basically state that targetPort defaults to port, so specifying it should make no difference:

By default and for convenience, the targetPort is set to the same value as the port field.

@kimus

This comment has been minimized.

Copy link

@kimus kimus commented Jun 9, 2020

@geerlingguy I was using k3d v3.0.0-beta.0, downgraded to v1.7.0 and the same problem persists.

I'm using k3d in a local macOS docker.

@madqb

This comment has been minimized.

Copy link

@madqb madqb commented Jun 10, 2020

Strange; maybe it's a Kubernetes version issue? Are you running on K3s 1.17 or something else? This matches the config that I've been deploying over and over again inside various Pi clusters here: https://github.com/geerlingguy/turing-pi-cluster/blob/master/roles/wordpress/templates/mariadb.yml#L85

And I haven't had any connection issues. The K8s Service docs basically state that targetPort defaults to port, so specifying it should make no difference:

By default and for convenience, the targetPort is set to the same value as the port field.

For reference my version output is: v1.18.3+k3s1. The reason I tried this fix was because it was the only difference I could find between your drupal yml and the wordpress one. As soon as I added it to wordpress, the pods came up no problem.

@kimus

This comment has been minimized.

Copy link

@kimus kimus commented Jun 11, 2020

update

after entering the wordpress pod, installed mariadb-client, and tryed to connect. So, with the rootuser everything connects fine to the mariadb pod:

# mariadb -h $WORDPRESS_DB_HOST -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 10.5.3-MariaDB-1:10.5.3+maria~bionic mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases
    -> ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| wordpress          |
+--------------------+
4 rows in set (0.056 sec)

So, I went and run kubectl rollout restart -n wordpress deployment.apps/wordpressand no errors shown after.

I guess it's related with wordpress starting before mariadb...

@ecasti-gmail-com

This comment has been minimized.

Copy link

@ecasti-gmail-com ecasti-gmail-com commented Jun 15, 2020

on mariadb.yml file, line 70, you should have /var/lib/mysql instead /var/lib/mariadb as mountPath, since that image has
configured:
datadir = /var/lib/mysql

@DaveBben

This comment has been minimized.

Copy link

@DaveBben DaveBben commented Jul 11, 2020

on mariadb.yml file, line 70, you should have /var/lib/mysql instead /var/lib/mariadb as mountPath, since that image has
configured:
datadir = /var/lib/mysql

Yes! If you don't change the mount, your files won't be saved. Just learned this the hard way.

@geerlingguy

This comment has been minimized.

Copy link
Owner Author

@geerlingguy geerlingguy commented Jul 11, 2020

@ecasti-gmail-com and @DaveBben - Thanks for reporting that. I've updated the mount in the gist above. I believe I must've copied out a version of the manifests from when I was changing around the image I was using for MariaDB around the time I was switching between Pi OS 32-bit and 64-bit... sorry about the confusion caused by this!

@raevillena

This comment has been minimized.

Copy link

@raevillena raevillena commented Oct 12, 2020

Been cruising around the youtube episodes and made to the point that I had wordpress running. The problem is that the ingress does not route to the wordpress app.

`root@master:~/wordpress# kubectl get all -n wordpress
NAME READY STATUS RESTARTS AGE
pod/wordpress-mariadb-54f564f668-n76xm 1/1 Running 0 24m
pod/wordpress-8bbd9f77c-f4cll 1/1 Running 0 6m37s

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/wordpress NodePort 10.43.160.149 80:30957/TCP 24m
service/wordpress-mariadb ClusterIP None 3306/TCP 24m

NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/wordpress-mariadb 1/1 1 1 24m
deployment.apps/wordpress 1/1 1 1 24m

NAME DESIRED CURRENT READY AGE
replicaset.apps/wordpress-mariadb-54f564f668 1 1 1 24m
replicaset.apps/wordpress-5c6b9f6dd9 0 0 0 24m
replicaset.apps/wordpress-8bbd9f77c 1 1 1 6m37s
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.