Skip to main content

[SOLVED] Minikube expose MySQL running on localhost as service - kubernetes

How to Expose MySQL Running on Localhost Using Minikube: A Simple Guide

In this chapter, we will look at how to expose a MySQL database running on our localhost as a service in Minikube. Minikube helps us create a local Kubernetes cluster. This makes it easier to test and develop applications that use Kubernetes. Exposing MySQL as a service in Minikube is important for our development. It lets our applications connect to the database easily. We will go through different ways to do this. This will help us access our MySQL instance from inside and outside the Minikube environment.

Here is a list of the solutions we will cover:

  • Solution 1: Install and Set Up Minikube
  • Solution 2: Deploy MySQL on Minikube
  • Solution 3: Create a Kubernetes Service for MySQL
  • Solution 4: Expose MySQL Service using NodePort
  • Solution 5: Access MySQL from Host Machine
  • Solution 6: Check MySQL Connection

By following these steps, we will learn how to expose MySQL running on localhost as a service in Kubernetes with Minikube. For more info on similar Kubernetes topics, we can check these links: How to share storage between pods and How to set dynamic values with Helm.

Solution 1 - Install and Configure Minikube

To run MySQL on localhost as a service in Kubernetes with Minikube, we first need to install and set up Minikube on our local machine. Minikube is a tool that helps us run Kubernetes locally. Here are the steps to install and set up Minikube.

Prerequisites

Before we install Minikube, we need to have the following things ready:

  • Virtualization Software: Minikube needs a hypervisor. For Windows, we can use Hyper-V or VirtualBox. For macOS, we can use VirtualBox or HyperKit.
  • kubectl: This is the command-line tool to work with Kubernetes clusters. We can install it by following the official guide.

Installation Steps

  1. Install Minikube: We can download and install Minikube using a package manager or by downloading the binary directly.

    For macOS with Homebrew:

    brew install minikube

    For Windows with Chocolatey:

    choco install minikube

    For Linux, we use these commands:

    curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
    sudo install minikube-linux-amd64 /usr/local/bin/minikube
  2. Start Minikube: After we install it, we can start Minikube with this command:

    minikube start

    This command sets up a local Kubernetes cluster. It may take some minutes.

  3. Check Minikube Installation: Once Minikube starts, we can check the status of our Minikube cluster with:

    minikube status
  4. Set Up kubectl: Minikube automatically sets up kubectl to talk with our cluster. We can check this by running:

    kubectl cluster-info
  5. Set Up Environment Variables (if needed): If we want to pass environment variables to our MySQL deployment, we can look at guides like how to pull environment variables in Kubernetes.

Additional Configuration

  • Access Dashboard: To see the Minikube dashboard, we can run:

    minikube dashboard

    This command opens a web UI to manage our Kubernetes apps.

  • Resource Allocation: We may want to give more resources to Minikube based on what our machine can handle. We can do this by setting CPU and memory:

    minikube start --cpus=4 --memory=8192

By following these steps, we will have Minikube installed and set up. We will be ready to deploy our MySQL database in a local Kubernetes environment. Next, we will deploy MySQL on Minikube.

Solution 2 - Deploy MySQL on Minikube

We can deploy MySQL on Minikube by creating a Kubernetes Deployment and a PersistentVolumeClaim. This helps our MySQL data to stay safe even if the pod restarts. Here are the steps to deploy MySQL on Minikube.

Step 1: Create a Persistent Volume Claim

First, we need to make a YAML file called mysql-pvc.yaml. This file will define a PersistentVolumeClaim for MySQL. This way, our MySQL database can keep its data even if we delete the pod.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

Step 2: Create a MySQL Deployment

Next, we create the MySQL deployment. We will create another YAML file called mysql-deployment.yaml with this content:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - name: mysql
          image: mysql:5.7
          ports:
            - containerPort: 3306
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: rootpassword
            - name: MYSQL_DATABASE
              value: mydatabase
          volumeMounts:
            - mountPath: /var/lib/mysql
              name: mysql-storage
      volumes:
        - name: mysql-storage
          persistentVolumeClaim:
            claimName: mysql-pvc

Step 3: Deploy MySQL

Now that we have our files ready, we can apply them to our Minikube cluster:

kubectl apply -f mysql-pvc.yaml
kubectl apply -f mysql-deployment.yaml

Step 4: Verify the Deployment

To see if MySQL is running well, we can use this command:

kubectl get pods

