如何解决使用 glusterfs 作为存储的 k8s 上的 postgres
我在 postgres
和 k8s
上部署了一个 glusterfs
数据库作为卷。但是每次我重新启动我的 pod 时,所有数据都会丢失。这是为什么?
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
解决方法
-
根据 Kubernetes 文档 https://kubernetes.io/docs/concepts/storage/volumes/#glusterfs,与 emptyDir 不同,emptyDir 在删除 Pod 时会被擦除,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
,定义 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绑定到pod上,如下图
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
,
您可以这样做:
1.对于数据库等有状态的集合服务,应该使用StatefulSet控制器进行部署;
2.存储数据资源应该是共享类型的,而不是使用本地卷作为存储,在创建POD对象时可能会调度到其他节点;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。