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 usingstatefulsets
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
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
相关文章
开源Docker工具
Docker 社区非常活跃,每天都会出现许多有用的工具,时时关注社区中发生的所有创新是很困难的。下面的有几款有趣又实用的 Docker 工具,这些工具提升了我的工作效率,减少了原本需要手工完成的工作。
technology radar
Recently, ThoughtWorks has released the latest issue of technology radar,...
Kubernetes Knative
本文通过 Kubernetes Event Source 示例介绍一下 Knative Eventing 中如何获取事件,并且将事件传递给 Serving 进行消费。其中事件处理基于...