Minikube expose MySQL running on localhost as service - kubernetes

To expose a MySQL database on localhost as a service in Kubernetes with Minikube, we can follow a simple process. First, we need to make sure that we have Minikube set up and running on our computer. We will create a Kubernetes deployment for MySQL and expose it as a service. This will let us access our database from outside. This guide will help us with the important steps to make our MySQL service reachable from our local machine.

In this article, we will talk about how to expose MySQL running on localhost as a service in Kubernetes. We will look at these topics:

  • Setting Up MySQL on Localhost for Minikube Access
  • Creating a Kubernetes Deployment for MySQL in Minikube
  • Exposing MySQL Service in Minikube Using NodePort
  • Accessing MySQL from Localhost Through Minikube Tunnel
  • Using Port Forwarding to Connect to MySQL in Minikube
  • Frequently Asked Questions

By the end of this article, we will understand how to expose our MySQL service in a Kubernetes environment using Minikube. This will help us manage our database easily.

Setting Up MySQL on Localhost for Minikube Access

To set up MySQL on our localhost for Minikube, we can follow these simple steps.

  1. Install MySQL: First, we need to install MySQL on our localhost. We can use package managers or download it from the MySQL website. If we are on Ubuntu, we can run:

    sudo apt update
    sudo apt install mysql-server
  2. Start MySQL Service: Next, we should start the MySQL service if it is not running yet:

    sudo service mysql start
  3. Secure MySQL Installation: We need to run a security script. This will help us set the root password and secure our MySQL installation:

    sudo mysql_secure_installation
  4. Create a Database and User: Now, let’s access the MySQL shell. We will create a database and a user for Kubernetes access:

    mysql -u root -p
    
    CREATE DATABASE mydatabase;
    CREATE USER 'k8suser'@'%' IDENTIFIED BY 'password';
    GRANT ALL PRIVILEGES ON mydatabase.* TO 'k8suser'@'%';
    FLUSH PRIVILEGES;
    EXIT;
  5. Configure MySQL to Allow Remote Access: We need to change the MySQL configuration file. This file is usually at /etc/mysql/mysql.conf.d/mysqld.cnf or /etc/my.cnf. We will allow connections from any host:

    [mysqld]
    bind-address = 0.0.0.0

    After that, we need to restart MySQL so the changes can take effect:

    sudo service mysql restart
  6. Check Firewall Settings: We should check that our firewall allows traffic on the MySQL port. The default is 3306:

    sudo ufw allow 3306
  7. Test MySQL Access: Finally, we can test if we can access MySQL from another machine or container. We will use the user we created:

    mysql -u k8suser -p -h <your-local-ip> mydatabase

Now we can connect to our MySQL instance on localhost from our Minikube environment. This makes it easy to work with databases for our Kubernetes apps.

Creating a Kubernetes Deployment for MySQL in Minikube

To create a Kubernetes deployment for MySQL in Minikube, we need to define a Deployment resource in a YAML file. Here is an example configuration that shows a MySQL deployment:

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
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: rootpassword
            - name: MYSQL_DATABASE
              value: mydatabase
            - name: MYSQL_USER
              value: user
            - name: MYSQL_PASSWORD
              value: userpassword
          ports:
            - containerPort: 3306

Save this YAML configuration in a file with name mysql-deployment.yaml.

To create the deployment, we run this command:

kubectl apply -f mysql-deployment.yaml

This command creates a MySQL deployment with one replica. It uses the MySQL 5.7 image. The environment variables set the root password, database name, user, and user password.

To check the status of our deployment, we can use:

kubectl get deployments

And to see the pods that we created with this deployment, we run:

kubectl get pods

If we want to see the logs for the MySQL pod, we can do this:

kubectl logs <pod-name>

Just replace <pod-name> with the name of the MySQL pod you got from the previous command. This setup helps us to deploy MySQL on Minikube easily and manage it with Kubernetes. For more details on managing deployments, you can look at this article.

Exposing MySQL Service in Minikube Using NodePort

We want to expose a MySQL service in Minikube with NodePort. To do this, we need to create a Kubernetes Service of type NodePort. This will help us access the MySQL database that runs inside our Minikube cluster. Here are the steps to expose the MySQL service.

  1. Create a Service YAML file (for example, mysql-service.yaml):
apiVersion: v1
kind: Service
metadata:
  name: mysql-service
spec:
  type: NodePort
  selector:
    app: mysql
  ports:
    - port: 3306
      targetPort: 3306
      nodePort: 30006
  1. Apply the Service configuration:
kubectl apply -f mysql-service.yaml
  1. Check if Service is created:
kubectl get services

You will see an output that includes mysql-service. The NodePort should be 30006.

  1. Access MySQL from your local computer:

Now we can connect to the MySQL database using the Minikube IP and the NodePort. First, we need to get the Minikube IP:

minikube ip

Then we connect to MySQL using a MySQL client:

mysql -h <minikube-ip> -P 30006 -u <username> -p

Replace <minikube-ip> with the IP we got from the last command. Also, put your MySQL username in place of <username>.

