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!
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
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
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
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