Code Monkey home page Code Monkey logo

absaoss / k3d-action Goto Github PK

View Code? Open in Web Editor NEW
176.0 9.0 23.0 94 KB

A GitHub Action to run lightweight ephemeral Kubernetes clusters during workflow. Fundamental advantage of this action is a full customization of embedded k3s clusters. In addition, it provides a private image registry and multi-cluster support.

License: Apache License 2.0

Shell 79.44% Makefile 20.56%
k3s k3d rancher cluster kind kubernetes containers action github-actions

k3d-action's Introduction

AbsaOSS/k3d-action

A GitHub Action to run lightweight ephemeral Kubernetes clusters during workflow. Fundamental advantage of this action is a full customization of embedded k3s clusters. In addition, it provides multi-cluster support.

Introduction

Applications running on Kubernetes clusters (microservices, controllers,...) come with their own set of complexities and concerns. In particular, E2E testing k8s based applications requires new approaches to confirm proper operation and continued availability under heavy load or in the face of resource failure. AbsaOSS/k3d-action allows to test the overall application functionality. For instance, the E2E use-case is operator testing in AbsaOSS/k8gb.

The full CI/CD pipeline tutorial with k3d-action by Viktor Farcic.

Getting started

AbsaOSS/k3d-action runs k3d which is a lightweight wrapper to run k3s (Rancher Lab’s minimal Kubernetes distribution) in containers. Thanks to that, we could spin up the test environment quickly with minimal memory requirements, which is especially important in multi-cluster environments.

AbsaOSS/k3d-action defines several input attributes and two outputs:

Inputs

  • cluster-name (Required) Cluster name.

  • args (Optional) list of k3d arguments defined by k3d command tree

  • k3d-version (Optional) version of k3d. If not set, default version will be used from the version mapping table.

Version mapping and override

Implementation of additional features brings complexity and sometimes it may happen that external dependencies get broken. To prevent potential issues, k3d version is fixed according to the mapping below.

k3d-action k3d
v1.1.0 v3.4.0
v1.2.0 v4.2.0
v1.3.0 v4.2.0
v1.4.0 v4.4.1
v1.5.0 v4.4.7
v2.0.0 v5.1.0
v2.1.0 v5.2.2
v2.2.0 v5.3.0
v2.3.0 v5.4.1
v2.4.0 v5.4.6

Users can override the default k3d version with the k3d-version: action parameter

# example
with:
  k3d-version: v5.2.2

Users can also override the default k3s image version, using k3d --image argument

# example
with:
  args: --image docker.io/rancher/k3s:v1.20.4-k3s1

or the image: field in the k3d configuration file.

Recent k3s image versions can be checked on the k3.io project releases page.

Single cluster setup

Although AbsaOSS/k3d-action strongly supports multi-cluster. Single cluster scenarios are very popular. The minimum single-cluster configuration looks like this :

      - uses: AbsaOSS/k3d-action@v2
        name: "Create Single Cluster"
        with:
          cluster-name: "test-cluster-1"
          args: --agents 1

k3d creates a cluster with one worker node (with traefik and metrics services), one agent and one default load-balancer node. In real scenarios you might prefer to do some port mapping and disable default load balancer. Such an action would look like this:

      - uses: AbsaOSS/k3d-action@v2
        name: "Create Single Cluster"
        with:
          cluster-name: "test-cluster-1"
          args: >-
            -p "8083:80@agent:0:direct"
            -p "8443:443@agent:0:direct"
            -p "5053:53/udp@agent:0:direct"
            --agents 3
            --no-lb
            --image docker.io/rancher/k3s:v1.20.4-k3s1
            --k3s-arg "--no-deploy=traefik,servicelb,metrics-server@server:*"

The created cluster exposes two TCP (:8083,:8443) and one UDP (:5053) ports. The cluster comprises one server, three agents and no load balancers. k3s-server-argument disable default traefik and metrics.

For more details see: Demo, Source

Multi-cluster setup

k3d creates a bridge-network for each separate cluster or attaches the created cluster to an existing network.

When you create a cluster named test-cluster-1, k3d will automatically create a network named k3d-test-cluster-1 with the range 172.18.0.0/16. When you create a second cluster test-cluster-2, k3d automatically creates a network named k3d-test-cluster-2 with a range of 172.19.0.0/16. Other clusters will have ranges 172.20.0.0/16,172.21.0.0/16 etc.

