Kode Kloud Jenkins

Jenkins Set Up Jenkins Server
Date: 26-May-2024
The DevOps team at xFusionCorp Industries is initiating the setup of CI/CD pipelines and has decided to utilize Jenkins as their server. Execute the task according to the provided requirements:
1. Install jenkins on jenkins server using yum utility only, and start its service. You might face timeout issue while starting the Jenkins service, please refer this link for help.
2. Jenkin's admin user name should be theadmin, password should be Adm!n321, full name should be Javed and email should be javed@jenkins.stratos.xfusioncorp.com.
Note:
1. For this task, access the Jenkins server by SSH using the root user and password S3curePass from the jump host.
2. Jenkin's admin user name should be theadmin, password should be Adm!n321, full name should be James and email should be james@jenkins.stratos.xfusioncorp.com.
Solution


# dnf install java-17-openjdk-devel wget rsyslog
# systemctl start rsyslog
# wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat/jenkins.repo
# rpm --import https://pkg.jenkins.io/redhat/jenkins.io-2023.key
# sudo dnf install jenkins
# sudo systemctl start jenkins
# sudo systemctl enable jenkins
# sudo systemctl status jenkins

https://8080-port-5b716e355ad747c1.labs.kodekloud.com/
once plugin installation is completed, provide the user details and complete the installation.


Tasks: Jenkins Views
Date 11-July-24
The DevOps team of xFusionCorp Industries is planning to create a number of Jenkins jobs for different tasks. So to easily manage the jobs within Jenkins UI they decided to create different views for all Jenkins jobs based on usage/nature of these jobs, - for example datacenter-crons view for all cron jobs. Based on the requirements shared below please perform the below mentioned task:
Soution: 
Click on the Jenkins button on the top bar to access the Jenkins UI. Login using username admin and password Adm!n321.
1. Create a Jenkins job named datacenter-test-job.
2. Configure this job to run a simple bash command i.e echo "hello world!!".
3. Create a view named datacenter-crons (it must be a global view of type List View) and make sure datacenter-test-job and datacenter-cron-job (which is already present on Jenkins) jobs are listed under this new view.
4. Schedule this newly created job to build periodically at every minute i.e * * * * * (please make sure to use the cron expression exactly same how it is mentioned here)
5. Make sure the job builds successfully.


Tasks: 
As the team was in the process of setting up a new Jenkins server, where they were adding agent nodes to handle workloads. They want to add a label for Jenkins master itself. Find below more details about the same.
Add a label kke-jenkins for Jenkins master.

Solution:

To add a label to the Jenkins master itself, follow these steps:

Step 1: Access Jenkins UI

Click on the Jenkins button on the top bar to open the Jenkins UI.
Log in with the username admin and password Adm!n321.
Step 2: Configure the Jenkins Master Node
From the Jenkins dashboard, click on Manage Jenkins in the left-hand sidebar.
Click on Manage Nodes and Clouds.
Click on the node representing the Jenkins master. It is usually named something like master or the hostname of your Jenkins server.
Click on Configure in the left-hand sidebar.
Step 3: Add the Label
In the node configuration page, find the Labels field.
Add kke-jenkins to the Labels field. If there are already other labels, separate them with a space.
Scroll down and click Save.
Verification
After saving, you can verify that the label has been applied by navigating back to Manage Nodes and Clouds.
The master node should now display the label kke-jenkins.



Task : Jenkins Views
The DevOps team of xFusionCorp Industries is planning to create a number of Jenkins jobs for different tasks. So to easily manage the jobs within Jenkins UI they decided to create different views for all Jenkins jobs based on usage/nature of these jobs, - for example devops-crons view for all cron jobs. Based on the requirements shared below please perform the below mentioned task:
Solution: 
Click on the Jenkins button on the top bar to access the Jenkins UI. Login using username admin and password Adm!n321.
1. Create a Jenkins job named devops-test-job.
2. Configure this job to run a simple bash command i.e echo "hello world!!".
3. Create a view named devops-crons (it must be a global view of type List View) and make sure devops-test-job and devops-cron-job (which is already present on Jenkins) jobs are listed under this new view.
4. Schedule this newly created job to build periodically at every minute i.e * * * * * (please make sure to use the cron expression exactly same how it is mentioned here)
5. Make sure the job builds successfully.









