set up your Kubernetes project with two instances, one for the CI server and another for the deployment server:
-
CI Server (t2.micro):
- Launch an EC2 instance with the Ubuntu AMI (t2.micro).
- SSH into the instance.
- Update the package list and install Docker:
sudo apt update sudo apt install docker.io -y
- Add your user to the docker group to run Docker commands without sudo:
sudo usermod -aG docker $USER && newgrp docker
- Clone your GitHub repository for the project:
git clone https://github.com/vishal815/DevOps-Dcoker-Kubernet-reddit-webapp-project.git
-
Deployment Server (t2.medium):
- Launch another EC2 instance with the Ubuntu AMI (t2.medium).
- SSH into the instance.
- Install Docker, Minikube, and kubectl:
sudo apt update sudo apt install docker.io -y sudo usermod -aG docker $USER && newgrp docker curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 sudo install minikube-linux-amd64 /usr/local/bin/minikube sudo snap install kubectl --classic minikube start --driver=docker
These steps will set up your CI server with Docker installed and your GitHub repository cloned, and your deployment server with Docker, Minikube, and kubectl installed.
- replace vishallazrus with your dockerhub user name.
Step 1: Clone the Source Code SSH into your CI server instance and clone the source code from your GitHub repository:
git clone https://github.com/vishal815/DevOps-Dcoker-Kubernet-reddit-webapp-project.git
Step 2: Containerize the Application using Docker Create a Dockerfile in the root of your project directory with the following content:
FROM node:14-alpine
WORKDIR /app
COPY . .
RUN npm install
EXPOSE 3000
CMD ["npm", "start"]
Step 3: Building Docker Image Build the Docker image using the Dockerfile you created:
docker build -t vishallazrus/reddit-webapp .
Step 4: Push the Image to DockerHub Login to DockerHub:
docker login
Push the Docker image to DockerHub:
docker push vishallazrus/reddit-webapp
After pushing the image to DockerHub, you can verify its presence by logging into hub.docker.com and checking if the image is listed there.
Here's a step-by-step guide with the correct commands to follow on your Deployment server after installing Docker and Minikube:
- Create a directory and navigate to it:
mkdir k8s
cd k8s
- Write a Kubernetes Manifest File for Deployment:
nano deployment.yaml
Paste the following content into the file and save it:
apiVersion: apps/v1
kind: Deployment
metadata:
name: reddit-webapp-deployment
spec:
replicas: 2
selector:
matchLabels:
app: reddit-webapp
template:
metadata:
labels:
app: reddit-webapp
spec:
containers:
- name: reddit-webapp
image: vishallazrus/reddit-webapp
ports:
- containerPort: 3000
- Write a Kubernetes Manifest File for Service:
nano service.yaml
Paste the following content into the file and save it:
apiVersion: v1
kind: Service
metadata:
name: reddit-webapp-service
spec:
type: NodePort
ports:
- port: 3000
targetPort: 3000
nodePort: 30000
selector:
app: reddit-webapp
- Deploy the App to Kubernetes & Create a Service: Apply the deployment and service YAML files to your Kubernetes cluster:
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
If You want to check your deployment & Service use the command kubectl get deployment
& kubectl get services
- Access the Application: To access the application, you can use Minikube's service command:
minikube service reddit-webapp-service
This command will open the application in your default web browser.
Alternatively, you can use port forwarding to access the application:
kubectl port-forward svc/reddit-webapp-service 3000:3000 --address 0.0.0.0 &
Now you can access the application using the Deployment server's IP address and port 3000, like so:
http://<ip_address_of_deployment_server>:3000
Ingress in Kubernetes allows for a more flexible and fine-grained control over incoming traffic, it can provide a number of different features such as load balancing, SSL termination, and rate limiting
Here are the steps and commands to configure Ingress and expose your application in Kubernetes project:
Step 6: Configure Ingress
- Create an ingress.yaml file:
nano ingress.yaml
- Insert the following code into the file and save it (replace "domain.com" and "reddit-clone-service" with your actual domain and service names):
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-reddit-app
spec:
rules:
- host: "domain.com"
http:
paths:
- pathType: Prefix
path: "/test"
backend:
service:
name: reddit-clone-service
port:
number: 3000
- host: "*.domain.com"
http:
paths:
- pathType: Prefix
path: "/test"
backend:
service:
name: reddit-clone-service
port:
number: 3000
- Enable Ingress in Minikube:
minikube addons enable ingress
- Apply the Ingress configuration:
kubectl apply -f ingress.yaml
- Verify that the Ingress resource is running correctly:
kubectl get ingress ingress-reddit-app
Step 8: Expose the App
- Expose your deployment:
kubectl expose deployment reddit-clone-deployment --type=NodePort
- Test your deployment using curl:
curl -L http://<minikube_ip>:<node_port>
Replace <minikube_ip>
and <node_port>
with the actual values from your setup.
- Expose your app service using port forwarding:
kubectl port-forward svc/reddit-clone-service 3000:3000 --address 0.0.0.0 &
- Test Ingress: Use curl to test your Ingress configuration:
curl -L http://domain.com/test
Replace "domain.com/test" with the actual URL path specified in your Ingress configuration.
Additionally, you can access the deployed application on your EC2 instance using its IP address and port 3000, but make sure to open port 3000 in the security group of your EC2 instance.
If you are stopping your instance and want to access your application using http://<ip_address_of_deployment_server>:3000
, you will need to follow these steps:
- Start the Kubernetes service and make sure your application's pods are running:
sudo systemctl start kubelet
kubectl get pods
Ensure that your security group allows inbound traffic on port 3000 to access your application externally. Note:- The shown IP is my temporary IP it will be destroyed after my instance restart.