mysqlkuberneteskubernetes-ingresskubernetes-pod

kubernetes-How do I expose mysql service to another pod?


I have a mysql service with pv, and a depoyment of a custom karaf image image.

I have hooks in my karaf, and when I install the kar liquibase should fill the databases, but I realise that the databases renain empty, so karaf cant access mysql.

I want to access in my machine, localhost.

Here are my .yaml configurations:

mysql pv:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi

mysql-deploy:

apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  ports:
  - port: 3306
  selector:
    app: mysql
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - image: mysql:5.7
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: db-secret
              key: mysql-root-password
        - name: MYSQL_USER
          valueFrom:
            secretKeyRef:
              name: db-secret
              key: mysql-user
        - name: MYSQL_PASSWORD
          valueFrom:
            secretKeyRef:
              name: db-secret
              key: mysql-password
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim

the secret with mysql pass:

apiVersion: v1
kind: Secret
metadata:
  name: db-secret
type: Opaque
data:
  mysql-password: cm9vdA==
  mysql-root-password: cm9vdA==
  mysql-user: eGNvcmVyb290

karaf deploy:

apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: karaf-deployment
spec:
  selector:
    matchLabels:
      app: karaf
  replicas: 1 # tells deployment to run 2 pods matching the template
  template:
    metadata:
      labels:
        app: karaf
    spec:
      containers:
      - name: karaf
        image: myuser/karaf:v1

Note: to connect to mysql locally I had to expose doing: kubectl expose deployment mysql --type=LoadBalancer --name=my-service

Not sure if that is right...

and I did the same for karaf, adding --port=8101 so I could ssh to karaf and install the kar...

I tried some sugestions I saw on the internet, such as create a ingress controller, or this one: Minikube expose MySQL running on localhost as service

but didnt work :(

thanks for your time!


Solution

  • In Your Karaf application.what is the connection source url? For example in a Spring boot application deploy in kubernetes and that use a mysql service the connection source url will be : jdbc:mysql://mysqlservice:3306/${DB_NAME}. Where "mysqlservice" will be the name of the service mysql define in kubernetes