Kubernetes Cluster
Overview
Kubernetes cluster that shows how to deploy and connect Go backend, React frontend, MongoDB, Redis cache with each other.
After running the cluster you can access the frontend by going to simple-frontend.io in your browser.
If you can increment the counter in:
-
simple-frontend.io then react frontend and go backend are successfully connected to each other.
-
simple-frontend.io/mongo then go backend and mongoDB are successfully connected to each other.
-
simple-frontend.io/redis then go backend and redis are successfully connected to each other.
Demo
Backend Connected
Mongo Connected
Redis Connected
Requirements
- Minikube
- Kubectl
- Docker
- Git
Cluster Diagram
Create Cluster
You can create the cluster:
Using Scripts Inside scripts
Directory
Run the following command
bash scripts/init.sh && bash scripts/build-and-push.sh && bash scripts/apply-config-files.sh
Explanation:
scripts/init.sh
: starts minikube, enables required addons, runs local container image registry on port5000
scripts/build-and-push.sh
: builds the required images and pushes them to local container image registryscripts/apply-config-files.sh
: applies configuration files inconfiguration
folder
If you want to delete all created resources by these scripts you can use use the following command bash scripts/clean.sh
Manually
Follow the following steps to create the cluster from scratch using minikube
, docker
, kubectl
and git
-
Start minikube
minikube start
-
To make the development process easier (Optional):
-
Create in-cluster container image registry
REGISTRY_PORT=5000 minikube start minikube addons enable registry kubectl port-forward --namespace kube-system service/registry "$REGISTRY_PORT":80 & [ -n "$(docker images -q alpine)" ] || docker pull alpine docker run --rm -it --network=host alpine ash -c "apk add socat && socat TCP-LISTEN:$REGISTRY_PORT,reuseaddr,fork TCP:$(minikube ip):$REGISTRY_PORT"
-
Push images to in-cluster registry
PORT=5000 BACKEND_IMAGE_TAG="localhost:$PORT/simple-go-backend" FRONTEND_IMAGE_TAG="localhost:$PORT/simple-react-frontend" docker build backend -t "$BACKEND_IMAGE_TAG" docker push "$BACKEND_IMAGE_TAG" docker build frontend -t "$FRONTEND_IMAGE_TAG" docker push "$FRONTEND_IMAGE_TAG"
If you want to skip the step of creating in-cluster registry and pushing images to it, you need to use the image uploaded previously do dockerhub. You have to replace:
localhost:5000/simple-react-frontend
inconnecting-frontend-backend/frontend-deployment.yaml
withoradwan/simple-react-frontend
and
localhost:5000/simple-go-backend
inconnecting-frontend-backend/backend-deployment.yaml
withoradwan/simple-go-backend
-
-
Enable ingress addon
minikube addons enable ingress
-
Add DNS records of ingress endpoint to
/etc/hosts
file using the following commandDNS_RECORD="$(minikube ip) simple-backend.io simple-frontend.io" HOSTS_PATH="/etc/hosts" grep -Fxq "$DNS_RECORD" "$HOSTS_PATH" || echo "$DNS_RECORD" | sudo tee -a "$HOSTS_PATH"
-
Create the persistent volume, persistent volume claim, config map
kubectl apply -f configuration/pv-pvc-cm-ingress
-
Create services, deployments
kubectl apply -f configuration/service-deployment
-
View minikube dashboard in the browser (Optional)
minikube dashboard
-
You can now access the frontend by openning simple-frontend.io in the browser