We should see a pod with a name like mysql-deployment-<hash> in the list. We can also check the logs to make sure MySQL has started correctly:

kubectl logs <mysql-pod-name>

We need to replace <mysql-pod-name> with the real name of our MySQL pod.

Step 5: Access MySQL

We can access the MySQL instance from a Kubernetes pod or by making it a service. For now, we can check that the pod is working and MySQL is running.

This process helps us set up a MySQL database on Minikube. It allows us to manage and use it in our Kubernetes environment. For more information on how to share storage between pods, we can check this guide.

Solution 3 - Create a Kubernetes Service for MySQL

To expose our MySQL deployment in Minikube, we need to create a Kubernetes Service. This service helps other pods and outside applications talk to the MySQL database. We can create a service of type ClusterIP, NodePort, or LoadBalancer. But for local work with Minikube, NodePort is usually the best option.

Here’s how we create a Kubernetes Service for MySQL:

  1. Define the MySQL Service Configuration

First, we create a YAML file named mysql-service.yaml. This file will define the MySQL service. Here is a simple example:

apiVersion: v1
kind: Service
metadata:
  name: mysql-service
spec:
  type: NodePort
  selector:
    app: mysql
  ports:
    - port: 3306 # MySQL default port
      targetPort: 3306 # Port where MySQL runs
      nodePort: 30000 # Port open on each Node

In this example:

  • The selector helps us find the pods that this service manages. We must make sure this matches the labels of our MySQL deployment.
  • The ports part tells us about the service port (port), the target port on the container (targetPort), and the nodePort. This is the port open on the Minikube node.
  1. Apply the Service Configuration

Next, we need to use the kubectl apply command to create the service in our Kubernetes environment:

kubectl apply -f mysql-service.yaml
  1. Verify the Service

After we create the service, we should check its creation and see the details by using:

kubectl get svc mysql-service

We should see something like this:

NAME            TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
mysql-service   NodePort   10.96.0.1      <none>        3306:30000/TCP   10s
  1. Access MySQL Service

Now we have our MySQL service running. We can access it from our local machine. We will use the Minikube IP and the node port we set (30000). To find the Minikube IP, we run:

minikube ip

Then, we can connect to MySQL using a MySQL client or command line:

mysql -h <minikube-ip> -P 30000 -u root -p

We should replace <minikube-ip> with the IP address we got from the last command.

By following these steps, we have created a Kubernetes Service for MySQL on Minikube. Now we can expose our MySQL database and work with it from our host machine. If we want to learn more about configuring services, we can check how to set different service types and their properties in Kubernetes.

Solution 4 - Expose MySQL Service using NodePort

To expose MySQL service that is running on Minikube, we can use the NodePort type of Kubernetes service. This helps us access our MySQL database from outside. We will use the IP address of the Minikube VM and a port. Let’s go through the steps.

Step 1: Create a NodePort Service for MySQL

First, we need to make a Kubernetes service of type NodePort. This will expose our MySQL pod. Here is an example of a YAML file for the NodePort service for MySQL:

apiVersion: v1
kind: Service
metadata:
  name: mysql-service
spec:
  type: NodePort
  selector:
    app: mysql
  ports:
    - port: 3306 # Port that MySQL listens on
      targetPort: 3306 # Port on the container
      nodePort: 30000 # Port exposed on the node

In this setup:

  • port: This is the port that the service shows.
  • targetPort: This is the port where the MySQL container runs.
  • nodePort: This is the port to reach your service from outside the cluster. You can choose a port from 30000 to 32767 or let Kubernetes pick one for you.

Step 2: Apply the Service Configuration

Now, save the YAML file as mysql-service.yaml. Then we will apply it with this command:

kubectl apply -f mysql-service.yaml

Step 3: Verify the Service

After we apply the configuration, we should check if the service is created and running by running:

kubectl get services

We will see an output that includes mysql-service and its NodePort.

Step 4: Access MySQL from Host Machine

To reach MySQL from our host machine, we need to find the IP address of our Minikube instance. We can do this by running:

minikube ip

If the output is 192.168.99.100, we can access MySQL with this command:

mysql -h 192.168.99.100 -P 30000 -u your_username -p

We need to replace your_username with our actual MySQL username. It will ask us for the password.

Additional Notes

  • We need to make sure that the MySQL pod is running. Also the pod should have the label app: mysql so that the service can send traffic to it.
  • If we have problems, we can check the logs of the MySQL pod for errors by using:
