Streamlining Kubernetes Local Development: Using Local Images for Testing and Debugging

Photo by Ian Taylor on Unsplash

Streamlining Kubernetes Local Development: Using Local Images for Testing and Debugging

When you deploy a containerized application to Kubernetes, you need to specify the container image to use in the container specification of the Kubernetes resource, such as a Deployment or a StatefulSet. If the container image is stored in a private container registry that requires authentication, you need to authenticate with the registry before Kubernetes can pull the image.

Typically, container images are stored in container registry such as Docker Hub, Google Container Registry, Amazon Elastic Compute Registry, or any other public or private container registry that you have access to.

When you run kubectl apply -f deployment.yml it will say your app is created and kubectl get pods will give your pods status. Pods status will show as ImagePullBackOff if the image specified in yml file is not available in minikube.

kubectl get pods
NAME                     READY   STATUS              RESTARTS   AGE
myapp-7f86594884-bp22h   0/1     ImagePullBackOff    0          35s
myapp-7f86594884-t2nlq   0/1     ImagePullBackOff    0          35s

To know more details about a specific pod

kubectl describe pod <POD_NAME>
....
Events:
  Type     Reason     Age               From               Message
  ----     ------     ----              ----               -------
  Normal   Scheduled  28s               default-scheduler  Successfully assigned default/myapp-7f86594884-bp22h to minikube
  Warning  Failed     20s               kubelet            Failed to pull image "myimage:v1": rpc error: code = Unknown desc = Error response from daemon: pull access denied for myapp, repository does not exist or may require 'docker login': denied: requested access to the resource is denied
  Warning  Failed     20s               kubelet            Error: ErrImagePull
  Normal   BackOff    20s               kubelet            Back-off pulling image "myimage:v1"
  Warning  Failed     20s               kubelet            Error: ImagePullBackOff
  Normal   Pulling    8s (x2 over 27s)  kubelet            Pulling image "myimage:v1"

The aforementioned issue can be resolved either by utilizing a local image available on your machine or by pushing your image to a container registry, such as Docker, and granting necessary permissions to fetch or pull the image. However, uploading and downloading images can be cumbersome during application testing.

If you are using Minikube for local Kubernetes development and testing, you can load a Docker image that you have built locally into the Minikube environment. This can help to alleviate the hassle of frequently uploading and downloading images from a remote container registry during the development and testing process.

Here is the easiest way to load your local Docker image into Minikube:

  • Build your image
docker build -t myimage:v1 .
  • Start Minikube
minikube start
  • Verify available images inside minikube
minikube image ls
registry.k8s.io/pause:3.9
registry.k8s.io/pause:3.6
registry.k8s.io/kube-scheduler:v1.26.1
registry.k8s.io/kube-proxy:v1.26.1
registry.k8s.io/kube-controller-manager:v1.26.1
registry.k8s.io/kube-apiserver:v1.26.1
  • Load Docker image into Minikube
minikube image load myimage:v1
  • Verify available images inside minikube
minikube image ls
registry.k8s.io/pause:3.9
registry.k8s.io/pause:3.6
registry.k8s.io/kube-scheduler:v1.26.1
registry.k8s.io/kube-proxy:v1.26.1
registry.k8s.io/kube-controller-manager:v1.26.1
registry.k8s.io/kube-apiserver:v1.26.1
docker.io/library/myimage:v1
  • Modify your deployment.yml file to use the image you loaded and specify imagePullPolicy flag to Never

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp

spec:
  replicas: 2
  selector:
    matchLabels:
      app: myapp

  # pods configuration
  template:
    metadata:
      labels:
        app: myapp

    spec:
      containers:
        - name: myapp-container
          image: myimage:v1
          imagePullPolicy: Never
  • Re-apply your configuration: kubectl apply -f deployment.yml
kubectl apply -f deployment.yml
  • Verify the pods status:
kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE   
myapp-7f86594884-2jvtz   1/1     Running   0          5s
myapp-7f86594884-f8s7q   1/1     Running   0          5s