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

8/31/2020

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: https://stackoverflow.com/questions/43354167/minikube-expose-mysql-running-on-localhost-as-service

but didnt work :(

thanks for your time!

-- Tiago Machado
kubernetes
kubernetes-ingress
kubernetes-pod
mysql

3 Answers

8/31/2020

Your service is already expose in the Kubernetes to other pods on port 3306. You can expose mysql to the host by using a service of type NodePort.

kubectl expose svc/mysql --name mysql-np --port 3306 --type NodePort
kubectl get svc mysql-np -ojsonpath='{.spec.ports[0].nodePort}'

For the data, are you deleting and recreating PV by any chance? The configuration you have shared should work.

-- Faheem
Source: StackOverflow

9/1/2020

As you are working the same namespace the pods created after you have a service called MySQL will have environment variables named:

  • MYSQL_SERVICE_HOST
  • MYSQL_SERVICE_PORT

You can use them in the second deployment to get access to the MySQL deployment.

-- Zambozo
Source: StackOverflow

9/1/2020

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

-- Andre Kouame
Source: StackOverflow