Pulling local repository docker image from kubernetes

11/1/2019

Trying to install a sample container App using Pod in my local environment I'm using kubernates cluster coming with docker desktop.

I'm creating the Pod using bellow command with the YML file kubectl create -f test_image_pull.yml

apiVersion: v1
kind: Pod
metadata:
# value must be lower case
  name: sample-python-web-app
spec:
  containers:
    - name: sample-hello-world
      image: local/sample:latest
      imagePullPolicy: Always
      command: ["echo", "SUCCESS"]

docker file used to build the image and this container running without any issue if u run with docker run

# Use official runtime python
FROM python:2.7-slim

# set work directory to app

WORKDIR /app
# Copy current directory
COPY . /app

# install needed packages
RUN pip install --trusted-host pypi.python.org -r requirement.txt

# Make port 80 available to outside container

EXPOSE 80

# Define environment variable

ENV NAME World

# Run app.py when the container launches

CMD ["python" , "app.py"]
from flask import Flask
from redis import Redis, RedisError
import os
import socket


#connect to redis
redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)
app = Flask(__name__)

@app.route("/")

def hello():
    try:
       visits = redis.incr("counter")
    except RedisError:
        visits = "<i>cannot connect to Redis, counter disabled</i>"

    html = "<h3>Hello {name}!</h3>" \
            "<b>Hostname:</b> {hostname}<br/>"  \
            "<b>Visits:</b> {visits}"


    return html.format (
              name=os.getenv("NAME", "world"),
              hostname=socket.gethostname(),
              visits=visits  
            )

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=80)            
Flask
Redis

Once I describe the pod it shows me below error

kubectl describe pod sample-python-web-app

Events:
  Type     Reason     Age                  From                     Message
  ----     ------     ----                 ----                     -------
  Normal   Scheduled  3m25s                default-scheduler        Successfully assigned default/sample-python-web-app to docker-desktop
  Normal   Pulling    97s (x4 over 3m22s)  kubelet, docker-desktop  Pulling image "local/sample:latest"
  Warning  Failed     94s (x4 over 3m17s)  kubelet, docker-desktop  Failed to pull image "local/sample:latest": rpc error: code = Unknown desc = Error response from daemon: pull access denied for local/sample, repository does not exist or may require 'docker login': denied: requested access to the resource is denied
  Warning  Failed     94s (x4 over 3m17s)  kubelet, docker-desktop  Error: ErrImagePull
  Normal   BackOff    78s (x6 over 3m16s)  kubelet, docker-desktop  Back-off pulling image "local/sample:latest"
  Warning  Failed     66s (x7 over 3m16s)  kubelet, docker-desktop  Error: ImagePullBackOff
-- THerath
kubernetes

2 Answers

11/1/2019

Kubernetes pulls container images from a Docker Registry. Per the doc:

You create your Docker image and push it to a registry before referring to it in a Kubernetes pod.

Moreover:

The image property of a container supports the same syntax as the docker command does, including private registries and tags.

So, the way the image is referenced in the pod's spec - "image: local/sample:latest" - Kubernetes looks on Docker Hub for the image in repository named "local".

You can push the image to Docker Hub or some other external Docker Registry, public or private; you can host Docker Registry on the Kubernetes cluster; or, you can run a Docker Registry locally, in a container.

To run a Docker registry locally:

docker run -d -p 5000:5000 --restart=always --name registry registry:2

Next, find what is the IP address of the host - below I'll use 10.0.2.1 as an example.

Then, assuming the image name is "local/sample:latest", tag the image:

docker tag local/sample:latest 10.0.2.1:5000/local/sample:latest

...and push the image to the local registry:

docker push 10.0.2.1:5000/local/sample:latest

Next, change in pod's configuration YAML how the image is referenced - from

    image: local/sample:latest

to

    image: 10.0.2.1:5000/local/sample:latest

Restart the pod.

EDIT: Most likely the local Docker daemon will have to be configured to treat the local Docker registry as insecure. One way to configure that is described here - just replace "myregistrydomain.com" with the host's IP (e.g. 10.0.2.1). Docker Desktop also allows to edit daemon's configuration file through the GUI.

-- apisim
Source: StackOverflow

11/1/2019

If you want to setup local repository for Kubernetes cluster, you might follow this guide .

I would recommend using Trow.io which is a image Management for Kubernetes to quickly create a registry that runs wihtin Kubernetes and provides a secure and fast way to get containers running on the cluster.

We're building an image management solution for Kubernetes (and possibly other orchestrators). At its heart is the Trow Registry, which runs inside the cluster, is simple to set-up and fully integrated with Kubernetes, including support for auditing and RBAC.

Why "Trow"

"Trow" is a word with multiple, divergent meanings. In Shetland folklore a trow is a small, mischievous creature, similar to the Scandanavian troll. In England, it is a old style of cargo boat that transported goods on rivers. Finally, it is an archaic word meaning "to think, believe, or trust". The reader is free to choose which interpretation they like most, but it should be pronounced to rhyme with "brow".

Whole installation process is described here.

-- Crou
Source: StackOverflow