CI/CD Using Jenkins and Spinnaker: Automating Deployment to Kubernetes

Introduction:

In modern software development, Continuous Integration and Continuous Deployment (CI/CD) pipelines play a crucial role in ensuring efficient and automated workflows. This blog focuses on setting up a CI/CD pipeline using Jenkins and Spinnaker for a Git-based application. When changes are pushed to the Git repository, Jenkins triggers the build process, builds the Docker image, pushes it to a Docker registry, and deploys the application to a Kubernetes cluster.

Let’s dive into the details of setting up this robust CI/CD pipeline.

Table Of Contents:

  • Overview

  • Prerequisites

  • Setting Up Jenkins

  • Setting Up Spinnaker

  • Integrating Jenkins with Spinnaker

  • Installation

  • Conclusion

1. Overview

In this setup, the CI/CD process involves:

  • Source Code Repository: GitHub/GitLab repository where developers push changes.

  • Jenkins: Automates the build and push image to docker and update the image tag in git repo

  • Docker: Builds Docker images for the application.

  • Docker Registry: Stores Docker images.

  • Spinnaker: Orchestrates deployments to Kubernetes.

Each commit to the Git repository triggers Jenkins, which builds the Docker image and pushes it to a Docker registry. Spinnaker then deploys the image to a Kubernetes cluster.

2. Prerequisites

Before starting with the setup, ensure the following prerequisites are in place:

  • Version Control System: GitHub or GitLab

  • Jenkins: Installed and configured for CI/CD automation

  • Spinnaker: Installed and configured for deployment orchestration

  • Docker: Installed for building Docker images

  • Kubernetes: Cluster setup for deploying applications

  • Docker Registry: Docker Hub, AWS ECR, or any private registry

3. Setting Up Jenkins

  1. Installation and Configuration:

    • Install Jenkins on your server.

    • Configure Jenkins with necessary plugins for Docker, Kubernetes, and Git.

    • Create necessary credentials for Docker registry and Kubernetes cluster.

  2. Jenkins Credentials:

    • Docker registry credentials (username and password/token).

    • Kubernetes credentials for accessing the cluster.

4. Setting Up Spinnaker

  1. Installation and Configuration:

    • Install Spinnaker with necessary integrations (Docker registry, Kubernetes, etc.).

    • Configure Spinnaker pipelines for deployments and manage resources.

  2. Pipeline Configuration:

    • Define stages for Configuration, Bake, and Deploy stages in Spinnaker.

5. Integrating Jenkins with Spinnaker

  • Webhook Integration:

    • Configure Spinnaker to listen for webhooks from Jenkins.

    • When a build completes successfully in Jenkins, a webhook is sent to Spinnaker to trigger deployment.

6.Installation:

Step: 1

Deploy the spinnaker on k8s cluster using helm, by using the below chart
spinnaker 2.2.7 · opsmx/opsmx

Step:2

Login to the halyard container and configure Git ,Jenkins , and Kubernetes

kubectl exec -it spinnaker-spinnaker-halyard-0 -n spinnaker -- bash
Defaulted container "halyard" out of: halyard, create-halyard-local (init)
bash-5.0$

Step: 3

Enable and configure Kubernetes using Hal commands

  hal config provider kubernetes enable

  hal config provider kubernetes account add k8s-account --provider-version v2 --context $(kubectl config current-context)
  note: k8s-account <account name we can add any this name will be reflected in spinnaker or we can get the nam default as well>

Step:4

Enable and configure git repo and git hub for artifacts.

  hal config artifact github enable
  hal config artifact github account add <ACCOUNT_NAME>  --token <GITHUB_TOKEN>

## We need to enable artifacts from git repo so we need to configure gitrepo

hal config artifact gitrepo enable
## Fetch the tooken from github and store it in the file like below.
echo $TOKEN > $TOKEN_FILE

## add account using like this 
hal config artifact gitrepo account add $ARTIFACT_ACCOUNT_NAME \
    --token-file $TOKEN_FILE

Step: 5

Enable and configure Jenkins

hal config ci jenkins enable
hal config ci jenkins master add jenkins-master --address <jenkins url> --username <username> --password <password>
hal config ci jenkins master edit jenkins-master --csrf true

Step: 6

After doing all this run below command to apply the changes:

hal deploy apply

Step:7

Follow the below repo for the source code and fork it to your git repo and enable github webhook with Jenkins. with push events

GitHub repo

Step:8
Configure the Jenkins pipeline using the GitHub repo and add all the needed credentials like the docker hub registry, git credentials on the credentials section of Jenkins like below

Step:9:

Now go to Spinnaker and create a application and create a pipeline like below

Pipeline Stages:

Configuration:

  • Go to automatic triggers and add Jenkins as type and add the account which you configured before and add the job. like below.

Bake:

Click on add stage and add a new stage as Bake Manifest

Here add the render engine as Helm and add git repo which we configured before ad the chart name. This stage will fetch the chart from repo and will make it as a artifact

Deploy:

Click on add stage and add a stage as Deploy manifest it will fetch the artifact produced from previous stage and deploy it on k8s cluster which we configured before. Here i deployed spinnaker on k8s so it the account name is default if it is an external account then we need to give another name while configuring before.

Step: 8:

Now trigger the pipeline either manually or automatically as we have enabled the trigger with Jenkins, so whenever a job gets triggered on Jenkins a spinnaker pipeline will be triggered with that build number.

6.Conclusion:

By integrating Jenkins and Spinnaker into your CI/CD pipeline, you create a powerful, automated workflow that ensures smooth application delivery to Kubernetes. This streamlined process automates code changes from version control through build and testing phases to the deployment of Docker images in a reliable and efficient way.

With Jenkins handling the build and image creation and Spinnaker managing deployments across Kubernetes, teams can focus on developing features while maintaining a strong and repeatable deployment process. The ability to monitor, roll back, and scale deployments ensures a highly resilient environment for your applications, ultimately driving faster innovation and improved development speed.

0
Subscribe to my newsletter

Read articles from Sharath Veerapaneni directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sharath Veerapaneni
Sharath Veerapaneni

I am a DevOps Engineer