This article only designs Postgres deployment operations, not conceptual knowledge.

Note: Compared with ordinary program references, Postgres is a stateful service because the data it stores needs to be persisted, which determines our choice of K8S-Statefulset Deployment rather than K8S-Deployment.

Prepare in advance

  • K8S cluster, this article uses AWS EKS cluster service
  • A server that can connect to a K8S cluster, with basic applications like Kubectl and Docker already installed

K8S resource file

postgres-namespace.yaml

apiVersion: v1
kind: Namespace
metadata:
  name: postgres
Copy the code

postgres-config.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: postgres-config
  namespace: postgres
  labels:
    app: postgres
data:
  POSTGRES_DB: master
  POSTGRES_USER: dba
  POSTGRES_PASSWORD: pg_pass
Copy the code

The database password here is information sensitive, and it is recommended to use the Secret resource instead of ConfigMap.

postgres-statefulset.yaml

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgres
  namespace: postgres
spec:
  serviceName: "postgres"
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres
          image: Postgres: 9.5
          envFrom:
            - configMapRef:
                name: postgres-config
          ports:
            - containerPort: 5432
              name: postgredb
          volumeMounts:
            - name: postgres-data
              mountPath: /var/lib/postgresql/data
              subPath: postgres
  volumeClaimTemplates:
    - metadata:
        name: postgres-data
      spec:
        accessModes: ["ReadWriteOnce"]
        storageClassName: gp2
        resources:
          requests:
            storage: 4Gi
Copy the code

In this YAML, you can see that we use the ConfigMap we created earlier

And at the bottom of the file, we use gP2’s storage class to help us create volumes automatically.

One benefit of StatefulSet is that it creates a volume for each POD, and if any of the pods are deleted, the volume remains unchanged.

postgres-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: postgres
  namespace: postgres
  labels:
    app: postgres
spec:
  ports:
    - port: 5432
      name: postgres
  type: LoadBalancer
  selector:
    app: postgres
Copy the code

Create a Postgres

Create the postgres command space

kubectl apply -f postgres-namespace.yaml
Copy the code

Create other resources

kubectl apply -f postgres-config.yaml
kubectl apply -f postgres-statefulset.yaml
kubectl apply -f postgres-service.yaml
Copy the code

Since we created the resource in an AWS EKS cluster, after the above command is executed, we can find the storage Volume we created in the AWS Volume.

View the volume information and find that it is actually mounted to a node in the cluster.

Verify the Postgres

We use the following command to view the postgres-service we created

kubectl get pod -n postgres
kubectl get svc -n postgres
Copy the code

If the following output is displayed, our Postgres has been successfully deployed

It is recommended to test the connection to Postgres using the DBeaver database visualization tool, using the host that is the output information in the external-IP column, while the other connection information is in our ConfigMap.