Cara menggunakan ingress mysql kubernetes

The classic task in software development — write an API application with a MySQL database used for data storage. Plenty of publications can be found about it and we are NOT going to be focused on this specific task.

With the addition of a small twist — it becomes a challenging and educational task. This small twist is Virtualization on the Kubernetes platform.
Let’s review a step-by-step tutorial on how to deploy the Rest API application using MySQL on the Kubernetes cluster. The focus will be on deployment aspects and application code is used just to provide evidence of a properly working concept.

There are 3 components in this deployment:

  • Kubernetes cluster
  • MySQL running on Kubernetes cluster
  • Simple Rest API Application running on Kubernetes cluster and able to use MySQL running on the same cluster

Click FOLLOW to never miss another article from me.

First thing first — Kubernetes cluster

K3s is a Lightweight Kubernetes.

k3d is a lightweight wrapper to run k3s (Rancher Lab’s minimal Kubernetes distribution) in docker.

What can be better than this to provide the best development experience ever? That means the ability to run all components locally on your laptop or virtual machine — to avoid any dependencies while working on the application.

Steps to install Kubernetes cluster

$ k3d cluster create --api-port 6550 -p "8081:80@loadbalancer"$ export KUBECONFIG="$(k3d kubeconfig write k3s-default)"$ k3d cluster list

As simple as elegant — one step and we have Kubernetes cluster running.

If you never used k3d you should at least try it to explore a new world of opportunities for running clusters locally with minimal resources usage.

If the definition for your KUBECONFIG are not persistent and you lost it after restarting of the terminal and your K3d cluster is still running using following commands to point to the correct cluster.

$ kubectl config get-contexts$ kubectl config use-context <your cluster name>

Be sure to click FOLLOW to never miss another article on tricks and tips, life lessons, and more!

Clone code for this tutorial from this GitHub repository

GitHub - LearnTechnoBios/rest-api-mysql-k3d

You can't perform that action at this time. You signed in with another tab or window. You signed out in another tab or…

github.com

MySQL running on Kubernetes cluster

We will use Kubernetes secret to store MySQL database password
To get

$ echo -n 'admin' | base64
8 encoded password run

$ echo -n 'admin' | base64

Use output in the mysql-secret.yaml

apiVersion: v1kind: Secretmetadata:name: mysql-passtype: Opaquedata:password: YWRtaW4=

to create a secret run

kubectl create -f mysql-secret.yaml

to verify creation

kubectl get secrets

use mysql-pod.yaml to deploy mysql pod on the cluster

apiVersion: v1kind: Podmetadata:name: k8s-mysqllabels:name: lbl-k8s-mysqlspec:containers:- name: mysqlimage: mysql:latestenv:- name: MYSQL_ROOT_PASSWORDvalueFrom:secretKeyRef:name: mysql-passkey: passwordports:- name: mysqlcontainerPort: 3306protocol: TCPvolumeMounts:- name: k8s-mysql-storagemountPath: /var/lib/mysqlvolumes:- name: k8s-mysql-storageemptyDir: {}

to create run

kubectl create -f mysql-pod.yaml

to verify creation

kubectl get pod

Wait until the pod is in the running state — it can take several minutes.

Now we have a pod running MySQL on our cluster
Let’s connect to MySQL pod and try to use it

$ kubectl exec k8s-mysql -it -- bash
echo $MYSQL_ROOT_PASSWORD
mysql --user=root --password=$MYSQL_ROOT_PASSWORD
show databases;

It is working fine for usage from the Pod. So far MySQL pod is not reachable from another pod.

To allow access from other pods and external systems — we should expose a port . We will use a service for it.

Use mysql-service.yaml

$ kubectl config get-contexts$ kubectl config use-context <your cluster name>
0

to create a run

$ kubectl config get-contexts$ kubectl config use-context <your cluster name>
1

to verify the creation

$ kubectl config get-contexts$ kubectl config use-context <your cluster name>
2

Deploy API Application and use MySQL

For very simple application use app.py

$ kubectl config get-contexts$ kubectl config use-context <your cluster name>
3

Add requirements.txt file

$ kubectl config get-contexts$ kubectl config use-context <your cluster name>
4

Add Dockerfile

$ kubectl config get-contexts$ kubectl config use-context <your cluster name>
5

Build image

$ kubectl config get-contexts$ kubectl config use-context <your cluster name>
6

Push docker image (don’t forget to login to the docker hub before run push command)

$ kubectl config get-contexts$ kubectl config use-context <your cluster name>
7

Click FOLLOW to never miss another article from me.

Use config-map.yaml to define values of environment variables that will be used in the API application

You should add the correct value for MYSQL_ROOT_HOST
Use the output of the following command

$ kubectl config get-contexts$ kubectl config use-context <your cluster name>
8

config-map.yaml

$ kubectl config get-contexts$ kubectl config use-context <your cluster name>
9

kubectl apply -f config-map.yaml

To deploy an application use web-application-deployment.yaml Add your image-name:image-tag — value that was used in the docker build command

Apa itu ingress di Kubernetes?

Sebuah obyek API yang mengatur akses eksternal terhadap Service yang ada di dalam klaster, biasanya dalam bentuk request HTTP. Ingress juga menyediakan load balancing, terminasi SSL, serta name-based virtual hosting.

Ingress untuk apa?

Ingress (atau Ingress Prime) adalah sebuah permainan realitas tertambah dalam telepon pintar yang dikembangkan oleh Niantic untuk perangkat-perangkat Android dan iOS. Ingress pertama kali dirilis untuk Android tanggal 14 Desember 2013 dan di iOS tanggal 14 Juli 2014.