/ticketzen-jenkins-k8s

Jenkins CICD for Ticketzen Microservice Application in Kubernetes

Primary LanguageShell

Jenkins on Local (Docker Windows \ Minikube \ etc)

Setting up Jenkins Agent

After installing kubernetes-plugin for Jenkins

  • Go to Manage Jenkins | Bottom of Page | Cloud | Kubernetes (Add kubenretes cloud)
  • Fill out plugin values
    • Name: kubernetes
    • Kubernetes URL: https://kubernetes.default:443
    • Kubernetes Namespace: jenkins
    • Credentials | Add | Jenkins (Choose Kubernetes service account option & Global + Save)
    • Test Connection | Should be successful! If not, check RBAC permissions and fix it!
    • Jenkins URL: http://jenkins
    • Tunnel : jenkins:50000
    • Apply cap only on alive pods : yes!
    • Add Kubernetes Pod Template
      • Name: jenkins-slave
      • Namespace: jenkins
      • Service Account: jenkins
      • Labels: jenkins-slave (you will need to use this label on all jobs)
      • Containers | Add Template
        • Name: jnlp
        • Docker Image: /jenkins-slave
        • Command to run :
        • Arguments to pass to the command:
        • Allocate pseudo-TTY: yes
        • Add Volume
          • HostPath type
          • HostPath: /var/run/docker.sock
          • Mount Path: /var/run/docker.sock
      • Timeout in seconds for Jenkins connection: 300
  • Save

Test a build

To run docker commands inside a jenkins agent you will need a custom jenkins agent with docker-in-docker working. Take a look and build the docker file in ./dockerfiles/

NOTES:

1. Update base image used in Jenkins slave Dockerfile to jdk >= 14 compatible with Jekins version (image: jenkins/jenkins:2.401.1-lts-alpine)
2. Build image <docker-id>/jenkins-slave

Push it to a registry and use it instead of above configured * Docker Image: jenkins/jnlp-slave If you do not use the custom image, the below pipeline will not work because default * Docker Image: jenkins/jnlp-slave public image does not have docker ability.

  • Add a Jenkins Pipeline
node('jenkins-agent-1') {

     stage('unit-tests') {
        sh(script: """
            docker run --rm alpine /bin/sh -c "echo hello world"
        """)
    }
}

Jenkins on Amazon Kubernetes

For running Jenkins on AMAZON, start here