SpringBoot Demo with MySQL running on Kubernetes
kubectl create namespace demoapp
kubectl config set-context --current --namespace=demoapp
kubectl create secret generic mysql-secrets \
--from-literal=rootpassword=r00tDefaultPassword1! \
--from-literal=username=demo \
--from-literal=password=defaultPassword1! \
--from-literal=database=DB
git clone https://github.com/junior/springboot-demo-k8s-mysql.git
cd springboot-demo-k8s-mysql/kubernetes
kubectl apply -f mysql-pvc-oci-bv.yaml
Use mysql-pvc-manual.yaml if deploying local
kubectl apply -f mysql-svc.yaml
kubectl apply -f mysql-dep.yaml
Note: This step will create a new LoadBalancer on the infrastructure
kubectl apply -f app-svc.yaml
Note: The app will create the necessary tables on the MySQL on the first run
kubectl apply -f app-dep.yaml
kubectl logs -l app=demoapp --follow
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
kubectl port-forward deploy/demoapp 8081:8081
Navigate to http://localhost:8081/users
kubectl get svc
Navigate to http://<demoapp_EXTERNAL_IP_ADDRESS>/users
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
kubectl autoscale deployment demoapp --cpu-percent=30 --min=1 --max=10
kubectl get hpa
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
helm install prometheus prometheus-community/kube-prometheus-stack
kubectl get secret prometheus-grafana \
-o jsonpath="{.data.admin-password}" | base64 --decode ; echo
kubectl port-forward svc/prometheus-grafana 8085:80
Navigate to http://localhost:8085/
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 .