|
| 1 | +# Setup K3d/K3s for GitHub Actions |
| 2 | + |
| 3 | +Install K3d/K3s and start a local Kubernetes cluster of a specific version. |
| 4 | + |
| 5 | +**K8s** is Kubernetes. |
| 6 | +**K3s** is a lightweight K8s distribution. |
| 7 | +**K3d** is a wrapper to run K3s in Docker. |
| 8 | + |
| 9 | +K3d/K3s are especially good for development and CI purposes, as it takes |
| 10 | +only 20-30 seconds of time till the cluster is ready. For comparison, |
| 11 | +Kind takes 1.5 minutes, Minikube takes 2 minutes till ready (as of Sep'2020). |
| 12 | + |
| 13 | + |
| 14 | +## Quick start |
| 15 | + |
| 16 | +Start with the simplest way: |
| 17 | + |
| 18 | +```yaml |
| 19 | +jobs: |
| 20 | + some-job: |
| 21 | + steps: |
| 22 | + - uses: nolar/setup-k3d-k3s@v1 |
| 23 | +``` |
| 24 | +
|
| 25 | +Change versions with the verbose way: |
| 26 | +
|
| 27 | +```yaml |
| 28 | +jobs: |
| 29 | + some-job: |
| 30 | + steps: |
| 31 | + - uses: nolar/setup-k3d-k3s@v1 |
| 32 | + with: |
| 33 | + version: v1.19 # E.g.: v1.19, v1.19.4, v1.19.4+k3s1 |
| 34 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 35 | +``` |
| 36 | +
|
| 37 | +
|
| 38 | +## Inputs |
| 39 | +
|
| 40 | +### `version` |
| 41 | + |
| 42 | +**Required** version of Kubernetes and/or K3s -- either full or partial. |
| 43 | + |
| 44 | +The following notations are supported: |
| 45 | + |
| 46 | +* `v1.19.4+k3s1` |
| 47 | +* `v1.19.4` |
| 48 | +* `v1.19` |
| 49 | +* `v1` |
| 50 | +* `latest` |
| 51 | + |
| 52 | +Defaults to `latest`. |
| 53 | + |
| 54 | +Keep in mind that K3d dates back only to v1.16. |
| 55 | +There are no 1.15 and older versions of K8s. |
| 56 | + |
| 57 | +When the version is partial, the latest detected one will be used, |
| 58 | +as found in [K3s releases](https://github.com/rancher/k3s/releases), |
| 59 | +according to the basic semantical sorting (i.e. not by time of releasing). |
| 60 | + |
| 61 | + |
| 62 | +### `k3d-name` |
| 63 | + |
| 64 | +A name of the cluster to be created. |
| 65 | + |
| 66 | +By default (i.e. if no value is provided), K3d/K3s define their own name. |
| 67 | +Usually it is `k3d-k3s-default`. |
| 68 | + |
| 69 | +Note: the name should not include the `k3d-` prefix, but must be used with it. |
| 70 | +The `k3d-` prefix is enforced by K3d and cannot be disabled. |
| 71 | + |
| 72 | + |
| 73 | +### `k3d-args` |
| 74 | + |
| 75 | +Additional args to pass to K3d. |
| 76 | +See `k3d cluster create --help` for available flags. |
| 77 | + |
| 78 | + |
| 79 | +### `github-token` |
| 80 | + |
| 81 | +A token for GitHub API, which is used to avoid rate limiting. |
| 82 | + |
| 83 | +The API is used to fetch the releases from the K3s repository. |
| 84 | + |
| 85 | +By default, or if it is empty, then the API is accessed anonymously, |
| 86 | +which implies the limit of approximately 60 requests / 1 hour / 1 worker. |
| 87 | + |
| 88 | +Usage: |
| 89 | + |
| 90 | +```yaml |
| 91 | +with: |
| 92 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 93 | +``` |
| 94 | + |
| 95 | + |
| 96 | +### `skip-readiness` |
| 97 | + |
| 98 | +Whether to return from the action as soon as possible, |
| 99 | +possibly providing a cluster that is only partially ready. |
| 100 | + |
| 101 | +By default (`false`), the readiness is awaited by checking for some preselected |
| 102 | +resources to appear (e.g., for a service account named "default"). |
| 103 | + |
| 104 | + |
| 105 | +## Outputs |
| 106 | + |
| 107 | +### `k3s-version` |
| 108 | + |
| 109 | +The specific K3s version that was detected and used. E.g. `v1.19.4+k3s1`. |
| 110 | + |
| 111 | + |
| 112 | +### `k8s-version` |
| 113 | + |
| 114 | +The specific K8s version that was detected and used. E.g. `v1.19.4`. |
| 115 | + |
| 116 | + |
| 117 | +## Examples |
| 118 | + |
| 119 | +With the latest version of K3s/K8s: |
| 120 | + |
| 121 | +```yaml |
| 122 | +steps: |
| 123 | + - uses: nolar/setup-k3d-k3s@v1 |
| 124 | +``` |
| 125 | + |
| 126 | +With the specific minor version of K8s, which implies the latest micro version |
| 127 | +of K8s and the latest possible version of K3s: |
| 128 | + |
| 129 | +```yaml |
| 130 | +steps: |
| 131 | + - uses: nolar/setup-k3d-k3s@v1 |
| 132 | + with: |
| 133 | + version: v1.19 |
| 134 | +``` |
| 135 | + |
| 136 | +With the very specific version of K3s: |
| 137 | + |
| 138 | +```yaml |
| 139 | +steps: |
| 140 | + - uses: nolar/setup-k3d-k3s@v1 |
| 141 | + with: |
| 142 | + version: v1.19.4+k3s1 |
| 143 | +``` |
| 144 | + |
| 145 | +The partial versions enable the build matrices with only the essential |
| 146 | +information in them, which in turn, makes it easier to configure GitHub |
| 147 | +branch protection checks while the actual versions of tools are upgraded: |
| 148 | + |
| 149 | +```yaml |
| 150 | +jobs: |
| 151 | + some-job: |
| 152 | + strategy: |
| 153 | + fail-fast: false |
| 154 | + matrix: |
| 155 | + k8s: [ v1.19, v1.18, v1.17, v1.16 ] |
| 156 | + name: K8s ${{ matrix.k8s }} |
| 157 | + runs-on: ubuntu-20.04 |
| 158 | + steps: |
| 159 | + - uses: nolar/setup-k3d-k3s@v1 |
| 160 | + with: |
| 161 | + version: ${{ matrix.k8s }} |
| 162 | +``` |
| 163 | + |
| 164 | +Multiple clusters in one job are possible, as long as there is enough memory |
| 165 | +(note: `k3d-` prefix is enforced by K3d): |
| 166 | + |
| 167 | +```yaml |
| 168 | +jobs: |
| 169 | + some-job: |
| 170 | + name: Multi-cluster |
| 171 | + runs-on: ubuntu-20.04 |
| 172 | + steps: |
| 173 | + - uses: nolar/setup-k3d-k3s@v1 |
| 174 | + with: |
| 175 | + version: v1.16 |
| 176 | + k3d-name: 1-16 |
| 177 | + - uses: nolar/setup-k3d-k3s@v1 |
| 178 | + with: |
| 179 | + version: v1.18 |
| 180 | + k3d-name: 1-18 |
| 181 | + - run: kubectl version --context k3d-1-16 |
| 182 | + - run: kubectl version --context k3d-1-18 |
| 183 | +``` |
| 184 | + |
| 185 | +Custom args can be passed to K3d (and through it, to K3s & K8s): |
| 186 | + |
| 187 | +```yaml |
| 188 | +jobs: |
| 189 | + some-job: |
| 190 | + name: Custom args |
| 191 | + runs-on: ubuntu-20.04 |
| 192 | + steps: |
| 193 | + - uses: nolar/setup-k3d-k3s@v1 |
| 194 | + with: |
| 195 | + k3d-args: --servers 2 --no-lb |
| 196 | + - run: kubectl get nodes # there must be two of them |
| 197 | +``` |
| 198 | + |
| 199 | +For real-life examples, see: |
| 200 | + |
| 201 | +* https://github.com/nolar/kopf/actions (all "K3s…" jobs). |
0 commit comments