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

As in our previous blog, we had just saw a very basic deployment of a python-flask application on Kubernetes using minikube . Moving forward to the second part of this blog, we will be extending the python-flask application to use the DB and deploy it within the K8s cluster. Moreover we can have a look at how the new updates are being rolled in the clustered environment. While the DB will be in place then the management of secrets and other connectivity issues will also needs to be be addressed in order to make it up and running.

Creating Configuration for MySQL Database

---
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
...

The configuration listed above is same as used in our previous blog, Once we will save this file, we will be running the samekubectl command and pass the file as the argument

$ kubectl apply -f db-deployment.yml

Once you apply the configuration and run the command kubectl get pods then you will see your pods in a running state. Once complete, you can inspect the components deployed via the standard kubectl commands.

$ kubectl get pods
$ kubectl get deployments/mysql
$ kubectl get services/mysql

After checking the status, you can see that the MySQL database is now up and running. For validating it we can simply use the local mysql client application to access the Service running.

kubectl get pods command status

Here we have both DB and Python application pods in running state, Now we will jump towards linking them .

Linking the DB with Python Application

Creation of a DB named pyapplicationdb inside the mysql container, First just taking the access of the container;

$ kubectl exec -it <podname> -- /bin/sh

once we have the access, we can do the magic easily;

$ 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

The DB is now being populated, so we will once again just make the python application up and see the results;

$ kubectl apply -f app-deployment.yml

Once it is up and running, we can see the results in our browser via minikube service command;

$ minikube service pyapplication --url

Managing Database credentials secretly

we will create a YAML configuration file for the database user password Secret. There is one extra step involved in creating a Secret configuration file which is to base64-encode the sensitive values prior to placing them into the YAML configuration file:

$ echo -n 'Passw0rd' | base64

Next step is to create the Secret YAML file, named secret.ymlin order to bind it to our db-deployment config;

apiVersion: v1
kind: Secret
metadata:
name: mysql-password
type: Opaque
data:
mysql_password: UGFzc3cwcmQ=

Save the file above and then run the kubectl command to create the secret:

$ 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

Now that we have a secret created within the cluster, we can update our app-deployment.yml file, so that it can fetch the credentials from the secrets we have just created.

- name: MYSQL_DB_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-password
key: mysql_password

Rolling out the updates

For doing it via the kubectl edit command;

$ 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

Happy coding and kubernetesing!

--

--

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