How to install kubectl in kubernetes container through docker image
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/kubectlRUN chmod +x ./kubectlRUN mv ./kubectl /usr/local/bin
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
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_imageWORKDIR /tmpRUN /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/v1kind: Deploymentmetadata: name: support labels: app: support namespace: defaultspec: 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