You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
@@ -23,13 +23,13 @@ In order to persist the data stored in PostgreSQL it’s necessary to create [Pe
23
23
There are two ways to create Persistent Volumes. Either you manually create a volume per replica of PostgreSQL or you configure[dynamic provisioning](https://minikube.sigs.k8s.io/docs/handbook/persistent_volumes/#dynamic-provisioning-and-csi). For simplicity we choose the manual approach first.
24
24
25
25
```bash
26
-
# create 3 persistent volumes
27
-
kubectl apply -f pv-0.yaml
28
-
kubectl apply -f pv-1.yaml
29
-
kubectl apply -f pv-2.yaml
26
+
$# create 3 persistent volumes
27
+
$kubectl apply -f pv-0.yaml
28
+
$kubectl apply -f pv-1.yaml
29
+
$kubectl apply -f pv-2.yaml
30
30
31
-
# list persistent volumes
32
-
kubectl get pv
31
+
$# list persistent volumes
32
+
$kubectl get pv
33
33
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
34
34
pv-postgresql-0 1Gi RWO Retain Available default 9s
35
35
pv-postgresql-1 1Gi RWO Retain Available default 7s
@@ -41,11 +41,11 @@ pv-postgresql-2 1Gi RWO Retain Available
41
41
A[Headless Service](https://kubernetes.io/docs/concepts/services-networking/service/#headless-services) is specified with`clusterIP: None` and omits to use a L4 load balancer. By also defining a selector, the endpoints controller creates`Endpoint` records and also modifies the DNS configuration so that`A` records are returned that point directly to the pods.
42
42
43
43
```bash
44
-
# create headless service
45
-
kubectl apply -f svc.yaml
44
+
$# create headless service
45
+
$kubectl apply -f svc.yaml
46
46
47
-
# describe headless service
48
-
kubectl describe svc postgresql-svc
47
+
$# describe headless service
48
+
$kubectl describe svc postgresql-svc
49
49
Name: postgresql-svc
50
50
Namespace: postgres
51
51
Labels: sfs=postgresql-sfs
@@ -68,11 +68,11 @@ Events: <none>
68
68
PostgreSQL uses environment variables for configuration. The most important one for the official[PostgreSQL Docker image](https://hub.docker.com/_/postgres) is`POSTGRES_PASSWORD`. We utilize[Secrets](https://kubernetes.io/docs/concepts/configuration/secret/) to inject the respective value into the container later on.
A[Stateful Set](https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/) is similar to a_Replica Set_ in a sense that it also handles pods for the configured number of replicas. In contrast to a_Replica Set_, it maintains a sticky identity for each of them. This means they are created in a fixed, sequential order and deleted counterwise. Their network identity is stable as well, what enables us to reference them by the automatically assigned DNS host inside of the cluster.
91
91
92
92
```bash
93
-
# create stateful set with 3 replicas
94
-
kubectl apply -f sfs.yaml
93
+
$# create stateful set with 3 replicas
94
+
$kubectl apply -f sfs.yaml
95
95
96
-
# list stateful sets
97
-
kubectl get statefulsets
96
+
$# list stateful sets
97
+
$kubectl get statefulsets
98
98
NAME READY AGE
99
99
postgresql-sfs 3/3 16s
100
100
101
-
# list pods
102
-
kubectl get pods
101
+
$# list pods
102
+
$kubectl get pods
103
103
NAME READY STATUS RESTARTS AGE
104
104
postgresql-sfs-0 1/1 Running 0 86s
105
105
postgresql-sfs-1 1/1 Running 0 83s
106
106
postgresql-sfs-2 1/1 Running 0 80s
107
107
108
-
# inspect logs of a random pod
109
-
kubectl logs postgresql-sfs-0
108
+
$# inspect logs of a random pod
109
+
$kubectl logs postgresql-sfs-0
110
110
111
111
PostgreSQL Database directory appears to contain a database; Skipping initialization
112
112
@@ -117,8 +117,8 @@ PostgreSQL Database directory appears to contain a database; Skipping initializa
117
117
2021-08-04 08:19:50.838 UTC [26] LOG: database system was shut down at 2021-08-03 14:33:17 UTC
118
118
2021-08-04 08:19:50.843 UTC [1] LOG: database system is ready to accept connections
119
119
120
-
# describe the service to see that 3 endpoints were created automatically
121
-
kubectl describe svc postgresql-svc
120
+
$# describe the service to see that 3 endpoints were created automatically
121
+
$kubectl describe svc postgresql-svc
122
122
Name: postgresql-svc
123
123
Namespace: postgres
124
124
Labels: sfs=postgresql-sfs
@@ -141,7 +141,7 @@ Events: <none>
141
141
You are able to directly connect to PostgreSQL by starting`bash` within a particular pod.
An alternative approach is running a temporary PostgreSQL container and using the included`psql` to connect to one of the database instances. Where the hostname is the automatically created DNS host of the service we deployed earlier. The format of that hostname is:`<service-name>.<namespace>.svc.cluster.local` and will be resolved to a random pod running a database server.