如何在Kubernetes中共享每个容器的体积?

时间:2018-11-08 11:41:22

标签: docker drupal kubernetes

例如,使用nginx:stable-alpine + drupal:8.6-fpm-alpine运行Drupal应用。

  • nginx容器需要与drupal容器共享/var/www/html才能传递静态内容。
  • drupal容器应使用GCP-PD之类的卷来持久存储或装载来自外部存储的站点数据/var/www/html/sites

在这种情况下,本地docker-compose.yml在下面。

version: "3"

volumes:
  www-data:

services:
  drupal:
    image: "drupal:8.6-fpm-alpine"
    volumes:
      - "www-data:/var/www/html"
      - "./sites:/var/www/html/sites"
    # ...

  nginx;
    image: "nginx:stable-alpine"
    depends_on:
      - drupal
    volumes:
      - "www-data:/var/www/html"
    # ...
# ...

如何转换为k8s deployment.yml?

(编辑)我尝试了以下操作,但没有成功。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mydrupal
  labels:
    app.kubernetes.io/name: mydrupal
spec:
  replicas: 2
  selector:
    matchLabels:
      app.kubernetes.io/name: mydrupal
  template:
    metadata:
      labels:
        app.kubernetes.io/name: mydrupal
    spec:
      volumes:
        - name: drupal-data
          persistentVolumeClaim:
            claimName: "drupal-pvc"
      # keep default files for the drupal installer, and chown.
      initContainers:
        - name: init-drupal-data
          image: drupal:8.6-fpm-alpine
          imagePullPolicy: IfNotPresent
          command: ['sh', '-c']
          args: ['cp -r -u /var/www/html/sites/* /tmp/drupal; chown -R www-data:www-data /tmp/drupal']
          volumeMounts:
            - name: drupal-data
              mountPath: /tmp/drupal
              subPath: sites
      securityContext:
        # www-data
        fsGroup: 33
      containers:
        - name: drupal
          image: drupal:8.6-fpm-alpine
          imagePullPolicy: IfNotPresent
          volumeMounts:
            # I want to sharing this directory with nginx container.
            - name: drupal-data
              mountPath: /var/www/html
            # I want to persist this directory using external managed storage.
            - name: drupal-data
              mountPath: /var/www/html/sites
              subPath: sites
          resources:
            limits:
              cpu: 800m
              memory: 512Mi
            requests:
              cpu: 200m
              memory: 256Mi
        - name: nginx
          image: nginx:1.14-alpine
          imagePullPolicy: IfNotPresent
          volumeMounts:
            - name: drupal-data
              mountPath: /usr/share/nginx/html
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /
              port: http
            initialDelaySeconds: 120
          readinessProbe:
            httpGet:
              path: /
              port: http
            initialDelaySeconds: 30
          resources:
            limits:
              cpu: 500m
              memory: 512Mi
            requests:
              cpu: 100m
              memory: 256Mi

我读了pv,pvc文档。 但找不到有关如何将容器内的目录显示为卷的任何解决方案。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

看看hostPath,它将允许您使用本地文件夹作为持久性存储。 https://kubernetes.io/docs/concepts/storage/volumes/#hostpath

根据为其他类型的pv / pvc设置kubernetes集群的方式,存在一些学习曲线和配置差异。