🛡️
CTHFM: Kubernetes
  • Welcome
  • Kubernetes Fundamentals
    • Kubernetes Components
      • Kubernetes Master Node
      • Worker Nodes
      • Pods
      • Service
      • ConfigMaps and Secrets
      • Namespaces
      • Deployments
      • ReplicaSets
      • Jobs and CronJobs
      • Horizontal Pod Autoscaler (HPA)
      • Kubernetes Ports and Protocols
    • Kubectl
      • Installation and Setup
      • Basic Kubectl
      • Working With Pods
      • Deployments and ReplicaSets
      • Services and Networking
      • ConfigMaps and Secrets
      • YAML Manifest Management
      • Debugging and Troubleshooting
      • Kubectl Scripting: Security
      • Customizing Kubectl
      • Security Best Practices
      • Common Issues
      • Reading YAML Files
    • MiniKube
      • Intro
      • Prerequisites
      • Installation MiniKube
      • Starting MiniKube
      • Deploy a Sample Application
      • Managing Kubernetes Resources
      • Configuring MiniKube
      • Persistent Storage in Minikube
      • Using Minikube for Local Development
      • Common Pitfalls
      • Best Practices
  • Kubernetes Logging
    • Kubernetes Logging Overview
    • Audit Logs
    • Node Logs
    • Pod Logs
    • Application Logs
    • Importance of Logging
    • Types of Logs
    • Collecting and Aggregating Logs
    • Monitoring and Alerting
    • Log Parsing and Enrichment
    • Security Considerations in Logging
    • Best Practices
    • Kubernetes Logging Architecture
  • Threat Hunting
    • Threat Hunting Introduction
    • What Makes Kubernetes Threat Hunting Unique
    • Threat Hunting Process
      • Hypothesis Generation
      • Investigation
      • Identification
      • Resolution & Follow Up
    • Pyramid of Pain
    • Threat Frameworks
      • MITRE Containers Matrix
        • MITRE Att&ck Concepts
        • MITRE Att&ck Data Sources
        • MITRE ATT&CK Mitigations
        • MITRE Att&ck Containers Matrix
      • Microsoft Threat for Kubernetes
    • Kubernetes Behavioral Analysis and Anomaly Detection
    • Threat Hunting Ideas
    • Threat Hunting Labs
  • Security Tools
    • Falco
      • Falco Overview
      • Falco's Architecture
      • Runtime Security Explained
      • Installation and Setup
      • Falco Rules
      • Tuning Falco Rules
      • Integrating Falco with Kubernetes
      • Detecting Common Threats with Falco
      • Integrating Falco with Other Security Tools
      • Automating Incident Response with Falco
      • Managing Falco Performance and Scalability
      • Updating and Maintaining Falco
      • Real-World Case Studies and Lessons Learned
      • Labs
        • Deploying Falco on a Kubernetes Cluster
        • Writing and Testing Custom Falco Rules
        • Integrating Falco with a SIEM System
        • Automating Responses to Falco Alerts
    • Open Policy Agent (OPA)
      • Introduction to Open Policy Agent (OPA)
      • Getting Started with OPA
      • Rego
      • Advanced Rego Concepts
      • Integrating OPA with Kubernetes
      • OPA Gatekeeper
      • Policy Enforcement in Microservices
      • OPA API Gateways
      • Introduction to CI/CD Pipelines and Policy Enforcement
      • External Data in OPA
      • Introduction to Decision Logging
      • OPA Performance Monitoring
      • OPA Implementation Best Practices
      • OPA Case Studies
      • OPA Ecosystem
    • Kube-Bench
    • Kube-Hunter
    • Trivy
    • Security Best Practices and Documentation
      • RBAC Good Practices
      • Official CVE Feed
      • Kubernetes Security Checklist
      • Securing a Cluster
      • OWASP
  • Open Source Tools
    • Cloud Native Computing Foundation (CNCF)
      • Security Projects
  • Infrastructure as Code
    • Kubernetes and Terraform
      • Key Focus Areas for Threat Hunters
      • Infastructure As Code: Kubernetes
      • Infrastructure as Code (IaC) Basics
      • Infastructure As Code Essential Commands
      • Terraform for Container Orchestration
      • Network and Load Balancing
      • Secrets Management
      • State Management
      • CI/CD
      • Security Considerations
      • Monitoring and Logging
      • Scaling and High Availability
      • Backup and Disaster Recovery
    • Helm
      • What is Helm?
      • Helm Architecture
      • Write Helm Charts
      • Using Helm Charts
      • Customizing Helm Charts
      • Customizing Helm Charts
      • Building Your Own Helm Chart
      • Advanced Helm Chart Customization
      • Helm Repositories
      • Helm Best Practices
      • Helmfile and Continuous Integration
      • Managing Secrets with Helm and Helm Secrets
      • Troubleshooting and Debugging Helm
      • Production Deployments
      • Helm Case Studies
Powered by GitBook
On this page
  • Persistent Storage in Minikube
  • 1. Understanding Persistent Storage in Kubernetes
  • 2. Creating Persistent Volumes in Minikube
  • Step 2: Apply the Persistent Volume
  • 3. Creating Persistent Volume Claims
  • Step 1: Define a Persistent Volume Claim
  • Step 2: Apply the Persistent Volume Claim
  • 4. Using Persistent Storage in a Pod
  • Step 1: Define a Pod that Uses the PVC
  • Step 2: Apply the Pod Configuration
  • Step 3: Verify the Pod and Storage
  • Step 4: Access the Persistent Storage
  • 5. Cleaning Up Resources
  • Step 1: Delete the Pod
  • Step 2: Delete the Persistent Volume Claim
  • Step 3: Delete the Persistent Volume
  • Conclusion
  1. Kubernetes Fundamentals
  2. MiniKube

