├── .travis.yml ├── LICENSE ├── README.md ├── after-cluster.sh ├── before-cluster.sh ├── build.sh ├── deploy.sh └── resources ├── entrypoint.sh ├── kubelet.sh ├── setup.sh ├── sgerrand.rsa.pub ├── start.sh └── supervisord.conf /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: required 2 | services: docker 3 | language: minimal 4 | env: 5 | - KUBERNETES_VERSION="v1.18.6" DOCKER_IMAGE_TAG="latest-1.18" MINIKUBE_VERSION="v1.15.1" 6 | - KUBERNETES_VERSION="v1.19.4" DOCKER_IMAGE_TAG="latest-1.19" MINIKUBE_VERSION="v1.15.1" 7 | - KUBERNETES_VERSION="v1.20.7" DOCKER_IMAGE_TAG="latest-1.20" MINIKUBE_VERSION="v1.15.1" 8 | - KUBERNETES_VERSION="v1.21.1" DOCKER_IMAGE_TAG="latest-1.21" MINIKUBE_VERSION="v1.15.1" 9 | - KUBERNETES_VERSION="v1.23.10" DOCKER_IMAGE_TAG="latest-1.23" MINIKUBE_VERSION="v1.27.1" DOCKER_IMAGE="20.10.20-dind" 10 | script: 11 | - ./build.sh bsycorp/kind:$DOCKER_IMAGE_TAG bsycorp/kind:$KUBERNETES_VERSION 12 | deploy: 13 | provider: script 14 | script: bash ./deploy.sh 15 | on: 16 | branch: master 17 | repo: bsycorp/kind 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | `kind`, like `docker:dind` but aims to make a full Kubernetes cluster available to test against in your CI pipeline. It is fast to startup and totally ephemeral, so you get a clean start for each CI run. 2 | 3 | [![Build Status](https://travis-ci.org/bsycorp/kind.svg?branch=master)](https://travis-ci.org/bsycorp/kind) 4 | 5 | ## Quickstart 6 | 7 | Use prebuilt images from Dockerhub: https://hub.docker.com/r/bsycorp/kind/ 8 | 9 | Run: 10 | 11 | `docker run -it --privileged -p 8443:8443 -p 10080:10080 bsycorp/kind:latest-1.12` 12 | 13 | Or more likely run CI in, [see examples](https://github.com/bsycorp/kind#can-i-use-it-on-my-cloud-cicd-provider) 14 | 15 | ## Why? 16 | 17 | We run workloads in Kubernetes, and run our CI on Kubernetes too, but in our CI pipeline we wanted a way to have a quick and reliable way of having the current code under test, built, deployed and tested but then have the environment be torn down after testing. We initially considered deploying to a proper cluster but the overhead of pushing images to a registry just for a CI test, and the setup / teardown of resources was too much, we wanted something totally ephemeral so building on top of docker's dind made sense. 18 | 19 | ## Isn't this solved already? 20 | 21 | Sort of, but not optimised for a fast-starting single-node cluster. There are examples of other solutions to this problem, it seems mostly from the Kubernetes devs themselves, as a way to automate development of integrations or Kubernetes itself. We wanted something that was both ephemeral, reliable and really fast, none of the available options gave us all of that. 22 | 23 | Other projects: 24 | 1. https://github.com/kubernetes/minikube/tree/master/deploy/docker (Deprecated) 25 | 2. https://github.com/kubernetes/test-infra/tree/master/dind (Replaces no.1) 26 | 3. https://github.com/kubernetes-sigs/kubeadm-dind-cluster/ (more info: http://callistaenterprise.se/blogg/teknik/2017/12/20/kubernetes-on-docker-in-docker/) 27 | 28 | Excluding no.1 which uses `localkube` (which is deprecated), both no.2 and no.3 create a proper cluster, with separate instances for a master and node(s). Because they start a multi node cluster they need to use `dind` inside `dind`, creating docker in docker in docker. As you can imagine adding another levels of abstraction can change how things behave, so disk access can be even slower than normal as we are two levels deep. Depending on the setup this can be caused because a fast storage driver like overlay2 can't be used _on_ overlay2, so docker will fall back to slower / more compatiable choices like AUFS (depends on Docker version). It can also have an impact on reliability, if you are hitting docker+kernel edge cases that trigger strange behaviour, it is likely you will see more strange behaviour with docker in docker in docker. 29 | 30 | As an applcation sitting on top of kubernetes, we don't really need a full multi-node cluster to run our CI tests, we just need a single node+master. Since we only need a single instance, we don't need the complexity of docker-in-docker-in-docker, docker-in-docker is just fine (as we only have 1 instance vs 2+). This should make disk access faster (only 1 `dind`, so can use `overlay2`) which should help make our kube deploy and tests fast and it should be easier and more reliable to prime/bootstrap everything during build time and just start it up at runtime. 31 | 32 | ## How does `kind` work? 33 | 34 | tl;dr; Building on docker-in-docker it uses `minikube` and `kubeadm` to bootstrap and pre-configure a cluster at build time that works at runtime. 35 | 36 | The simplest way to get a Kubernetes cluster running in CI is to use `minikube` and start with `--vm-driver none`, this uses `kubeadm` to bootstrap a set of local processes to start Kubernetes. This doesn't work out of the box in `dind` as `kubeadm` assumes it is running in a systemd environment, which alpine is not. It also downloads binaries and bootstraps the cluster everytime it is run which depending on your network and resources takes around 4 minutes. 37 | 38 | To make this process fast, `kind` aims to move all the cluster bootstrapping to the container build phase, so when you run `kind` it is already bootstrapped and is effectively just starting the `kubelet` process with a preconfigured `etcd`, `apiserver` etc. To achieve this we need to initially configure `kubeadm` with a static IP that will be routable both during the build phase, and the run phase, we have arbitrarily chosen `172.30.99.1` for that address. 39 | 40 | During the build phase `kind` adds `172.30.99.1` to the default network interface for the container `eth0` and forces `kubeadm` and friends to use this address when bootstrapping the cluster. During the container run phase (when you are running `kind` in your environment) this static IP address is again attached to the default network interface `eth0` for the container so when `kubelet` is run the IP that it has been configured against is still routable. 41 | 42 | Doing this network trickery means we can move all the hard work into the build phase, and `kind` can startup fast. In our CI environment using `kind` a single node cluster comes up and is ready to use in 30 seconds, down from 4 minutes in the simple minikube implementation (3+ minutes is a lot in a CI pipeline!). 43 | 44 | A further optimisation is to have the build phase `docker pull` any dependent images your Kubernetes resources will require, so when your CI process is deploying your Kubernetes resources it doesn't have to pull in any images over the network. To do this you will need to build your own version of `kind` and update the `/after-cluster.sh` file with the images you want to pull in. 45 | 46 | ## How do I use this? 47 | 48 | `kind` is a docker image that only runs as `--privileged`, that is designed to be run as a CI service, where by it is accessible over a known interface, normally `localhost`. Much like `docker:dind` on which it is based. 49 | 50 | Running it as a service means the container running your tests needs to know when `kind` is ready, and how to get the `kubectl` config to make cli calls. This is achieved via the config endpoint. By default this is exposed over port `10080` and is just a simple http server hosting files. 51 | 52 | There are few important events that `kind` exposes: 53 | - When the docker host is ready, `http://localhost:10080/docker-ready` will return 200 54 | - When the Kubernetes is ready, `http://localhost:10080/kubernetes-ready` will return 200 55 | - When kubectl config is ready, `http://localhost:10080/config` will return 200 56 | 57 | As you want your docker images you want to test to be built into `kind` docker host, the general process is to wait for `.../docker-ready` before doing `docker build..`, then wait for `.../kubernetes-ready` before deploy, then test. 58 | 59 | ## Can I use it on my Cloud CI/CD provider? 60 | 61 | Not sure as we are running this in an on-premise GitLab install, but interested to hear feedback from people where it does or doesn't work. As above it is designed to be like `docker:dind` but with Kubernetes, so in theory anywhere `docker:dind` runs this should run, and like `docker:dind` it requires the container be launched as `--privileged` which generally cloud providers don't like. 62 | 63 | Tested an known to Work: 64 | - GitLab On-Premise (CE or EE*) ([example](https://github.com/bsycorp/kind-gitlab-example)) 65 | - CircleCI `machine` executors ([example](https://github.com/bsycorp/kind-circleci-example)) 66 | - Travis CI ([example](https://github.com/bsycorp/kind-travis-example)) 67 | 68 | These should work: 69 | - GitLab.com with BYO Docker or Kubenetes runners 70 | - Codeship Pro 71 | 72 | Unlikely to work: 73 | - Bitbucket Pipelines, has a magic `docker: true` flag so will likely not work 74 | 75 | ## How do I build this for myself? 76 | 77 | Pre-built images are available on dockerhub (https://hub.docker.com/r/bsycorp/kind/), but if you want to bake in your own images to make it as fast as possible, you will want to built it yourself. 78 | 79 | Run `./build.sh ` to build the image. Add your custom images to `/images.sh` to have them be available at runtime. These environment variables are available to configure the build: 80 | 81 | - DOCKER_IMAGE: defaults to `19.03.5-dind` 82 | - MINIKUBE_VERSION: defaults to `v1.0.1` 83 | - KUBERNETES_VERSION: defaults to `v1.14.8` 84 | - STATIC_IP: defaults to `172.30.99.1` 85 | 86 | We use git submodules to pull in this project and then add images and CI configuration around it, but there are other ways to do it. 87 | 88 | ## Build hooks 89 | 90 | There are two hooks available during the `kind` build, `before-cluster.sh` and `after-cluster.sh`. As their names suggest they are run directly before and after the kube cluster is created. 91 | 92 | Examples of this scripts exist in the repo already, but they can be overwritten / extended to add extra functionality. 93 | 94 | ## kubectl client configuraiton 95 | 96 | To use generated kube config: 97 | 98 | ``` 99 | wget http://localhost:10080/config 100 | cp config ~/.kube/config 101 | kubectl get nodes 102 | ``` 103 | 104 | To create/add custom local kube config with creds: 105 | 106 | ``` 107 | wget http://localhost:10080/ca.crt 108 | wget http://localhost:10080/client.crt 109 | wget http://localhost:10080/client.key 110 | 111 | kubectl config set-cluster kind-cluster --server=https://localhost:8443 \ 112 | --certificate-authority=ca.crt 113 | 114 | kubectl config set-credentials kind-admin \ 115 | --certificate-authority=ca.pem \ 116 | --client-key=client.key \ 117 | --client-certificate=client.crt 118 | 119 | kubectl config set-context kind --cluster=kind-cluster --user=kind-admin 120 | kubectl config use-context kind 121 | kubectl get nodes 122 | ``` 123 | 124 | ## How do I pull images from a private registry? 125 | 126 | Depending how you authenticate to your registry this can be a bit tricky. As the `./build.sh` script actually starts a docker container and configures it, because of this the container running your build hook scripts won't have access to all the environment variables / binaries it normally would. To work around this we just write out the build hook script in our outer CI process dynamically during the build. 127 | 128 | Your `kind` CI build script might look like: 129 | 130 | ``` 131 | echo "$(aws ecr get-login --no-include-email --region some-region)" >> ./kind/before-cluster.sh 132 | echo "docker pull 12345.ecr.amazonaws.com/smth:latest" >> ./kind/before-cluster.sh 133 | 134 | or 135 | 136 | echo "docker login -u username -p $REGISTRY_TOKEN_FROM_CI registry.smth.com" >> ./kind/before-cluster.sh 137 | echo "docker pull registry.smth.com/app:latest" >> ./kind/before-cluster.sh 138 | ``` 139 | 140 | Then the `before-cluster.sh` hook will fire during the build and have all the details it needs to login and pull the private images. 141 | 142 | or dynamically supply the credentials when running the image: 143 | 144 | ``` 145 | docker run --privileged \ 146 | -e REGISTRY="registry.smth.com" \ 147 | -e REGISTRY_USER="username" \ 148 | -e REGISTRY_PASSWORD='$REGISTRY_TOKEN' \ 149 | bsycorp/kind:latest-1.10 150 | ``` 151 | -------------------------------------------------------------------------------- /after-cluster.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | docker pull nginx:1.13 # used by config-serve 3 | 4 | # Add images here for them to be available at runtime 5 | # for example: 6 | # docker pull quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.9.0 7 | -------------------------------------------------------------------------------- /before-cluster.sh: -------------------------------------------------------------------------------- 1 | # -------------------------------------------------------------------------------- /build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # KUBERNETES_VERSION="v1.10.0" 3 | # MINIKUBE_VERSION="v0.28.2" 4 | TAG_LATEST="$1" 5 | TAG_VERSION="$2" 6 | 7 | if [ -z "$DOCKER_IMAGE" ]; then 8 | DOCKER_IMAGE="19.03.5-dind" 9 | echo "Defaulting Docker image to $DOCKER_IMAGE" 10 | fi 11 | 12 | if [ -z "$MINIKUBE_VERSION" ]; then 13 | MINIKUBE_VERSION="v1.3.1" 14 | echo "Defaulting Minikube version to $MINIKUBE_VERSION" 15 | fi 16 | 17 | if [ -z "$KUBERNETES_VERSION" ]; then 18 | KUBERNETES_VERSION="v1.14.8" 19 | echo "Defaulting Kubernetes version to $KUBERNETES_VERSION" 20 | fi 21 | 22 | if [ -z "$STATIC_IP" ]; then 23 | STATIC_IP="172.30.99.1" 24 | echo "Defaulting static IP to $STATIC_IP" 25 | fi 26 | 27 | if [ -z "$NETWORK" ]; then 28 | NETWORK="default" 29 | echo "Defaulting docker network to $NETWORK" 30 | fi 31 | 32 | function finish { 33 | echo "Cleanup" 34 | docker rm -f $CONTAINER_ID 35 | docker volume prune -f | true 36 | } 37 | trap finish ERR 38 | 39 | set -e 40 | 41 | echo "Starting dind" 42 | CONTAINER_ID=$(docker run --network $NETWORK --privileged -d --rm -e DOCKER_TLS_CERTDIR='' --hostname=minikube docker:$DOCKER_IMAGE dockerd) 43 | docker cp resources/entrypoint.sh $CONTAINER_ID:/entrypoint.sh 44 | docker cp resources/setup.sh $CONTAINER_ID:/setup.sh 45 | docker cp resources/start.sh $CONTAINER_ID:/start.sh 46 | docker cp resources/kubelet.sh $CONTAINER_ID:/kubelet.sh 47 | docker cp resources/supervisord.conf $CONTAINER_ID:/etc/supervisord.conf 48 | docker cp resources/sgerrand.rsa.pub $CONTAINER_ID:/etc/apk/keys/sgerrand.rsa.pub 49 | docker cp before-cluster.sh $CONTAINER_ID:/before-cluster.sh 50 | docker cp after-cluster.sh $CONTAINER_ID:/after-cluster.sh 51 | if [ -x before-setup.sh ]; then 52 | ./before-setup.sh $CONTAINER_ID 53 | fi 54 | 55 | echo "Starting setup" 56 | docker exec -e "KUBERNETES_VERSION=$KUBERNETES_VERSION" -e "MINIKUBE_VERSION=$MINIKUBE_VERSION" -e "MINIKUBE_EXTRA_ARGS" -e "STATIC_IP=$STATIC_IP" $CONTAINER_ID /setup.sh 57 | if [ -x after-setup.sh ]; then 58 | ./after-setup.sh $CONTAINER_ID 59 | fi 60 | 61 | echo "Commiting new container" 62 | docker commit \ 63 | -c 'ENTRYPOINT ["/entrypoint.sh"]' \ 64 | -c 'CMD ["/usr/bin/supervisord", "--nodaemon", "-c", "/etc/supervisord.conf"]' \ 65 | -c 'ENV PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' \ 66 | -c 'EXPOSE 2375/tcp' \ 67 | -c 'EXPOSE 8443/tcp' \ 68 | -c 'EXPOSE 10080/tcp' \ 69 | $CONTAINER_ID $TAG_LATEST 70 | 71 | docker tag $TAG_LATEST $TAG_VERSION 72 | -------------------------------------------------------------------------------- /deploy.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | docker login -u $DOCKERUSER -p $DOCKERPASS 3 | docker push bsycorp/kind 4 | -------------------------------------------------------------------------------- /resources/entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | exec "$@" -------------------------------------------------------------------------------- /resources/kubelet.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | sleep 2 3 | 4 | # by default kind will set capacity to be the capacity of the host, and not honour any limits that have been imposed on it 5 | # this tries to reserve an amount inverse to the limit on kind to fix that 6 | if [ ! -z "$ENFORCE_CAPACITY" ]; then 7 | MEMORY_TOTAL=$(cat /proc/meminfo | grep MemTotal | awk '{print $2}') 8 | MEMORY_LIMIT=$(($(cat /sys/fs/cgroup/memory/memory.limit_in_bytes) / 1024)) 9 | MEMORY_OUTSIDE_LIMIT=$(($MEMORY_TOTAL - $MEMORY_LIMIT)) 10 | echo "Memory to reserve to ensure correct node capacity: $MEMORY_OUTSIDE_LIMIT" 11 | 12 | CPU_TOTAL=$(grep -c processor /proc/cpuinfo) 13 | CPU_LIMIT=$(($(cat /sys/fs/cgroup/cpu/cpu.cfs_quota_us) / $(cat /sys/fs/cgroup/cpu/cpu.cfs_period_us))) 14 | CPU_OUTSIDE_LIMIT=$(($CPU_TOTAL - $CPU_LIMIT)) 15 | echo "CPU to reserve to ensure correct node capacity: $CPU_OUTSIDE_LIMIT" 16 | 17 | SYSTEM_RESERVED="--system-reserved=cpu=$CPU_OUTSIDE_LIMIT,memory=${MEMORY_OUTSIDE_LIMIT}Ki" 18 | echo "Using arguments: $SYSTEM_RESERVED" 19 | fi 20 | /usr/bin/kubelet --hostname-override=minikube \ 21 | $SYSTEM_RESERVED \ 22 | --cluster-domain=cluster.local \ 23 | --bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf \ 24 | --pod-manifest-path=/etc/kubernetes/manifests \ 25 | --cluster-dns=10.96.0.10 \ 26 | --authorization-mode=Webhook \ 27 | --client-ca-file=/var/lib/localkube/certs/ca.crt \ 28 | --fail-swap-on=false \ 29 | --kubeconfig=/etc/kubernetes/kubelet.conf \ 30 | --cgroup-driver=cgroupfs -------------------------------------------------------------------------------- /resources/setup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | set -e 3 | mkdir -p /var/kube-config 4 | echo $KUBERNETES_VERSION > /var/kube-config/kubernetes-version 5 | KUBERNETES_MAJOR_MINOR_VERSION="$(echo ${KUBERNETES_VERSION:1} | cut -d. -f 1-2)" 6 | echo $MINIKUBE_VERSION > /var/kube-config/minikube-version 7 | echo $STATIC_IP > /var/kube-config/static-ip 8 | echo "Building against kubernetes $KUBERNETES_VERSION" 9 | 10 | # start docker daemon, useful to check how it starts, we want it to be using overlay2 and not show errors etc. 11 | docker info 12 | 13 | # add deps 14 | apk add --update sudo curl ca-certificates bash less findutils supervisor tzdata socat lz4 conntrack-tools sed 15 | 16 | # add a static / known ip to the existing default network interface so that we can configure kube component to use that IP, and can re-use that IP again at boot time. 17 | ORIG_IP=$(hostname -i) 18 | ip addr add $STATIC_IP/32 dev eth0 19 | echo "$STATIC_IP control-plane.minikube.internal" >> /etc/hosts 20 | 21 | # create fake systemctl so minikube / kubeadm doesn't crack it 22 | echo "#!/bin/sh" > /usr/local/bin/systemctl 23 | chmod +x /usr/local/bin/systemctl 24 | 25 | # add glibc as kube/minikube/things need it 26 | curl -Lo glibc.apk https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.28-r0/glibc-2.28-r0.apk 27 | # avoid this error - https://github.com/sgerrand/alpine-pkg-glibc/issues/51 28 | apk del libc6-compat || true 29 | apk add glibc.apk 30 | rm -f glibc.apk 31 | 32 | # fire before cluster hook 33 | source /before-cluster.sh 34 | 35 | # get kube binaries 36 | curl -Lo /usr/local/bin/minikube https://storage.googleapis.com/minikube/releases/$MINIKUBE_VERSION/minikube-linux-amd64 && chmod +x /usr/local/bin/minikube 37 | curl -Lo /usr/local/bin/kubectl https://storage.googleapis.com/kubernetes-release/release/$KUBERNETES_VERSION/bin/linux/amd64/kubectl && chmod +x /usr/local/bin/kubectl 38 | 39 | function replaceHost(){ 40 | find /var /etc /root -type f -not -path "/etc/hosts" -not -size +1M -exec grep -il "$ORIG_IP" {} \; | xargs sed -i "s|$ORIG_IP|$STATIC_IP|g" || true 41 | } 42 | 43 | # start minikube, will fail, but s'ok is just for downloading things 44 | minikube start --vm-driver=none --kubernetes-version $KUBERNETES_VERSION --bootstrapper kubeadm --apiserver-ips $STATIC_IP,127.0.0.1 --apiserver-name minikube --extra-config=apiserver.advertise-address=$STATIC_IP --extra-config=kubeadm.ignore-preflight-errors=FileContent--proc-sys-net-bridge-bridge-nf-call-iptables,Service-Docker $MINIKUBE_EXTRA_ARGS || true 45 | if [ ! -f /usr/bin/kubeadm ]; then 46 | ln -s /var/lib/minikube/binaries/$KUBERNETES_VERSION/kubeadm /usr/bin/kubeadm 47 | fi 48 | if [ ! -f /usr/bin/kubelet ]; then 49 | ln -s /var/lib/minikube/binaries/$KUBERNETES_VERSION/kubelet /usr/bin/kubelet 50 | fi 51 | 52 | # fix minikube generated configs, this shouldn't be required if minikube behaved itself / had args for all the things 53 | replaceHost 54 | 55 | # try and start kubelet in the background, keep restarting it as it will fail until kubeadm runs. 56 | { 57 | while [ ! -f /tmp/setup-done ]; do 58 | sleep 5 59 | echo "(Re)starting kubelet.." 60 | # ensure replace host is run before kubelet is fired, can't exactly time when kubeadm creates configs 61 | replaceHost 62 | 63 | # some versions of minikube put certs in different places, so align 64 | if [ ! -f /var/lib/localkube/certs/ca.crt ]; then 65 | mkdir -p /var/lib/localkube/certs 66 | cp /var/lib/minikube/certs/ca.crt /var/lib/localkube/certs/ca.crt || true 67 | fi 68 | 69 | /kubelet.sh || true 70 | done 71 | } & 72 | 73 | # run kubeadm to create cluster - ignore preflights as there will be failures because of swap, systemd, lots of things.. 74 | if [ ! -f /var/lib/kubeadm.yaml ]; then 75 | if [ -f /var/tmp/minikube/kubeadm.yaml ] 76 | then 77 | cp /var/tmp/minikube/kubeadm.yaml /var/lib/kubeadm.yaml 78 | elif [ -f /var/tmp/minikube/kubeadm.yaml.new ] 79 | then 80 | cp /var/tmp/minikube/kubeadm.yaml.new /var/lib/kubeadm.yaml 81 | fi 82 | fi 83 | /usr/bin/kubeadm config migrate --old-config /var/lib/kubeadm.yaml --new-config /var/lib/kubeadm.yaml 84 | /usr/bin/kubeadm init --config /var/lib/kubeadm.yaml --ignore-preflight-errors=all 85 | 86 | # use kube-config that contains the certs, rather than referencing files 87 | mkdir -p /root/.kube 88 | cp /etc/kubernetes/admin.conf /root/.kube/config 89 | 90 | # fix scheduler auth 91 | /usr/local/bin/kubectl create rolebinding -n kube-system kube-scheduler --role=extension-apiserver-authentication-reader --serviceaccount=kube-system:kube-scheduler || true 92 | 93 | # workaround for https://github.com/kubernetes/kubernetes/issues/50787 and related 'conntrack' errors, kubeadm config should work but it doesn't for some reason. 94 | /usr/local/bin/kubectl -n kube-system get cm kube-proxy -o yaml | sed 's|maxPerCore: .*|maxPerCore: 0|g' > kube-proxy-cm.yaml 95 | /usr/local/bin/kubectl -n kube-system delete cm kube-proxy 96 | /usr/local/bin/kubectl -n kube-system create -f kube-proxy-cm.yaml 97 | rm -f kube-proxy-cm.yaml 98 | 99 | # workaround for https://github.com/bsycorp/kind/issues/19 100 | /usr/local/bin/kubectl -n kube-system get cm coredns -o yaml | sed 's|/etc/resolv.conf|8.8.8.8 9.9.9.9|g' > coredns-cm.yaml 101 | /usr/local/bin/kubectl -n kube-system delete cm coredns 102 | /usr/local/bin/kubectl -n kube-system create -f coredns-cm.yaml 103 | rm -f coredns-cm.yaml 104 | 105 | # force storage provisioner, as its not default in later versions, need both or yaml isn't downloaded 106 | /usr/local/bin/minikube addons enable storage-provisioner || true 107 | /usr/local/bin/kubectl apply -f /etc/kubernetes/addons/storage-provisioner.yaml || true 108 | # setup default storage class if its missing 109 | if [ -z "$(/usr/local/bin/kubectl get storageclass -o jsonpath='{.items[*].metadata.name}')" ]; then 110 | echo "Creating a default storage class as its missing, and required" 111 | cat < /tmp/setup-done 156 | killall kubelet 157 | sleep 5 158 | docker rm -f $(docker ps -q) 159 | docker container prune -f 160 | 161 | # create cache of docker images used so far. 162 | tar -c -C /var/lib/docker ./ | lz4 -3 > /docker-cache.tar.lz4 163 | 164 | # cleanup 165 | rm -f /setup.sh 166 | 167 | # cleanup extra binaries 168 | rm -f /usr/local/bin/minikube 169 | rm -f /usr/bin/kubeadm 170 | rm -rf ~/.minikube/ 171 | rm -rf /*-cluster.sh 172 | -------------------------------------------------------------------------------- /resources/sgerrand.rsa.pub: -------------------------------------------------------------------------------- 1 | -----BEGIN PUBLIC KEY----- 2 | MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApZ2u1KJKUu/fW4A25y9m 3 | y70AGEa/J3Wi5ibNVGNn1gT1r0VfgeWd0pUybS4UmcHdiNzxJPgoWQhV2SSW1JYu 4 | tOqKZF5QSN6X937PTUpNBjUvLtTQ1ve1fp39uf/lEXPpFpOPL88LKnDBgbh7wkCp 5 | m2KzLVGChf83MS0ShL6G9EQIAUxLm99VpgRjwqTQ/KfzGtpke1wqws4au0Ab4qPY 6 | KXvMLSPLUp7cfulWvhmZSegr5AdhNw5KNizPqCJT8ZrGvgHypXyiFvvAH5YRtSsc 7 | Zvo9GI2e2MaZyo9/lvb+LbLEJZKEQckqRj4P26gmASrZEPStwc+yqy1ShHLA0j6m 8 | 1QIDAQAB 9 | -----END PUBLIC KEY----- -------------------------------------------------------------------------------- /resources/start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | error_trap() { 4 | echo "Error on line $1" 5 | echo "Startup timeout, didn't become healthy after 3 mins.. details:" 6 | kubectl get po -n kube-system -o=custom-columns=NAME:.metadata.name --no-headers | xargs -I % sh -c 'kubectl -n kube-system describe po %; kubectl -n kube-system logs %' || true 7 | kubectl get po || true 8 | } 9 | trap 'error_trap $LINENO' ERR 10 | 11 | KUBERNETES_VERSION=$(cat /var/kube-config/kubernetes-version) 12 | STATIC_IP=$(cat /var/kube-config/static-ip) 13 | echo "$STATIC_IP control-plane.minikube.internal" >> /etc/hosts 14 | 15 | echo "Clean up.." # cleanup stuff that might be left over from build-phase, sometimes throws Resource busy errors on build phase cleanup so needs to be done here. 16 | rm -rf /var/run/docker* 17 | rm -rf /var/lib/kubelet 18 | 19 | echo "Setting up networking.." # use hard-coded IP to make kube happy (all the things are configured against it, otherwise we need to bootstrap kube everytime) 20 | if (ip addr list | grep -v $STATIC_IP); then 21 | ip addr add $STATIC_IP/32 dev eth0 22 | fi 23 | 24 | echo "Extracting cache.." # extract the tarred up docker images from build phase, we do this so when dind starts again in run phase we have all our stuff still, and its fast. 25 | (mkdir -p /var/lib/docker; cd /var/lib/docker; lz4 -d /docker-cache.tar.lz4 | tar -x) 26 | 27 | supervisorctl -c /etc/supervisord.conf start dockerd 28 | sleep 2 29 | docker info 30 | echo "Docker ready" 31 | touch /var/kube-config/docker-ready 32 | 33 | echo "Logging into docker-registry $REGISTRY with user $REGISTRY_USER" 34 | if [[ -n "${REGISTRY}" ]] && [[ -n "${REGISTRY_USER}" ]] && [[ -n "${REGISTRY_PASSWORD}" ]]; then 35 | docker login -u "$REGISTRY_USER" -p "$REGISTRY_PASSWORD" $REGISTRY || echo "WARN: Login Failed!" 36 | else 37 | echo "no docker-registry/-credentials supplied" 38 | fi 39 | 40 | echo "Starting config server.." 41 | supervisorctl -c /etc/supervisord.conf start config-serve 42 | 43 | # start cluster 44 | echo "Starting Kubernetes.." 45 | supervisorctl -c /etc/supervisord.conf start kubelet 46 | 47 | sleep 15 48 | kubectl wait --for=condition=ready --timeout 3m pod --all --all-namespaces 49 | kubectl get po --all-namespaces 50 | 51 | # ready 52 | touch /var/kube-config/kubernetes-ready 53 | echo "Kubernetes ready" 54 | if [ -f "/kubernetes-ready.sh" ]; then 55 | /bin/bash /kubernetes-ready.sh 56 | fi 57 | -------------------------------------------------------------------------------- /resources/supervisord.conf: -------------------------------------------------------------------------------- 1 | [supervisord] 2 | 3 | [unix_http_server] 4 | file=/supervisor.sock ; the path to the socket file 5 | 6 | [supervisorctl] 7 | serverurl=unix:///supervisor.sock ; use a unix:// URL for a unix socket 8 | 9 | [rpcinterface:supervisor] 10 | supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface 11 | 12 | [program:dockerd] 13 | command=/usr/local/bin/dockerd-entrypoint.sh 14 | autostart=false 15 | stdout_logfile=/dev/stdout 16 | stdout_logfile_maxbytes=0 17 | environment=DOCKER_DRIVER=overlay2 18 | 19 | [program:kubelet] 20 | command=/kubelet.sh 21 | autostart=false 22 | autorestart=true 23 | startretries=99999 24 | stdout_logfile=/dev/stdout 25 | stdout_logfile_maxbytes=0 26 | 27 | [program:config-serve] 28 | command=/usr/local/bin/docker run --rm -v /var/kube-config/:/usr/share/nginx/html:ro -p 10080:80 nginx:1.13 29 | autostart=false 30 | stdout_logfile=/dev/stdout 31 | stdout_logfile_maxbytes=0 32 | 33 | [program:start] 34 | command=/start.sh 35 | autostart=true 36 | autorestart=false 37 | stdout_logfile=/dev/stdout 38 | stdout_logfile_maxbytes=0 --------------------------------------------------------------------------------