Repository hosting a Google Kubernetes Engine (GKE) microservices app and CI/CD pipeline
This application is based on the freeCodeCamp guide, Learn Kubernetes in Under 3 Hours: A Detailed Guide to Orchestrating Containers, by Rinor Maloku
The CI/CD pipeline developed to deploy the app and supporting infrastructure is depicted below.
The steps outlined here are based on the hands-on lab from freeCodeCamp
NOTE: for local app, navigate to
App.js
insa-frontend
and comment out line 24 and uncommment line 23.
- Navigate to
sa-frontend
and execute the following commandnpm install
OPTIONAL: start the frontend app via the below command
npm start
- Build the app
npm run build
- Copy the contents of
sa-frontend/build
to an nginx server at[your_nginx_installation_dir]/html
- Navigate to
sa-webapp
and execute the following commandmvn install
OPTIONAL: start the webapp via the below command (first navigate to
/target
)java -jar sentiment-analysis-web-0.0.1-SNAPSHOT.jar --sa.logic.api.url=http://localhost:5000
- Navigate to
sa-logic/sa
and execute the followign commandspython -m pip install -r requirements.txt
python -m textblob.download_corpora
OPTIONAL: start the logic app via the below command
python sentiment_analysis.py
- To see the app in aciton (local), run the image of nginx which has the updated
/html
fodler and execute the OPTIONAL commands in steps 5 and 6 in sparate terminal sessions
For this section, it is assumed that the reader has the Docker Desktop application and a Docker Hub account.
- Build all the Docker images
Logic Service
docker build -f Dockerfile -t <DOCKER_HUB_USERNAME>/sentiment-analysis-logic .
WebApp Service
docker build -f Dockerfile -t <DOCKER_HUB_USERNAME>/sentiment-analysis-web-app .
Frontend Service
docker build -f Dockerfile -t <DOCKER_HUB_USERNAME>/sentiment-analysis-frontend .
- Push all images to your Docker Hub
docker push <DOCKER_HUB_USERNAME>/sentiment-analysis-<SERVICE>
SERVICE being one of {
logic
,web-app
,frontend
} - Pull the images from Docker Hub
docker pull <DOCKER_HUB_USERNAME>/sentiment-analysis-<SERVICE>
SERVICE being one of {
logic
,web-app
,frontend
} - Run the Logic Service
docker run -d -p 5050:5000 <DOCKER_HUB_USERNAME>/sentiment-analysis-logic
- Run the WebApp service
docker run -d -p 8080:8080 -e SA_LOGIC_API_URL='http://<CONTAINER_IP>:5000' <DOCKER_HUB_USERNAME>/sentiment-analysis-web-app
NOTE: to retrieve the
<CONTAINER_IP>
, rundocker container list
followed bydocker inspect <CONTAINER ID>
and search the response for the container IP address. - Run the Frontend Service
docker run -d -p 80:80 <DOCKER_HUB_USERNAME>/sentiment-analysis-frontend
It is assumed the reader has installed minikube and that the images for sa-webapp
and sa-logic
have been built.
- Execute
minikube start
- Navigate to
resrouce-manifests
and execute the below command to deploy the logic servicekubectl apply -f sa-logic-deployment.yaml
- Deploy the logic service entry point service
kubectl apply -f service-sa-logic.yaml
- Deploy the webapp service
kubectl apply -f sa-web-app-deployment.yaml
- Deploy the webapp loadbalancer service
kubectl apply -f service-sa-web-app-lb.yaml
- Run the webapp load balancer service and identify the URL via
minikube service sa-web-app-lb --url
- Navigate to
sa-frontend
and modifysrc/App.js
on line 23 by replacing the URL in thefetch(...)
with the URL from step 7 - Rebuild and deploy the frontend
In
sa-frontend
docker build -f Dockerfile -t boszin/sentiment-analysis-frontend . docker push boszin/sentiment-analysis-frontend
In
resource-manifests
kubectl apply -f sa-frontend-deployment.yaml
- Deploy the frontend loadbalancer service
kubectl apply -f service-sa-frontend-lb.yaml
OPTIONAL: check your deployement via
minikube service sa-frontend-lb
These steps are based on the guide here
- Navigate to
terraform/terraform.tfvars
and setproject_id
to your Google Cloud Project - In the
terraform
folder, executeterraform init
- Execute
terraform apply
, review the chagnes and if all looks well, typeyes
- Configure your
kubectl
via the below commandgcloud container clusters get-credentials <kubernetes_cluster_name> --region <region>
<kubernetes_cluster_name>
and<region>
should have been an output from step 4. - To tear down the deployement (and avoid incurred costs) run
terraform destroy
when done experimenting
Ensure gcloud
and gke-gcloud-auth-plugin
have been installed and are configured
- Navigate to the
resource-manifests
fodler and execute the same commands as Deploying App to Local Kubernetes Cluster - For step 6 in the above reference, replace
minikube service list
withkubectl describe services sa-web-app-lb
to get the URL.
This repository is enabled with GitHub workflows (see .github/workflows
) which automate the manual tasks and commands performed above. The workflows are based on the guide here.
NOTE 1: Changes to the WebApp Microservice will require an update to the Frontend service. Line 23 in
App.js
must be updated with the URL of the WebApp Loadbalancer upon re-deploy; and the frontend must be rebuilt and redeployed as a result. Parametarizing this URL is a future enhancement.
NOTE 2: Ensure that the account used for your CI/CD pipelines has the following permissions:
- Compute Admin
- Compute Network Admin
- Kubernetes Engine Admin
- Service Account User
- Storage Admin
Other references:
- https://cloud.google.com/kubernetes-engine/docs/archive/using-container-image-digests-in-kubernetes-manifests#using_kustomize
- https://cloud.google.com/blog/products/identity-security/enabling-keyless-authentication-from-github-actions
- https://github.com/google-github-actions/auth#setup
To test out the Blue/Green deployment scheme, perform the following:
- In
sa-frontend/App.js
change line 49 by adding "(Blue)" to the title - In
resource-manifests-gke/sa-frontend/deployment.yml
edit the deployement to be namedsa-frontend-blue
- In the same location, ensure the
service.yml
points to thesa-frontend-blue
selector - Push the code to build and deploy the deployement and service
- Update line 49 in
sa-frontend/App.js
from "Blue" to "Green" - In
resource-manifests-gke/sa-frontend/deployment.yml
edit the deployement to be namedsa-frontend-green
and push your code to automatically build the new deployement - When you are ready, open the frontend page and you will see it has "Blue" in the title
- In a new terminal, navigate to
resource-manifests-gke/sa-frontend
and modifyservice.yml
to point to thesa-frontend-green
selector - Execute
kubectl apply -f service.yml
- Go back to your browser and refresh the page a few times. After a few seconds, you should now see that the title went from having "Blue" to having "Green"
To enable the Managed Anthos Service Mesh, follow the steps here
Note: the GKE deployment for this application can be enabled with Workload Identity via Terraform (see here); however, the functionality was ommited for the original inention of the project.