Kubernetes is a great place to run many types of workloads that require automation and scale. Due to the particular requirements of stateful services–security, reliability, performance– they benefit in particular from this automation, enabling teams to move faster to market without sacrificing reliability.

This blog will provide steps to run PostgreSQL database on Windows Kubernetes (development environment).

If you want to run PostgreSQL database on Kubernetes (product environment),it is better to setup PostgreSQL cluster using statefulsets of Kubernetes.

  • Install Docker Desktop on Windows
  • Create docker volume
  • Create Config Maps
  • Create Persistent Storage Volume
  • Create Deployment
  • Create Service

Install Docker Desktop on Windows

Read docker install docs

Create docker volume

docker volume create postgresdata

volume name: postgresdata

If possible, avoid volume mounts from the Windows host, and instead mount on the Linux VM, or use a data volume (named volume) or data container. There are a number of issues with using host-mounted volumes and network paths for database files. See Volume mounts from host paths use a nobrl option to override database locking.

Create Config Maps

We will be using config maps for storing PostgreSQL configurations

postgres-configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: postgres-config
  labels:
    app: postgres
data:
  POSTGRES_DB: db
  POSTGRES_USER: user
  POSTGRES_PASSWORD: pwd

Create configmap

kubectl create -f postgres-configmap.yaml

Create Persistent Storage Volume

postgres-storage.yaml

kind: PersistentVolume
apiVersion: v1
metadata:
  name: postgres-pv-volume
  labels:
    type: local
    app: postgres
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: postgresdata
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: postgres-pv-claim
  labels:
    app: postgres
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi

using volume name(postgresdata) as path

Create storage

kubectl create -f postgres-storage.yaml

Create Deployment

postgres-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
spec:
  selector:
    matchLabels:
      app: postgres
  replicas: 1
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres
          image: postgres:10.4
          imagePullPolicy: "IfNotPresent"
          ports:
            - containerPort: 5432
          envFrom:
            - configMapRef:
                name: postgres-config
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgredb
      volumes:
        - name: postgredb
          persistentVolumeClaim:
            claimName: postgres-pv-claim

Create Deployment

kubectl create -f postgres-deployment.yaml

Create Service

postgres-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: postgres
  labels:
    app: postgres
spec:
  type: NodePort
  ports:
    - port: 5432
  selector:
    app: postgres

Create Service

kubectl create -f postgres-service.yaml

For connecting PostgreSQL, we need to get the Node port from the service deployment.

kubectl get svc postgres
标签: Kubernetes
⇠ Google AI 乳腺癌检测准确率胜过人类专家 顶级免费安全测试工具 ⇢

GFW VPN

提供vpn服务,针对中国互联网用户,完全可以突破GFW的封锁. 经过了长期测试,运行非常的稳定.

Send Mail

注册账号