kubernetes
kubectl
pod-creation
volumes
container-orchestration

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 TypeDescription
emptyDirA 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.
hostPathMaps 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.
configMapProvides a way to inject configuration data into a pod's containers. It decouples configuration artifacts from image content to keep containerized applications portable.
secretUsed to store sensitive data such as passwords and tokens. This data can be mounted as files or exposed as environment variables.
persistentVolumeClaimRepresents a user's request for, and a binding to, a persistent volume.

Creating a Pod with Volume Using kubectl run

Basic Steps

  1. Define the Volume: Determine the type and characteristics of the volume you need based on your application requirements.
  2. Create and Edit YAML Configuration: While kubectl run generates a pod specification in YAML, it might be necessary to edit the specification to attach volumes manually.
  3. Apply the Configuration: Use kubectl commands 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:

bash
kubectl run mypod --image=nginx --restart=Never --dry-run=client -o yaml > mypod.yaml

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:

yaml
1apiVersion: v1
2kind: Pod
3metadata:
4  name: mypod
5spec:
6  containers:
7    - name: mycontainer
8      image: nginx
9      volumeMounts:
10        - mountPath: "/data"
11          name: myvol
12  volumes:
13    - name: myvol
14      emptyDir: {}

In this configuration:

  • volumeMounts refers 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:

bash
kubectl apply -f mypod.yaml

Verify the Pod and Volume

To verify that the pod is running and the volume is functioning, run:

bash
kubectl get pods
kubectl describe pod mypod

Inside the container, you can execute a shell to see the mounted volume:

bash
1kubectl exec -it mypod -- /bin/sh
2# Inside the shell
3cd /data
4ls

Advanced Topics

Persistent Volumes vs. Ephemeral Volumes

  • Ephemeral Volumes like emptyDir are 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.


Course illustration
Course illustration

All Rights Reserved.