<p>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.</p>
<p>The backend work when I testing it with database in docker container.</p>
<p>But when I move to working with Kubernetes my backend won't connect to database.</p>
<p>After I ran command <code>kubectl apply -f .</code> backend pods will keep spawning and dying because it won't connect to database.</p>
<p>Here all my Kubernetes files for deployment.</p>
<p>mariadb-deployment.yaml</p>
<pre><code>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
</code></pre>
<p>mariadb-svc.yaml</p>
<pre><code>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
</code></pre>
<p>backend-deployment.yaml</p>
<pre><code>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
</code></pre>
<p>backend-svc.yaml</p>
<pre><code>kind: Service
apiVersion: v1
metadata:
  name: scalable-p1-backend
spec:
  selector:
    app: scalable-p1-backend
  type:  ClusterIP
  ports:
  - protocol: TCP
    port: 80
    targetPort: 5000
</code></pre>
<p>I have edit those files manytime already and try following every solution that I found and it not seem to work.</p>
<p>Error message from Backend logs.</p>
<pre><code>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)
</code></pre>

TaeXtreme
<p>Have you tried</p>
<pre><code>        - name: SPRING_DATASOURCE_URL 
          value: jdbc:mariadb://mariadb.default.svc:3306/pastebin
</code></pre>