By following these steps, we expose the MySQL service running in Minikube using NodePort. Now we can access the database from outside.

Accessing MySQL from Localhost Through Minikube Tunnel

We can access MySQL that runs in our Minikube cluster from localhost by using the Minikube tunnel. This method helps us expose the MySQL service. Then we can connect to it directly with a local port.

  1. Start Minikube Tunnel: First, we need to open a terminal. Then we run the command below to start the tunnel. This command sets up a tunnel for our Minikube services.

    minikube tunnel

    It will ask us for our system password. This is because it needs elevated privileges.

  2. Create MySQL Service: We need to make sure we have a Kubernetes service for our MySQL deployment. If we do not have one, we should create a service YAML file. We can name it mysql-service.yaml:

    apiVersion: v1
    kind: Service
    metadata:
      name: mysql
    spec:
      type: LoadBalancer
      ports:
        - port: 3306
          targetPort: 3306
      selector:
        app: mysql

    Now we apply this configuration with the command below:

    kubectl apply -f mysql-service.yaml
  3. Get the MySQL Service IP: After we apply the service, we need to get the external IP for our MySQL service. We do this by running:

    kubectl get services

    We should look for the EXTERNAL-IP of the mysql service. It must be reachable on port 3306.

  4. Connect to MySQL: Next, we use a MySQL client to connect to the MySQL database. We do this using the external IP and port. For example:

    mysql -h <EXTERNAL-IP> -P 3306 -u root -p

    We enter our password when it asks us.

By following these steps, we can access our MySQL database that runs in Minikube from our local machine using the Minikube tunnel. This way is easy to work with services in our Kubernetes cluster without needing extra network setup. For more information on managing services in Kubernetes, we can check this article on Kubernetes services.

Using Port Forwarding to Connect to MySQL in Minikube

We can use port forwarding in Minikube to reach our MySQL service that runs in a Kubernetes cluster. This way helps us debug or access services without showing them to everyone.

To connect to our MySQL service with port forwarding, we should follow these steps:

  1. Find the MySQL Pod Name: First, we need to know the name of the MySQL pod in our Minikube cluster. We can find this by using this command:

    kubectl get pods

    We should look for the pod name that matches our MySQL deployment.

  2. Set Up Port Forwarding: Next, we use the kubectl port-forward command to send a port from our local machine to the MySQL pod. We replace <mysql-pod-name> with the real pod name and set the local port (like 3306):

    kubectl port-forward <mysql-pod-name> 3306:3306

    This command sends the local port 3306 to the pod’s port 3306.

  3. Connect to MySQL: After we set up port forwarding, we can connect to MySQL using our favorite MySQL client. For example, in the command line, we run:

    mysql -h 127.0.0.1 -P 3306 -u <username> -p

    We replace <username> with our MySQL username. We enter the password when it asks.

  4. Accessing the Database: Now, after we connect, we can run MySQL commands like we are connected to a local MySQL server.

This way is fast for us to access our MySQL service in Minikube without making it public. For more info on managing services in Kubernetes, we can check this guide on Kubernetes services.

Frequently Asked Questions

1. How do we expose MySQL running on Minikube?

To expose MySQL on Minikube, we can create a Kubernetes service with type NodePort. This lets outside users access our MySQL instance through a certain port. We define this service in a YAML file. Then we apply it using kubectl apply -f <filename>.yaml. This way, we can connect to MySQL from our localhost or any other client.

2. What are the steps to set up MySQL on localhost for Minikube?

To set up MySQL on localhost for Minikube, we first install MySQL on our local machine. Then we create a Kubernetes deployment in Minikube. We need to define the MySQL deployment in a YAML file. We should specify the image and the environment variables for the MySQL root password. After that, we use kubectl apply -f <filename>.yaml to deploy it. For more details, we can check how do I deploy a stateful application e.g. MySQL on Kubernetes.

3. Can we access MySQL in Minikube using port forwarding?

Yes, we can access MySQL in Minikube using port forwarding. We use the command kubectl port-forward svc/<mysql-service-name> <local-port>:<mysql-port>. This command forwards traffic from our local machine to the MySQL service in Minikube. This means we can connect to MySQL directly from our localhost without using a NodePort service.

4. What is the difference between NodePort and ClusterIP services in Kubernetes?

NodePort and ClusterIP are two kinds of Kubernetes services. A ClusterIP service exposes the service on a cluster-internal IP. This means it is only available inside the cluster. On the other hand, a NodePort service exposes the service on each node’s IP at a fixed port. This allows outside traffic to reach it. For more information, we can look at the difference between ClusterIP, NodePort, and LoadBalancer service types in Kubernetes.

5. How can we connect to MySQL from localhost when using Minikube?

To connect to MySQL from localhost while using Minikube, we can use either port forwarding or a Minikube tunnel. With port forwarding, we run kubectl port-forward svc/<mysql-service-name> <local-port>:<mysql-port>. If we want to use a Minikube tunnel, we can run minikube tunnel. This will expose the MySQL service and let us access it via the NodePort. This helps us manage our database easily from our local setup.