{"id":16945389,"url":"https://github.com/felipecruz91/k8s-postgresql-persistent-volume","last_synced_at":"2025-04-13T22:13:31.218Z","repository":{"id":54490781,"uuid":"238537577","full_name":"felipecruz91/k8s-postgresql-persistent-volume","owner":"felipecruz91","description":"Deploy a PostgreSQL database with a PV and PVC","archived":false,"fork":false,"pushed_at":"2020-02-11T10:18:14.000Z","size":7,"stargazers_count":7,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-13T22:13:26.518Z","etag":null,"topics":["kubernetes","persistentvolume","persistentvolumeclaim","postgresql"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/felipecruz91.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-02-05T20:03:36.000Z","updated_at":"2025-02-18T16:44:31.000Z","dependencies_parsed_at":"2022-08-13T17:40:14.806Z","dependency_job_id":null,"html_url":"https://github.com/felipecruz91/k8s-postgresql-persistent-volume","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/felipecruz91%2Fk8s-postgresql-persistent-volume","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/felipecruz91%2Fk8s-postgresql-persistent-volume/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/felipecruz91%2Fk8s-postgresql-persistent-volume/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/felipecruz91%2Fk8s-postgresql-persistent-volume/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/felipecruz91","download_url":"https://codeload.github.com/felipecruz91/k8s-postgresql-persistent-volume/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248788912,"owners_count":21161728,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["kubernetes","persistentvolume","persistentvolumeclaim","postgresql"],"created_at":"2024-10-13T21:22:17.380Z","updated_at":"2025-04-13T22:13:31.106Z","avatar_url":"https://github.com/felipecruz91.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# k8s-postgresql-persistent-volume\n\n## Create Secret\n\n```shell\n$ kubectl create secret generic postgres-secret \\\n--from-literal=POSTGRES_USER=postgresadmin \\\n--from-literal=POSTGRES_PASSWORD=admin123 \\\n--dry-run -o yaml \u003e secrets.yaml\n ```\n\nCreate the secret:\n\n ```shell\n$ kubectl apply -f secrets.yaml\nsecret/postgres-secret created\n```\n\nDescribe the secret:\n\n```shell\n$ kubectl describe secret postgres-secret\nName:         postgres-secret\nNamespace:    default\nLabels:       \u003cnone\u003e\nAnnotations:\nType:         Opaque\n\nData\n====\nPOSTGRES_PASSWORD:  8 bytes\nPOSTGRES_USER:      13 bytes\n```\n\n## Create Persistent Volume\n\nAs you all know that Docker containers are ephemeral in nature. All the data which is generated by or in the container will be lost after termination of the container instance.\n\nTo save the data, we will be using Persistent volumes and persistent volume claim resource within Kubernetes to store the data on persistent storages.\n\nHere, we are using local directory/path as Persistent storage resource (/opt/postgres-data)\n\nFile: postgres-storage.yaml\n\n```\nkind: PersistentVolume\napiVersion: v1\nmetadata:\n  name: postgres-pv-volume\n  labels:\n    type: local\nspec:\n  storageClassName: manual\n  capacity:\n    storage: 5Gi\n  accessModes:\n    - ReadWriteMany\n  hostPath:\n    path: \"/opt/postgres-data\"\n```\n\n```\n$ k apply -f postgres-storage.yaml\n```\n\n## Create Persistent Volume Claim\n\nFile: postgres-pvc.yaml\n```\nkind: PersistentVolumeClaim\napiVersion: v1\nmetadata:\n  name: postgres-pv-claim\nspec:\n  storageClassName: manual\n  accessModes:\n    - ReadWriteMany\n  resources:\n    requests:\n      storage: 5Gi\n```\n\n```\n$ k apply -f postgres-pvc.yaml\n```      \n\n## PostgreSQL Deployment\n\nPostgreSQL manifest for deployment of PostgreSQL container uses PostgreSQL 10.4 image. It is using PostgreSQL configuration like username, password, database name from the configmap that we created earlier. It also mounts the volume created from the persistent volumes and claims to make PostgreSQL container’s data persists.\n\n```\n$ k apply -f postgres-deployment.yaml\n```      \n\n## PostgreSQL Service\n\nTo access the deployment or container, we need to expose PostgreSQL service. Kubernetes provides different type of services like ClusterIP, NodePort and LoadBalancer.\n\nWith ClusterIP we can access PostgreSQL service within Kubernetes. NodePort gives the ability to expose service endpoint on the Kubernetes nodes. For accessing PostgreSQL externally, we need to use a Load Balancer service type which exposes the service externally.\n\n```\n$ k expose deployment postgres \\\n--type=NodePort \\\n--target-port=5432 \\\n--dry-run -o yaml \u003e postgres-service.yaml\n```\n\n## Install psql CLI\n\n```\n$ sudo apt-get install -y postgresql-client\n```\n\n## Connect to PostgreSQL\n\nFor connecting PostgreSQL, we need to get the Node port from the service deployment.\n\n```\n$ k get svc postgres\nNAME       TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE\npostgres   NodePort   10.100.34.227   \u003cnone\u003e        5432:30223/TCP   6m24s\n```\n\nWe need to use port 30223 to connect to PostgreSQL from machine/node present in kubernetes cluster with credentials given in the configmap earlier.\n\n```\n$ psql -h localhost -U postgresadmin --password -p 30223\n```\n\n## Delete resources\n\nIndividually:\n\n```\n$ kubectl delete service postgres \n$ kubectl delete deployment postgres\n$ kubectl delete secret postgres-secret\n$ kubectl delete pvc postgres-pv-claim\n$ kubectl delete pv postgres-pv-volume\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffelipecruz91%2Fk8s-postgresql-persistent-volume","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffelipecruz91%2Fk8s-postgresql-persistent-volume","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffelipecruz91%2Fk8s-postgresql-persistent-volume/lists"}