Skip to content

Instantly share code, notes, and snippets.

@raj1rana
Created May 20, 2022 09:20
Show Gist options
  • Save raj1rana/6f18e25b2c720e78a43bf41b20fa3cbe to your computer and use it in GitHub Desktop.
Save raj1rana/6f18e25b2c720e78a43bf41b20fa3cbe to your computer and use it in GitHub Desktop.
create a presistence volume in k8

to create a volume in k8 use the following snippet

---
apiVersion: v1
kind: PersistentVolume
metadata:
  annotations:
    generated.by: 8gwifi.org
  name: pvo.0
  namespace: default
spec:
  accessModes:
  - ReadWriteOnce
  capacity:
    storage: 10Gi
  hostPath:
    path: wordpress
    type: Directory
  persistentVolumeReclaimPolicy: Retain

then you need to create a PV claim

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  annotations:
    generated.by: 8gwifi.org
  name: claimname.0
  namespace: default
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  volumeName: pvo.0

then you need to mount it to the pod

apiVersion: apps/v1
kind: Deployment
metadata:
  name: Wordpress-deployment
spec:
  selector:
    matchLabels:
      type: webapp
  replicas: 2
  template:
    metadata:
      labels:
        type: webapp
    spec:
      volumes: # this is the volumes condifuration used in case you want to attach multiple PV's 
        - name: wordpresspv # name of the PV 
          persistentVolumeClaim: # PV claim 
            claimName: wordpress-claim # PV claim name 
      containers:
      - name: wordpress
        image: wordpress
        ports:
          - containerPort: 80
            protocol: tcp
        volumeMounts: #herer we will mount the volume for container 
          - mountPath: /var/www/html #path where to mount
            name: wordpresspv #name of your pv 
        env:
          - name: WORDPRESS_DB_HOST
            valueFrom:
              secretKeyRef:
                name: wordpress_secret
                key: WORDPRESS_DB_HOST
          - name: WORDPRESS_DB_USER
            valueFrom:
              secretKeyRef:
                name: wordpress_secret
                key: WORDPRESS_DB_USER
          - name: WORDPRESS_DB_PASSWORD
            valueFrom:
              secretKeyRef:
                name: wordpress_secret
                key: WORDPRESS_DB_PASSWORD
          - name: WORDPRESS_DB_NAME
            valueFrom:
              secretKeyRef:
                name: wordpress_secret
                key: WORDPRESS_DB_NAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment