Tuesday, August 24, 2021

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 and Deploy PHP based Docker container into EKS cluster. 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 into AWS ECR
- Automating Docker container Deployment into EKS Cluster



Pre-requistes:
1. Amazon EKS Cluster is setup and running. Click here to learn how to create Amazon EKS cluster.
3. Setup Jenkins slave, install docker in it.
4. Docker, Docker pipeline and Kubernetes Deploy plug-ins are installed in Jenkins
5. Create an IAM role with Administrator policy, attach to Jenkins EC2 instance


Step #1 - Create Credentials for Kubernetes Cluster
Go to your Jenkins where you have installed Docker as well. Go to credentials -->
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 #2 - set a clusterrole as cluster-admin

By default, clusterrolebinding has system:anonymous set which blocks the cluster access. Execute the following command to set a clusterrole as cluster-admin which will give you the required access.

kubectl create clusterrolebinding cluster-system-anonymous --clusterrole=cluster-admin --user=system:anonymous

Step # 3 - Create a pipeline in Jenkins, name can be anything


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 any 
      environment {
        registry = "acct_id.dkr.ecr.us-east-2.amazonaws.com/
your_ecr_repo"
    }

    
    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 AWS ECR
    stage('Pushing to ECR') {
     steps{  
         script {
                sh 'aws ecr get-login-password --region us-east-2 | docker login --username AWS --password-stdin 
acct_id.dkr.ecr.us-east-2.amazonaws.com'
                sh 'docker push 
acct_id.dkr.ecr.us-east-2.amazonaws.com/your_ecr_repo:latest'
         }
        }
      }
    
      stage ('K8S Deploy') {
       
                kubernetesDeploy(
                    configs: 'phpK8SDeploy.yaml',
                    kubeconfigId: 'K8S',
                    enableConfigSubstitution: true
                    )           
                  
        }

  }
}  


Step # 5- Click on Build - 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 PHP App
Once build is successful, go to browser and enter http://public_dns_name:8086
You should see page like below:



No comments:

Post a Comment

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...