Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install kubectl in kubernetes container through docker image

I want to run 'kubectl' commands in the container, so i want to install kubectl in the container, through while building the Docker image. Any help is appreciated!

like image 244
Chandra Avatar asked Aug 07 '18 01:08

Chandra


People also ask

Is kubectl installed with Docker?

Use the kubectl commandThe kubectl binary is not automatically packaged with Docker Desktop for Linux. To install the kubectl command for Linux, see Kubernetes documentation. It should be installed at /usr/local/bin/kubectl .

Does kubectl require Docker?

Can Kubernetes Run Without Docker? The answer is both yes and no. Kubernetes, in itself, is not a complete solution. It depends on a container runtime to orchestrate; you can't manage containers without having containers in the first place.


2 Answers

kubectl is written in go, so is completely self-contained.

You can therefore copy it from another container that already includes it. Like I do here, copying it from bitnami/kubectl:1.20.9:

FROM bitnami/kubectl:1.20.9 as kubectl

FROM ubuntu-or-whatever-image:tag

# Do whatever you need to with the
# ubuntu-or-whatever-image:tag image, then:

COPY --from=kubectl /opt/bitnami/kubectl/bin/kubectl /usr/local/bin/

I like this better than fiddling with curl, because now you're taking advantage of Dockers ability to cache the bitnami/kubectl:1.20.9 image.

like image 91
Peter V. Mørch Avatar answered Oct 22 '22 09:10

Peter V. Mørch


put this in your Dockerfile

RUN curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
RUN chmod +x ./kubectl
RUN mv ./kubectl /usr/local/bin
like image 24
Alain Gauthier Avatar answered Oct 22 '22 10:10

Alain Gauthier