TaeXtreme
TaeXtreme

Reputation: 322

Java Spring Backend cannot connect to Mariadb in Kubernetes

I was doing project for my class and the goal of the project was to implement simple backend application with Mariadb database. I choose Java Spring Boot for my backend.

The backend work when I testing it with database in docker container.

But when I move to working with Kubernetes my backend won't connect to database.

After I ran command kubectl apply -f . backend pods will keep spawning and dying because it won't connect to database.

Here all my Kubernetes files for deployment.

mariadb-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mariadb
  labels:
    app: mariadb
    type: database
spec:
  replicas: 1
  selector:
      matchLabels:
        app: mariadb
  template:
    metadata:
      labels:
        app: mariadb
        type: database
    spec:
      containers:
        - name: mariadb
          image: mariadb
          ports:
            - containerPort: 3306
          env:
            - name: MARIADB_ROOT_PASSWORD
              value: rootpass
            - name: MARIADB_DATABASE
              value: pastebin
            - name: MARIADB_USER
              value: dev
            - name: MARIADB_PASSWORD
              value: devpass

mariadb-svc.yaml

apiVersion: v1
kind: Service
metadata:
  name: mariadb
  labels:
    name: mariadb
spec:
  ports:
    - protocol: TCP
      port: 3306
      targetPort: 3306
  type: ClusterIP
  selector:
    app: mariadb
    name: mariadb

backend-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: scalable-p1-backend
  labels:
    app: scalable-p1-backend
spec:
  replicas: 1
  selector:
    matchLabels:
      app: scalable-p1-backend
  template:
    metadata:
      labels:
        app: scalable-p1-backend
    spec:
      containers:
      - name: scalable-p1-backend
        image: ghcr.io/cs-muic/scalable-p1-taextreme/scalable-p1-backend:latest
        env:   
            - name: SPRING_DATASOURCE_URL 
              value: jdbc:mariadb://mariadb:3306/pastebin
            - name: SPRING_DATASOURCE_USERNAME
              value: dev
            - name: SPRING_DATASOURCE_PASSWORD
              value: rootpass
      imagePullSecrets:
      - name: scalable-p1-taextreme-secret

backend-svc.yaml

kind: Service
apiVersion: v1
metadata:
  name: scalable-p1-backend
spec:
  selector:
    app: scalable-p1-backend
  type:  ClusterIP
  ports:
  - protocol: TCP
    port: 80
    targetPort: 5000

I have edit those files manytime already and try following every solution that I found and it not seem to work.

Error message from Backend logs.

java.sql.SQLNonTransientConnectionException: Could not connect to address=(host=mariadb-svc)(port=3306)(type=master) : Socket fail to connect to host:mariadb-svc, port:3306. Connection refused (Connection refused)

Upvotes: 0

Views: 496

Answers (1)

Lazy0147
Lazy0147

Reputation: 7

Have you tried

        - name: SPRING_DATASOURCE_URL 
          value: jdbc:mariadb://mariadb.default.svc:3306/pastebin

Upvotes: 1

Related Questions