Before starting, ensure you have Node.js and npm installed. These are required to run and manage the React application. If they are not installed, follow the instructions for your operating system:
Use the following commands to install Node.js and npm:
sudo apt update # Updates the package list to ensure you get the latest versions of available software
sudo apt install nodejs # Installs Node.js, the JavaScript runtime required to run the app
sudo apt install npm # Installs npm (Node Package Manager), which is used to manage project dependencies
Download and install Node.js from the official website:
- Node.js Download
- The installer includes npm, so no additional installation is required.
Install Node.js using Homebrew:
brew install node # Installs both Node.js and npm
If you donβt have Homebrew installed, visit Homebrew's website for installation instructions.
If this is your first time setting up the project, follow these steps:
If the project is hosted on GitHub, you need to download it to your computer using:
git clone <repository_url> # Replaces <repository_url> with the actual project repository link
cd <project_directory> # Moves into the project directory
Dependencies are external libraries the project needs to run. Install them using:
npm install # Installs all necessary project dependencies listed in package.json
To start the application, navigate to the project directory and run:
npm start # Starts the development server and runs the React app
Once started, open http://localhost:3000 in your browser to view the application.
If you need to keep the app running even after closing the terminal, use:
sudo nohup npm start > log_file.log 2>&1 & # Runs the app in the background, logging output to log_file.log
To test the application and check for errors:
npm test # Runs tests in interactive mode, allowing you to debug potential issues
To prepare the application for deployment:
npm run build # Creates an optimized version of the app, storing it in the 'build' folder
The build version is minified and ready for hosting on a web server.
If you need to modify internal configurations of the project:
npm run eject # Permanently removes default React setup and exposes configuration files
β Warning: This cannot be undone!
PM2 helps keep your application running even after reboots and provides better process monitoring.
sudo npm install -g pm2 # Installs PM2 globally, allowing it to manage applications
pm2 start npm --name "my-app" -- start # Runs the React app under PM2 process manager
pm2 save # Saves the current PM2 process list so it restarts on reboot
pm2 startup # Configures PM2 to start automatically when the system reboots
pm2 list # Shows all applications running under PM2
pm2 stop my-app # Stops the application named 'my-app'
This guide simplifies the setup and ensures your app runs smoothly. π
This guide provides the steps to build, tag, push, pull, and rename Docker images for the Cupcake Frontend project.
To build the Docker image, run:
docker build -t cupcake-frontend:1.0 .
To run the Docker container in detached mode with port mapping:
docker run -d --name cupcake-frontend-container -p 3000:3000 cupcake-frontend:1.0
Assign a tag to the Docker image for Google Container Registry:
docker tag cupcake-frontend:1.0 us-west2-docker.pkg.dev/ucr-ursa-major-ridgwell-lab/cupcake/cupcake-frontend:1.0
- Configure Docker to authenticate with Google Container Registry:
gcloud auth configure-docker us-west2-docker.pkg.dev
- Push the Docker Image Push the tagged Docker image to Google Container Registry:
docker push us-west2-docker.pkg.dev/ucr-ursa-major-ridgwell-lab/cupcake/cupcake-frontend:1.0
- Pull the Docker Image a. On the Same Machine as the Push: If you are on the same machine where the image was pushed, use:
docker pull us-west2-docker.pkg.dev/ucr-ursa-major-ridgwell-lab/cupcake/cupcake-frontend:1.0
b. On a Different Machine or Account: If pulling from another machine or account with appropriate permissions (especially with sudo):
Create the .docker directory for root:
sudo mkdir -p /root/.docker
Copy the config.json for Docker authentication:
sudo cp ~/.docker/config.json /root/.docker/config.json
Pull the Docker image:
sudo docker pull us-west2-docker.pkg.dev/ucr-ursa-major-ridgwell-lab/cupcake/cupcake-frontend:1.0
To rename the pulled image for local use:
docker tag us-west2-docker.pkg.dev/ucr-ursa-major-ridgwell-lab/cupcake/cupcake-frontend:1.0 cupcake-frontend:1.0
- Ensure you have sufficient permissions to access Google Container Registry.
- Adjust the version tag (1.0) as required for different builds.
- Use sudo for Docker commands if necessary in your environment.
This guide outlines how to deploy the CupCake Frontend application on a Google Kubernetes Engine (GKE) cluster.
Ensure you have the following installed:
- Google Cloud SDK
- kubectl for Kubernetes
- GKE Auth Plugin
To verify installations:
gcloud version
kubectl version --client
Ensure GKE API is enabled:
gcloud services enable container.googleapis.com
Create GKE cluster(it is already created, create only if new cluster needs to be created):
gcloud container clusters create cupcake-cluster \
--region us-west2 \
--num-nodes=2 \
--enable-autoupgrade
Connect kubectl
to the cluster:
gcloud container clusters get-credentials cupcake-cluster --region us-west2
- Push Docker image to Google Artifact Registry (GAR):
gcloud auth configure-docker us-west2-docker.pkg.dev
# Tag and push the image
docker tag cupcake-frontend:1.0 us-west2-docker.pkg.dev/ucr-ursa-major-ridgwell-lab/cupcake/cupcake-frontend:1.0
docker push us-west2-docker.pkg.dev/ucr-ursa-major-ridgwell-lab/cupcake/cupcake-frontend:1.0
- **Create Kubernetes manifests:(needs to be done only once per cluster) **
deployment.yaml
:
apiVersion: apps/v1
kind: Deployment
metadata:
name: cupcake-frontend-deployment
labels:
app: cupcake-frontend
spec:
replicas: 3
selector:
matchLabels:
app: cupcake-frontend
template:
metadata:
labels:
app: cupcake-frontend
spec:
containers:
- name: cupcake-frontend
image: us-west2-docker.pkg.dev/ucr-ursa-major-ridgwell-lab/cupcake/cupcake-frontend:1.0
ports:
- containerPort: 3000
env:
- name: API_URL
value: "http://cupcake.ctoaster.org:8000"
imagePullPolicy: Always
service.yaml
:
apiVersion: v1
kind: Service
metadata:
name: cupcake-frontend-service
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 3000
selector:
app: cupcake-frontend
- Deploy to GKE:
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
- Check status:
kubectl get pods
kubectl get svc
Expected output:
NAME READY STATUS RESTARTS AGE
cupcake-frontend-deployment-abc123 1/1 Running 0 10s
Find External IP:
kubectl get svc
Expected output:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
cupcake-frontend-service LoadBalancer 34.118.232.251 34.94.127.96 80:31234/TCP 5m
Access the app:
http://34.94.127.96
This guide explains how to automatically scale the number of pods for your Kubernetes deployment based on CPU utilization.
Ensure the following are configured:
- Your Kubernetes deployment defines CPU
requests
andlimits
. - The metrics server is installed. You can install it using:
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
Create a file named hpa.yaml
with the following contents:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: cupcake-frontend-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: cupcake-frontend-deployment
minReplicas: 2
maxReplicas: 5
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 60
Apply the HPA configuration to your cluster:
kubectl apply -f hpa.yaml
Check the current status and behavior of the HPA using:
kubectl get hpa
kubectl top pods
- Check pod status:
kubectl get pods
- View pod logs:
kubectl logs -f <pod-name>
- Check deployment:
kubectl describe deployment cupcake-frontend-deployment
- Check service and external IP:
kubectl get svc
To delete the GKE cluster and avoid billing:
# Delete cluster
gcloud container clusters delete cupcake-cluster --region us-west2
# Delete Docker image from GAR
gcloud artifacts docker images delete us-west2-docker.pkg.dev/ucr-ursa-major-ridgwell-lab/cupcake/cupcake-frontend:1.0
For any issues or further assistance:
- Check GKE logs:
kubectl describe pod <pod-name>
- Visit Google Kubernetes Engine Docs
- Contact the project admin or Slack channel.
π That's it! Your CupCake frontend is now running on Kubernetes!