Day 36 Task: Managing Persistent Volumes in Your Deployment 💥

What are Persistent Volumes in k8s

In Kubernetes, a Persistent Volume (PV) is a piece of storage in the cluster that has been provisioned by an administrator. A Persistent Volume Claim (PVC) is a request for storage by a user. The PVC references the PV, and the PV is bound to a specific node.

Task 1:

  • Create a Persistent Volume using a file on your node.

  • First create a file of volumes in your node

mkdir volumes
cd volumes
pwd

  • Come to your Master server and create a file of persistent volume
# vim persistentvolume.yml
-------------------------------------------------------------------------------
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv-volume
  namespace: django-app
  labels:
    app: mysql
spec:
  storageClassName: manual
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/home/ubuntu/volumes" # This path you should take from Node by creating a volumes folder

kubectl apply -f persistentvolume.yml -n django-app
kubectl get pv -n django-app

  • Now that you've created a persistent volume, it's not enough to simply create it. You need to claim access to that volume by using a persistent volume claim.
# vim persistent-volume-claim.yml
-------------------------------------------------------------------------------
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
  namespace: django-app
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi

kubectl apply -f persistent-volume-claim.yml -n django-app
kubectl get pvc -n django-app
  • Now your volumes and volume-claim should to deployment
# vim deployment.yml
-------------------------------------------------------------------------------
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-deploymnet
  namespace: django-app
  labels:
    app: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - name: mysql
          image: mysql:8
          ports:
            - containerPort: 3306
          env:
            - name: MYSQL_DATABASE
              valueFrom:
                configMapKeyRef:
                  name: mysql-config
                  key: MYSQL_DATABASE
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql-secret
                  key: MYSQL_ROOT_PASSWORD
          voumeMounts:
            - name: mysql-persistent-storage
              mountPath: /var/lib/mysql
      volumes:
        - name: mysql-persistent-storage
          persistentVolumeClaim:
            claimName: mysql-pv-claim

kubectl apply -f deployment.yml -n django-app
kubectl get pods -n django-app

  • Go to your node and use this command
cd volumes
ls

Task 2:

  • Now, I'd like to run a pod by selecting any pod from the node.

  • Go to node and do # docker ps

  • To enter into the pod use this command # docker exec -it <container id> bash

  • Use this command for to enter into you database # mysql -u root -p and enter your password

  • If you want to see your database use this command # show databases;

0
Subscribe to my newsletter

Read articles from Yuvraj Nath Chouhan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Yuvraj Nath Chouhan
Yuvraj Nath Chouhan

🚀 Aspiring DevOps & DevSecOps Engineer | Cloud DevOps (Azure & AWS) | Automation & Security Champion | Passionate Learner🌐 Hi, I’m Yuvraj! 🔥 I'm embarking on an exciting journey in the world of DevOps & DevSecOps, armed with hands-on expertise and a drive to excel. From automating tasks to building robust CI/CD pipelines, I’m dedicated to turning complex challenges into efficient and secure solutions. What I Bring to the Table: ●Linux & Shell Scripting 🖥️ Automating processes and mastering command-line operations. ●Git & Git Branching 🔀 Seamless version control for effective collaboration. ●Build Tools 🔧 (Maven & npm) Crafting stable builds for quality software. ●CI/CD Tools 🚄 (Jenkins, GitHub Actions, Azure DevOps, GitLab CI/CD) Ensuring smooth & automated deployments. ●Security in DevOps 🛡️ Embedding security at every stage to safeguard development processes. ●Artifact Management 📦 (Nexus & Azure Artifacts) Managing software versions efficiently. ●Containerization & Orchestration 🐳⎈ (Docker & Kubernetes) Deploying scalable, agile applications. ●Infrastructure as Code (IaC) 📜 (Terraform & Ansible) Automating environment provisioning. ●Monitoring & Troubleshooting 🔍 Keeping systems in check with proactive issue resolution. ●Real-world & Corporate Projects 🌟 Hands-on experience with practical implementations. ✨ Always eager to learn, collaborate, and innovate! ✨ I’m ready to transform my academic knowledge and project experiences into real-world impact. Let’s connect and explore how we can drive meaningful change together! 🤝