Learn how to deploy Postgres in Kubernetes with ConfigMap. Our PostgreSQL Support team is here to help you with your questions and concerns.
Deploy Postgres in Kubernetes with ConfigMap
Many of our customers run into trouble when deploying PostgreSQL in a Kubernetes environment. According to our experts, this comes with its own set of challenges. We need to choose the right deployment strategy to avoid trouble.
Today, we are going to explore a popular deployment option that uses ConfigMap with a PersistentVolume. This approach simplifies deployment and boosts security by keeping secrets separate from application code.
ConfigMaps in Kubernetes keeps data separate from the code. In other words, it prevents secrets from being exposed in our application’s source code. With ConfigMaps, we can streamline the deployment and updating of applications. Let’s take a quick look at deploying PostgreSQL using ConfigMap.
- To begin with, paste the following code into the terminal. It will create a ConfigMap named “postgres-config”:
cat <<EOF > postgres-config.yaml apiVersion: v1 kind: ConfigMap metadata: name: postgres-config labels: app: postgres data: POSTGRES_DB: postgresdb POSTGRES_USER: admin POSTGRES_PASSWORD: psltest EOF
Copy CodeWe can change the values of POSTGRES_DB, POSTGRES_USER, and POSTGRES_PASSWORD as per our needs.
- Then, run this command to create the ConfigMap for our PostgreSQL deployment:
kubectl apply -f postgres-config.yaml
Copy Code - Then, verify the ConfigMap’s presence using:
kubectl get configmap
Copy Code - Now it is time for deploying a PersistentVolume and PersistentVolumeClaim. We can use the manifest file “postgres-pvc-pv.yaml” and apply it:
kubectl apply -f postgres-pvc-pv.yaml
Copy CodeThen, check if the PVC is bound to the PV:
kubectl get pvc
Copy Code - Now, create a new PostgreSQL deployment with this command:
kubectl apply -f postgres-deployment.yaml
Copy CodeWe can ensure successful creation by checking deployments and related objects:
kubectl get deployments
Copy Code - Then, run this command to create a service allowing connection to psql:
kubectl apply -f postgres-service.yaml
Copy CodeWe can list all objects for verification:
kubectl get all
Copy Code - Finally, connect to the PostgreSQL pod directly from the host command line:
kubectl exec -it [pod-name] -- psql -h localhost -U admin --password -p 5432 postgresdb
Copy Code
Alternative Method: Configuring PostgreSQL with ConfigMaps and Secrets
ConfigMaps and Secrets offer options for storing environment parameters securely. We can use the provided manifest files to create a ConfigMap named “postgres” and a Secret for superuser credentials.
apiVersion: v1
kind: ConfigMap
metadata:
name: postgres
data:
POSTGRES_DB: myapp_production
Copy Code
Kubernetes Secrets can be expressed via a manifest. However, our experts do not recommend this.
We can create a secret resource named postgres to store a superuser username and password as seen here:
kubectl create secret generic postgres \
--from-literal=POSTGRES_USER="root" \
--from-literal=POSTGRES_PASSWORD="secret-password"
Copy Code
Then, apply the manifests:
kubectl apply -f postgres-configmap.yaml
kubectl apply -f postgres-secrets.yaml
Copy Code
[Need assistance with a different issue? Our team is available 24/7.]
Conclusion
In brief, our Support Experts demonstrated how to deploy Postgres in Kubernetes with ConfigMap.
PREVENT YOUR SERVER FROM CRASHING!
Never again lose customers to poor server speed! Let us help you.
Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.
0 Comments