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

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

$ 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

$ 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

$ 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

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The Weekly Dharma: July 1

Lifecycle Methods in SwiftUI

Programming languages with the most amount of jobs

I am getting cheaper pricing for hosting from other providers. Why is your price high?

Engineering Journal sdc

AIOps Series V 丨 RCA Based on Knowledge Graph

Stylizing Video by Example

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/

More from Medium

How to install docker on RHEL using Ansible role

Docker Network Drivers Overview | Networking in Docker #3

Linux CLI Apache Web Server Install

Test Automation of Python project in Dockerized Jenkins