Wordpress Error establishing a database connection using kubernetes

9/3/2019

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?

-- Dhananjay
docker
kubernetes
kubernetes-pod
kubernetes-service

2 Answers

9/3/2019

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
-- wolmi
Source: StackOverflow

9/3/2019

include below line in mysql service YAML

targetPort: 3306

-- P Ekambaram
Source: StackOverflow