The following example creates a total of four clusters, the first two are created on the network nw01, 172.18.0.0/16, the next two clusters are created on the network nw02, 172.19.0.0/16.

      - uses: AbsaOSS/k3d-action@v2
        name: "Create 1st Cluster in 172.18.0.0/16"
        with:
          cluster-name: "test-cluster-1"
          args: >-
            -p "80:80@agent:0:direct"
            -p "443:443@agent:0:direct"
            -p "5053:53/udp@agent:0:direct"
            --agents 3
            --no-lb
            --k3s-arg "--no-deploy=traefik,servicelb,metrics-server@server:*"
            --network "nw01"

      - uses: AbsaOSS/k3d-action@v2
        name: "Create 2nd Cluster in 172.18.0.0/16"
        with:
          cluster-name: "test-cluster-2"
          args: >-
            -p "81:80@agent:0:direct"
            -p "444:443@agent:0:direct"
            -p "5054:53/udp@agent:0:direct"
            --agents 3
            --no-lb
            --k3s-arg "--no-deploy=traefik,servicelb,metrics-server@server:*"
            --network "nw01"

      - uses: AbsaOSS/k3d-action@v2
          name: "Create 1st Cluster in 172.19.0.0/16"
          with:
            cluster-name: "test-cluster-3"
            args: >-
              -p "82:80@agent:0:direct"
              -p "445:443@agent:0:direct"
              -p "5055:53/udp@agent:0:direct"
              --agents 3
              --no-lb
              --k3s-arg "--no-deploy=traefik,servicelb,metrics-server@server:*"
              --network "nw02"

      - uses: AbsaOSS/k3d-action@v2
        name: "Create 2nd Cluster in 172.19.0.0/16"
        with:
          cluster-name: "test-cluster-4"
          args: >-
            -p "83:80@agent:0:direct"
            -p "446:443@agent:0:direct"
            -p "5056:53/udp@agent:0:direct"
            --agents 3
            --no-lb
            --k3s-arg "--no-deploy=traefik,servicelb,metrics-server@server:*"
            --network "nw02"

AbsaOSS/k3d-action creates four identical clusters in two different bridge networks.

For more details see: Demo, Source

Config file support

From v1.2.0 you can configure action via config files or mix arguments together with config files. This setup is useful when you want to share the configuration for local testing and testing within k3d-action.

      - uses: ./
        name: "Create single k3d Cluster"
        with:
          cluster-name: "test-cluster-1"
          args: >-
            --agents 1
            --config=<path to config yaml>

All you need to do is to place configuration file somewhere into your project. However, keep in mind, that command line arguments will always take precedence over configuration, so the previous example will result in only one agent, not three as configured.

apiVersion: k3d.io/v1alpha3
kind: Simple
image: docker.io/rancher/k3s:v1.20.5-k3s1
servers: 1
agents: 3 # The action will overwrite this by 1
ports:
  - port: 0.0.0.0:80:80
    nodeFilters:
      - agent:0:direct
  - port: 0.0.0.0:443:443
    nodeFilters:
      - agent:0:direct
  - port: 0.0.0.0:5053:53/udp
    nodeFilters:
      - agent:0:direct
options:
  k3d:
    wait: true
    timeout: "60s"
    disableLoadbalancer: true
  k3s:
    extraArgs:
      - arg: --no-deploy=traefik,servicelb,metrics-server
        nodeFilters:
          - server:*
  kubeconfig:
    updateDefaultKubeconfig: true
    switchCurrentContext: true

For more details see: Demo, Source action, Source config

Private Registry

Before test starts, you need to build your app and install into the cluster. This requires interaction with the image registry. Usually you don't want to push a new image into the remote registry for each test. Instead, you can import the image directly into the created cluster:

docker build . -t <repository>:<semver>
k3d image import <repository>:<semver> -c <cluster-name>

Example below demonstrates how to interact with imported docker registry:

    steps:
      - uses: actions/checkout@v2
      - uses: AbsaOSS/k3d-action@v2
        name: "Create single k3d Cluster with imported Registry"
        with:
          cluster-name: test-cluster-1
          args: >-
            --agents 3
            --no-lb
            --k3s-arg "--no-deploy=traefik,servicelb,metrics-server@server:*"
      - name: "Docker repo demo"
        run: |
          docker build . -t myproj/demo:v1.0.0
          k3d image import myproj/demo:v1.0.0 -c test-cluster-1 --verbose
          kubectl apply -f pod.yaml

    # pod.yaml
    #
    # apiVersion: v1
    # kind: Pod
    # metadata:
    #   name: test-pod
    # spec:
    #   containers:
    #   - name: demo-app
    #     image: myproj/demo:v1.0.0

For further details see:

k3d-action's People

Contributors

arkadius avatar jkremser avatar kuritka avatar somaritane avatar viacheslavkudinov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

k3d-action's Issues

[suggestion] Add parameter to wait for cluster to become ready

Hello.

Is it possible to add some check, so the action would wait until cluster becomes "ready".

Apparently, "readiness" is quite ambigous term, but maybe some minimal condition could be defined and added to docs,
so users of this action will have to create less boilerplate code like, sleep \ bash loops etc in their GitHub workflows.

[feat] possibility to override k3s image

It is impossible to use custom k3s because

  • When passed via k3d --config yaml file, it's ignored because explicitely declared via cmd line argument ro k3d cluster create
  • When passed via action input.args it is also ignored because explicitely overrden by action cmd line

