How to install kubectl binary in kubernetes container through docker image?

74 Views Asked by At

I'm trying to build a docker image with a kubectl binary package installed in it to later use it in Kubernetes deployment as an image to perform kubectl operations on different clusters. Unfortunately, the docker build was a success and also if I run locally with the docker run command, the kubectl commands work as expected. But, when I deploy this as a pod, and try to execute the kubectl command it throws me out, and the container restarts.

Below is my Dockerfile

FROM python:3.12-alpine3.18

USER root

RUN addgroup -S sregroup  && adduser -S sreuser -G sregroup

# ensure local python is preferred over distribution python
ENV PATH /usr/local/bin:$PATH

# runtime dependencies
RUN set -eux

RUN apk update && \
    apk add --no-cache bash mailx  iputils bind-tools drill netcat-openbsd postfix curl openssl ca-certificates gcc musl-dev libffi-dev openssl-dev git tzdata

RUN pip3 install --no-cache-dir wheel

COPY requirements.txt .

RUN pip3 install --no-cache-dir -r requirements.txt

RUN mkdir /.kube && chmod g+rwX /.kube

RUN curl -LO https://s3.us-west-2.amazonaws.com/amazon-eks/1.27.9/2024-01-04/bin/linux/amd64/kubectl && \
    cp kubectl /usr/local/bin/kubectl && \
    chmod +x /usr/local/bin/kubectl

USER sreuser

ENTRYPOINT ["/bin/sh"]

Below is the container status and error

$ k get po
NAME                 READY   STATUS    RESTARTS        AGE
kubectlll-test       1/1     Running   1 (7m40s ago)   8m45s

$ k exec -it kubectlll-test -- /bin/bash
kubectlll-test:/tmp$ kubectl version
command terminated with exit code 137

Can anyone please help me in resolving this issue.

2

There are 2 best solutions below

0
Xavier Llauca On

"You must replace CMD ["sleep", "infinity"] with ENTRYPOINT ["/bin/sh"]. Additionally, you can simplify it without the need to install additional packages, as shown in the following example:"

Note: The instruction CMD ["sleep", "infinity"] is used to keep the container running indefinitely without consuming many resources.

FROM alpine:3.19.1

RUN apk -U upgrade \
    && apk add --no-cache ca-certificates bash git openssh curl jq \
    && curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" \
    && chmod +x kubectl \
    && mv kubectl /usr/local/bin/kubectl \
    && kubectl version --client

CMD ["sleep", "infinity"]
0
deep bajaj On

Your container is getting stopped/killed since there are no tasks within the container that are required to be performed.

To keep it running you may echo something or use the below syntax for your reference:-

FROM python:3.12-alpine3.18

USER root

RUN addgroup -S sregroup  && adduser -S sreuser -G sregroup

# ensure local python is preferred over distribution python
ENV PATH /usr/local/bin:$PATH

# runtime dependencies
RUN set -eux

RUN apk update && \
    apk add --no-cache bash mailx  iputils bind-tools drill netcat-openbsd postfix curl openssl ca-certificates gcc musl-dev libffi-dev openssl-dev git tzdata

RUN pip3 install --no-cache-dir wheel

COPY requirements.txt .

RUN pip3 install --no-cache-dir -r requirements.txt

RUN mkdir /.kube && chmod g+rwX /.kube

RUN curl -LO https://s3.us-west-2.amazonaws.com/amazon-eks/1.27.9/2024-01-04/bin/linux/amd64/kubectl && \
    cp kubectl /usr/local/bin/kubectl && \
    chmod +x /usr/local/bin/kubectl

USER sreuser

ENTRYPOINT ["/bin/sh", "-c" , "echo Hello World", "tail -f /dev/null"]

Or you may use additional commands as CMD incase you want to overwrite something else as per your choice