Persistent Storage in Minikube

Persistent Storage in Minikube

In this lesson, we will explore how to manage persistent storage in Minikube. Persistent storage is crucial for applications that need to retain data across Pod restarts or between different versions of your application. You’ll learn how to create Persistent Volumes (PVs) and Persistent Volume Claims (PVCs) in Minikube, and how to use them in your Kubernetes applications. By the end of this lesson, you’ll understand how to implement and manage persistent storage in your Minikube environment.

1. Understanding Persistent Storage in Kubernetes

Kubernetes uses two main abstractions to manage storage:

  • Persistent Volume (PV): A piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned using Storage Classes. PVs are independent of the Pod lifecycle and can exist beyond the life of a Pod.

  • Persistent Volume Claim (PVC): A request for storage by a user. PVCs specify the size, access modes, and other requirements for the storage they request. Kubernetes binds a PVC to a suitable PV that meets the criteria.

2. Creating Persistent Volumes in Minikube

Minikube provides local storage by default, which can be used to create PVs. These PVs can then be used by your applications to persist data.

Step 1: Define a Persistent Volume

Create a YAML file named pv.yaml with the following content:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: minikube-pv
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"
  • Explanation:

    • capacity.storage: Specifies the amount of storage available (10Gi in this case).

    • accessModes: Defines how the volume can be accessed. ReadWriteOnce means the volume can be mounted as read-write by a single node.

    • hostPath.path: Specifies the path on the Minikube node where the data will be stored.

Step 2: Apply the Persistent Volume

Use the following command to create the Persistent Volume:

kubectl apply -f pv.yaml
  • Explanation:

    • kubectl apply -f: Applies the configuration from the specified YAML file.

Step 3: Verify the Persistent Volume

Check that the Persistent Volume has been created:

kubectl get pv
  • Expected Output:

    • You should see the minikube-pv listed with a status of Available, indicating that it’s ready to be used by a PVC.

3. Creating Persistent Volume Claims

Once you have a Persistent Volume, you need to create a Persistent Volume Claim to request storage for your application.

Step 1: Define a Persistent Volume Claim

Create a YAML file named pvc.yaml with the following content:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: minikube-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  • Explanation:

    • accessModes: Requests the same access mode as defined in the PV (ReadWriteOnce).

    • resources.requests.storage: Specifies the amount of storage requested (5Gi in this case).

Step 2: Apply the Persistent Volume Claim

Use the following command to create the PVC:

kubectl apply -f pvc.yaml
  • Explanation:

    • This command creates the PVC based on the configuration in the YAML file.

Step 3: Verify the Persistent Volume Claim

Check that the PVC has been created and bound to the PV:

kubectl get pvc
  • Expected Output:

    • You should see minikube-pvc with a status of Bound, indicating that it has been successfully matched to a PV.

4. Using Persistent Storage in a Pod

Now that you have a PV and PVC, you can use them in your Pods to persist data.

Step 1: Define a Pod that Uses the PVC

Create a YAML file named pod-with-pvc.yaml with the following content:

apiVersion: v1
kind: Pod
metadata:
  name: test-pod
spec:
  containers:
  - name: busybox
    image: busybox
    command: [ "sleep", "3600" ]
    volumeMounts:
    - mountPath: "/mnt/storage"
      name: storage
  volumes:
  - name: storage
    persistentVolumeClaim:
      claimName: minikube-pvc
  • Explanation:

    • volumeMounts.mountPath: Specifies where the volume will be mounted inside the container (/mnt/storage).

    • volumes.persistentVolumeClaim.claimName: References the PVC that the Pod will use.

Step 2: Apply the Pod Configuration

Create the Pod using the following command:

kubectl apply -f pod-with-pvc.yaml
  • Explanation:

    • This command creates a Pod that uses the PVC to mount the Persistent Volume.

Step 3: Verify the Pod and Storage

Check that the Pod is running:

kubectl get pods
  • Expected Output:

    • You should see test-pod listed with a status of Running.

Step 4: Access the Persistent Storage

You can access the persistent storage inside the Pod by using kubectl exec:

kubectl exec -it test-pod -- sh
  • Navigate to the mounted directory:

    cd /mnt/storage
  • You can now create files or directories in this location, and they will persist across Pod restarts because they are stored in the Persistent Volume.

5. Cleaning Up Resources

Once you’re done experimenting, it’s important to clean up the resources you created to free up system resources.

Step 1: Delete the Pod

kubectl delete pod test-pod

Step 2: Delete the Persistent Volume Claim

kubectl delete pvc minikube-pvc

Step 3: Delete the Persistent Volume

kubectl delete pv minikube-pv

Conclusion

In this lesson, you learned how to configure and manage persistent storage in Minikube using Persistent Volumes and Persistent Volume Claims. You now understand how to create storage that persists across Pod restarts and how to integrate this storage with your Kubernetes applications. This is a crucial skill for running stateful applications in Kubernetes. In the next lesson, we’ll explore advanced Minikube features and how to use Minikube for local development workflows.

PreviousConfiguring MiniKubeNextUsing Minikube for Local Development

Last updated 9 months ago