Integrating Kind with Travis

Share this :
Share this :

Kind is a tool for running local Kubernetes clusters using Docker container “nodes”. Kind was primarily designed for testing Kubernetes itself, but may be used for local development or CI, and in this use case we will be using Kind with Travis. Let’s learn how.

Usage

Let’s get started with installing Kind, here’s a bash script I created that you can run in your .travis.yml file for one approach, but I’ll also be sharing the approach I did that didn’t require this bash script:

#!/bin/bash

curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.11.0/kind-linux-amd64
chmod +x ./kind
mv ./kind /usr/bin/kind
echo

apt-get install -y apt-transport-https ca-certificates curl gnupg lsb-release
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/nul
curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
apt-get update
apt-get install -y docker-ce docker-ce-cli containerd.io kubectl
echo

echo "Installing OpenStack client"
sleep 2
apt-get  install -y python3-pip
pip3 install python-openstackclient python-octaviaclient
mkdir -p /etc/openstack
echo

wget https://raw.githubusercontent.com/kubernetes-sigs/cluster-api-provider-openstack/master/templates/env.rc -O /tmp/env.rc
echo

curl -L https://github.com/kubernetes-sigs/cluster-api/releases/download/v0.4.0/clusterctl-linux-amd64 -o clusterctl
chmod +x ./clusterctl
mv ./clusterctl /usr/local/bin/clusterctl

kind create cluster

echo
echo "Create the cluster for Travis:"
echo "kubectl cluster-info --context kind-kind"
echo "kubectl get nodes"
echo
echo "Initialize the cluster:"
echo "clusterctl init --infrastructure 'openstack:v0.4.0' --core 'cluster-api:v0.4.1' --control-plane 'kubeadm:v0.4.1' --bootstrap 'kubeadm:v0.4.1'"

This bash script will fetch a few things, in particular, Kind, OpenStack then it will create a cluster for you. The way I did it is it a bit different, because I want to give you options and flexibility, here’s how I set it up just purely using YAML.

Travis CI

So the .travis.yml file I created currently looks like this:

dist: jammy
language: go
go:
  - 1.15.x
services:
  - docker
jobs:
  include:
    - stage: Kind example
      before_script:
        - >-
          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/
        - GO111MODULE="on" go get sigs.k8s.io/kind@v0.10.0
        - kind create cluster
        - kubectl config use-context kind-kind
        - kubectl create sa default
      script:
        - kubectl run busybox --image=busybox
        - kubectl get pods

If Kind is successfully built in your VM, you’ll see this in your Travis build log:

Please view my build for more information.

In Conclusion

You’ve just integrated Kind with Travis and started a Kind cluster! As per usual here’s my repo so you can follow it step by step and see for yourself how easy it is to integrate Kind with Travis.

As always, if you have any questions about integrating Kind and Travis, please email me at montana@travis-ci.org and I will assist you with this tutorial.

Happy building!

© Copyright 2024, All Rights Reserved