kubectl logs <mysql-pod-name>

This way of exposing the MySQL service using NodePort helps us interact with our MySQL database securely while using Minikube.

For more details on exposing services, we can look at the Kubernetes service external IP documentation.

Solution 5 - Access MySQL from Host Machine

To access the MySQL service in your Minikube cluster from your host machine, we need to make sure that the service is open and that we have the right connection details. This guide assumes that we have already set up MySQL in Minikube and opened it using a Kubernetes service with NodePort.

Step 1: Find the NodePort

First, we need to find out the NodePort that Kubernetes gave to our MySQL service. We can do this by running this command:

kubectl get services

We should look for the MySQL service in the output. We need to note the NodePort in the PORT(S) column. The output will look like this:

NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
mysql        NodePort    10.96.0.1       <none>        3306:XXXX/TCP   10m

In this example, XXXX is the NodePort.

Step 2: Get Minikube IP

Next, we need to find the IP address of our Minikube cluster. We can get it with this command:

minikube ip

This command will give us an IP address, usually something like 192.168.99.100.

Step 3: Connect to MySQL

Now we have both the Minikube IP and the NodePort. We can connect to the MySQL service from our host machine using a MySQL client. Use this command, replacing <MINIKUBE_IP> and <NODE_PORT> with your real values:

mysql -h <MINIKUBE_IP> -P <NODE_PORT> -u root -p

We will be asked to enter the MySQL root password that we set when we deployed MySQL.

Example

If our Minikube IP is 192.168.99.100 and the NodePort for our MySQL service is 30000, the command will look like this:

mysql -h 192.168.99.100 -P 30000 -u root -p

Additional Notes

  • If we have trouble connecting, we should check that the firewall allows traffic through the NodePort.
  • We might also want to use a MySQL GUI client. It can make it easier to access our MySQL database.

By following these steps, we can access our MySQL database in the Minikube environment from our host machine. For more details about using Kubernetes services, we can check this article.

Solution 6 - Verify MySQL Connection

To check if our MySQL instance on Minikube is working well, we need to verify the MySQL connection. This step helps us make sure that the MySQL service we exposed with NodePort or another method can be reached from our host machine.

Steps to Verify MySQL Connection

  1. Get the NodePort: First, we need to find the NodePort for our MySQL service. We can do this by running this command:

    kubectl get services

    Look for the MySQL service and note the NodePort value. It usually looks like 30000:3306/TCP, where 30000 is the NodePort.

  2. Obtain Minikube IP: Next, we need to find the IP address of our Minikube instance. We can get this by running:

    minikube ip

    This command will give us an IP address, like 192.168.99.100.

  3. Connect Using MySQL Client: Now that we have the NodePort and Minikube IP, we can connect to our MySQL database using a MySQL client. If we have the MySQL client installed on our machine, we run this command, replacing <NODE_PORT> with the NodePort we got earlier and <MINIKUBE_IP> with the IP address we found:

    mysql -h <MINIKUBE_IP> -P <NODE_PORT> -u root -p

    We will be asked to enter the MySQL root password. If we set a different username or password when we deployed MySQL, we should use those.

  4. Check Connection: If we connect successfully, we will see a MySQL prompt. This means we are connected to the MySQL server. We can run a simple query to check everything is good, like:

    SHOW DATABASES;

    This command should show us a list of databases in our MySQL instance.

  5. Troubleshooting Connection Issues: If we have trouble connecting, we should check these things:

    • Make sure the MySQL pod is running well by looking at the pod status with:

      kubectl get pods
    • Check that the MySQL service is set up right and listening on the correct port. We can check this by looking at the service details:

      kubectl describe service <mysql-service-name>
    • If we need to investigate more, we can use the Kubernetes port forwarding feature to access the MySQL service directly. We can do this with:

      kubectl port-forward service/<mysql-service-name> 3306:3306

    Then we can try to connect to MySQL using localhost as the host:

    mysql -h localhost -P 3306 -u root -p

For more details and help with problems, we can look at how to expose ports in Minikube and how to access Kubernetes API.

Conclusion

In this article, we looked at how to expose MySQL that runs on localhost as a service in Minikube. We covered important steps like installing Minikube, deploying MySQL, and making a Kubernetes service. By doing these steps, we can manage our MySQL database in a Kubernetes environment.

For more tips on Kubernetes setup, we can check our guides on mounting local directories into pods and setting dynamic values.

Comments