kUBERNETES__1734079298

Download as pdf or txt
Download as pdf or txt
You are on page 1of 44

Kubernetes Project Deployment Guide

Architecture:

This document is a step-by-step guide to setting up a Kubernetes project on AWS using tools
like Jenkins, Docker, Kops, and GitHub. It helps you automate the process of deploying a
Python application using a CI/CD pipeline.

Here's what it covers:

1. Setting Up Jenkins on an Ubuntu EC2 instance to automate tasks.


2. Creating a GitHub Repository to store and manage your code.
3. Building a Jenkins Pipeline to pull code, build a Docker image, and push it to
DockerHub.
4. Setting Up a Kubernetes Cluster using Kops on AWS to run your application.
5. Deploying the Application to Kubernetes automatically through Jenkins.
6. Setting Up Webhooks for automatic deployments whenever you update your code.

This guide is perfect for developers who want to quickly set up and automate their application
deployments using modern DevOps tools.
Step 1: Create Ubuntu EC2 & Install the Jenkins Server
AMI: Ubuntu 24.04 AMI
Instance Type: t2.medium
Security Group: ALL TCP
### Update all software packages on Ubuntu server

sudo su - root
sudo apt-get update
sudo apt-get upgrade
### Install Java on Ubuntu server

sudo apt-get install default-jdk


java -version

### Jenkins Installation


1. Install Jenkins on Ubuntu using the official documentation.
2. Access Jenkins Url with Public ip on port 8080
3. Retrieve the initial admin password:

$cat /var/lib/jenkins/secrets/initialAdminPassword
4. Install the Suggested Plugins
5. Setup a UserName and Password
## Step 2: Get to know about Python Code and Create GitHub Repo
vi main.py

#uvicorn main:app --reload

from fastapi import FastAPI, Request, Form

from fastapi.templating import Jinja2Templates

app = FastAPI()

templates = Jinja2Templates(directory="/code")

@app.get("/")

def form_post(request: Request):

return templates.TemplateResponse('form.html',
context={'request': request})

vi pod.yaml
apiVersion: v1
kind: Service
metadata:
name: loadbalancer-svc
spec:
type: LoadBalancer
selector:
app: hello
ports:
- name: http
protocol: TCP
port: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: loadbalancer-pod
spec:
replicas: 3
selector:
matchLabels:
app: hello
template:
metadata:
labels:
app: hello
spec:
containers:
- name: hello
image: alvinselva/alvink8simage:latest
imagePullPolicy: Always
vi requirements.txt

aiofiles==0.5.0
aniso8601==7.0.0
ansible-lint==5.0.7
astroid==2.5.1
async-exit-stack==1.0.1
async-generator==1.10
awscli==1.22.97
boto3==1.21.42
botocore==1.24.42
bracex==2.1.1
certifi==2021.5.30
chardet==4.0.0
charset-normalizer==2.0.4
click==7.1.2
colorama==0.4.3
commonmark==0.9.1
cryptography==3.4.7
dnspython==2.1.0
docutils==0.15.2
email-validator==1.1.3
enrich==1.2.6
fastapi==0.68.1
graphene==2.1.9
graphql-core==2.3.2
graphql-relay==2.0.1
h11==0.12.0
idna==3.2
isort==5.7.0
itsdangerous==1.1.0
Jinja2==2.11.3
jmespath==0.10.0
lazy-object-proxy==1.5.2
MarkupSafe==2.0.1
mccabe==0.6.1
packaging==20.9
pathspec==0.8.1
pdfminer.six==20211012
promise==2.3
py4j==0.10.9
pyasn1==0.4.8
pycparser==2.20
pydantic==1.8.2
Pygments==2.8.1
pylint==2.7.2
pyparsing==2.4.7
python-dateutil==2.8.1
python-dotenv==0.19.0
python-multipart==0.0.5
requests==2.26.0
rich==10.1.0
rsa==4.5
ruamel.yaml==0.17.4
ruamel.yaml.clib==0.2.2
Rx==1.6.1
s3transfer==0.5.0
six==1.15.0
starlette==0.14.2
tenacity==7.0.0
toml==0.10.2
typing-extensions==3.7.4.3
urllib3==1.26.4
uvicorn==0.13.4
watchgod==0.7
wcmatch==8.1.2
websockets==8.1
wrapt==1.12.1
vi Dockerfile

FROM python:3.9
WORKDIR /code
COPY ./requirements.txt
/code/requirements.txt
RUN pip install --no-cache-dir --
upgrade -r /code/requirements.txt
COPY ./main.py /code/main.py
COPY ./form.html /code/form.html
CMD ["uvicorn", "main:app", "--host",
"0.0.0.0", "--port", "80"]

vi form.html
copy the source code from this link:
https://www.w3schools.com/w3css/tryit.asp?filename=tryw3css_templates_band&stacked=h

<!DOCTYPE html>
<html>
<head>
<title>W3.CSS Template</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel='stylesheet' href='https://fonts.googleapis.com/css?family=Roboto'>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-
awesome/4.7.0/css/font-awesome.min.css">
<style>
html,body,h1,h2,h3,h4,h5,h6 {font-family: "Roboto", sans-serif}
</style>
</head>
<body class="w3-light-grey">
<!-- Page Container -->
<div class="w3-content w3-margin-top" style="max-width:1400px;">
<!-- The Grid -->
<div class="w3-row-padding">
Push to files to Github
git init
git add .
git commit -m "First commit"
git push
Step 3: Create a Jenkins Pipeline Job And clone the Repo

