I am new to Kubernetes and trying to deploying WordPress and MySQL using the Kubernetes pod containers but its throwing the error "Error establishing a database connection" while running the Kubernetes
Below are my pod and service definition files
secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: mysql-pass
type: Opaque
data:
username: XXXXX
password: XXXXX
mysql-pod-defination-file.yaml
apiVersion: v1
kind: Pod
metadata:
name: mysql-app
labels:
name: mysql-app-label
spec:
containers:
- image: mysql:5.6
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-pass
key: password
name: mysql-image
ports:
- containerPort: 3306
wordpress-pod-defination-file.yaml
apiVersion: v1
kind: Pod
metadata:
name: wordpress-app
labels:
name: wordpress-app-label
spec:
containers:
- image: wordpress:apache
name: wordpress-image
env:
- name: WORDPRESS_DB_HOST
value: db
- name: WORDPRESS_DB_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-pass
key: password
ports:
- containerPort: 80
mysql-service-defiantion-file.yaml
apiVersion: v1
kind: Service
metadata:
name: db
labels:
name: db-service
app: demo-wordpress-app
spec:
ports:
- port: 3306
selector:
name: mysql-app-label
wordpress-service-defination-file-yaml
apiVersion: v1
kind: Service
metadata:
name: wordpress-service
labels:
name: wordpress-service
app: demo-wordpress-app
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
selector:
name: wordpress-app-label
I am able to make the successful connection to the mysql database from inside the minikube Pod container
is anything wrong in my definition files?
You need to add targetPort
on the mysql service yaml definition.
apiVersion: v1
kind: Service
metadata:
name: db
labels:
name: db-service
app: demo-wordpress-app
spec:
ports:
- port: 3306
targetPort: 3306
selector:
name: mysql-app-label
include below line in mysql service YAML
targetPort: 3306