How to Create and Run a Kubernetes and Postgres StatefulSet App

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

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.

Prerequisites

  • Installation of minikube is required to run Kubernetes locally. Execute the minikube start command to obtain the currently installed version of Kubernetes in stdout and start the machine. Execute the sudo command 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

  • The configMap is 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 .yaml or .yml file extension in a terminal-based editor, preferably an IDE with a YAML syntax-checker extension:

pg-config.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
apiVersion: v1

kind
: ConfigMap

metadata
:

name
: pgconfig

labels
:

app
: Postgres

## The PostgreSQL credentials

data
:

POSTGRES_DB
: ordb

POSTGRES_USER
: objectrocket

POSTGRES_PASSWORD
: orkb123

Now create the resource in Kubernetes by executing the following command:

1
2
3
linux@linux-NECq:~$ kubectl create -f pg-config.yaml

configmap/Postgres-config created
  • NOTE: Remember that whenever the terminal throws an error into the command stating it needs permission of the root to access, simply write sudo before the command to grant privileges to the root access.
  • Keep in mind that kubectl is 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:

pg-service.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
apiVersion: v1

kind
: Service

metadata
:

name
: pgservice

labels
:

app
: Postgres

spec
:

ports
:

- port
: 5432

name
: pgservice

clusterIP
: None

selector
:

app
: Postgres

Now execute the following command to create the resource:

1
2
3
linux@linux-NECq:~$ kubectl create -f pg-service.yaml

service/pgservice created
  • Finally, execute the following command to create an stateful application where the deployment is organized and has stable storage and network identifiers:

pg-stateful.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
apiVersion: apps/v1

kind
: StatefulSet

metadata
:

name
: pgstateful

spec
:

serviceName
: "Postgres"

replicas
: 2

selector
:

matchLabels
:

app
: Postgres

template
:

metadata
:

labels
:

app
: Postgres

spec
:

containers
:

- name
: Postgres

image
: Postgres:latest

envFrom
:

- configMapRef
:

name
: pgconfig

ports
:

- containerPort
: 5432

name
: ordb

## this will persist the data of the Postgres database

volumeMounts
:

- name
: ordb

mountPath
: /var/lib/PostgreSQL/data

subPath
: Postgres

volumeClaimTemplates
:

- metadata
:

name
: ordb

spec
:

accessModes
: [ "ReadWriteOnce" ]

storageClassName
: gp2

resources
:

requests
:

storage
: 3Gi

Using the same command again, run and create the resource as follows:

1
2
3
linux@linux-NECq:~$ kubectl create -f pg-stateful.yaml

statefulset.apps/Postgres-demo created

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:

1
2
3
4
5
linux@linux-NECq:~$ kubectl get service pgservice

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:

1
psql -h 10.101.91.220 -U objectrocket -password -p 30563 ordb

Enter the password and press enter at the system prompt. Access to the psql command line interface should now be granted.

Conclusion

This tutorial explained how to create and run a Kubernetes and Postgres StatefulSet application. The article specifically covered how to create theconfigMap 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

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.