Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sampathshivakumar/3b1171d3fd3c244935b1494e18237468 to your computer and use it in GitHub Desktop.
Save sampathshivakumar/3b1171d3fd3c244935b1494e18237468 to your computer and use it in GitHub Desktop.

How to Deploy a Flask Application to a EKS Cluster using Jenkins and ArgoCD.

GitOps

Prerequisites.

  • Jenkins Server up and running.
  • Docker and git installed inside Jenkins Server.
  • Docker Hub account.
  • AWS Account.
  • GitHub Account.
  • EKS Cluster running
  • Basic Understanding of Jenkins, Docker and Kubernetes.

Reference:-

Note:- Most of things are hardcoded in jenkins and deployment files,So Do Not change the following.

  • Repository names that you fork.
  • Jenkins job names
  • docker-hub repo name
  • Don't change anything expect your username,passwords.

After forking Repositories make these changes immediately.

# In Python-Source-Code/Jenkinsfile

replace "dockersampath/packages" with "<your-dockerhub-username>/<repo-name>"
Note:- better don't change your repo name in docker hub.
# In K8s-Manifests/Jenkinsfile

replace bellow two line:-
sh "git config user.email sampathshivakumar@gmail.com"   ---> sh "git config user.email <your-email-id>" 
sh "git config user.name sampathshivakumar"              ---> sh "git config user.name <git-user-name>"

sh "sed -i 's+dockersampath/packages.*+dockersampath/packages:${DOCKERTAG}+g' deployment.yaml"
sh "sed -i 's+<your-docker-hub-usename>/packages.*+<your-docker-hub-usename>/packages:${DOCKERTAG}+g' deployment.yaml"
# In K8s-Manifests/deployment.yaml
replace
- image: dockersampath/packages:5  ---> - image: <your-docker-hub-username>/packages:5

Don't worry we start from scratch.

Jenkins installation.

Select Amazon Linux-2 AMI.

t2.micro

Enter the following command as root.

# To download Jenkins repo.
wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
# To import key.
rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key

# Install java.
amazon-linux-extras install  java-openjdk11 -y
# Check Version of Java.
java --version

#Install Jenkins.
yum install jenkins

# You can enable the Jenkins service to start at boot with the command.
systemctl enable jenkins

# You can start the Jenkins service with the command.
systemctl start jenkins

# You can check the status of the Jenkins service using the command.
systemctl status jenkins

Unlocking Jenkins.

**Browse to http://localhost:8080 and unlock jenkins by inputing password. 7

Install selected plugins.

8

9

Create Username and Password.

10

11

12

You should see Jenkins Dashboard.

Untitled

Now let's download the Docker in the same server.

# Become root user
sudo su -

# Apply updates
yum update -y

# Install Docker
yum install docker -y

# Let’s check the version and info of the docker
docker info

1

# Enable Docker during boot time
systemctl enable docker.service

# Start Docker
systemctl start docker.service

# Check the status of Docker
systemctl status docker.service

You should see 2

Commands to control the docker service

sudo systemctl start docker.service -->   To start the service
sudo systemctl stop docker.service -->    To stop the service
sudo systemctl restart docker.service --> To restart the service
sudo systemctl status docker.service -->  To get the service status

Now both Jenkins and Docker are Installed in our server.

Install Git also in the same server.

# Install git
yum install git -y
 
# Check the version of git
git --version

Integrate Docker with Jenkins.

You have to run docker commands using jenkins user, while runing jenkins job.

# Add Jenkins user to Docker Group.
usermod -a -G docker jenkins

# Reload a Linux user's group assignments to docker
newgrp docker

# Check the user id. to see group we added 
id jenkins 

3

Install the following plugins in Jenkins using Jenkis Dashboard.

  • Docker Pipeline. 4

5

Create a Repository name "packages" in your Docker Hub.

6

Add your GitHub and Docker Hub credentials in your Jenkins credentials.

  • Save GitHub credentials with ID "github"
  • Save Docker Hub credentials with ID "dockerhub"
  • Dont change "ID", "Repository" or any names as we are involving save name in jenkins-pipelines file. 7

Note:-For GitHub use Personal access tokens as password. 9

Note:-GitHub username means not your email. You can find it here on your GitHub page. 8

Lets create two jobs on Jenkins.

Job-1 10 Select Pipeline from scm and give Python-Source-Code-git repo url and change branch to main and click save.

11

Job-2 12

Select This project is parameterized, give name "DOCKERTAG",Default Value as "latest"
15