: Configure Jenkins Job for Package Installation
The DevOps team of xFusionCorp Industries is planning to create a number of Jenkins jobs for different tasks. So to easily manage the jobs within Jenkins UI they decided to create different views for all Jenkins jobs based on usage/nature of these jobs, - for example datacenter-crons view for all cron jobs. Based on the requirements shared below please perform the below mentioned task:
Click on the Jenkins button on the top bar to access the Jenkins UI. Login using username admin and password Adm!n321.
1. Create a Jenkins job named datacenter-test-job.
2. Configure this job to run a simple bash command i.e echo "hello world!!".
3. Create a view named datacenter-crons (it must be a global view of type List View) and make sure datacenter-test-job and datacenter-cron-job (which is already present on Jenkins) jobs are listed under this new view.
4. Schedule this newly created job to build periodically at every minute i.e * * * * * (please make sure to use the cron expression exactly same how it is mentioned here)
5. Make sure the job builds successfully.
ChatGPT
To achieve the tasks required, follow these steps to create a Jenkins job, configure it, create a view, add the job to the view, and schedule it to run periodically:

Step 1: Log in to Jenkins
Click on the Jenkins button on the top bar.

Log in using the credentials:
* Username: admin
* Password: Adm!n321

Step 2: Create a Jenkins Job

1. On the Jenkins dashboard, click on New Item.
2. Enter datacenter-test-job as the job name.
3. Select Freestyle project and click OK.

Step 3: Configure the Job

1. Under the Build section, click on Add build step and select Execute shell.
2. In the Command field, enter:

# echo "hello world!!"

3. Under the Build Triggers section, select Build periodically.
4. In the Schedule field, enter:

* * * * *

Click Save.

Step 4: Create a New View

1. On the Jenkins dashboard, click on + or New View on the left side panel.
2. Enter datacenter-crons as the view name.
3. Select List View and click OK.
4. In the configuration for the new view, under Job Filters, check the boxes for datacenter-test-job and datacenter-cron-job.
5. Click OK.

Step 5: Verify Job Configuration
1. Ensure datacenter-test-job and datacenter-cron-job are listed under the datacenter-crons view.
2. Wait for the scheduled time and verify that datacenter-test-job runs every minute.
3. To check the build logs and ensure it is successful:
* Click on the datacenter-test-job.
* Select a recent build from the build history.
* Click on Console Output to see the logs.

If done correctly, you should see the message "hello world!!" in the console output of each build.




Tasks: Jenkins Parameterized Builds
Date: 25 July 2024

A new DevOps Engineer has joined the team and he will be assigned some Jenkins related tasks. Before that, the team wanted to test a simple parameterized job to understand basic functionality of parameterized builds. He is given a simple parameterized job to build in Jenkins. Please find more details below:
Click on the Jenkins button on the top bar to access the Jenkins UI. Login using username admin and password Adm!n321.
1. Create a parameterized job which should be named as parameterized-job
2. Add a string parameter named Stage; its default value should be Build.
3. Add a choice parameter named env; its choices should be Development, Staging and Production.
4. Configure job to execute a shell command, which should echo both parameter values (you are passing in the job).
5. Build the Jenkins job at least once with choice parameter value Development to make sure it passes.
Note:
1. You might need to install some plugins and restart Jenkins service. So, we recommend clicking on Restart Jenkins when installation is complete and no jobs are running on plugin installation/update page i.e update centre. Also, Jenkins UI sometimes gets stuck when Jenkins service restarts in the back end. In this case, please make sure to refresh the UI page.

Solution: 
To create and test a parameterized job in Jenkins with the specified requirements, follow these steps:
1. Access Jenkins UI

A. Log in to Jenkins**: Use the credentials provided (username: `admin`, password: `Adm!n321`).

2. Create a Parameterized Job

A. Create a New Job:
   - Click on `New Item` on the Jenkins dashboard.
   - Enter the name `parameterized-job`.
   - Select `Freestyle project` and click `OK`.

B. Configure Job Parameters:

   - In the `General` tab, scroll down to `This project is parameterized` and check the box.
   - Click `Add Parameter` and choose `String Parameter`.
     - Set the name to `Stage`.
     - Set the default value to `Build`.
   - Click `Add Parameter` again and choose `Choice Parameter`.
     - Set the name to `env`.
     - Add the choices `Development`, `Staging`, and `Production`, each on a new line.

3. Add a Build Step:
  
 - Scroll down to the `Build` section and click `Add build step`.
   - Choose `Execute shell`.
   - Enter the following command to echo the parameter values:
     ```bash
     echo "Stage: ${Stage}"
     echo "Environment: ${env}"
     ```
4. Test the Job

A. Build the Job:
   - Go to the `parameterized-job` and click on `Build with Parameters`.
   - Select `Development` from the `env` dropdown.
   - Click `Build`.
B. Check Build Output:
   - After the build completes, click on the build number (e.g., `#1`) under `Build History`.
   - Click on `Console Output` to see the echoed parameters.
By following these steps, you can create a parameterized job in Jenkins and verify that it works as expected. If there are any issues, you can check the Jenkins logs or revisit the job configuration for any errors.


Tasks : Jenkins Workspaces 
Jenkens: Jenkins Workspaces

Some developers are working on a common repository where they are testing some features for an application. They are having three branches (excluding the master branch) in this repository where they are adding changes related to these different features. They want to test these changes on Stratos DC app servers so they need a Jenkins job using which they can deploy these different branches as per requirements. Configure a Jenkins job accordingly.

Click on the Jenkins button on the top bar to access the Jenkins UI. Login using username admin and password Adm!n321.

Similarly, click on Gitea button to access the Gitea page. Login to Gitea server using username sarah and password Sarah_pass123.

1. There is a Git repository named web_app on Gitea where developers are pushing their changes. It has three branches version1, version2 and version3 (excluding the master branch). You need not to make any changes in the repository.
2. Create a Jenkins job named app-job.
3. Configure this job to have a choice parameter named Branch with choices as given below:
version1
version2
version3
4. Configure the job to fetch changes from above mentioned Git repository and make sure it should fetches the changes from the respective branch which you are passing as a choice in the choice parameter while building the job. For example if you choose version1 then it must fetch and deploy the changes from branch version1.
5, Configure this job to use custom workspace rather than a default workspace and custom workspace directory should be created under /var/lib/jenkins (for example /var/lib/jenkins/version1) location rather than under any sub-directory etc. The job should use a workspace as per the value you will pass for Branch parameter while building the job. For example if you choose version1 while building the job then it should create a workspace directory called version1 and should fetch Git repository etc within that directory only.
6. Configure the job to deploy code (fetched from Git repository) on storage server (in Stratos DC) under /var/www/html directory. Since its a shared volume.
7. You can access the website by clicking on App button.

Solution: 




1. Access Jenkins UI:
Click on the Jenkins button on the top bar.
Log in with the username admin and password Adm!n321.



2. Install Pluggin 
Click Jenkins > Manage Jenkins > Manage Plugins and click Available tab.
Search for Git. Select Gitea, SSH & Publish over SSH plugin and click Download now and install after restart



3. Setup Credentials for GIT user ( Sarah)
Jenkins > Manage Jenkins > Credentials, click Global under Stores scoped to Jenkins and Add Credentials


4. Configure Publish Over SSH
Jenkins > Manage Jenkins > Configure System under Publish over SSH > SSH  Servers click Add





5. Create New Job:
 Click on New Item.
 Enter app-job as the name.
 Select Freestyle project and click OK.



6 Configure Job Parameters
In the General section, check the box for This project is parameterized.
Click Add Parameter and select Choice Parameter.
Name the parameter Branch.
Enter the choices:
version1
version2
version3


7. Configure Source Code Management
Select Git:
Under the Source Code Management section, select Git.
Repository URL and Credentials:
Enter the repository URL from Gitea: http://gitea.example.com/sarah/web_app.git 
Add credentials if required by clicking Add next to the Credentials dropdown. Use the credentials you created earlier for sarah.



8. Configure Build Environment
Custom Workspace:
Check the box for Use custom workspace.
In the Directory field, enter:
$JENKINS_HOME/$Branch





9. Under Build Environment , Configure SSH transfer to Storage

10. Build with Parameters



11. Check the console output 

12. Check the app. 




Refer to below link for more details. 
https://www.nbtechsupport.co.in/2021/07/jenkins-workspaces.html

Tasks: Jenkins Database Backup Job

There is a requirement to create a Jenkins job to automate the database backup. Below you can find more details to accomplish this task:
Click on the Jenkins button on the top bar to access the Jenkins UI. Login using username admin and password Adm!n321.

1. Create a Jenkins job named database-backup.
2. Configure it to take a database dump of the kodekloud_db01 database present on the Database server in Stratos Datacenter, the database user is kodekloud_roy and password is asdfgdsd.
3. The dump should be named in db_$(date +%F).sql format, where date +%F is the current date.
4. Copy the db_$(date +%F).sql dump to the Backup Server under location /home/clint/db_backups.
5. Further, schedule this job to run periodically at */10 * * * * (please use this exact schedule format).

Solution: 

 Step 1: Access Jenkins UI
To begin, access your Jenkins User Interface:
1. Click on the Jenkins button located on the top bar.
2. Log in using the credentials:
   - Username: admin
   - Password: Adm!n321

 Step 2: Install Necessary Plugins
Next, we need to install some essential plugins:
1. Navigate to Jenkins > Manage Jenkins > Manage Plugins and select the Available tab.
2. Search for the following plugins:
   - SSH
   - Publish over SSH
3. Select these plugins and click Download now and install after restart.

 Step 3: Configure Publish Over SSH
