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.

Sign up to discover human stories that deepen your understanding of the world.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Chris Edward Rego
Chris Edward Rego

Written by Chris Edward Rego

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

Responses (4)

Write a response

@chrisedrego,
I tried the same in my cluster it's working fine. just wanted to understand , let say we have wanted to run 5 mysql service and 4 redis in. what all the changes we have to do in file ` name: tcp-services` configmap?
do we need to add…

2

Hi Chris, Able to connect via ingress to mysql, but facing an intermittent issue with connecting to mysql, which means sometimes I am able to connect and most of the time it says "connection refused".
Let me know if you have any context regarding this

Hi Chris! I am performing this install on my GKE cluster. When I apply the deployment yaml I get the error message: "The Deployment "mysql" is invalid: spec.template.spec.containers[0].volumeMounts[1].name: Not found: "mysql-conf"" Am I missing some configuration for the mysql-config volume mount? Thanks! -Mark