Select Pipeline from scm and give K8s-Manifests-git-repo url and change branch to main and click save.

13

14

Select Job-1 "buildimage" and click on "Build Now"

If you have configured everything correctly you should see all stages of pipeline executed successfully 16

Job-2 should also get trigged automatically 17

Congratulations you have done 90 % of Project as of now. Now we just need to setup EKS Cluster and install ArgoCD init.

Launch a new instance and install AWS CLI, eksctl, kubectl in to Create, and interact with EKS Cluster in AWS.

Select Amazon Linux-2 AMI.

t2.micro

AWS CLI Installation

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

# Check AWS CLI version
aws --version

Now Configure the AWS CLI with your AWS "Access key" and "Secret access key".

# trype aws configure and press enter, then follow the flow.
aws configure

18

Now Check by listing s3 buckets in your AWS account using AWS CLI. 19

Done, its showing my Bucket successfully, AWS CLI is configured correctly

Now lets install eksctl.

ARCH=amd64
PLATFORM=$(uname -s)_$ARCH
curl -sLO "https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$PLATFORM.tar.gz"
tar -xzf eksctl_$PLATFORM.tar.gz -C /tmp && rm eksctl_$PLATFORM.tar.gz
sudo mv /tmp/eksctl /usr/local/bin

# Check the eksctl version.
eksctl version

20

Lets Install kubectl

curl -O https://s3.us-west-2.amazonaws.com/amazon-eks/1.27.1/2023-04-19/bin/linux/amd64/kubectl
chmod +x ./kubectl
mkdir -p $HOME/bin && cp ./kubectl $HOME/bin/kubectl && export PATH=$HOME/bin:$PATH
echo 'export PATH=$HOME/bin:$PATH' >> ~/.bashrc
kubectl version --short --client

21

Command to Create EKS Cluster using eksctl command

eksctl create cluster --name <name-of-cluster> --nodegroup-name <nodegrpname> --node-type <instance-type> --nodes <no-of-nodes>

eksctl create cluster --name mycluster --nodegroup-name ng-test --node-type t3.medium --nodes 2

22

It will take 5-10 mins to create cluster.

We can see as of now there is no cluster in EKS 23

25

It's Done. Now lets see the cluster 24

Lets test some kubectl commands 26

EKS Cluster is up and ready.

Now lets install ArgoCD in EKS Cluster.

# This will create a new namespace, argocd, where Argo CD services and application resources will live.
kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml

Download Argo CD CLI

curl -sSL -o argocd-linux-amd64 https://github.com/argoproj/argo-cd/releases/latest/download/argocd-linux-amd64
sudo install -m 555 argocd-linux-amd64 /usr/local/bin/argocd
rm argocd-linux-amd64

Access The Argo CD API Server

# By default, the Argo CD API server is not exposed with an external IP. To access the API server, 
choose one of the following techniques to expose the Argo CD API server:
* Service Type Load Balancer
* Port Forwarding

Lets go with Service Type Load Balancer.

# Change the argocd-server service type to LoadBalancer.
kubectl patch svc argocd-server -n argocd -p '{"spec": {"type": "LoadBalancer"}}'

27

Get the load balancer url

kubectl get svc -n argocd

28

Lets access the ArgoCD GUI 29

30

Enter the user name and Password 31

Login Using The CLI

argocd admin initial-password -n argocd

32

33

ArgoCD Dashboard 34

Click on New App 35

Enter Repository URL,set path to ./ , Cluster URL to https://kubernetes.default.svc, namespace to default and click save. 36

You should see the below, once your done 37

Click on it 38

You can see the pods running in EKS Cluster

39

We can see the out of pods using load balancer url

kubectl get svc

40

41

ArgoCD will automatically syn for every 3 mins to manifest repo to pull and apply changes to EKS Cluster.

If your are intrested you can apply github web hook to automatically trigger jenkins job when developer commit changes in git repo. So that ArgoCD can pull those changes and apply in EKS Cluster.

Lets Do some changes in code and the output will automatically change or not.

Yes it observed some changes 42

43

Here is the output


44

We have successfully Deployed a Flask Application to a EKS Cluster using Jenkins and ArgoCD.

Clean up Cluster

eksctl delete cluster --name <name-of-cluster>

eksctl delete cluster --name mycluster

Thank you for reading this post! I hope you found it helpful. If you have any feedback or questions,Please connect with me on LinkedIn at https://www.linkedin.com/in/sampathsivakumar-boddeti-1666b810b/. Your feedback is valuable to me. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment