Skip to content

Commit 1d6d78e

Browse files
committed
Update docs
1 parent 09d2838 commit 1d6d78e

File tree

1 file changed

+37
-35
lines changed

1 file changed

+37
-35
lines changed

README.md

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ PostgreSQL on Kubernetes
44
## Prerequisites
55

66
```bash
7-
# install minikube to create a single-node cluster
8-
brew install minikube
7+
$ # install minikube to create a single-node cluster
8+
$ brew install minikube
99

10-
# start cluster using VMs
11-
minikube start --vm=true
10+
$ # start cluster using VMs
11+
$ minikube start --vm=true
1212

13-
# create a custom namespace and context
14-
kubectl create namespace postgres
15-
kubectl config set-context postgres --namespace postgres --cluster minikube --user minikube
16-
kubectl config use-context postgres
13+
$ # create a custom namespace and context
14+
$ kubectl create namespace postgres
15+
$ kubectl config set-context postgres --namespace postgres --cluster minikube --user minikube
16+
$ kubectl config use-context postgres
1717
```
1818

1919
## Persistent Volumes
@@ -23,13 +23,13 @@ In order to persist the data stored in PostgreSQL it’s necessary to create [Pe
2323
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.
2424

2525
```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
3030

31-
# list persistent volumes
32-
kubectl get pv
31+
$ # list persistent volumes
32+
$ kubectl get pv
3333
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
3434
pv-postgresql-0 1Gi RWO Retain Available default 9s
3535
pv-postgresql-1 1Gi RWO Retain Available default 7s
@@ -41,11 +41,11 @@ pv-postgresql-2 1Gi RWO Retain Available
4141
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.
4242

4343
```bash
44-
# create headless service
45-
kubectl apply -f svc.yaml
44+
$ # create headless service
45+
$ kubectl apply -f svc.yaml
4646

47-
# describe headless service
48-
kubectl describe svc postgresql-svc
47+
$ # describe headless service
48+
$ kubectl describe svc postgresql-svc
4949
Name: postgresql-svc
5050
Namespace: postgres
5151
Labels: sfs=postgresql-sfs
@@ -68,11 +68,11 @@ Events: <none>
6868
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.
6969

7070
```bash
71-
# create secret from literal
72-
kubectl create secret generic postgresql-secrets --from-literal=POSTGRES_PASSWORD=tes6Aev8
71+
$ # create secret from literal
72+
$ kubectl create secret generic postgresql-secrets --from-literal=POSTGRES_PASSWORD=tes6Aev8
7373

74-
# describe secret
75-
kubectl describe secrets postgresql-secrets
74+
$ # describe secret
75+
$ kubectl describe secrets postgresql-secrets
7676
Name: postgresql-secrets
7777
Namespace: postgres
7878
Labels: <none>
@@ -90,23 +90,23 @@ POSTGRES_PASSWORD: 8 bytes
9090
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.
9191

9292
```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
9595

96-
# list stateful sets
97-
kubectl get statefulsets
96+
$ # list stateful sets
97+
$ kubectl get statefulsets
9898
NAME READY AGE
9999
postgresql-sfs 3/3 16s
100100

101-
# list pods
102-
kubectl get pods
101+
$ # list pods
102+
$ kubectl get pods
103103
NAME READY STATUS RESTARTS AGE
104104
postgresql-sfs-0 1/1 Running 0 86s
105105
postgresql-sfs-1 1/1 Running 0 83s
106106
postgresql-sfs-2 1/1 Running 0 80s
107107

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
110110

111111
PostgreSQL Database directory appears to contain a database; Skipping initialization
112112

@@ -117,8 +117,8 @@ PostgreSQL Database directory appears to contain a database; Skipping initializa
117117
2021-08-04 08:19:50.838 UTC [26] LOG: database system was shut down at 2021-08-03 14:33:17 UTC
118118
2021-08-04 08:19:50.843 UTC [1] LOG: database system is ready to accept connections
119119

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
122122
Name: postgresql-svc
123123
Namespace: postgres
124124
Labels: sfs=postgresql-sfs
@@ -141,7 +141,7 @@ Events: <none>
141141
You are able to directly connect to PostgreSQL by starting `bash` within a particular pod.
142142

143143
```bash
144-
kubectl exec -it postgresql-sfs-0 -- bash
144+
$ kubectl exec -it postgresql-sfs-0 -- bash
145145
root@postgresql-sfs-0:/# PGPASSWORD=tes6Aev8 psql -U postgres
146146
psql (13.3 (Debian 13.3-1.pgdg100+1))
147147
Type "help" for help.
@@ -154,7 +154,9 @@ exit
154154
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.
155155

156156
```bash
157-
kubectl run -it --rm pg-psql --image=postgres:13.3 --restart=Never --env="PGPASSWORD=tes6Aev8" -- psql -h postgresql-svc.postgres.svc.cluster.local -U postgres
157+
$ kubectl run -it --rm pg-psql --image=postgres:13.3 --restart=Never \
158+
--env="PGPASSWORD=tes6Aev8" -- \
159+
psql -h postgresql-svc.postgres.svc.cluster.local -U postgres
158160
If you don't see a command prompt, try pressing enter.
159161
postgres=# \q
160162
pod "pg-psql" deleted
@@ -163,7 +165,7 @@ pod "pg-psql" deleted
163165
To check that the DNS hostname works we deploy a busybox instance.
164166
165167
```bash
166-
kubectl run -it --rm busybox --image=busybox --restart=Never -- sh
168+
$ kubectl run -it --rm busybox --image=busybox --restart=Never -- sh
167169
If you don't see a command prompt, try pressing enter.
168170
/ # ping postgresql-svc.postgres.svc.cluster.local
169171
PING postgresql-svc.postgres.svc.cluster.local (172.17.0.3): 56 data bytes

0 commit comments

Comments
 (0)