使用glusterfs作为存储的k8s上的postgres



我在k8sglusterfs上部署了一个postgres数据库作为卷。但每次我重新启动我的吊舱时,所有的数据都会丢失。为什么?

apiVersion: apps/v1       
kind: Deployment               
metadata:     
name: postgres-deployment
namespace: gitlab
labels:                                                                  
app: postgres 
spec:
replicas: 1
selector:
matchLabels:
app: postgres
template:
metadata:
labels:
app: postgres
spec:
containers:
- name: postgres
image: postgres:13.1
ports:
- containerPort: 5432
volumeMounts:
- mountPath: /var/lib/postgresql
name: postgres
env:
- name: POSTGRES_USERNAME
valueFrom:
secretKeyRef:
name: gitlab
key: postgres_username
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: gitlab
key: postgres_password
- name: POSTGRES_DB
valueFrom:
secretKeyRef:
name: gitlab
key: postgres_db 
volumes:
- name: postgres
glusterfs:
endpoints: glusterfs-cluster
path: gv

定义PVC和PV对象。请参阅以下内容以供参考。

---
kind: PersistentVolume
apiVersion: v1
metadata:
name: postgres-pv
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 10GB
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
labels:
app: postgres
name: postgres-pv-claim
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10GB

然后将PVC绑在吊舱上,如所示

apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: postgres
spec:
template:
metadata:
labels:
app: postgres
spec:
containers:
- name: postgres
...
volumeMounts:
- mountPath: /var/lib/postgresql/data
name: postgres-pv-claim
volumes:
- name: postgres-pv-claim
persistentVolumeClaim:
claimName: postgres-pv-claim
  • 根据Kubernetes文档https://kubernetes.io/docs/concepts/storage/volumes/#glusterfs,与在移除Pod时擦除的emptyDir不同,glusterfs卷的内容被保留,而该卷只是被卸载。我建议在链接上提出一个问题https://github.com/kubernetes/kubernetes/issues/new/choose

  • 如果你想安装带有PostgresSQL后端的GitLab,在Helm Charts下面使用会更容易。

https://docs.gitlab.com/charts/

https://artifacthub.io/packages/helm/bitnami/postgresql

https://artifacthub.io/packages/helm/bitnami/postgresql-ha

您可以执行以下操作:

1.对于数据库等有状态集服务,应使用StatefulSet控制器进行部署;

2.存储数据资源应该是共享类型的,而不是使用本地卷作为存储,在创建POD对象时,本地卷可以被调度到其他节点;

最新更新