Proper dockerfile for ansible which runs on k8s cluster

4/21/2020

I have aks cluster and I create a pod for ansible from basic image like nginx and install everything from shell manually by shell commands(pip3 install ansible, pip3 install openshift). Now I need to create an ansible image with necessary updates, here is my dockerfile. It works fine on laptop, but when I push repo and create a pod it backoff error. Maybe someone help me to understand how I can deploy correct image for ansible. Thanks

Dockerfile:

FROM ubuntu:16.04. 

RUN apt-get update && apt-get install -y --no-install-recommends \
python3.5 \
python3-pip \
python3-setuptools \
curl \
sudo \
nano \
&& \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
RUN pip3 install --upgrade pip
RUN pip3 install ansible
RUN pip3 install openshift
RUN curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
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 sudo mv ./kubectl /usr/local/bin/kubectl
# Define working directory.
WORKDIR /data

# Define default command.
CMD ["bash"]

and here is ansible_pod.yaml:

apiVersion: v1
kind: Pod
metadata:
  name: ansible
spec:
  volumes:
    - name: ansible-data
      persistentVolumeClaim:
        claimName: ansible-data
  containers:
    - name: ansible
      image: ansible:latest
      volumeMounts:
        - mountPath: /scripts
          name: ansible-data
      restartPolicy: Never

---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: ansible-data
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
     storage: 1Gi
-- Bora Özkan
ansible
dockerfile
kubernetes

1 Answer

4/22/2020

create a pod it backoff error.

That's because you have your CMD as bash, but (a) bash expects to be able to read from stdin if not provided with a command, and your PodSpec does permit that (b) even if it did, what do you expect the container to do? Just sit there until someone runs kubectl exec in order to run arbitrary ansible playbook commands?

I would guess the shortest path to a non-backoff error would be to ask your already created docker image to just sleep indefinitely, until someone does kubectl exec into the pod and/or you come up with what you expect the pod to do

  containers:
    - name: ansible
      image: ansible:latest
      command: ["sleep", "infinity"]

While this isn't what you asked, you seem to be spending a lot of energy to install python into a generic image, instead of using a docker.io/library/python:3.5 image that comes with python already installed and merely needs the rest of those binaries laid on top of it

-- mdaniel
Source: StackOverflow