Getting Hands-on with Kubernetes using Simple Python Application (Part II)

Creating Configuration for MySQL Database

We can now have a look at the YAML configuration file for the deployment of MySQL database in our K8s environment. For that purpose we will be creating a file named “db-deployment.yml” with the following content listed below:

---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
labels:
app: db
spec:
selector:
matchLabels:
app: db
template:
metadata:
labels:
app: db
spec:
containers:
- name: mysql
image: mysql
imagePullPolicy: Never
env:
- name: MYSQL_ROOT_PASSWORD
value: Passw0rd
ports:
- containerPort: 3306
name: db-container
---
apiVersion: v1
kind: Service
metadata:
name: mysql
labels:
app: db
spec:
ports:
- port: 3306
protocol: TCP
name: mysql
selector:
app: db
type: LoadBalancer
...
$ kubectl apply -f db-deployment.yml
$ kubectl get pods
$ kubectl get deployments/mysql
$ kubectl get services/mysql
kubectl get pods command status

Linking the DB with Python Application

As the DB is now up and running and we have YAML files for both application and the database. Now we can setup the application in order to talk with the the database. In order to do so, we can just fill our DB with some sample values for testing the connectivity.

$ kubectl exec -it <podname> -- /bin/sh
$ mysql -u root -pmysql> create database pyapplicationdb;
mysql> use pyapplicationdb;
mysql> create table random_words(word_id int auto_increment, word varchar(255) not null, primary key(word_id));
mysql> insert into random_words (word) values ('randword1'), ('randword2'), ('randword3');
mysql> select * from random_words;
creation of DB and records within MYSQL container
$ kubectl apply -f app-deployment.yml
$ minikube service pyapplication --url

Managing Database credentials secretly

Currently, we have used a very basic technique just to setup things. There is a lot of improvement to be done in order to make it looks good. First of all we have passed the DB credentials in a plain format in our db-deployment.yml configuration file. Kubernetes secrets will be helpful in this case, To use a Secret, a Pod needs to reference the Secret. A Secret can be used with a Pod in three ways:

$ echo -n 'Passw0rd' | base64
apiVersion: v1
kind: Secret
metadata:
name: mysql-password
type: Opaque
data:
mysql_password: UGFzc3cwcmQ=
$ kubectl create -f secret.yml# inspect that the secret was created
$ kubectl get secrets/mysql-password
# decode the secret to validate the contents
$ kubectl get secrets/mysql-password -o yaml
- name: MYSQL_DB_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-password
key: mysql_password

Rolling out the updates

As we are now up and running. So now if we have to perform or make some changes or update. We have couple of different ways to achieve this. One thing is to do it via command-line (being explicit to change the image of the deployed application) or may be via the kubectl edit command, which will allow updating the Deployment YAML config file and then handling the changes for you.

$ kubectl edit deployment/pyapplication# check pods now and we can see updates to the pods being made 
$ kubectl get pods
# check the rollout status for the updates made
$ kubectl rollout status deployment/pyapplication

In A Nutshell

It was just a basic tutorial just to get the concept of how things are working here. Obviously it was not written with the best practices. There are a lot of things to be included or added. Just to give a hint, we can work more on secrets, configmaps, persistent volumes.
Moreover modern practices involves different tools for deploying the cluster, some of the examples are helmcharts and there are a lot others as well.
Moreover we can also keep an eye on curated list with different options available. Here is Nubenetes or this list may be.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Syed Saad Ahmed

Syed Saad Ahmed

Python, DevOps, Cryptography, Infra-Structure Automation. https://syedsaadahmed.com/