Skip to content

Instantly share code, notes, and snippets.

@MdSahil-oss
Last active January 3, 2024 19:23
Show Gist options
  • Save MdSahil-oss/aafeeef1e7338459ca12166c47ca9553 to your computer and use it in GitHub Desktop.
Save MdSahil-oss/aafeeef1e7338459ca12166c47ca9553 to your computer and use it in GitHub Desktop.

Auto Deploying application on K8s using Jenkins

Screenshot from 2024-01-02 20-05-55

Technologies (I used):

  • AWS
  • K8s
  • CI/CD (Jenkins)
  • Docker

Link to the project

Description (Workflow):

Created Resources:

  • In this project, Firstly I created a source code repository on github.
  • Created a Jenkins multi-branch pipeline.
  • Created a Kubernete cluster using AWS-EKS.
  • A public Docker registry to store images

Workflow (Working):

  • On every push or update to the source-code repository Jenkins pipeline gets triggered.
  • On getting triggered Jenkins does the followings:
    • Fetch the new/updated source code from the repository.
    • Build contianer/docker image of fetched source code.
    • pushes the built image to the dockerhub.
    • And Deploy the YAML files (K8s resources) to Kubernetes (AWS EKS).
  • At the end K8s pulls the latest build from the dockerhub (image registry) and restart the application.

How did I build this project?

  • Firstly, Created a source code repository.
  • Created EKS cluster in my AWS environment.
  • Then, I started a multibranch job type pipeline of Jenkins with the following Jenkinsfile:
    pipeline {
      environment {
        dockerimagename = "mdsahiloss/simple-user-profile-page"
        dockerImage = ""
      }
      agent any
      stages {
        stage('Checkout Source') {
          steps {
            git 'https://github.com/MdSahil-oss/simple-user-profile-page.git'
          }
        }
        stage('Build image') {
          steps{
            script {
              dockerImage = docker.build dockerimagename
            }
          }
        }
        stage('Pushing Image') {
          environment {
              registryCredential = 'DockerhubCredentials'
               }
          steps{
            script {
              docker.withRegistry( 'https://registry.hub.docker.com', registryCredential ) {
                dockerImage.push("latest")
              }
            }
          }
        }
        stage('Deploying Application container to Kubernetes') {
          steps {
                withKubeConfig([
                            clusterName: 'minikube',
                            namespace: 'default',
                            contextName: 'jenkins-minikube',
                            serverUrl:   'https://192.168.49.2:8443',
                            credentialsId: 'k8s-creadentials'
                            ]) {
                sh 'kubectl delete -f ./k8s && kubectl apply -f ./k8s'
            }
          }
        }
      }
    }
    That does everything followings:
    • Builds container image
    • Push container image to dockerhub (image registry)
    • Redeploy yaml resources to K8s cluster.

That's all about this project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment