在kuebernetes上通过nfs-server持久化postgresql

2021-11-15 17:32:16 浏览数 (1)

kubernetes提供了多种持久存储数据的方式,见官网 此处我们使用nfs-server做数据持久化的方式。直接开始部署我们的Postgresql吧

1. 初始化postgresql的配置

file: 01-postgresql-configmap.yaml

代码语言:javascript复制
apiVersion: v1
kind: ConfigMap
metadata:
name: postgres-config
namespace: kube-ops
labels:
app: postgres-prom
data:
POSTGRES_DB: postgresdb
POSTGRES_USER: postgresadmin
POSTGRES_PASSWORD: admin123

2. 创建对应的存储卷

file: 02-postgresql-pv.yaml,注意此处我们需要提前准备好nfs-server,以及暴露出/data目录供网络存储

代码语言:javascript复制
apiVersion: v1
kind: PersistentVolume
metadata:
name: postgres-pv-volume
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
nfs:
server: 192.168.10.234path: /data

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: postgres-pv-claim
namespace: kube-ops
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi

3. 部署PostgraSQL

当已有服务使用了nfs作为本地持久化存储的时候,我们直接启动postgresql会初始化错误,因此我们可以使用subPath: postgresql进行区分 file: 03-postgresql-dp.yaml

代码语言:javascript复制
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: postgres-prom
namespace: kube-ops
spec:
replicas: 1template:
metadata:
labels:
app: postgres-prom
spec:
containers:
- name: postgres
image: postgres:10.4imagePullPolicy: "IfNotPresent"ports:
- containerPort: 5432envFrom:
- configMapRef:
name: postgres-config
volumeMounts:
- mountPath: /var/lib/postgresql/data
subPath: postgresql
name: postgredb
volumes:
- name: postgredb
persistentVolumeClaim:
claimName: postgres-pv-claim

4. 暴露postgresql服务

file: 04-postgresql-svc.yaml

代码语言:javascript复制
apiVersion: v1
kind: Service
metadata:
name: postgres
namespace: kube-ops
labels:
app: postgres-prom
spec:type: NodePort
ports:
- port: 5432selector:
app: postgres-prom

以上就是部署postgresql需要的文件,一切准备就绪之后,我们就可以开始部署了

代码语言:javascript复制
kubectl apply -f .

➜  13-1-postgresql git:(master) ✗ (☸ kubernetes-admin@kubernetes:default) k get pods -n kube-ops -o wide -l app=postgres-prom
NAME                             READY   STATUS    RESTARTS   AGE   IP            NODE
postgres-prom-6f54c97948-fp8th   1/1     Running   0          18m   10.244.2.63   dev-k8s-node1

0 人点赞