Skip to content

Instantly share code, notes, and snippets.

@senthilsweb
Last active October 25, 2021 03:18
Show Gist options
  • Save senthilsweb/4f7dd052e03444939ea944c4847585d1 to your computer and use it in GitHub Desktop.
Save senthilsweb/4f7dd052e03444939ea944c4847585d1 to your computer and use it in GitHub Desktop.
K8s KinD Deployment

Introduction

PostgreSQL Database and Web application (PGAdmin) deployed in Kubernetes cluster using KinD. Access to the web application is exposed using nodeport @ http://localhost:8081

Follow step # 1 and step # 3 two mandatory steps to setup the K8s cluster and deployment. Persistent volume setup is optional (Step # 2).

  1. Create KinD cluster
  2. Apply Persistent volume
  3. Deploy application

Commands (Main Path)

kind create cluster --name zypress-cluster --config pvs-kind-cluster.yaml
kubectl apply -f pvs-deployment.yaml

Commands (Alternative Path)

Follow the order

kind create cluster --name zypress-cluster --config pvs-kind-cluster.yaml
kubectl apply -f pv.yaml
kubectl apply -f pvs-deployment.yaml

Commands (Miscellaneous)

kubectl get all -o wide 
kubectl get po -o wide 
kubectl delete -f pvs-deployment.yaml
kind delete cluster --name zypress-cluster
kubectl cluster-info
kubectl get pv pvc-eeead8e3-943d-45c2-b532-363dfa70820c_default_ingress-nginx -o yaml > pv.yaml  

References

Port exposes the Kubernetes service on the specified port within the cluster. Other pods within the cluster can communicate with this server on the specified port.

TargetPort is the port on which the service will send requests to, that your pod will be listening on. Your application in the container will need to be listening on this port also.

NodePort exposes a service externally to the cluster by means of the target nodes IP address and the NodePort. NodePort is the default setting if the port field is not specified.

.yaml file(s)

kind create cluster --name zypress-cluster --config pvs-kind-cluster.yaml

kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
  extraPortMappings:
  - containerPort: 30123
    hostPort: 8081
    listenAddress: 127.0.0.1
- role: worker
  extraMounts:                                                                                                                                                                                          
    - hostPath: ./shared-storage                                                                                                                                                                          
      containerPath: /var/local-path-provisioner  
- role: worker
  extraMounts:                                                                                                                                                                                          
  - hostPath: ./shared-storage                                                                                                                                                                          
    containerPath: /var/local-path-provisioner 

kubectl apply -f pvs-deployment.yaml

apiVersion: v1
kind: Pod
metadata:
  name: db
  namespace: default
spec:
  containers:
  - name: db
    image: codydearkland/cmbu-db-app:latest
    ports:
    - containerPort: 5432
    volumeMounts:
        - name: volume
          mountPath: /var/lib/postgresql/data
  volumes: 
  - name: volume
    persistentVolumeClaim:
      claimName: ingress-nginx
---
apiVersion: v1
kind: Pod
metadata:
  name: zypress
  namespace: default
  labels:
    app: zypress
spec:
  containers:
  - name: zypress
    image: dpage/pgadmin4
    env:
    - name: PGADMIN_DEFAULT_EMAIL
      value: "admin@admin.com"
    - name: PGADMIN_DEFAULT_PASSWORD
      value: "admin"
    ports:
    - containerPort: 80
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: ingress-nginx
  labels:
    # insert any desired labels to identify your claim
    app: ingress-nginx
spec:
  storageClassName: standard
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      # The amount of the volume's storage to request
      storage: 2Gi
---
apiVersion: v1
kind: Service
metadata:
  name: ingress-nginx
  namespace: default
  labels:
    app: ingress-nginx
spec:
  type: NodePort
  ports:
  - port: 31234
    nodePort: 30123
    targetPort: 80
    protocol: TCP
  selector:
    app: zypress

kubectl apply -f pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pvc-7bd64e54-7e50-4168-a9e7-89fa8dc282ec
spec:
  accessModes:
  - ReadWriteOnce
  capacity:
    storage: 2Gi
  claimRef:
    apiVersion: v1
    kind: PersistentVolumeClaim
    name: ingress-nginx
    namespace: default
  hostPath:
    path: /var/local-path-provisioner/pvc-7bd64e54-7e50-4168-a9e7-89fa8dc282ec_default_ingress-nginx
    type: DirectoryOrCreate
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - zypress-cluster-worker2
  persistentVolumeReclaimPolicy: Retain
  storageClassName: standard
  volumeMode: Filesystem
status:
  phase: Bound
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment