Saturday, May 17, 2025

Jenkins part6

Jenkins part6

Class 26th Jenkins May 17th
Jenkins Master-slave(Controller-Agent) setup
Post Build actions 
What are Artifact ?
Configure Artifacts on Nexus and S3 - practical
                                           Jenkins Master-slave(Controller-Agent) setup
A Jenkins Master-slave setup (now called controller-agent)is used to distribute build and deployment tasks across multiple machines, improving efficiency  and scalability 
This setup is ideal for running parallel builds, handling large workloads and using environments for testing . (We have multiples instead doing in one instance do it in multiple instance or environments)
Key components 
 Jenkins Master (controller)
.The Main Jenkins server that manages jobs, configurations ,users and plugin management 
.Assigns build jobs to slaves(agents) based on labels and resource availability
.Stores build history and manages scheduling 
 Jenkins Slave(agent) 
.A separate machine that executes build tasks assigned by the master.
.Can run on different OS(Linux,Windows,macOS)
.Can have specific tools Installed (java,Node.Js,Docker,etc)
.Communicates with the master via SSH,JNLP or Cloud-based connectors

So fat we have done 
Git clone code from server
Through mvn installed the build and launch application using jar

Now we implement slave ,We can setup pipeline in controller ( i.e crete job) and build is done in Slave 
For that we need setup node where we want build the setup 
                                                Controller Agent and Slave Creation steps 

Step1: Already i have created on instance Controller, Now we launch one more instances as agent 
While create instance take amazon Linux kernel version 5.10 only 
 Agent Machine (Java runtime /Git need to install)

