Dary Winata
Dary Winata

Reputation: 53

Kubernetes Mariadb service cannot be accessed

i wanted to make wordpress with kubernetes, but wordpress cant use host from mariadb-service. This is my script

---
apiVersion: v1
kind: Service
metadata:
  name: db-wordpress
  labels:
    app: mariadb-database
spec:
  selector:
    app: mariadb-database
  ports:
  - port: 3306
  clusterIP: None
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mariadb-database
spec:
  selector:
    matchLabels:
      app: mariadb-database
  template:
    metadata:
      labels:
        app: mariadb-database
    spec:
      containers:
      - name: mariadb-database
        image: darywinata/mariadb:1.0
        env:
          - name: MYSQL_ROOT_PASSWORD
            valueFrom:
              secretKeyRef:
                name: database-secret
                key: password
          - name: MYSQL_USER
            value: blibli
          - name: MYSQL_PASSWORD
            valueFrom:
              secretKeyRef:
                name: database-secret
                key: password
          - name: MYSQL_DATABASE
            value: wpdb
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                - key: servicetype
                  operator: In
                  values:
                  - database-mariadb

im already fight this error for 1 week, can somebody help me with this?

note: inside docker container port 3306 is not listening, idk this is wrong or not.

Upvotes: 0

Views: 390

Answers (1)

meaningqo
meaningqo

Reputation: 1928

Hi there and welcome to stackoverflow.

There are two issues with your setup. First of all, I have tried running your mysql docker image locally and compared to the official mysql image it is not listening to any port. Without the mysql process listening to any port, you will not be able to connect to it.

Also, you might want to consider a standard internal service type instead of one with clusterIP: None which is called a headless service and usually used for statefulsets and not deployments. more information can be found on the official documentation

So in order to connect from your application to your pod:

  1. Fix problem with your custom mysql image so it actually listens on port 3306 (or whatever you have configured in your image)

Upvotes: 1

Related Questions