junior / springboot-demo-k8s-mysql

SpringBoot Demo with MySQL running on Kubernetes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Springboot demo app

SpringBoot Demo with MySQL running on Kubernetes

Deploy to Kubernetes

Create namespace

kubectl create namespace demoapp

Default namespace to demoapp

kubectl config set-context --current --namespace=demoapp

Create MySQL Secrets

kubectl create secret generic mysql-secrets \
  --from-literal=rootpassword=r00tDefaultPassword1! \
  --from-literal=username=demo \
  --from-literal=password=defaultPassword1! \
  --from-literal=database=DB

Clone the repo if using OCI CloudShell or local

git clone https://github.com/junior/springboot-demo-k8s-mysql.git
cd springboot-demo-k8s-mysql/kubernetes

Deploy MySQL 8

Create PVC for MySQl on Oracle Cloud Infrastructure using CSI for Block Volume

kubectl apply -f mysql-pvc-oci-bv.yaml

Use mysql-pvc-manual.yaml if deploying local

Create Service for MySQL

kubectl apply -f mysql-svc.yaml

Create Deployment for MySQL

kubectl apply -f mysql-dep.yaml

Deploy the Spring Boot Demo App

Create Service for Demo App

Note: This step will create a new LoadBalancer on the infrastructure

kubectl apply -f app-svc.yaml

Create Deployment for Demo App

Note: The app will create the necessary tables on the MySQL on the first run

kubectl apply -f app-dep.yaml

Optional: Check logs

kubectl logs -l app=demoapp --follow

Optional: Insert Data to MySQL

Connect to mysql
kubectl run -it --rm --image=mysql:8 --restart=Never mysql-client -- mysql DB -h mysql -pr00tDefaultPassword1!

Press enter

If you don't see a command prompt, try pressing enter.

mysql>
insert into users (first_name, last_name) values ('joe', 'doe');

Expected results:

If you don't see a command prompt, try pressing enter.

mysql> insert into users (first_name, last_name) values ('joe', 'doe');
Query OK, 1 row affected (0.00 sec)

mysql> quit
Bye
pod "mysql-client" deleted

Optional: Test with port-forward

kubectl port-forward deploy/demoapp 8081:8081

Navigate to http://localhost:8081/users

Test with LoadBalancer IP Address

kubectl get svc

Navigate to http://<demoapp_EXTERNAL_IP_ADDRESS>/users

Create Horizontal Pod Autoscaler for Demo App

Install metrics server

kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml

Create autoscale for Demo App

kubectl autoscale deployment demoapp --cpu-percent=30 --min=1 --max=10

Check HPA

kubectl get hpa

Increase load

kubectl run -i --tty load-generator --rm --image=busybox --restart=Never -- /bin/sh -c "while sleep 0.01; do wget -q -O- http://demoapp/users; done"

Within a minute or so, we should see the higher CPU load by executing:

kubectl get hpa

Prometheus and Grafana

Install the grafana-prometheus stack

helm install prometheus prometheus-community/kube-prometheus-stack

get the grafana admin password

kubectl get secret prometheus-grafana \
 -o jsonpath="{.data.admin-password}" | base64 --decode ; echo

Test Grafana with port-forward

kubectl port-forward svc/prometheus-grafana 8085:80

Navigate to http://localhost:8085/

Build Demo App image

Skip this step if you just want to test the app on Kubernetes

docker build --pull --no-cache --squash --rm --progress plain -f Dockerfile -t sbdemo .

About

SpringBoot Demo with MySQL running on Kubernetes

License:Universal Permissive License v1.0


Languages

Language:Java 85.6%Language:Dockerfile 14.4%