Monday, June 29, 2020

How to create an Elastic IP Address in AWS and assign to your EC2 instance | Associate Elastic IP address to EC2 instance

If you are using AWS free tier account, every time you stop and restart the EC2 instance the public ip/dns name changes. To avoid this you can use Elastic IP address provided by AWS. Elastic IP address is static IP address or permanent address provided by AWS. 

How to create Elastic IP address:

Go to AWS console, Click on EC2, Elastic IPs.

Click on Allocate Elastic IP address


Now it should create Elastic IP address.



Click on Actions, Associate Elastic IP address and choose your instance from Instances textbox and pick up the Private ip address automatically.

That's it! Elastic IP(static) address have been assigned to your EC2 instance.

Wednesday, June 24, 2020

Install AWS CLI version 2 on Mac OS | How to install AWS CLI 2 on Mac OS

Install AWS CLI version 2 on Mac OS
Download the file using the curl command. The -o option specifies the file name that the downloaded package is written to. In this example, the file is written to AWSCLIV2.pkg in the current folder.

curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"

Run the standard macOS installer program, specifying the downloaded .pkg file as the source. Use the -pkg parameter to specify the name of the package to install, and the -target / parameter for which drive to install the package to. The files are installed to /usr/local/aws-cli, and a symlink is automatically created in /usr/local/bin. You must include sudo on the command to grant write permissions to those folders.

sudo installer -pkg AWSCLIV2.pkg -target /

aws --version


Thursday, June 11, 2020

How to configure webhooks in Bitbucket to trigger a build in Jenkins? How to trigger automated builds in Jenkins through Bitbucket?

Webhooks are triggers that enables developers to trigger Jenkins jobs automatically every time there is a code change. Jenkins builds can be triggered by following ways:
  • pull - using poll scm
  • push mechanism - by triggering a build from Bitbucket or GitHub for every repository changes.
  • Slack commands
we will see in this article how to trigger (push) build in Jenkins for every change in bitbucket repository:


Pre-requistes:
Install Bitbucket plug-in Jenkins:



Changes needed in Jenkins
Select the job you would like to configure Webhook is for. Click on configure, under Build triggers section, click on Build when a change is pushed to Bitbucket. Save the build job.
Changes needed in Bitbucket
1. Go to Bitbucket, choose the repository where you have your webapp configured., go to settings, click on web hooks.
2. enter title, url which should be like --> http://jenkins_server_url/bitbucket-hook/
for e.g.,  http://ec2-18-191-42-55.us-east-2.compute.amazonaws.com:8080/bitbucket-hook/
3. status should be active.
4. click on skip certificate verification
5. triggers --> repository push
Click save. you should be able to see the webhooks.


Now click on View requests. Make sure the url is correct 

Now make a code change in Bitbucket to see if that triggers a build in Jenkins automatically.
You can also watch the steps in my YouTube channel as well:
 

Tuesday, June 9, 2020

Deploy Python App into Kubernetes Cluster using Jenkins Pipeline | Containerize Python App and Deploy into AKS Cluster

We will learn how to automate Docker builds using Jenkins and Deploy into Kubernetes Cluster setup using AKS. We will use Python based application. I have already created a repo with source code + Dockerfile. The repo also have Jenkinsfile for automating the following:

- Automating builds using Jenkins
- Automating Docker image creation
- Automating Docker image upload into Docker registry
- Automating Deployments to Kubernetes Cluster

Pre-requistes:
1. AKS Cluster is setup and running. Click here to learn how to create AKS cluster.
2. Jenkins Master is up and running. 
3. Setup Jenkins slave to run Docker builds
4. Docker, Docker pipeline and Kubernetes Deploy plug-ins are installed in Jenkins



5. Docker hub account setup in https://cloud.docker.com


Step #1 - Create Credentials for Docker Hub
Go to Jenkins UI, click on Credentials -->


Click on Global credentials
Click on Add Credentials


Now Create an entry for your Docker Hub account. Make sure you enter the ID as dockerhub

Step #2 - Create Credentials for Kubernetes Cluster
Click on Add Credentials, use Kubernetes configuration from drop down.


execute the below command to get kubeconfig info, copy the entire content of the file:
sudo cat ~/.kube/config


Enter ID as K8S and choose enter directly and paste the above file content and save.

Step # 3 - Create a pipeline in Jenkins
Create a new pipeline job.


Step # 4 - 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 {
         label 'myslave'
     }
        environment {
        //once you sign up for Docker hub, use that user_id here
        registry = "your_docker_hub_user_id/mypython-app"
        //- update your credentials ID after creating credentials for connecting to Docker Hub
        registryCredential = 'dockerhub'
        dockerImage = ''
    }
    stages {

        stage ('checkout') {
            steps {
            checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'https://github.com/akannan1087/myPythonDockerRepo']]])
            }
        }
       
        stage ('Build docker image') {
            steps {
                script {
                dockerImage = docker.build registry
                }
            }
        }
       
         // Uploading Docker images into Docker Hub
    stage('Upload Image') {
     steps{   
         script {
            docker.withRegistry( '', registryCredential ) {
            dockerImage.push()
            }
        }
      }
    }
   
    stage ('K8S Deploy') {
        steps {
            script {
                kubernetesDeploy(
                    configs: 'k8s-deployment.yaml',
                    kubeconfigId: 'K8S',
                    enableConfigSubstitution: true
                    )           
               
            }
        }
    }
  
    }  
}

Step # 5 - Build the pipeline
Once you create the pipeline and changes values per your Docker user id and credentials ID, click on 

Step # 6 - Verify deployments to K8S

kubectl get pods


kubectl get deployments
kubectl get services

Steps # 7 - Access Python App in K8S cluster
Once build is successful, go to browser and enter master or worker node public ip address along with port number mentioned above
http://master_or_worker_node_public_ipaddress:port_no_from_above

You should see page like below:


Please watch the above steps in YouTube channel:

Tuesday, June 2, 2020

Migrate jenkins jobs from one server to another | Migrate Jenkins jobs from one Jenkins server to another Jenkins server

How to migrate Jenkins jobs from one Jenkins instance to another Jenkins instance?
Jenkins jobs can be migrated in many ways. You can establish SSH connection b/w source machine and target machine and use scp command to copy from one instance to another instance. You can also use Job import plug-in to migrate jobs.

Pre-requistes:

Make sure you install job import plugin in target Jenkins instance.

Steps to migrate jobs from source Jenkins to Target Jenkins instance:

Make sure you have right Jenkins source URL by logging to Source Jenkins.
Click on Manage Jenkins --> Configure System
Copy the Jenkins URL.
Logout from source Jenkins.

Steps in Target Jenkins
1. Login to Target Jenkins
2. Go to Manage Jenkins -->configure system--> click on Job import plug-in
Add source Jenkins url and login credentials.


3. Click on Save
4. Go to Jenkins -->Job Import plug-in
 5. Select source Jenkins from down and check search into folder option
  click on Query

6. Now it should pull all the jobs, choose the jobs you would like to migrate and click on Import.
Make sure you choose install required plug-ins option as well.
 Now you should see the message.
7. Go to Jenkins home page and refresh it. You should see the jobs migrated successfully.
You can watch the steps in my YouTube channel as well:

Note: Please note that Credentials will not be migrated, only Jobs and plug-ins will be migrated.

Install kubectl on Mac OS | How to install kubectl in MAC OS

Kubernetes uses a command line utility called  kubectl  for communicating with the cluster API server. It is  tool for controlling  Kubernet...