This is Control Agent server (For already installed these all software's please reference below blog Jenkins 

Controller
https://oracleask.blogspot.com/2025/05/jenkins.html
[root@ip-10-0-0-6 ~]# hostnamectl set-hostname controller
[root@ip-10-0-0-6 ~]# exit
logout
[ec2-user@ip-10-0-0-6 ~]$ sudo -i
[root@controller ~]#

We have created Two Agent Servers Just identification changed hostname ,
and need to installed  java 

Agent1
[root@ip-10-0-3-147 ~]# hostnamectl set-hostname Agent1
[root@ip-10-0-3-147 ~]# hostname
Agent1
[ec2-user@agent1 ~]$ exit
logout
[ec2-user@agent1 ~]$ sudo -i
[ec2-user@agent1 ~]$yum install -y java-17-amazon-corretto-devel
[ec2-user@agent1 ~]$yum install -y git
Agent2
[root@ip-10-0-1-157 ~]# hostnamectl set-hostname Agent2
[root@ip-10-0-1-157 ~]# exit
logout
[ec2-user@ip-10-0-1-157 ~]$ sudo -i
[root@agent2 ~]#
[root@agent2 ~]$yum install -y java-17-amazon-corretto-devel
[root@agent2 ~]$yum install -y git


Step2:  Need to Setup the node in Controller server Jenkins portal
Dashboard>Manage Jenkins >nodes 

Node Name :Agent1
Select Permanent Agent Click create

Number of executors (here Number Current request came simultaneously, we can mentioned how many need perform in the node based on server configuration it will depend) :1
Remote root directory (here we need give the specific path to where git clone and jar file creation of the project): /home/ec2-user/jenkins  
Label: (it is very important it connect between node and job)ccitlabel
Usage: Select :Only build jobs with label expressions matching this node
Launch method Select : Launch agents SSH
 Host Need to give Agent1 Private Ip address: 10.0.1.61
We can give credential click here System add domain one way 


or Another way   >Manage Jenkins >credentials > click add system
    Click Global credentials >Click add credentials

Step3: As of now followed one way both are same final credential stored in globally above click add domain ,Below agent key pem


Select Enter directly give you .pem file of the agent1 key click add 
See Final added credentials 

Select Credentials ec2-user and click save

See here below Agent1 initially it showing launch ,after launch getting any error that means your not installed java in you agent machine, just installed relaunch  0/1 number of concurrent executions

Step4: Create Job Newitem > name ccitjob select  Freestyle Project >click Ok
New option came  in General  Restrict where you project can be run ,this option will come only
when Node creation code after only ,give label name ccitlabel1 same name  at time of  node creation.
Git https Url need to specify

Another window just configure the Mvn click save 

Select mvn which you added configuration and enter command clean package and click save 

Job Created Successfully and click Build

Build Failed with git issue 
./master to change to ./main Build again,now Build success

Step5: See below agent1 Workspace created 
[root@ip-10-0-1-64 run]# cd  /home/ec2-user/jenkins
[root@ip-10-0-1-64 jenkins]# ls
remoting  remoting.jar  tools  workspace
[root@ip-10-0-1-64 jenkins]# ls -lrt
total 1368
-rw-rw-r-- 1 ec2-user ec2-user 1396936 May 20 14:15 remoting.jar
drwxrwxr-x 4 ec2-user ec2-user      34 May 20 14:15 remoting
drwxrwxr-x 4 ec2-user ec2-user      40 May 20 14:54 workspace
drwxrwxr-x 3 ec2-user ec2-user      50 May 20 14:54 tools
[root@ip-10-0-1-64 jenkins]# cd workspace
[root@ip-10-0-1-64 workspace]# ls
ccitjob  ccitjob@tmp
[root@ip-10-0-1-64 workspace]# ls -rlt
total 0
drwxrwxr-x 2 ec2-user ec2-user  6 May 20 14:54 ccitjob@tmp
drwxrwxr-x 5 ec2-user ec2-user 58 May 20 14:54 ccitjob
[root@ip-10-0-1-64 ccitjob]# ls
pom.xml  src  target
[root@ip-10-0-1-64 ccitjob]# cd target
[root@ip-10-0-1-64 target]# ls
ccit-0.0.1.jar  ccit-0.0.1.jar.original  classes  generated-sources  maven-archiver  maven-status
[root@ip-10-0-1-64 target]# ls -lrt
total 19660
drwxrwxr-x 3 ec2-user ec2-user       25 May 20 14:54 generated-sources
drwxrwxr-x 3 ec2-user ec2-user       35 May 20 14:54 maven-status
drwxrwxr-x 4 ec2-user ec2-user       34 May 20 14:54 classes
drwxrwxr-x 2 ec2-user ec2-user       28 May 20 14:54 maven-archiver
-rw-rw-r-- 1 ec2-user ec2-user     3706 May 20 14:54 ccit-0.0.1.jar.original
-rw-rw-r-- 1 ec2-user ec2-user 20125842 May 20 14:54 ccit-0.0.1.jar

Step6: Create one more node agent2, you just copy existing node 
Click agent2 instance private ip and security key click save ,Same label ccitlable1 added another Credential added description some name original first for differencation select save

Two Agent  successfully configurations

Build the job Successfully completed 

.jat create target folder
[ec2-user@ip-10-0-1-207 ccitjob]$ cd target
[ec2-user@ip-10-0-1-207 target]$ ls
ccit-0.0.1.jar  ccit-0.0.1.jar.original  classes  generated-sources  maven-archiver  maven-status

Artifacts are created  target folder first if you delete the  workspace try to rebuild it will not created
Because  free style Node multi node will not support 

[ec2-user@ip-10-0-1-207 target]$ ls -lrt
total 19660
drwxrwxr-x 3 ec2-user ec2-user       25 May 20 20:03 generated-sources
drwxrwxr-x 3 ec2-user ec2-user       35 May 20 20:03 maven-status
drwxrwxr-x 4 ec2-user ec2-user       34 May 20 20:03 classes
drwxrwxr-x 2 ec2-user ec2-user       28 May 20 20:03 maven-archiver
-rw-rw-r-- 1 ec2-user ec2-user     3706 May 20 20:03 ccit-0.0.1.jar.original
-rw-rw-r-- 1 ec2-user ec2-user 20125842 May 20 20:03 ccit-0.0.1.jar
[ec2-user@ip-10-0-1-207 target]$ pwd
/home/ec2-user/jenkins/workspace/ccitjob/target

--Build success but workspace target folder not create, due to free style mutiple nodes not supported 
for the overcome instead of Freestyle create job select pipeline
[ec2-user@ip-10-0-1-64 jenkins]$ ls -lrt
total 1368
-rw-rw-r-- 1 ec2-user ec2-user 1396936 May 20 14:15 remoting.jar
drwxrwxr-x 4 ec2-user ec2-user      34 May 20 14:15 remoting

Step7:  Piple line Creating new job click ok

Freestyle we will do all user inface, pipeline we will Groovy  script 

pipeline {
    agent {label 'ccitlabel1'}
    tools {
        maven 'ccit-mvn'
    }
    stages {
        stage('Clone') {
            steps {
                git branch: 'main', url: 'https://github.com/Vakatisubbu/ccit-javaapp.git'
             }
        }
        stage('Build') {
            steps {
                sh "mvn clean package"
                }
        }
    }
}


click Build the job ,job Success i have done Single time ,so that is the reason agent2 only 
build done

Step8: Single time ,so that is the reason agent2 only build done,if you we execute paralled multiple time it build both the agent1 agent2 server ,please build multiple time now 

[ec2-user@ip-10-0-1-207 workspace]$ ls -lrt
total 0
drwxrwxr-x 2 ec2-user ec2-user  6 May 20 20:15 ccitjob@tmp
drwxrwxr-x 5 ec2-user ec2-user 58 May 20 20:16 ccitjob
drwxrwxr-x 5 ec2-user ec2-user 58 May 20 21:05 ccitjob-pipeline
drwxrwxr-x 2 ec2-user ec2-user  6 May 20 21:06 ccitjob-pipeline@tmp
[ec2-user@ip-10-0-1-207 workspace]$ pwd
/home/ec2-user/jenkins/workspace
multiple time executed screen below 


See below Same time frame artifices created both are serves ,which mean above build while you do multiple, the pipeline though we will build multiple agents or server same time.

Agent 1

[ec2-user@ip-10-0-1-64 jenkins]$ ls -lrt
total 1368
-rw-rw-r-- 1 ec2-user ec2-user 1396936 May 20 14:15 remoting.jar
drwxrwxr-x 4 ec2-user ec2-user      34 May 20 14:15 remoting
drwxrwxr-x 3 ec2-user ec2-user      50 May 20 21:15 tools
drwxrwxr-x 3 ec2-user ec2-user      26 May 20 21:15 caches
drwxrwxr-x 4 ec2-user ec2-user      58 May 20 21:15 workspace
[ec2-user@ip-10-0-1-64 jenkins]$ cd workspace
[ec2-user@ip-10-0-1-64 workspace]$ ls
ccitjob-pipeline  ccitjob-pipeline@tmp
[ec2-user@ip-10-0-1-64 workspace]$ ls -lrt
total 0
drwxrwxr-x 5 ec2-user ec2-user 58 May 20 21:15 ccitjob-pipeline
drwxrwxr-x 2 ec2-user ec2-user  6 May 20 21:15 ccitjob-pipeline@tmp

Agent2 

[ec2-user@ip-10-0-1-207 workspace]$ ls -lrt
total 0
drwxrwxr-x 2 ec2-user ec2-user  6 May 20 20:15 ccitjob@tmp
drwxrwxr-x 5 ec2-user ec2-user 58 May 20 20:16 ccitjob
drwxrwxr-x 5 ec2-user ec2-user 58 May 20 21:15 ccitjob-pipeline
drwxrwxr-x 2 ec2-user ec2-user  6 May 20 21:15 ccitjob-pipeline@tmp


                                                            Stage 
Step1:
Using stage view we can get know job which stage failed see 
First check Tool install second check Clone Third check Build


Step2:
Creating One more job 

Create job >ccit-pipejob1 >Choose pipeline 
Groovy  script added below save and build  Build success 

pipeline {
    agent any

    stages {
        stage('Step1') {
            steps {
                echo 'This is Step1'
            }
        }
        stage('Step2') {
            steps {
                echo 'This is Step2'
            }
        }
        stage('Step3') {
            steps {
                echo 'This is Step3'
            }
        }
         stage('Step4') {
            steps {
                echo 'This is Step4'
            }
        }
    }
}
Step3:
See here Stages showing build Which stage failed, simple get know 
one more plugins we have using we have better view 



Step4:
Stage Better view add plugins 
Dashboard >Manage jenkins >plugins >available plugins  click install


Step5: Now you see the better view for immediate track or trace the issue for build issues, easy to understand which stage facing issue 



What are Artifacts ?
Artifacts are that output files generated during a jenkins build process.These can include compiled binaries,JAR files ,WAR files ,Docker images,or any other files needed for deployment 

.jar.war (called Artifacts) these artifact storage purpose we use some services s3,nexus,aws code artifcats 

main advantages version for files 


--Thanks 





No comments:

Post a Comment