Suggestions

  • If passed k3d yaml config is used, parse the file and try to retrieve k3s version from it.
  • If pased via input.args parse argument and override the k3s image
  • Otherwise, used default action version

Error: Failed to create node ...The container name is already in use by container

Hi,

We've got such configuration in our project:

      - uses: AbsaOSS/[email protected]
        with:
          cluster-name: "k3s-default"
          args: >-
            --agents 1
            --no-lb
            --k3s-arg "--no-deploy=traefik,servicelb,metrics-server@server:*"

And from time to time we've got such message (https://github.com/TouK/nussknacker/runs/4712393353?check_suite_focus=true) :

Run AbsaOSS/[email protected]
  with:
    cluster-name: k3s-default
    args: --agents 1 --no-lb --k3s-arg "--no-deploy=traefik,servicelb,metrics-server@server:*"
  env:
    CROSS_BUILD: true
    CI: true
    JAVA_HOME: /home/runner/.jabba/jdk/[email protected]
Run /home/runner/work/_actions/AbsaOSS/k3d-action/v2.1.0/run.sh deploy
  /home/runner/work/_actions/AbsaOSS/k3d-action/v2.1.0/run.sh deploy
  shell: /usr/bin/bash --noprofile --norc -e -o pipefail {0}
  env:
    CLUSTER_NAME: k3s-default
    ARGS: --agents 1 --no-lb --k3s-arg "--no-deploy=traefik,servicelb,metrics-server@server:*"
    K3D_VERSION: 
Downloading [email protected] see: https://raw.githubusercontent.com/rancher/k3d/main/install.sh
Preparing to install k3d into /usr/local/bin
k3d installed into /usr/local/bin/k3d
Run 'k3d --help' to see what you can do with it.
Deploy cluster k3s-default 
INFO[0000] Prep: Network                                
INFO[0000] Created network 'k3d-k3s-default'            
INFO[0000] Created volume 'k3d-k3s-default-images'      
INFO[0000] Starting new tools node...                   
INFO[0000] Pulling image 'docker.io/rancher/k3d-tools:5.2.2' 
INFO[0001] Creating node 'k3d-k3s-default-server-0'     
INFO[0001] Pulling image 'docker.io/rancher/k3s:v1.21.7-k3s1' 
INFO[0004] Creating node 'k3d-k3s-default-agent-0'      
INFO[0005] Using the k3d-tools node to gather environment information 
INFO[0005] Starting new tools node...                   
INFO[0005] Starting Node 'k3d-k3s-default-tools'        
ERRO[0005] Failed to run tools container for cluster 'k3s-default' 
INFO[0005] Deleting cluster 'k3s-default'               
ERRO[0005] failed to gather environment information used for cluster creation: failed to run k3d-tools node for cluster 'k3s-default': failed to create node 'k3d-k3s-default-tools': runtime failed to create node 'k3d-k3s-default-tools': failed to create container for node 'k3d-k3s-default-tools': docker failed to create container 'k3d-k3s-default-tools': Error response from daemon: Conflict. The container name "/k3d-k3s-default-tools" is already in use by container "409a0bc5a19cca47cd1f485d9e710ac59b28b397d2cd15d3614e9dcc2fb6f015". You have to remove (or rename) that container to be able to reuse that name. 
ERRO[0005] Failed to create cluster >>> Rolling Back    
INFO[0005] Deleting cluster network 'k3d-k3s-default'   
INFO[0005] Deleting image volume 'k3d-k3s-default-images' 
FATA[0005] Cluster creation FAILED, all changes have been rolled back! 
Error: Process completed with exit code 1.

Have you got an idea why this error appear? It looks like one of option:

  1. github actions doesn't clean containers between jobs
  2. k3d creates doubled container

Install only mode

It would be nice to have a way to use this action without the need of creating a cluster, only for install k3d purposes. Imagine a user who wants to start n of k8s clusters and the n is not known in advance but it's dynamically evaluated during the build or whatever.

The officially suggested way of curl -s https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh | bash isn't super secure.

Something like skipClusterCreation: true,installOnly: true, dontCreate: true or create: false.

[question] How to cache the action files

Please provide some info about how to cache the k3d \ k3s files.

It's required to speed up jobs using this action.

I believe the best would be somehow cache the local registry, created by action.

Multi-Cluster Federatation with kubefed

It seems the multi-cluster examples are isolated from one another, which I'm not sure what benefit that provides (AFAIK, there's no fault tolerance for services deployed across more than one cluster)

Do you think there is any benefit to making kubefed apis available in this action? ref kubernetes-retired/kubefed#1412

Fix k3d / k3s version

AbsaOSS/k3d-action works with the latest k3d. Current k3d-action fails on creating cluster because [email protected] doesn't support backward compatibility, so cluster creation fails on error.

Please

  • Each version of k3d-action would work against specific k3d
  • Fix tests
  • release AbsaOSS/[email protected]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.