Create kubernetes pod with volume using kubectl run
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 on Kubernetes, managing data storage is a critical aspect. Kubernetes supports various storage options through volumes. This article focuses on creating Kubernetes Pods with volumes using kubectl run, a streamlined and often convenient way to launch single pods directly from the command line.
Understanding Volumes in Kubernetes
Before proceeding with the technical steps, it is essential to understand why volumes are necessary:
- Persistence: Unlike container storage, volumes retain data even if the container crashes or restarts.
- Sharing Data: Volumes can be accessed by multiple containers in a pod, providing a mechanism for data sharing.
- Multiple Types: Kubernetes supports various volume types such as
emptyDir,hostPath,configMap,secret, and persistent volumes.
Volume Types Explained
Here’s a brief overview of some common volume types:
| Volume Type | Description |
emptyDir | A temporary storage space that is created with the pod. It persists across container restarts within the pod but is removed when the pod is terminated. |
hostPath | Maps a directory from the host node's filesystem to the pod. This is useful for tasks like logging where logs need to be accessed on the host node. |
configMap | Provides a way to inject configuration data into a pod's containers. It decouples configuration artifacts from image content to keep containerized applications portable. |
secret | Used to store sensitive data such as passwords and tokens. This data can be mounted as files or exposed as environment variables. |
persistentVolumeClaim | Represents a user's request for, and a binding to, a persistent volume. |
Creating a Pod with Volume Using kubectl run
Basic Steps
- Define the Volume: Determine the type and characteristics of the volume you need based on your application requirements.
- Create and Edit YAML Configuration: While
kubectl rungenerates a pod specification in YAML, it might be necessary to edit the specification to attach volumes manually. - Apply the Configuration: Use
kubectlcommands to deploy the configuration to your Kubernetes cluster.
Example: Using emptyDir with kubectl run
Here’s a step-by-step example of creating a pod with an emptyDir volume.
Step 1: Initial Pod Creation
Start by creating a simple pod using kubectl run:
This command creates a YAML file named mypod.yaml with a basic pod configuration.
Step 2: Edit the YAML to Add a Volume
Edit mypod.yaml to include an emptyDir volume:
In this configuration:
volumeMountsrefers to the path inside the container where the volume is mounted.emptyDir: {}represents the temporary directory created for this volume.
Step 3: Deploy the Pod
Use the following command to create the pod in the Kubernetes cluster:
Verify the Pod and Volume
To verify that the pod is running and the volume is functioning, run:
Inside the container, you can execute a shell to see the mounted volume:
Advanced Topics
Persistent Volumes vs. Ephemeral Volumes
- Ephemeral Volumes like
emptyDirare deleted once the pod is terminated, suitable for temporary data. - Persistent Volumes (PV) are designed for long-term storage; a
PersistentVolumeClaim (PVC)is used to request storage resources.
Security Considerations
When mounting sensitive data with volumes like secret, ensure that the pod's service account has limited permissions, and consider encrypting data at rest and in transit.
Conclusion
Creating a Kubernetes pod with volumes using kubectl run is a fundamental skill that enhances your ability to manage applications requiring persistent or shared data storage. With the right configuration, you can efficiently manage data across your containers, ensuring both short-term and long-term data retention as per your application's needs.

