如何在Kubernetes中使用PersistentVolume for PostgreSQL数据

时间:2018-05-16 05:40:28

标签: postgresql kubernetes

我们正在开发Flask&amp ;;的Web服务器。 PostgreSQL在Kubernetes中使用DB-server,并考虑使用PersistentVolume来使数据持久化。

但是,对于指定为Volume的目录,所有权将被强制为“root”用户。

在PostgreSQL中,如果用户和所有者不匹配,则无法设置服务器。 而且,我们无法在user ='root'下设置服务器。 因此,我们无法使PostgreSQL服务器数据持久化。

Dockerfile

FROM ubuntu:latest

ARG project_dir=/app/

WORKDIR $project_dir

RUN apt update
RUN apt install --yes python3 python3-pip postgresql-9.5
RUN apt clean
RUN ln -s /usr/bin/python3 /usr/bin/python
RUN ln -s /usr/bin/pip3 /usr/bin/pip

RUN pip install flask
RUN pip install flask_sqlalchemy
RUN pip install psycopg2

ADD app.py $project_dir
ADD templates/ $project_dir/templates/

USER postgres

RUN /etc/init.d/postgresql start && \
  psql --command "CREATE USER docker WITH SUPERUSER PASSWORD 'docker';" && \
  createdb -O docker docker

RUN echo "host all all 0.0.0.0/0 md5" >> /etc/postgresql/9.5/main/pg_hba.conf
RUN echo "listen_addresses='*'" >> /etc/postgresql/9.5/main/postgresql.conf

EXPOSE 5000

CMD /usr/lib/postgresql/9.5/bin/postgres -D /var/lib/postgresql/9.5/main -c config_file=/etc/postgresql/9.5/main/postgresql.conf & python /app/app.py

development.yaml

apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: dummyproject
  labels:
    app: dummyproject
spec:
  replicas: 1
  selector:
    matchLabels:
      app: dummyproject
  template:
    metadata:
      labels:
        app: dummyproject
    spec:
      containers:
      - name: dummyproject
        image: dummyproject:0.1.0
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 5000
        volumeMounts:
        - mountPath: /var/lib/postgresql/
          name: mydata
      volumes:
        - name: mydata
          persistentVolumeClaim:
            claimName: nfs-claim1

如果您知道解决方案,请告诉我。

2 个答案:

答案 0 :(得分:0)

随意以root身份运行PostgreSQL。容器中的root与裸机Linux上的root不同。 UID == 0并不意味着超级大国。现在用户访问是通过功能机制来控制的,默认情况下你的容器没有任何危险的功能(除非你明确要求Kubernetes使用)。

答案 1 :(得分:0)

这里有2个选项:

  1. 在容器中将UID设置为0,正如@Alexandr Lurye在上面所说的那样。现在或多或少都是安全的。
  2. 您可以使用InitContainer更改所有者。这是我的答案 - https://serverfault.com/questions/906083/how-to-mount-volume-with-specific-uid-in-kubernetes-pod/907160#907160