kubernetes - Persistence volume example.

This is how we add persistence storage to container that can be used by application running in container. You can create volume like drive in windows OS which can be treated by Kubernetes  like hard disk. This volume can be mounted to pod to create storage with containers running in cluster.

apiVersion: v1
kind: PersistentVolume
metadata:
  name: myapp-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/Users/hemraj/rnd/kubes/20200131"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: myapp-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---
apiVersion: v1
kind: Pod
metadata:
  name: my-httpd
  labels:
    app: my-httpd
spec:
  volumes:
    - name: myapp-pv-storage
      persistentVolumeClaim:
        claimName: myapp-pv-claim
  containers:
    - name: httpd
      image: phpstorm/php-73-apache-xdebug-27
      ports:
        - containerPort: 80
      volumeMounts:
        - mountPath: "/var/www/html"
          name: myapp-pv-storage
---
apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: my-httpd
  ports:
    - protocol: TCP
      name: one
      port: 80
---

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: my-ingress
spec:
  backend:
    serviceName: my-service
    servicePort: 80


---
kind: Service
apiVersion: v1
metadata:
  name: ingress-nginx
  namespace: ingress-nginx
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
spec:
  externalTrafficPolicy: Local
  type: LoadBalancer
  selector:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
  ports:
    - name: http
      port: 80
      protocol: TCP
      targetPort: http
    - name: https
      port: 443
      protocol: TCP
      targetPort: https

 

Tags