17

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

1
  • What have you tried so far? Do you have a Dockerfile? An existing pattern for accessing the Kubernetes API from pods that you're working from?
    – David Maze
    Aug 7, 2018 at 2:40

5 Answers 5

30

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
14

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.

2
  • so much better than the other answers - thanks Peter
    – Brian
    Jan 21, 2022 at 1:20
  • 1
    I like this solution, but how would I know for certain that the binary will work with different architectures and platform? For example, if my base image may be alpine linux or ubuntu, will this solution still work? What happens if the CPU architecture also differs?
    – Alex
    Feb 27 at 4:40
8

You just need to map kubectl (e.g. /usr/local/bin/kubectl) binary file and kubeconfig (e.g. /root/.kube/config) into your container.

For example (yaml file for Deployment):

      containers:
      - image: container-image-name
        name: container-name
        volumeMounts:
        - name: kubectl-binary
          mountPath: /usr/local/bin/kubectl
          readOnly: true
        - name: kubectl-config
          mountPath: /root/.kube/config
          readOnly: true
      volumes:
      - name: kubectl-binary
        hostPath:
          path: /usr/local/bin/kubectl
      - name: kubectl-config
        hostPath:
          path: /root/.kube/config

P.S.

use the following command to download kubectl binary file on each node, and copy /root/.kube/config to each node:

$ curl -L https://dl.k8s.io/v1.10.6/bin/linux/amd64/kubectl -o /usr/local/bin/kubectl
1
  • Thanks, in my case paths are little different but its working fine. For kubectl - /usr/bin/kubectl For config - /etc/kubernetes/config
    – Chandra
    Aug 7, 2018 at 19:52
3

Weike's solution works fine for me with different kubectl path, any how if some one looking for solution to install the kubectl in the Docker image then here is the Docker file (it also installs python and kubernetes python client api, if we want to access cluster through python client api):

FROM base_image

WORKDIR /tmp


RUN  /usr/bin/curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl \
     && chmod +x ./kubectl  \
     &&  mv ./kubectl /usr/local/bin/kubectl \
     && zypper install -y python2 \
     && zypper install -y python2-pip \
     && pip install kubernetes \
     && zypper install -y git \
     && zypper clean -a \
     && git clone --recursive https://github.com/kubernetes-client/python.git \
     && cd python \
     && python setup.py install

Also here is my deployment file to map kubectl binary and configuration to container to access kubectl in the kubernetes container with in the pod:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: support
  labels:
    app: support
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: support
  template:
    metadata:
      labels:
        app: support
    spec:
      terminationGracePeriodSeconds: 3
      imagePullSecrets:
      - name: mysecret
      containers:
        - name: support
          image: image-name
          command:
            - "/bin/sh"
            - "-c"
            - "sleep infinity"
          volumeMounts:
          - name: kubectl-binary
            mountPath: /usr/bin/kubectl
            readOnly: true
          - name: kubectl-config
            mountPath: /etc/kubernetes/config
            readOnly: true
      volumes:
        - name: kubectl-binary
          hostPath:
            path: /usr/bin/kubectl
        - name: kubectl-config
          hostPath:
            path: /etc/kubernetes/config
0

If you have docker you can do whatever you want really since you can pull and start any image. So if you want to build and deploy:

docker login
docker build foo/bar .
docker push
docker run -v ~/.kube:/root/.kube lachlanevenson/k8s-kubectl set image deploy bar app=foo/bar

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.