How to Create and Run a Kubernetes and Postgres StatefulSet App
This tutorial will explain how to create and run a Kubernetes and Postgres StatefulSet application. Kubernetes is an open-source program originally designed by Google to automate application deployment and management. StatefulSets are designed for use with distributed systems and stateful applications. StatefulSets are a new feature in Kubernetes, since version 1.5 when StatefulSets replaced PetSets.
Installation of minikube is required to run Kubernetes locally. Execute the
minikube startcommand to obtain the currently installed version of Kubernetes in stdout and start the machine. Execute the
sudocommand in a Linux system if root permission is required.
Docker must be installed on the device in order to pull the Postgres image in the docker container.
Possess a basic proficiency in Docker to create and run a Kubernetes and Postgres StatefulSet application.
Create the Kubernetes resources
configMapis used to store the configurations of the credentials in PostgreSQL. This will pass and inject the resources that are required for file configurations.
Now execute the following code to create a YAML file using the
.yml file extension in a terminal-based editor, preferably an IDE with a YAML syntax-checker extension:
## The PostgreSQL credentials
Now create the resource in Kubernetes by executing the following command:
- NOTE: Remember that whenever the terminal throws an error into the command stating it needs permission of the root to access, simply write
sudobefore the command to grant privileges to the root access.
Keep in mind that
kubectlis a command line interface tool that allows the cluster to interact with the operating device.
Now execute the following command code to create a Postgres service that performs the same function and exposes the other backends:
- port: 5432
Now execute the following command to create the resource:
- Finally, execute the following command to create an stateful application where the deployment is organized and has stable storage and network identifiers:
- name: Postgres
- containerPort: 5432
## this will persist the data of the Postgres database
- name: ordb
accessModes: [ "ReadWriteOnce" ]
Using the same command again, run and create the resource as follows:
Now execute the following command to verify the app and configmap has actually been injected from Kubernetes; here it uses the service resource to display the StatefulSet:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
pgservice NodePort 10.101.91.220 -none- 5432:30563/TCP 23m
Note that the Postgres StatefulSet app is working and can now connect to the port 30563.
Now run the following command to connect to the PostgreSQL database:
Enter the password and press enter at the system prompt. Access to the psql command line interface should now be granted.
This tutorial explained how to create and run a Kubernetes and Postgres StatefulSet application. The article specifically covered how to create the
configMap needed to store the configurations of the credentials in PostgreSQL to pass the resources required for file configurations. The tutorial also explained how to connect to the PostgreSQL database and provided the code to create a YAML file. Remember to add
sudo before the command to grant privileges to the root access whenever the terminal places an error into the command used to create the resource in Kubernetes.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.Get Started