Skip to content

Instantly share code, notes, and snippets.

@vishnuhd
Last active September 8, 2019 20:24
Show Gist options
  • Save vishnuhd/4f6b007ff47794be84bd979d3e1be1df to your computer and use it in GitHub Desktop.
Save vishnuhd/4f6b007ff47794be84bd979d3e1be1df to your computer and use it in GitHub Desktop.
Deploy Jenkins to K8s using Helm

Deploy Jenkins to K8s using Helm in Jenkins namespace

kubectl create ns jenkins
  • Install helm chart for Jenkins with jenkins namespace
helm install --name my-jenkins stable/jenkins --set namespaceOverride=jenkins
  • NOTES:
  1. Get your 'admin' user password by running:
  printf $(kubectl get secret --namespace jenkins my-jenkins -o jsonpath="{.data.jenkins-admin-password}" | base64 --decode);echo
  1. Get the Jenkins URL to visit by running these commands in the same shell: NOTE: It may take a few minutes for the LoadBalancer IP to be available. You can watch the status of by running kubectl get svc --namespace jenkins -w my-jenkins
  export SERVICE_IP=$(kubectl get svc --namespace jenkins my-jenkins --template "{{ range (index .status.loadBalancer.ingress 0) }}{{ . }}{{ end }}")
  echo http://$SERVICE_IP:8080/login
  1. Login with the password from step 1 and the username: admin

Setup for docker in docker

  • Create a container tamplate with name and image as docker in Manage Jenkins -> Configure System.
  • Add a host path volume with host path and mount path as /var/run/docker.sock
  • Find below some useful Jenkinsfiles :
pipeline {
    agent {
        kubernetes {
            label "my-jenkins-jenkins-slave"
            defaultContainer 'docker'
        }
    }
    stages{
        stage("Build and Push to ACR") {
            steps {
                 sh "docker ps"
            }
        }
    }
}

AND

def label = "worker-${UUID.randomUUID().toString()}"

podTemplate(label: label, containers: [
  containerTemplate(name: 'docker', image: 'docker', command: 'cat', ttyEnabled: true),
  containerTemplate(name: 'kubectl', image: 'lachlanevenson/k8s-kubectl:v1.8.8', command: 'cat', ttyEnabled: true),
  containerTemplate(name: 'helm', image: 'lachlanevenson/k8s-helm:latest', command: 'cat', ttyEnabled: true)
],
volumes: [
  hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock')
]) {
  node(label) {
    def myRepo = "test"
 
    stage('Test') {
      script{
          echo "Test"
      }
    }
    stage('Build') {
      script{
          echo "Build"
      }
    }
    stage('Create Docker images') {
      container('docker') {
          sh "docker ps"
      }
    }
    stage('Run kubectl') {
      container('kubectl') {
        sh "kubectl get pods"
      }
    }
    stage('Run helm') {
      container('helm') {
        sh "helm list"
      }
    }
  }
}

Issues

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