Hands on DevSecOps Coaching that is provided on AWS and Azure Cloud platforms. Contact Coach AK at devops.coaching@gmail.com for more info. You can also reach out to Coach AK at +1(469) 733-5248
if you want to execute some commands during boot up(also launch), you can execute it easily by mentioning during step 3 of EC2 launch. Bootstrap scripts run only once - when the instance is instantiated for the 1st time.
Please
follow the below steps to create an EC2 instance. We will be installing
Java, Maven, Jenkins and Apache during boot up.
Steps to add bootstrap script during EC2 launch
1: Go to AWS console. click on All services, Click on Compute --> Click on EC2
2. Click on Launch instance. Choose an Amazon machine image (AMI), click next
3. select Ubuntu Server 18.04 LTS (HVM), SSD Volume Type
click next 4. choose an instance type as t2.small, 2GB memory. click next
5. in step 3, go down, under advanced details. Copy the script from this link.
6. Now continue like how you provision EC2 instance. 7. once you provision, you will be able to see Java, Maven and Jenkins installed in EC2 instance after launch. Check the Logs in EC2 instance
Login to EC2 instance, and type the below command:
tail -f /var/log/cloud-init-output.log
This will give the output of bootstrap execution
Verify if Java got installed.
java -version mvn --version
Go to the browser and try to access Jenkins in the browser, Jenkins should be coming up.(make sure you open port 8080 in the firewall rules)
Java, Maven and Jenkins should be installed successfully.
We will learn how to automate Docker builds using Jenkins. We will use PHP based application. I have already created a repo with source code + Dockerfile. The repo also have Jenkinsfile for automating the following:
Pre-requisites:
1. Jenkins is up and running
2. Docker installed on Jenkins instance
3. Docker pipeline and Docker pipeline plug-ins are installed in Jenkins
4. user account setup in https://cloud.docker.com
5. Port 8086 open in security firewall rules
Step #1 - Create Credentials for Docker Hub
Go to your Jenkins where you have installed Docker as well. Go to credentials -->
Click on Global credentials
Click on Add Credentials
Now Create an entry for Docker Hub credentials
Make sure you take note of the ID as circled below:
Step # 2 - Create a scripted pipeline in Jenkins, name can be anything
Step # 3 - Copy the pipeline code from below
Make sure you change yellow highlighted section values below:
Your docker user id should be updated.
your registry credentials ID from Jenkins from step # 1 should be copied
pipeline { agent any environment { //TODO # 1 --> once you sign up for Docker hub, use that user_id here registry = "your_docker_userid/myphp-app-may20" //TODO #2 - update your credentials ID after creating credentials for connecting to Docker Hub registryCredential = 'your_credentials_id_from_step 1_above' dockerImage = '' } stages { stage('Cloning Git') { steps { checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '', url: 'https://bitbucket.org/ananthkannan/phprepo/']]]) } } // Building Docker images stage('Building image') { steps{ script { dockerImage = docker.build registry } } } // Uploading Docker images into Docker Hub stage('Upload Image') { steps{ script { docker.withRegistry( '', registryCredential ) { dockerImage.push() } } } } // Stopping Docker containers for cleaner Docker run stage('docker stop container') { steps { sh 'docker ps -f name=myPhpContainer -q | xargs --no-run-if-empty docker container stop' sh 'docker container ls -a -fname=myPhpContainer -q | xargs -r docker container rm' } } // Running Docker container, make sure port 8096 is opened in stage('Docker Run') { steps{ script { dockerImage.run("-p 8086:80 --rm --name myPhpContainer") } } } } }
Step # 4 - Click on Build - Build the pipeline
Once you create the pipeline and changes values per your Docker user id and credentials ID, click on
We will learn how to automate Docker builds using Jenkins. We will use Python based application. I have already created a repo with source code + Dockerfile. We will be creating Jenkins pipeline for automating builds.
Pre-requisites:
1. Jenkins is up and running
2. Docker installed on Jenkins instance and configured.
3. Docker plug-in installed in Jenkins
4. user account setup in https://cloud.docker.com
5. port 8096 is opened up in firewall rules.
Step #1 - Create Credentials for Docker Hub
Go to your Jenkins where you have installed Docker as well. Go to credentials -->
Click on Global credentials
Click on Add Credentials
Now Create an entry for Docker Hub credentials
Make sure you take note of the ID as circled below:
Step # 2 - Create a pipeline in Jenkins, name can be anything
Step # 3 - Copy the pipeline code from below
Make sure you change red highlighted values below:
Your docker user id should be updated.
your registry credentials ID from Jenkins from step # 1 should be copied
pipeline { agent any environment { //once you sign up for Docker hub, use that user_id here registry = "your_docker_user_id/mypythonapp" //- update your credentials ID after creating credentials for connecting to Docker Hub registryCredential = 'fa32f95a-2d3e-4c7b-8f34-11bcc0191d70' dockerImage = '' } stages { stage('Cloning Git') { steps { checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '', url: 'https://bitbucket.org/ananthkannan/mypythonrepo']]]) } } // Building Docker images stage('Building image') { steps{ script { dockerImage = docker.build registry } } } // Uploading Docker images into Docker Hub stage('Upload Image') { steps{ script { docker.withRegistry( '', registryCredential ) { dockerImage.push() } } } } // Stopping Docker containers for cleaner Docker run stage('docker stop container') { steps { sh 'docker ps -f name=mypythonappContainer -q | xargs --no-run-if-empty docker container stop' sh 'docker container ls -a -fname=mypythonappContainer -q | xargs -r docker container rm' } } // Running Docker container, make sure port 8096 is opened in stage('Docker Run') { steps{ script { dockerImage.run("-p 8096:5000 --rm --name mypythonappContainer") } } } } }
Step # 4 - Click on Build - Build the pipeline
Once you create the pipeline and changes values per your Docker user id and credentials ID, click on
Every time developer makes code changes, you would want to
Jenkins to automate Docker images creation and pushing into Docker
registry. Let us see how to do this.
Pre-requisites:
Jenkins is up and running
Docker is installed in Jenkins machine. Click here to learn how to install Docker.
Docker plug-in installed in Jenkins.
Docker pipeline plug-in installed in Jenkins.
Steps:
Now Login to Jenkins EC2 instance, execute below commands:
Add jenkins user to Docker group sudo usermod -a -G docker jenkins
Restart Jenkins service sudo service jenkins restart
Reload system daemon files
sudo systemctl daemon-reload Restart Docker service as well
Please find steps needed for installing Docker in Ubuntu 22.0.4 instance. You can install in Docker in many ways. But try only one option.
Docker installation steps using default repository from Ubuntu
Update local packages by executing below command: sudo apt update
Install the below packages sudo apt install gnupg2 pass -y
gnupg2 is tool for secure communication and data storage. It can be used to encrypt data and to create digital signatures
Install docker sudo apt install docker.io -y
Add Ubuntu user to Docker group sudo usermod -aG docker $USER
We need to reload shell in order to have new group settings applied. Now you need to logout and log back in command line or execute the below command: newgrp docker
The Docker service needs to be setup to run at startup. To do so, type in each command followed by enter: