Setting up a Standalone MYSQL Instance on Kubernetes & exposing it using Nginx Ingress Controller.

Chris Edward Rego
2 min readSep 18, 2020

A simple straightforward guide to setup a standalone MySQL instance in Kubernetes an expose it using Nginx Ingress Controller.

In this guide, we would have a simple deployment, in which the data is persisted using the Persistent Volume and then service is exposed to the rest of the world using ClusterIP Service along with an Ingress definition.

Overall setup for MYSQL on Kubernetes exposed using Nginx Ingress

Let’s create a namespace named mysql along with a secret which will contain the root user’s password for MYSQL

kubectl create ns mysql kubectl create secret generic mysql-creds --from-literal=ROOT_PASSWORD=changeme -n mysql

Below we have the configmap, deployment, service, Ingress, PVC configuration which we can apply.

my.cnf (MYSQL Configuration)
Persistent Storage Volume Claim for MYSQL Intance
MYSQL Deployment along with Persistent Volume and MYSQL Configuration injected
Exposing the deployment inside of the cluster Service: ClusterIP

Exposing MYSQL or TCP/UDP service often requires a bit of the change in the Nginx-ingress-controller configuration. As mentioned in the official documentation.

  • Ensure that Nginx-Ingress-Controller deployment specifies the name of the configmaps to configure tcp/udp configuration.

tcp-services-configmap — udp-services-configmap

containers:
- args:
- /nginx-ingress-controller
- --v=1
- --configmap=$(POD_NAMESPACE)/nginx-configuration
- --tcp-services-configmap=$(POD_NAMESPACE)/tcp-services
- --udp-services-configmap=$(POD_NAMESPACE)/udp-services
  • Adding details in the configmap which involves the port number to be exposed in our case it’s 3306(MYSQL) along with the namespace followed by the name of the service and the port number.
MYSQL default port is 3306, which gets

If everything goes well we can try connecting to MYSQL Instance using the cli

mysql -u root -h mysql.<DOMAIN>.com -p

We finally are able to connect the MYSQL Instance!

In context to the same, there is a similar blog which discusses how to easily provision MYSQL cluster using Presslabs MYSQL Operator

https://medium.com/@chrisedrego/mysql-operator-a-mysql-affair-with-kubernetes-dc21e19e93b7

if you found this article useful, feel free to 👏 clap many times or share it with your friends.

--

--

Chris Edward Rego

Lead DevSecOps. Talks about Cloud Architecture, DevOps & SRE. For more info check https://www.linkedin.com/in/chrisedrego/