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:
- Docker Installed Locally: The local images must be built and stored in Docker on your host machine.
- Kubernetes Cluster: You should have a running Kubernetes cluster along with
kubectlconfigured to interact with it. - 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:
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.
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:
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:
Step 5: Verify
Check whether the pod is running as expected:
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 Concept | Description |
| Export Local Image | docker save and load used to export and import images. |
| Image Deployment | Use imagePullPolicy: Never to ensure local image usage. |
| Node Preparation | Load the image on each node manually. |
| Scaling Complexity | Manual loading impedes automated scaling solutions. |
| Network Efficiency | Saves 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.

