I have created two yaml files. One for wordpress and one for Mysql.
I can create the pods without the a problem and I can access wordpress with terminal portforwarding in my browser.
But anytime I do this it says "Error astablishing a database connection". I don’t know why and I need help
I created both files with kubectl apply -f ."filename.yaml"
and I portforwarded wordpress with kubectl port-forward wordpress 8080:80
wordpress.yaml:
apiVersion: v1
kind: Pod
metadata:
name: wordpress
spec:
containers:
- name: app
image: wordpress:latest
env:
- name: "WORDPRESS_DB_HOST"
value: "mysql-service"
- name: "WORDPRESS_DB_NAME"
value: "MysqlDB"
- name: "WORDPRESS_DB_USER"
value: "admin"
- name: "WORDPRESS_DB_PASSWORD"
value: "Test"
ports:
- containerPort: 80
database.yaml:
apiVersion: v1
kind: Pod
metadata:
name: database
labels:
name: mysql-pod
spec:
containers:
- name: database
image: mysql:latest
env:
- name: "MYSQL_USER"
value: "admin"
- name: "MYSQL_PASSWORD"
value: "Test"
- name: "MYSQL_DATABASE"
value: "MysqlDB"
- name: "MYSQL_ROOT_PASSWORD"
value: "TFBern_3013"
ports:
- containerPort: 3306
--- # Mysql Service definiert
apiVersion: v1
kind: Service
metadata:
name: mysql-service
spec:
ports:
- port: 3306
selector:
name: mysql-pod
I also tried ping from wordpress to database and that worked without a problem. I just haven’t figured out to ping from database to wordpress beacause I don’t know how to install the ping package on bash-5.1
2
Answers
it works now with following yaml files
wordpress.yaml:
`apiVersion: v1 kind: Pod metadata: name: wordpress spec: containers:
database.yaml:
kind: Pod metadata: name: database labels: name: mysql-pod spec: containers:
ports:
It looks like you might be missing a Kubernetes service for your MySQL pod, which is crucial for enabling communication between your WordPress and MySQL pods. Here’s how to set up the service:
Why You Need a Service
A Kubernetes Service provides a stable IP address for your pods and handles load balancing. Without this, the hostname ‘database’ used in your WordPress configuration cannot be resolved, causing the connection error.
Update the MySQL Pod Configuration:
Add a label to your MySQL pod so it can be targeted by the service:
Create a Service:
Save this as
mysql-service.yaml
:Apply the Configuration:
Run these commands:
Then try again and you shouldn’t get the “Failed to establish database connection” error anymore.
If it persists, do post more details and I’ll share my thoughts.
UPDATE
Just saw that you have a mysql-service, so as David mentioned, set WORDPRESS_DB_HOST=mysql-service and you should be good.