Search code examples
javaspringkubernetescloudibm-cloud

Spring DataSource in Kubernetes for MySQL


I have deployed in the IBM Cloud a MySQL Database and a Spring Boot Application with Kubernetes. MySQL database has a deployment and a service but it should not accessible in the outside that's way i gave him a cluster ip. But the Spring application is load balancing, because it should be visible from clients. My MySQL data source didn't work which i write in my spring data source. In local case i write there just localhost, but in the Cloud this doesn't work ? Has anybody a idea how that works ?

Deployment file: 
apiVersion: apps/v1 
kind: Deployment
metadata:
  name: db
  enviroment: test 
spec:
  selector:
    matchLabels:
      app: mysql
      enviroment: test
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql
        enviroment: test
    spec:
      containers:
      - image: de.icr.io/test/mysql:5.7
        name: db
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: ************
        - name: MYSQL_USER
          value: ********
        - name: MYSQL_PASSWORD
          value: ***********
        - name: MYSQL_DATABASE
          value: *******
        ports:
        - containerPort: 3306
          name: db
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql   
      volumes:
         - name: mypvc
           persistentVolumeClaim:
           claimName: mysql-pv-claim

That is my services:

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

I know you will not need my perssistens volume claime but i send it.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mypvc
  labels:
   billingType: "monthly"
   region: eu-de
   zone: Frankfurt
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 20Gi
  storageClassName: ibmc-file-bronze

And at last the Spring Boot datasource i don't know what i will write in the url for the connection.

 <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource"
        primary="true">
        <property name="driverClassName"
            value="com.mysql.jdbc.Driver" />
        <property name="url"
            value="jdbc:mysql://db/nlfb?useSSL=false" />

        <property name="username" value="*****" />
        <property name="password" value="******" />
    </bean>

kind regards and thanks for your help.


Solution

  • You have to use the service name of Kubernetes Deployments. Which is releated to the particular deployment i.e. if you r servicename is db and it is placed on namespace dev then you have to write dev.db