Reputation: 1307
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!
Upvotes: 18
Views: 46658
Reputation: 942
I had the same problem for devcontainers. I solved it, by adding a devcontainer feature:
"features": {
"ghcr.io/devcontainers/features/kubectl-helm-minikube:1": {
"kubectl": "latest",
"helm": "none",
"minikube": "none"
}
}
Upvotes: 0
Reputation: 15907
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.
Upvotes: 29
Reputation: 889
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
Upvotes: 38
Reputation: 3728
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
Upvotes: 0
Reputation: 1307
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
Upvotes: 3
Reputation: 1270
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
Upvotes: 8