Backup Jenkins on Kubernetes


You can use the instructions on this page to create pre and post backup rules with PX-Backup, which take application-consistent backups for Jenkins on Kubernetes in production.

Jenkins stores data within a directory known as JENKINS_HOME. Workspaces, plug-ins, jobs, user content and overall configuration exist within this directory and are vital when recovering from various types of failures.

See below for a snippet of what this might look like in a Kubernetes spec file.

...
        volumeMounts:
        - name: jenkins-home
          mountPath: /var/jenkins_home
      volumes:
        - name: jenkins-home
          persistentVolumeClaim:
            claimName: jenkins-data
...

Once you configure a PVC to be mounted at /var/jenkins_home you can use the below guide for pre and post backup rules.

Installation

Prerequisites

If you have not used Portworx, PX-Backup, or run Data services on Kubernetes it is a good idea to review the following information:

NOTE:

  • You should configure a persistent volume for JENKINS_HOME so that the entire directory is backed by a volume to capture all data associated with Jenkins.
  • These examples assume that the Jenkins CLI is available within the Jenkins pod. You can make sure its available in the Jenkins pod(s) by running the below command.

    kubectl exec <jenkins-deployment-pod> -n jenkins -- /bin/sh -c "wget http://<operations-center-url>:>port-number>/jnlpJars/jenkins-cli.jar -O /var/jenkins_home/cli.jar"
    

Create rules for Jenkins

Create rules for Jenkins that will run both before and after the backup operation runs:

Create a pre-exec backup rule for Jenkins

Assume the Jenkins deployment has a job called job-1, you will use the pre-exec rule to make sure this job is stopped before taking our snapshot.

NOTE: Stopping a job is not necessary to backup Jenkins. It is used as an example of a pre backup rule. You may run other commands available within the Jenkins pod here as well.
  1. Navigate to SettingsRulesAdd New.
  2. Add a name for your Rule.
  3. Add the following app label:

    app=jenkins
  4. Add the following action:

    java -jar /var/jenkins_home/cli.jar -s http://jenkins:8080 -webSocket -auth username:password stop-builds job-1

Create a post-exec backup rule for Jenkins

After your backup is triggered, you can start the build for job-1 again using a post-exec rule. This ensures your job is not running during the backup and is restarted after the backup data is captured.

  1. Navigate to SettingsRulesAdd New.
  2. Add a name for your Rule.
  3. Add the following app label:

    app=jenkins
  4. Add the following action:

    java -jar /var/jenkins_home/cli.jar -s http://jenkins:8080 -webSocket -auth username:password build job-1

Use the rules during backup of Jenkins

During the backup creation process, select the rules in the pre-exec and post-exec drop downs:

Once you’ve filled out the backup form, click Create

Demo

Watch this short demo of the above information.


Last edited: Thursday, Oct 1, 2020