Project Intoduction
Today we are going to deploy 2 version of tetris game on aws EKS ckuster for this we are using terraform to create a base instance called the jenkins server than from this intance we setup pipeline for two version of tetris , which build docker image and push it to dockerhub than setup eks cluster and argo cd inside the cluster to deploy our application
Completion steps →
Step 1 → Setup Terraform and configure aws on your local machine
Step 2 → Building a simple Infrastructure from code using terraform
Step 3 → Setup Sonarqube and jenkins
Step 4 → ci-cd pipeline to build and push the image to docker hub
Step 5→ Update Image name In deployment repo
Step 6→EKS cluster creation
Step 7→Install Argo cd
Step 8 →Deploy Application with ArgoCD
Step 9 →Destruction
Step 1 → Setup Terraform and configure aws on your local machine
- open your terminal and make a separate folder for Tetris→mkdir tetris-game
- cd tetris-game
- clone the github repo
git clone https://github.com/Aakibgithuber/Tetris-gamev1.git
1. Setup Terraform
To install terraform copy and paste the below commands
sudo su
snap install terraform --classic
which terraform
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:728/0*EztCJv7pgzaSa4qJ.png?resize=582%2C59&ssl=1)
2. Configure aws
1. Create an IAM user
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*_ZOI5WKrCuiJTyCL.png?resize=700%2C389&ssl=1)
click on iam
2. click on user →create user
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:320/0*E2-cjaSFpHREtvLk.png?resize=256%2C364&ssl=1)
3. Give a name to your user and tick on provide user access to management console and then click on I want an IAM user option
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*q1X0CiCSQwZe5h1y.png?resize=700%2C394&ssl=1)
5. choose a password for your user →click next
6. Attach the policies directly to your iam user → click next
note →I will provide the administrator accesss for now but we careful while attaching the policies at your workapce
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*sLLz0hlhGa0rPb7S.png?resize=700%2C394&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*W-guwEFnrWRWPDGj.png?resize=700%2C394&ssl=1)
review and create user
7. click on create user
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*e6fIORSf33f7r3PW.png?resize=700%2C394&ssl=1)
8. download your password file if it is autogenerated otherwise it is your’s choice
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*K9k4bGzQvNdeA78c.png?resize=700%2C162&ssl=1)
9. Now click on your IAM user →security credentials
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*F32uvZ773TC_UKM1.png?resize=700%2C80&ssl=1)
10. scroll down to access keys and create an access keys
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*oj3KuuZT-sjFT66y.png?resize=700%2C271&ssl=1)
11.choose aws cli from the options listed
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*URzAr4a2F76DYlik.png?resize=700%2C394&ssl=1)
12. click next and download you csv file for username and password
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*09l6MSvzLd0bX9L1.png?resize=700%2C394&ssl=1)
13. go to your terminal and type →aws configure
14. Now it is ask to your access key and secret key for this open your csv file and paste the access and secret key and remain everything default
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*Bp-ZUjMhm0JpTM63.png?resize=700%2C77&ssl=1)
15. Now you are ready to configure aws from your terminal
Step 2 → Building a simple Infrastructure from code using terraform
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:825/1*-fZ9enxOmsTKX7xAyPbgsw.png?resize=660%2C85&ssl=1)
- go to folder → cd Jenkins-terraform
- there are four files present main.tf, install_jenkins.sh , provider.tf, backend.tf
- open the file →vim Main.tf
- main.tf includes userdata which links install_jenkins.sh file on which execution install jenkins,docker,trivy,and start the sonarqube container on port 9000
Note →you need to edit the key_name and ami section according to your aws account
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*Z6uVGCEoz93oGrqun_ZUHA.png?resize=700%2C314&ssl=1)
5. exit from the file and edit backend.tf →vim backend.tf
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*_mY183keSQrBMzVQH6kM9g.png?resize=700%2C147&ssl=1)
Now run terraform commands →
terraform init
terraform validate
terraform plan
terraform apply --auto-approve
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:1083/1*Jvc7Lf2MSlt9F6jbgvo-ig.png?resize=500%2C629&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:1083/1*nZ8kOSM5CUgrQy_urJmc2A.png?resize=500%2C629&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*ZXAM2YvJdibt2HpLQOLr-A.png?resize=700%2C509&ssl=1)
Go to your aws console and checkout the ec2 instances
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*H47PSOkCIZCn7G0_7pZsSQ.png?resize=700%2C99&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*NYbpM8A18gRaXIc1aJZdUg.png?resize=700%2C319&ssl=1)
Here we see Jenkins-Server instance is created by terraform with the given configuration
Step 3 → Setup Sonarqube and jenkins
1.Sonarqube →
copy the public ip of your machine
- go to your browser and type →<publicip>:9000
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*CfRg3jZ4dABNsonL.png?resize=700%2C165&ssl=1)
sonarqube window open
2. iniatially username and password is admin
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:714/0*AykhNZePseFIY48F.png?resize=571%2C595&ssl=1)
3. update your password
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*1txa4CephrgYWca5.png?resize=700%2C394&ssl=1)
4. welcome window of Sonarqube
2. Jenkins →
- on browser type →<public_ip>:8080
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*dK5fWA5qiBOdpsd0.png?resize=700%2C394&ssl=1)
2. for this go to your ec2 and connect it
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*HiyCUqCJhXscoCJh.png?resize=700%2C394&ssl=1)
3. run the below commands
sudo su
cat /var/lib/jenkins/secrets/initialAdminPassword
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*s1-dTQ6VSxav_E8l.png?resize=700%2C50&ssl=1)
output is your password and paste it to your jenkins
4. Install the suggested plugins
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*qtVyizwhBAS7jSHZ.png?resize=700%2C394&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*SpOJSAzx00tGqbrO.png?resize=700%2C394&ssl=1)
5. Setup your jenkins user
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*WbhbIC8cO9koRTDP.png?resize=700%2C394&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*5u4s6oyd7z0_PsTU.png?resize=700%2C394&ssl=1)
Step 4 → ci-cd pipeline to build and push the image to docker hub
1. Install Plugins listed below
- Terrform
- Eclipse Temurin Installer (Install without restart)
- SonarQube Scanner (Install without restart)
- NodeJs Plugin (Install Without restart)
- Parameterized Trigger (to trigger another pipeline if one completed)
- owasp →The OWASP Plugin in Jenkins is like a “security assistant” that helps you find and fix security issues in your software. It uses the knowledge and guidelines from the Open Web Application Security Project (OWASP) to scan your web applications and provide suggestions on how to make them more secure. It’s a tool to ensure that your web applications are protected against common security threats and vulnerabilities.
- Prometheus metrics →to moniter jenkins on grafana dashboard
- Download all the docker realated plugins
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*5Xk-nE7x5k6LWfU9.png?resize=700%2C394&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*Z1aYD8CstrvZYp2vIIZJtQ.png?resize=700%2C394&ssl=1)
1. add credentials of Sonarqube and Docker
1st we genrate a token for sonarqube to use in jenkins credentials as secret text
a. setup sonarqube credentials
- go to http://publicip:9000
- now enter your username and password
- click on security →users →token →generate token
- token_name==sonar-token
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*0qUEFuqiYoESObjr.png?resize=700%2C394&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*G68Y83To0OAPKyhr-7Oi9w.png?resize=700%2C270&ssl=1)
5. copy the token and go to your jenkins →manage jenkins →credentials →global →add credentials
6. select secret text from dropdown
7. secret text ==your token , id =sonar-token →click on create
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*QO29DiayNgDcClC-09bwBw.png?resize=700%2C312&ssl=1)
b. setup projects in sonarqube for jenkins
- go to your sonarqube server
- click on projects
- in the name field type tetris
- click on set up
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:569/1*jPfFFJ35TxUnrOq9LKgTFw.png?resize=455%2C507&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:545/0*QeMS7Aai0EbmBeGW.png?resize=436%2C217&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:978/1*T83jKX2y60qM_76OvJkaJg.png?resize=337%2C310&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:1711/1*U5tNm_83qNSqatn9PiMzxQ.png?resize=427%2C428&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:1761/1*o9lHZleNEO86XaBH9L3RxA.png?resize=238%2C791&ssl=1)
Sonarqube project for jenkins is setup now
C. Now setup Webhooks for sonarqube
webhook in SonarQube is a way for SonarQube to notify or inform other systems or tools when something important happens. It’s like a virtual hand raising to say, “Hey, something noteworthy just occurred here!”
- Go to adiministration →configuration →webhooks
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:368/1*CPW6vXiParAs3OdCoDV-Qw.png?resize=294%2C205&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:711/1*e9sK6iDg_ez3FExUntRRag.png?resize=569%2C641&ssl=1)
2. click on create and your webhook is created
Sonarqube is setup here
d. Setup docker credentials
- go to your jenkins →manage jenkins →credentials →global →add credentials
- provide your username and password of your dockerhub
- id==docker
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*i5cW97KlGkoRrzQa.png?resize=700%2C394&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*AgJL9jSEWmjPYqW_.png?resize=700%2C144&ssl=1)
credentials for both are setup
e. setup git credentials →click on me to see how to generate token in github
- Now copy that token from github and
- go to your jenkins →manage jenkins →credentials →global →add credentials
- provide your username and password of your github
- id==github
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*bMt_p9ELXVA02V7KqOhmbg.png?resize=700%2C394&ssl=1)
2. Now we are going to setup tools for jenkins
go to manage jenkins → tools
a. add jdk
- click on add jdk and select installer adoptium.net
- choose jdk 17.0.8.1+1version and in name section enter jdk 17
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*EHpOJ6KpDQFhLeHI.png?resize=700%2C394&ssl=1)
b. add node js
- click on add nodejs
- enter node16 in name section
- choose version nodejs 16.2.0
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*AfXwombNJc-_17Di.png?resize=700%2C394&ssl=1)
c. add docker →
- click on add docker
- name==docker
- add installer ==download from docker.com
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*90XVLvCICMEwoO40.png?resize=700%2C394&ssl=1)
d. add sonarqube →
- add sonar scanner
- name ==sonar-scanner
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*ec5Z5l-xHD6ssoU2.png?resize=700%2C394&ssl=1)
e. add owasp dependency check →
Adding the Dependency-Check plugin in the “Tools” section of Jenkins allows you to perform automated security checks on the dependencies used by your application
- add dependency check
- name == DP-Check
- from add installer select install from github.com
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*iQNfw5nMckBwxDJk.png?resize=700%2C394&ssl=1)
f. terraform
- go to manage jenkins →tools →search for terraform
- add terraform
- provide the name in the name field and untick the install automatically option and give the path /usr/bin/
- As terraform is installed in this section it takes it from there
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:720/1*JdcgQUtVXjGwbaR87UNVmA.png?resize=576%2C47&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*39ie_oIhEu0Dcevyho7sVA.png?resize=700%2C285&ssl=1)
5. click on apply and save
3. Configure global setting for sonarube
- go to manage jenkins →Configure global setting →add sonarqube servers
- name ==sonar-server
- server_url==http://public_ip:9000
- server authentication token == sonar-token→it is created in sonarqube security configurations
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*q97jcO8HkfpFO4mW.png?resize=700%2C319&ssl=1)
All the tools and configurations for jenkins are set up now let’s run the pipeline
4. pipeline script for building image and push it to dockerhub(Version1)
- click on new item and name it tetris image build and click on ok
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*9e8eEMqXbTcoGTeyZ8r4bA.png?resize=700%2C394&ssl=1)
2. scroll to pipeline script and paste the following script there
pipeline{
agent any
tools{
jdk 'jdk17'
nodejs 'node16'
}
environment {
SCANNER_HOME=tool 'sonar-scanner'
}
stages {
stage('clean workspace'){
steps{
cleanWs()
}
}
stage('Checkout from Git'){
steps{
git branch: 'main', url: 'https://github.com/Aakibgithuber/Tetris-gamev1.git'
}
}
stage("Sonarqube Analysis "){
steps{
withSonarQubeEnv('sonar-server') {
sh ''' $SCANNER_HOME/bin/sonar-scanner -Dsonar.projectName=TetrisVersion1.0 \
-Dsonar.projectKey=TetrisVersion1.0 '''
}
}
}
stage("quality gate"){
steps {
script {
waitForQualityGate abortPipeline: false, credentialsId: 'sonar-token'
}
}
}
stage('Install Dependencies') {
steps {
sh "npm install"
}
}
stage('OWASP FS SCAN') {
steps {
dependencyCheck additionalArguments: '--scan ./ --disableYarnAudit --disableNodeAudit', odcInstallation: 'DP-Check'
dependencyCheckPublisher pattern: '**/dependency-check-report.xml'
}
}
stage('TRIVY FS SCAN') {
steps {
sh "trivy fs . > trivyfs.txt"
}
}
stage("Docker Build & Push"){
steps{
script{
withDockerRegistry(credentialsId: 'docker', toolName: 'docker'){
sh "docker build -t tetrisv1 ."
sh "docker tag tetrisv1 aakibkhan1212/tetrisv1:latest "
sh "docker push aakibkhan1212/tetrisv1:latest "
}
}
}
}
stage("TRIVY"){
steps{
sh "trivy image aakibkhan1212/tetrisv1:latest > trivyimage.txt"
}
}
}
}
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*oRY717u6xuKDHiXPu1STRQ.png?resize=700%2C339&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*jA-KNpjGvPNzd2LFyjXI3w.png?resize=700%2C351&ssl=1)
Your image is build and push to dockerhub let’s see changes in dockerhub
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*XqC-bhsqfoXiy0teX8Ta2Q.png?resize=700%2C120&ssl=1)
5. pipeline script for building image and push it to dockerhub(Version2)
- for version 2 you only need to change the url of your repo
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*QS2th00NtBujj38knoPjAA.png?resize=700%2C394&ssl=1)
2. pipeline script for version 2
pipeline{
agent any
tools{
jdk 'jdk17'
nodejs 'node16'
}
environment {
SCANNER_HOME=tool 'sonar-scanner'
GIT_REPO_NAME = "Tetris-deployment-file"
GIT_USER_NAME = "Aakibgithuber"
}
stages {
stage('clean workspace'){
steps{
cleanWs()
}
}
stage('Checkout from Git'){
steps{
git branch: 'main', url: 'https://github.com/Aakibgithuber/tetris-gamev2.git'
}
}
stage("Sonarqube Analysis "){
steps{
withSonarQubeEnv('sonar-server') {
sh ''' $SCANNER_HOME/bin/sonar-scanner -Dsonar.projectName=tetrisv1 \
-Dsonar.projectKey=tetrisv1 '''
}
}
}
stage("quality gate"){
steps {
script {
waitForQualityGate abortPipeline: false, credentialsId: 'Sonar-token'
}
}
}
stage('Install Dependencies') {
steps {
sh "npm install"
}
}
stage('OWASP FS SCAN') {
steps {
dependencyCheck additionalArguments: '--scan ./ --disableYarnAudit --disableNodeAudit', odcInstallation: 'DP-Check'
dependencyCheckPublisher pattern: '**/dependency-check-report.xml'
}
}
stage('TRIVY FS SCAN') {
steps {
sh "trivy fs . > trivyfs.txt"
}
}
stage("Docker Build & Push"){
steps{
script{
withDockerRegistry(credentialsId: 'docker', toolName: 'docker'){
sh "docker build -t tetrisv2 ."
sh "docker tag tetrisv2 aakibkhan1212/tetrisv2:latest "
sh "docker push aakibkhan1212/tetrisv2:latest "
}
}
}
}
stage("TRIVY"){
steps{
sh "trivy image aakibkhan1212/tetrisv2:latest > trivyimage.txt"
}
}
stage('Checkout Code') {
steps {
git branch: 'main', url: 'https://github.com/Aakibgithuber/Tetris-deployment-file.git'
}
}
stage('Update Deployment File') {
steps {
script {
withCredentials([string(credentialsId: 'github', variable: 'GITHUB_TOKEN')]) {
NEW_IMAGE_NAME = "aakibkhan1212/tetrisv1:latest"
sh "sed -i 's|image: .*|image: $NEW_IMAGE_NAME|' deployment.yml"
sh 'git add deployment.yml'
sh "git commit -m 'Update deployment image to $NEW_IMAGE_NAME'"
sh "git push @github.com/${GIT_USER_NAME}/${GIT_REPO_NAME">https://${GITHUB_TOKEN}@github.com/${GIT_USER_NAME}/${GIT_REPO_NAME} HEAD:main"
}
}
}
}
}
}
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*mhiKEmNlod5wS4b4BYBhrw.png?resize=700%2C394&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*jo--05cEqr416QsSvI9eyg.png?resize=700%2C263&ssl=1)
Step 5→ Update Image name In deployment repo
Everything is done now we need to update the image name in my second repository that is used for creating pod inside our Elastic kubernetes cluster
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:698/1*725olAAPkECry3-J65QWmQ.png?resize=558%2C198&ssl=1)
2. you need to automate that when image is build and push by above pipeline than it is automatically update the image name here with the image name created by above pipeline
3. So for that we need to create another pipeline and use the triggerd option means when one pipeline succesfully complete which push the image to dockerhub than it triggers to another pipeline which is than update the image name in the tetris deployment repository
#add inside environment
environment {
GIT_REPO_NAME = "Tetris-deployment-file"
GIT_USER_NAME = "Aakibgithuber" # change your Github Username here
}
# add these stages after trivy image scan
stage('Checkout Code') {
steps {
git branch: 'main', url: 'https://github.com/Aakibgithuber/Tetris-deployment-file.git'
}
}
stage('Update Deployment File') {
steps {
script {
withCredentials([gitUsernamePassword(credentialsId: 'github', gitToolName: 'Default')]){
NEW_IMAGE_NAME = "aakibkhan1212/tetrisv1:latest" #update your image here
sh "sed -i 's|image: .*|image: $NEW_IMAGE_NAME|' deployment.yml"
sh 'git add deployment.yml'
sh "git commit -m 'Update deployment image to $NEW_IMAGE_NAME'"
sh "git push @github.com/${GIT_USER_NAME}/${GIT_REPO_NAME">https://${GITHUB_TOKEN}@github.com/${GIT_USER_NAME}/${GIT_REPO_NAME} HEAD:main"
}
}
}
}
Or you could also build a single pipeline for all that
Full pipeline script below →
pipeline{
agent any
tools{
jdk 'jdk17'
nodejs 'node16'
}
environment {
SCANNER_HOME=tool 'sonar-scanner'
GIT_REPO_NAME = "Tetris-deployment-file"
GIT_USER_NAME = "Aakibgithuber"
}
stages {
stage('clean workspace'){
steps{
cleanWs()
}
}
stage('Checkout from Git'){
steps{
git branch: 'main', url: 'https://github.com/Aakibgithuber/Tetris-gamev1.git'
}
}
stage("Sonarqube Analysis "){
steps{
withSonarQubeEnv('sonar-server') {
sh ''' $SCANNER_HOME/bin/sonar-scanner -Dsonar.projectName=tetrisv1 \
-Dsonar.projectKey=tetrisv1 '''
}
}
}
stage("quality gate"){
steps {
script {
waitForQualityGate abortPipeline: false, credentialsId: 'Sonar-token'
}
}
}
stage('Install Dependencies') {
steps {
sh "npm install"
}
}
stage('OWASP FS SCAN') {
steps {
dependencyCheck additionalArguments: '--scan ./ --disableYarnAudit --disableNodeAudit', odcInstallation: 'DP-Check'
dependencyCheckPublisher pattern: '**/dependency-check-report.xml'
}
}
stage('TRIVY FS SCAN') {
steps {
sh "trivy fs . > trivyfs.txt"
}
}
stage("Docker Build & Push"){
steps{
script{
withDockerRegistry(credentialsId: 'docker', toolName: 'docker'){
sh "docker build -t tetrisv1 ."
sh "docker tag tetrisv1 aakibkhan1212/tetrisv1:latest "
sh "docker push aakibkhan1212/tetrisv1:latest "
}
}
}
}
stage("TRIVY"){
steps{
sh "trivy image aakibkhan1212/tetrisv1:latest > trivyimage.txt"
}
}
stage('Checkout Code') {
steps {
git branch: 'main', url: 'https://github.com/Aakibgithuber/Tetris-deployment-file.git'
}
}
stage('Update Deployment File') {
steps {
script {
withCredentials([string(credentialsId: 'github', variable: 'GITHUB_TOKEN')]) {
NEW_IMAGE_NAME = "aakibkhan1212/tetrisv1:latest"
sh "sed -i 's|image: .*|image: $NEW_IMAGE_NAME|' deployment.yml"
sh 'git add deployment.yml'
sh "git commit -m 'Update deployment image to $NEW_IMAGE_NAME'"
sh "git push @github.com/${GIT_USER_NAME}/${GIT_REPO_NAME">https://${GITHUB_TOKEN}@github.com/${GIT_USER_NAME}/${GIT_REPO_NAME} HEAD:main"
}
}
}
}
}
}
4. click on build
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*jA-KNpjGvPNzd2LFyjXI3w.png?resize=700%2C351&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*TW0nGUh81KzUsCkztYm9pQ.png?resize=700%2C138&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:510/1*W8HBG5wRSsp-KNu3EpPNuQ.png?resize=408%2C243&ssl=1)
Step 6→EKS cluster creation
1. Create cluster with terraform using jenkins
- go to new item in jenkins and name it eks terraform
- choose the pipeline from the listing and click ok
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*yCwFUQ6dvoU2NDnJxZDrMg.png?resize=700%2C394&ssl=1)
3. After that tick on This project is parameterised which enables you to run your your pipeline in two ways first commmand is apply to create infrastructure and second is destroy to delete all infrastructure after pipeline runs
4. click on add parameter →choice parameter →provide name and then two commands apply and destroy
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:604/1*qCG-pin2IHeVSWKOxICOyw.png?resize=483%2C694&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*a4N1tWxuGAL2gCG5gRrnkQ.png?resize=700%2C323&ssl=1)
4. pipeline script
- Paste the below code to pipeline script section
pipeline{
agent any
stages {
stage('Checkout from Git'){
steps{
git branch: 'main', url: 'https://github.com/Aakibgithuber/Tetris-gamev1.git'
}
}
stage('Terraform version'){
steps{
sh 'terraform --version'
}
}
stage('Terraform init'){
steps{
dir('Eks-terraform') {
sh 'terraform init'
}
}
}
stage('Terraform validate'){
steps{
dir('Eks-terraform') {
sh 'terraform validate'
}
}
}
stage('Terraform plan'){
steps{
dir('Eks-terraform') {
sh 'terraform plan'
}
}
}
stage('Terraform apply/destroy'){
steps{
dir('Eks-terraform') {
sh 'terraform ${Action} --auto-approve'
}
}
}
}
}
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*ZFEFzz3lbr_-0zgl2169Iw.png?resize=700%2C352&ssl=1)
2. click on apply and save
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*UZRSjIan7iNB9GYA0AFASA.png?resize=700%2C394&ssl=1)
3. on clicking on Build with parmaters option you will see two option apply and destroy as explained above
4. select apply and click on build
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*mWNb62ggQvf4DoK5IgbNzg.png?resize=700%2C394&ssl=1)
5. It takes about 15 to 20 mins to build a cluster
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*8ICGCBPgl0yD9M0A8Woj1A.png?resize=700%2C158&ssl=1)
2. Create cluster without terraform
Note!! use this step if you don’t want to use jenkins pipeline for eks cluster creation
- In your base ec2 run the below command
eksctl create cluster --name EKS_CLOUD --region ap-south-1 --node-type t2.medium --nodes-min 1 --nodes-max 2
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*7Am3pSX1QCPElqqecStF6g.png?resize=700%2C394&ssl=1)
aws eks update-kubeconfig --region ap-south-1 --name EKS_CLOUD
Step 7→Install Argo cd
Argo CD is a tool that helps software developers and teams manage and deploy their applications to Kubernetes clusters more easily. It simplifies the process of keeping your applications up to date and in sync with your desired configuration by automatically syncing your code with what’s running in your Kubernetes environment. It’s like a traffic cop for your chmod 700 get_helm.shapplications on Kubernetes, ensuring they are always in the right state without you having to manually make changes.
First we need to install helm →Install helm
snap install helm --classic
- Add the Argo CD Helm repository:
helm repo add argo-cd https://argoproj.github.io/argo-helm
2. Update your Helm repositories:
helm repo update
3. Create a namespace for Argo CD (optional but recommended):
kubectl create namespace argocd
4. Install Argo CD using Helm:
helm install argocd argo-cd/argo-cd -n argocd
5. To see installation of argocd run
kubectl get all -n argocd
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*Ncuej4CtLmmZ_xjN-0sPKg.png?resize=700%2C394&ssl=1)
Expose argocd-server
By default argocd-server is not publicaly exposed. For the purpose of this workshop, we will use a Load Balancer to make it usable:
kubectl patch svc argocd-server -n argocd -p ‘{“spec”: {“type”: “LoadBalancer”}}’
snap install jq
export ARGOCD_SERVER=`kubectl get svc argocd-server -n argocd -o json | jq --raw-output '.status.loadBalancer.ingress[0].hostname'`
echo $ARGOCD_SERVER
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*1kJ63EXrWzbZpvYi.png?resize=700%2C59&ssl=1)
you have given a url copy and paste it on your chrome browser
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*hrws42UR_iklNsqaS3Ur1w.png?resize=700%2C394&ssl=1)
username ==admin
For Password →
export ARGO_PWD=`kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath=”{.data.password}” | base64 -d`
echo $ARGO_PWD
- output == password for argocd
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*piPIShXDH42mWqwk6BCA-g.png?resize=700%2C37&ssl=1)
copy and paste the password on argocd page and you are logged in
Step 8 →Deploy Application with ArgoCD
Sign into argo cd using above steps →
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/0*5w6zKN46Yi7T12rq.png?resize=700%2C394&ssl=1)
- go to setting →repositories →connect repo
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*28OYhFALHigl_XSB1USa8A.png?resize=700%2C394&ssl=1)
select these options and on repo url copy n paste below github url https://github.com/Aakibgithuber/Tetris-deployment-file.git
2. click on connect output →successful
3. Now go to application →newapp
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*9khNP3DK1uTbxN2VxOTAzA.png?resize=700%2C224&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*vToibJM4v4cflQYq7xO71Q.png?resize=700%2C357&ssl=1)
4. select the above options and click on create
5. Now click on sync →force →sync →ok
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*gU6t3GAEIKzvqs4Et5W7bA.png?resize=700%2C394&ssl=1)
6. click on tetris-service →details
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*bXTqMbCVJTwx4f4jtEjNXw.png?resize=700%2C394&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*JEvTHR_NRdY2zUwoUXusmA.png?resize=700%2C394&ssl=1)
7.copy the load balancer ingress url and paste it to web browser
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*cOarx64MyNjJ2jK1YjzGXA.png?resize=700%2C394&ssl=1)
run the below command and open the listed port
kubectl get all
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*OzvRjzdbJpPCCmYOFIEDzA.png?resize=700%2C100&ssl=1)
8. This is the version 1 of application for version 2
- go to your github and locate the deployment repo and change the image name in deployment.yaml file
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*tK39mwJXe35VUNb7E1ftsg.png?resize=700%2C358&ssl=1)
2. change the image name from v1 to v2
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:496/1*36V2vx5ju_YrogWoD1VXNg.png?resize=397%2C155&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:511/1*cLQqqsCgtASBg40rszAsKg.png?resize=409%2C142&ssl=1)
3. go to your argo cd and click on sync →force →sync →ok
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*Jmd9Qlp87EkBM1IXpQ-R2Q.png?resize=700%2C89&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*voEevLX0n6KuZ7uibgIAVw.png?resize=700%2C394&ssl=1)
4. now again refresh the load balancer ingress
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*KObKcoupCpB1nXeVKzMPJg.png?resize=700%2C394&ssl=1)
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*raUmVvyyvYyEtSMK40hKng.png?resize=700%2C394&ssl=1)
Here is our new version of game
our both version of game is deployed now let’s destroy all the resources
Step 9 →Destruction
- Deletion of app in argo cd
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*UTslbNA-Nt7pGXCgCLnpNg.png?resize=700%2C394&ssl=1)
2. Delete argo cd from terminal of jenkins server node
kubectl delete svc argocd-server -n argocd
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*xpUcQvFVfNu7IcNNyBvrng.png?resize=700%2C40&ssl=1)
3. Now delete the EKS cluster (two ways )
- delete using jenkins pipeline by terraform
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*MglzzjIgXu-ss_Q5ss1u7Q.png?resize=700%2C394&ssl=1)
2. delete from terminal if you have used aws cli to create → Cluster run the following command on jenkins server node
eksctl delete cluster --name EKS_CLOUD--region ap-south-1
aws cloudformation delete-stack --stack-name eksctl-EKS_CLOUD
4. delete the base EC2 or jenkins server → go to your terminal and run the
terraform destroy --auto-approve
![](https://i0.wp.com/miro.medium.com/v2/resize:fit:875/1*tZShVgQp651R35bjZLuwpQ.png?resize=700%2C475&ssl=1)