Create a Pipeline Job in v Jenkins


1. Install Stageview plugin from manageJenkins -> Plugins -> Available plugins
1. Use the following pipeline script:

pipeline {
agent any
stages {
stage('Pull Code From GitHub') {
steps {
git 'https://github.com/Alvinbsi/k8s_project_nov13.git’
}
}
}
}
Step 4: Containerize the Python app and publish to DockerHub
Docker Installation
sudo apt install docker.io
1. Open DockerHub account
docker login

Give Jenkins Permission to run sudo commands


Run the three stages in Jenkinsfile

pipeline {
agent any

stages {
stage('Pull Code From GitHub') {
steps {
git ‘https://github.com/Alvinbsi/k8s_project_nov13.git'
}
}
stage('Build the Docker image') {
steps {
sh 'sudo docker build -t alvinimage /var/lib/jenkins/workspace/alvinproj'
sh 'sudo docker tag alvinimage alvinselva/alvinimage:latest'
sh 'sudo docker tag alvinimage alvinselva/alvinimage:${BUILD_NUMBER}'
}
}
stage('Push the Docker image') {
steps {
sh 'sudo docker image push alvinselva/alvinimage:latest'
sh 'sudo docker image push alvinselva/alvinimage:${BUILD_NUMBER}'
}
}
}
}
Step 5: Create Kubernetes Cluster

Install Kops

curl -Lo kops https://github.com/kubernetes/kops/releases/download/$(curl -s


https://api.github.com/repos/kubernetes/kops/releases/latest | grep tag_name | cut -d '"' -f
4)/kops-linux-amd64
chmod +x ./kops
sudo mv ./kops /usr/local/bin/

Install kubectl
curl -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/$(curl -s
https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl

AWS CLI Installation

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"


sudo apt install unzip
unzip awscliv2.zip
sudo ./aws/install --bin-dir /usr/local/bin --install-dir /usr/local/aws-cli --update
Create IAM user with Admin access policy and generate IAM access & Secret Key
aws configure
aws s3 ls
Create IAM user with Admin access policy and generate IAM access &
Secret Key
aws configure
aws s3 ls
Set Env Variables for Kops

export AWS_ACCESS_KEY_ID=
export AWS_SECRET_ACCESS_KEY=
export NAME=**alvin**.k8s.local
export KOPS_STATE_STORE=s3://<bucketname>

Create Kubernetes Cluster

kops create cluster --zones ap-south-1a ${NAME}


kops update cluster --name alvin.k8s.local --yes –admin
Step 6: Deploy to kubernetes

Add a new stage in Jenkins file


pipeline {
agent any

stages {
stage('Pull Code From GitHub') {
steps {
git 'https://github.com/Alvinbsi/k8s_project_nov13.git'
}
}
stage('Build the Docker image') {
steps {
sh 'sudo docker build -t alvinimage /var/lib/jenkins/workspace/alvinproj'
sh 'sudo docker tag alvinimage alvinselva/alvinimage:latest'
sh 'sudo docker tag alvinimage alvinselva/alvinimage:${BUILD_NUMBER}'
}
}
stage('Push the Docker image') {
steps {
sh 'sudo docker image push alvinselva/alvinimage:latest'
sh 'sudo docker image push alvinselva /alvinimage:${BUILD_NUMBER}'
}
}
stage('Deploy on Kubernetes') {
steps {
sh 'sudo kubectl apply -f /var/lib/jenkins/workspace/alvinproj/pod.yaml'
sh 'sudo kubectl rollout restart deployment loadbalancer-pod'
}
}
}
}
Configure Webhook in github & Jenkins

Github Repo Setting -> Webhooks -> http://<publicip>:8080/github-webhook/


Jenkins Job -> Build Triggers -> GitHub hook trigger for GITScm polling
Make a Commit & Run the Deployment
Conclusion

This document outlines the end-to-end process of setting up a Kubernetes project using Kops,
Jenkins, Docker, and GitHub on AWS infrastructure. By following these steps, you can
automate the deployment of your Python application into a Kubernetes cluster seamlessly.

The process is broken down into clear stages:

1. Setting Up Jenkins: Using an Ubuntu EC2 instance to install Jenkins for continuous
integration.
2. Creating a GitHub Repository: Managing your code in a version-controlled
environment.
3. Building a Jenkins Pipeline: Automating code deployment using Jenkins to pull from
GitHub and containerize with Docker.
4. Containerizing with Docker: Building and pushing Docker images to DockerHub for
scalable deployments.
5. Provisioning a Kubernetes Cluster: Using Kops to create a fully managed Kubernetes
cluster on AWS.
6. Automating Kubernetes Deployments: Extending the Jenkins pipeline to deploy
containers onto your Kubernetes cluster.

By leveraging CI/CD pipelines, containerization, and Kubernetes orchestration, you’ve set


up a highly efficient workflow that enables rapid development, testing, and production
deployment. This infrastructure ensures scalability, resilience, and ease of management for your
application.

You might also like