├── Dockerfile └── README.md /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM alpine:3.8 2 | 3 | # Install kubectl 4 | # Note: Latest version may be found on: 5 | # https://aur.archlinux.org/packages/kubectl-bin/ 6 | ADD https://storage.googleapis.com/kubernetes-release/release/v1.6.4/bin/linux/amd64/kubectl /usr/local/bin/kubectl 7 | 8 | ENV HOME=/config 9 | 10 | RUN set -x && \ 11 | apk add --no-cache curl ca-certificates && \ 12 | chmod +x /usr/local/bin/kubectl && \ 13 | \ 14 | # Create non-root user (with a randomly chosen UID/GUI). 15 | adduser kubectl -Du 2342 -h /config && \ 16 | \ 17 | # Basic check it works. 18 | kubectl version --client 19 | 20 | USER kubectl 21 | 22 | ENTRYPOINT ["/usr/local/bin/kubectl"] 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Supported tags and respective `Dockerfile` links 2 | 3 | * [`1`, `1.6`, `1.6.4`, `latest`](https://github.com/wernight/docker-kubectl/blob/master/Dockerfile) [![](https://images.microbadger.com/badges/image/wernight/kubectl.svg)](https://microbadger.com/images/wernight/kubectl "Get your own image badge on microbadger.com") 4 | * [`1`, `1.5`, `1.5.3`](https://github.com/wernight/docker-kubectl/blob/master/Dockerfile) [![](https://images.microbadger.com/badges/image/wernight/kubectl:1.5.3.svg)](https://microbadger.com/images/wernight/kubectl:1.5.3 "Get your own image badge on microbadger.com") 5 | * [`1`, `1.3`, `1.3.6`](https://github.com/wernight/docker-kubectl/blob/v1.3.6/Dockerfile) [![](https://images.microbadger.com/badges/image/wernight/kubectl:1.3.6.svg)](https://microbadger.com/images/wernight/kubectl:1.3.6 "Get your own image badge on microbadger.com") 6 | * [`1.2`, `1.2.4`](https://github.com/wernight/docker-kubectl/blob/v1.2.4/Dockerfile) [![](https://images.microbadger.com/badges/image/wernight/kubectl:1.2.4.svg)](https://microbadger.com/images/wernight/kubectl:1.2.4 "Get your own image badge on microbadger.com") 7 | 8 | ## What is `kubectl` 9 | 10 | `kubectl` is a CLI tool to control a cluster [Kubernetes](http://kubernetes.io/). 11 | 12 | ## Usage 13 | 14 | $ docker run --rm wernight/kubectl --help 15 | 16 | Note: Entrypoint is set to kubectl so do **not** type `wernight/kubectl kubectl`. 17 | 18 | ### Usage example 1 19 | 20 | For example to access a local Kubernetes cluster you may run: 21 | 22 | $ docker run --rm --net=host --user $UID \ 23 | -v ~/.kube:/config/.kube \ 24 | wernight/kubectl cluster-info 25 | 26 | * `-net=host`: (optional) allows to connect to a local Kubernetes cluster. 27 | * `--user $UID`: (optional) by default runs as random UID `2342`, this allows to access your existing `~/.kube` if you have one. As you can note, you can run `kubectl` as any UID/GID. 28 | * `-v XXX:/config`: (optional) allows to store its configuration and possibly access existing configuration. Note that `/config` will always be the directory containing `.kube` (it's the forced `HOME` directory). Can be read-only. Alternatively you can mount a Kubernetes service account for example: `-v /var/run/secrets/kubernetes.io/serviceaccount:/var/run/secrets/kubernetes.io/serviceaccount:ro`. 29 | 30 | ### Usage example 2 31 | 32 | Here we use the service-account, so this should work from within a Pod on your cluster as long as you've docker installed (and may be `DOCKER_HOST` set up properly): 33 | 34 | $ docker run \ 35 | -v /var/run/secrets/kubernetes.io/serviceaccount/:/var/run/secrets/kubernetes.io/serviceaccount/:ro \ 36 | wernight/kubectl \ 37 | -s https://kubernetes \ 38 | --token="$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" \ 39 | --certificate-authority=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt \ 40 | cluster-info 41 | 42 | Note: Alternatively to using kube-dns, you can use environment variables set within Kubernetes containers: `https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_PORT_443_TCP_PORT`. 43 | 44 | ### Alias 45 | 46 | You may setup an alias to run this is if you were running `kubectl` directly. 47 | Here is a function POSIX-compatible that work for most shells: 48 | 49 | kubectl () { 50 | docker run --rm -it --user $UID:$GID \ 51 | -v /var/run/secrets/kubernetes.io/serviceaccount:/var/run/secrets/kubernetes.io/serviceaccount:ro \ 52 | -w /code -v "$PWD":/code:ro \ 53 | wernight/kubectl "$@" 54 | } 55 | 56 | ### Why use it 57 | 58 | It's mostly meant to be used during continuous integration or as part of an automated build/deployment: 59 | 60 | * So that your machine (e.g. build server) doesn't need `kubectl` to be installed; only Docker. 61 | * To avoid `kubectl config use-context` and similar to affect your build and other projects' builds. 62 | 63 | ## Feedbacks 64 | 65 | Suggestions are welcome on our [GitHub issue tracker](https://github.com/wernight/docker-kubectl/issues). --------------------------------------------------------------------------------