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

3 Ways to Foster a Great Remote Engineering Culture

Rescuing kvkbd

Test driven development with flutter, repository & BLoC

Simple Way to Send Emails in Laravel

Reduce Cost and Increase Productivity with Value Added IT Services from buzinessware — {link} -

Unit Testing on dbt Models Using a Static Test Dataset in Snowflake

Common Test Automation Misconceptions

Rivet’s expanded free tier, and how it stacks up against competing Ethereum node infrastructure…

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 automatically build an Airflow client on a AWS EC2 backed by RDS using Terraform

Kubernetes deployment of multi-stage pipeline tasks using Redis

Integrating AWS APIs in Python — Using AWS S3 with the boto3 SDK

Minikube and Kubectl — Setup in Linux