AWS Kubernetes Cluster - WordPress Error establishing a database connection

4/15/2021

I am novice person 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 .

Wordpress Error

Overall Kubect status

AWS Inbound Rules

mysql-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-deployment
  labels:
    app: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:5.7
        ports:
        - containerPort: 80
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: DEVOPS1
        - name: MYSQL_USER
          value: wpuser
        - name: MYSQL_PASSWORD
          value: DEVOPS12345
        - name: MYSQL_DATABASE
          value: wpdb

mysql-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: mysql-service
spec:
  selector:
    app: mysql
  ports:
    - protocol: TCP
      port: 3306
      targetPort: 3306

wordpress-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress-deployment
  labels:
    app: wordpress
spec:
  replicas: 3
  selector:
    matchLabels:
      app: wordpress
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      containers:
      - name: wordpress
        image: wordpress
        ports:
        - containerPort: 80
        env:
        - name: WORDPRESS_DB_HOST
          value: mysql-service
        - name: WORDPRESS_DB_USER
          value: wpuser
        - name: WORDPRESS_DB_PASSWORD
          value: wpdb
        - name: WORDPRESS_DEBUG
          value: "1"

wp-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: wordpress-service
spec:
  type: NodePort
  selector:
    app: wordpress
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

Also I opened same thread on Kubernetes forum . Click Here to view it

-- Sameera
amazon-web-services
docker
kubernetes
mysql
wordpress

1 Answer

4/15/2021

Please change containerPort in your mysql-deployment file from 80 to 3306:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-deployment
  labels:
    app: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:5.7
        ports:
        - containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: DEVOPS1
        - name: MYSQL_USER
          value: wpuser
        - name: MYSQL_PASSWORD
          value: DEVOPS12345
        - name: MYSQL_DATABASE
          value: wpdb

Check similar problem: wordpress-database-connection-kubernetes.

-- Malgorzata
Source: StackOverflow