Saturday, May 16, 2020

How To Use BootStrap Scripts in AWS EC2 Instance during Launch - Running commands on your Linux instance at launch | How to Run commands on your Linux instance at launch

What is bootstrap script in aws?

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.


Shell script for setting up Jenkins in Ubuntu instance | Setup Jenkins using Shell Script

#!/bin/bash
# Shell script for installing Java, Jenkins and Maven in Ubuntu 22.0.4 EC2 instance

# Command for installing Java 17
sudo apt-get update
sudo apt install openjdk-17-jdk -y

 # Command for installing maven
sudo apt-get install maven -y

# Script for Jenkins installation


#Append debian package repo address to the system
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
  https://pkg.jenkins.io/debian binary/ | sudo tee \
  /etc/apt/sources.list.d/jenkins.list > /dev/null

# Install Jenkins

sudo apt-get update && sudo apt-get install jenkins -y

echo "Jenkins installed successfully.."

Monday, May 4, 2020

Top 15 Docker commands - Popular Docker commands

  • docker images - Lists all the docker images stored locally
  • docker build - Builds an image from a specified docker file
  • docker pull - pulls images from the docker repository
  • docker run - creates a container from an image
  • docker ps - lists the running containers
  • docker ps -a - shows all the running and exited containers
  • docker exec - access the running container
  • docker stop - stops a running container
  • docker kill - kills the container by stopping its execution immediately
  • docker commit - creates a new image of an edited container on the local system
  • docker login - This command is used to login to the docker hub repository
  • docker push - pushes an image to the docker hub repository
  • docker rm - deletes a stopped container
  • docker rmi - deletes an image from local storage

Sunday, May 3, 2020

Automate Docker builds using Jenkins - Dockerize PHP App | Containerize PHP App | Build Docker images using Jenkins

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:

- Automating builds
- Automating Docker image creation
- Automating Docker image upload
- Automating Docker container provisioning

Watch Steps in YouTube Channel:


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 

Steps # 5 - Access PHP App
Once build is successful, go to browser and enter http://public_dns_name:8086
You should see page like below:

Create Jenkins Pipeline for automating Docker image creation and push docker image into Docker Hub | Dockerize Python App

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.


- Automating builds
- Automating Docker image creation
- Automating Docker image upload
- Automating Docker container provisioning

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 

Steps # 5 - Access Python App
Once build is successful, go to browser and enter http://public_dns_name:8096
You should see page like below:




Saturday, May 2, 2020

Docker Jenkins Integration | Building Docker Images using Jenkins | How to automate Docker Images creation using Jenkins

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

sudo service docker stop
sudo service docker start
 
Watch the above steps in YouTube video:

Install Docker Ubuntu | How to install Docker in Ubuntu 22.0.4 | Setup Docker on Ubuntu

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:

sudo systemctl start docker

sudo systemctl enable docker
sudo systemctl status docker

The above screenshot should confirm that Docker daemon is up and running.

Docker installation steps using Official Docker Repository (Alternative installation steps)

sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-cache policy docker-ce
sudo apt-get install -y docker-ce
sudo systemctl status docker

now press q to come out this.
ps -ef | grep docker

docker --version

sudo docker run hello-world

the above command should display the below message..

you should see below message 
Hello from Docker!
This message shows that your installation appears to be working correctly.

Setup Docker Registry
Now we need to setup Docker registry. You have one of three options.

Option 1 - DockerHub as Docker Registry
Also, create an account(keep all lowercase in your username)  in the below website for storing docker images in public docker registry..

https://cloud.docker.com/
 
Option 2 - Configure Nexus as Docker Registry
Please click below link to configure Nexus as Docker Registry.
https://www.cidevops.com/2020/02/how-to-configure-nexus-as-docker.html

Option 3 - Configure AWS ECR as Docker Registry
Please click below link to configure Amazon ECR as Docker Registry.
https://www.cidevops.com/2020/05/how-to-setup-elastic-container-registry.html
 
Option 4 - Configure Azure Container Registry
Please click below link to configure ACR in Azure.
https://www.coachdevops.com/2019/12/how-to-upload-docker-images-to-azure.html

Microservices Introduction
Containerization is increasingly popular because containers are:
  • Flexible: Even the most complex applications can be containerized.
  • Lightweight: Containers leverage and share the host kernel.
  • Interchangeable: You can deploy updates and upgrades on-the-fly.
  • Portable: You can build locally, deploy to the cloud, and run anywhere.
  • Scalable: You can increase and automatically distribute container replicas.
  • Stackable: You can stack services vertically and on-the-fly.

DevSecOps Bootcamp Dec 2024 Schedule | DevSecOps & AWS Azure Cloud Coaching by Coach AK | DevSecOps and Cloud Computing Online Classes

Lot of new topics covered like GitHub Actions, GitHub Advanced Security, Helm, Prometheus and Grafana..) The DevOps requirements in the IT m...