Kubernetes
Pod
Local Image
Container Orchestration
DevOps

Pull a local image to run a pod in Kubernetes

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

When deploying applications in Kubernetes, it is common to pull Docker images from external registries like Docker Hub or a private image repository. However, there might be scenarios where you need to use local images stored on your host machine to create Kubernetes pods. This can be particularly useful in development environments or situations where you have restricted network access.

Prerequisites

Before trying to pull a local image to run in a Kubernetes pod, you need to ensure:

  1. Docker Installed Locally: The local images must be built and stored in Docker on your host machine.
  2. Kubernetes Cluster: You should have a running Kubernetes cluster along with kubectl configured to interact with it.
  3. kubectl Access: Ensure you have the necessary permissions to deploy pods on your Kubernetes cluster.

Pulling Local Images

Step 1: Exporting the Local Image

Use Docker to save the image into a tar archive on your host:

bash
docker save -o my-image.tar my-image:latest

Here, my-image is the name of the image, and this command exports the image to a file called my-image.tar.

Step 2: Load Image into Kubernetes Nodes

For each node in your Kubernetes cluster, you need to manually load the image.

bash
1scp my-image.tar user@node:/path/to/upload
2ssh user@node
3
4# Then on the node's terminal
5docker load -i /path/to/upload/my-image.tar

Repeat this process for each node in your cluster. This step is crucial because pods might be scheduled on different nodes.

Step 3: Update Kubernetes Deployment Manifest

Kubernetes needs to know that it should look for the local image. Use the following deployment manifest as a reference:

yaml
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4  name: local-image-deployment
5  labels:
6    app: local-image
7spec:
8  replicas: 1
9  selector:
10    matchLabels:
11      app: local-image
12  template:
13    metadata:
14      labels:
15        app: local-image
16    spec:
17      containers:
18      - name: my-container
19        image: my-image:latest
20        imagePullPolicy: Never  # This is critical for local images

Ensure imagePullPolicy is set to Never. This instructs Kubernetes not to attempt pulling the image from any registry.

Step 4: Deploy the Pod

Apply the deployment configuration using kubectl:

bash
kubectl apply -f my-deployment.yaml

Step 5: Verify

Check whether the pod is running as expected:

bash
kubectl get pods

If your pod does not start, use kubectl describe pod <pod-name> to gather more information about the issue.

Advantages and Considerations

Advantages

  • Flexibility in Development: Using local images allows rapid testing and development cycles since changes can be made quickly and immediately.
  • Network Efficiency: It saves network resources, especially in environments with restricted or costly internet access.

Considerations

  • Complexity in Scaling: Using local images often implies manual intervention, especially when scaling across multiple nodes.
  • Persistent States: The same image version must be loaded on all nodes within the cluster to ensure consistent behavior.
  • Security: There's no automated or centralized access control for local images unless integrated with image scanning tools.

Summary Table

Key ConceptDescription
Export Local Imagedocker save and load used to export and import images.
Image DeploymentUse imagePullPolicy: Never to ensure local image usage.
Node PreparationLoad the image on each node manually.
Scaling ComplexityManual loading impedes automated scaling solutions.
Network EfficiencySaves bandwidth, highly critical in isolated networks.

Conclusion

Running Kubernetes pods using local images can be a powerful way to leverage existing Docker assets without needing a registry. This approach, while sometimes manual and requires careful node management, can be highly beneficial in controlled development environments or localized testing scenarios. However, it is crucial to understand when this methodology is appropriate and the trade-offs it brings in scalability and management complexity.


Course illustration
Course illustration

All Rights Reserved.