Jesper O. Christensen

Cloud Architect @ Brandheroes

Continuous delivery to Kubernetes with Travis CI

2020-01-04 jesper o. christensenKubernetes...

unsplash.com

Continuous delivery is a hot buzz-word in todays software world. Finding the right CI tool to fit your needs is not always easy. Travis CI might not provide you with a fancy UI or be as powerful as Jenkins, but it’s one of the simpler pipelines to setup, supports a lot of different languages and supports deployment to a variety of different services.

In this post I will show you how you can make Travis CI update your deployments in your Kubernetes cluster.

Travis does not come with “out-the-box” support to Kubernetes. But we can very easily add the tools we need to speak with our Kubernetes Cluster.

We need two things for that to work.

  • Kubernetes CLI (kubectl)
  • Access to the cluster

Let’s get to it!

Installing the Kubernetes CLI

Lets start by installing the Kubernetes CLI in the install lifecycle step like this:

install:
  - curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
  - chmod +x ./kubectl
  - sudo mv ./kubectl /usr/local/bin/kubectl

So with these three lines we have downloaded and installed the Kubernetes CLI and made kubectl accessible from the command line.

Now we need the config file for kubectl to access our cluster.

Accessing our cluster

There are multiple ways you could setup the access to your cluster.

The easiest way in my mind is to base64 encode your config file and adding it as an environment variables under the settings for your repository

To base64 encode your config file you can use the following cmd:

cat ${HOME}/.kube/config | base64 | pbcopy

So lets add the environment variable as KUBE_CONFIG like this:

unsplash.com

You can ofcourse choose to only make it available for the branches that is actually gonna require access to the cluster, like a master and develop branch.

Now lets add the creation of the config file to the install step like so:

install:
  - curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
  - chmod +x ./kubectl
  - sudo mv ./kubectl /usr/local/bin/kubectl
  - mkdir ${HOME}/.kube
  - echo "$KUBE_CONFIG" | base64 --decode > ${HOME}/.kube/config

Here we create the .kube folder and then decode the KUBE_CONFIG environment variable and creates the config file with the content of the variable.

Now lets add a small test to the .travis.yml file to see if it works!

install:
  - curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
  - chmod +x ./kubectl
  - sudo mv ./kubectl /usr/local/bin/kubectl
  - mkdir ${HOME}/.kube
  - echo "$KUBE_CONFIG" | base64 --decode > ${HOME}/.kube/config

script:
  - kubectl get pods

If you review the console of the Travis CI build you should be able to verify that kubectl gave you the correct pods.

Closing thoughts

Beware that if your .kube config contains several contexts, you might need to choose the correct one before running the commands you wanna run.

In general i would advise that your config only contains contexts relevant to your repository.

Also if you do not want your third party CI provider to have the necessary data to access your clusters you should look into GitOps and Flux which is my preferred way to do deployments to my cluster.