To set up the SSH connection for transferring files:
1. Go to Jenkins > Manage Jenkins > Configure System.
2. Scroll down to the Publish over SSH section.
3. Under SSH Servers, click Add and fill in the following details:
   - Hostname: stdb01
   - Username: peter
   - Password: Sp!dy

 Step 4: Create a New Job
Let's create a new Jenkins job for the backup process:
1. Click on New Item.
2. Enter the name `database-backup`.
3. Select Freestyle project and click OK.

 Step 5: Configure the Job
To set up the job configuration:
1. Build Triggers:
   - Go to the Build Triggers section.
   - Check the box for Build periodically.
   - Enter the cron expression `*/10 * * * *` to schedule the job to run every 10 minutes.

 Step 6: Configure SSH Transfer and Backup Script
Under Build Environment, configure the SSH transfer to the storage server and set up the backup script:
1. Execute Shell:

   #!/bin/bash
   DATE=$(date +%F)
   BACKUP_FILE="db_${DATE}.sql"

   # Dump the database
   mysqldump -u kodekloud_roy -p'asdfgdsd' kodekloud_db01 > /tmp/${BACKUP_FILE}

   # Copy the backup to the Backup Server
   scp /tmp/${BACKUP_FILE} clint@stbkp01:/home/clint/db_backups/

   # Clean up
   rm /tmp/${BACKUP_FILE}

 Step 7: Set Up Password-less SSH Connection
For secure and automated file transfers:
1. Log in to the database server as `peter`:
  
   ssh peter@stdb01

2. Generate an SSH key pair:
  
   ssh-keygen

3. Copy the SSH key to the backup server:
  
   ssh-copy-id clint@stbkp01

 Step 8: Build and Check the Console Output
Finally, build the job and verify the process:
1. Click Build Now to start the job.
2. Monitor the Console Output to ensure the backup is created and transferred successfully.

Tasks: Jenkins Scheduled Jobs
The devops team of xFusionCorp Industries is working on to setup centralised logging management system to maintain and analyse server logs easily. Since it will take some time to implement, they wanted to gather some server logs on a regular basis. At least one of the app servers is having issues with the Apache server. The team needs Apache logs so that they can identify and troubleshoot the issues easily if they arise. So they decided to create a Jenkins job to collect logs from the server. Please create/configure a Jenkins job as per details mentioned below:
Click on the Jenkins button on the top bar to access the Jenkins UI. Login using username admin and password Adm!n321
1. Create a Jenkins jobs named copy-logs.
2. Configure it to periodically build every 9 minutes to copy the Apache logs (both access_log and error_logs) from App Server 3 (from default logs location) to location /usr/src/dba on Storage Server.

Solution: 

 Step 1: Access Jenkins UI
To begin, access your Jenkins User Interface:
1. Click on the Jenkins button located on the top bar.
2. Log in using the credentials:
   - Username: admin
   - Password: Adm!n321

 Step 2: Install Necessary Plugins
Next, we need to install some essential plugins:
1. Navigate to Jenkins > Manage Jenkins > Manage Plugins and select the Available tab.
2. Search for the following plugins:
   - SSH
   - Publish over SSH
3. Select these plugins and click Download now and install after restart.

 Step 3: Configure Publish Over SSH
To set up the SSH connection for transferring files:
1. Go to Jenkins > Manage Jenkins > Configure System.
2. Scroll down to the Publish over SSH section.
3. Under SSH Servers, click Add and fill in the following details:
   - Hostname: stapp03
   - Username: banner
   - Password: BigGr33n

 Step 4: Create a New Job
Let's create a new Jenkins job for the backup process:
1. Click on New Item.
2. Enter the name `copy-logs`.
3. Select Freestyle project and click OK.

 Step 5: Configure the Job
To set up the job configuration:
1. Build Triggers:
   - Go to the Build Triggers section.
   - Check the box for Build periodically.
   - Enter the cron expression `H/9 * * * *` to schedule the job to run every 10 minutes.

 Step 6: Configure SSH Transfer and Backup Script
Under Build Environment, configure the SSH transfer to the storage server and set up the backup script:
1. Execute Shell:

# Copy logs to the Storage Server
scp /var/log/httpd/* natasha@ststor01:/usr/src/dba/

 Step 7: Set Up Password-less SSH Connection
For secure and automated file transfers:
1. Log in to the database server as `peter`:
  
   ssh banner@stapp03

2. Generate an SSH key pair:
  
   ssh-keygen

3. Copy the SSH key to the backup server:
  
   ssh-copy-id natasha@ststor01

 Step 8: Build and Check the Console Output
Finally, build the job and verify the process:
1. Click Build Now to start the job.
2. Monitor the Console Output to ensure the backup is created and transferred successfully.

No comments:

Post a Comment