简体   繁体   English

kubernetes php nginx 部署共享卷

[英]kubernetes php nginx deployment share volume

In kubernetes we try to have a immutable deployment of our PHP code by deploying the php code in a prepackaged container.在 kubernetes 中,我们尝试通过将 php 代码部署在预先打包的容器中来对我们的 PHP 代码进行不可变的部署。

By nature kubernetes volume replace the directory with an empty volume, but I would like to keep the data of the php container so we can share that with the nginx container which has a vhost configured to connect to the php container.本质上,kubernetes 卷将目录替换为空卷,但我想保留 php 容器的数据,以便我们可以与 nginx 容器共享该数据,该容器具有配置为连接到 php 容器的 vhost。

---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: serviceability
spec:
  replicas: 1
  template:
    metadata:
     spec:
      containers:
      - name: my-stuff-php
        image: our-php-service-in-fpm-container:latest
        ports:
        - containerPort: 9000
          name: transport
          protocol: TCP
        volumeMounts:
        - name: my-volume
          mountPath: /var/www/html
      - name: my-stuff-nginx
        image: nginx:latest
        ports:
        - containerPort: 80
          name: http
          protocol: TCP
        volumeMounts:
        - name: my-volume
          mountPath: /var/www/html
      volumes:
      - name: my-volume
        emptyDir: {}

A similar setup on docker-compose works as docker-compose behaves different with regards to volumes. docker-compose 上的类似设置可以工作,因为 docker-compose 在卷方面的行为不同。

How can I share the existing data in /var/www/html from my php container with the nginx container?如何与 nginx 容器共享来自我的 php 容器的 /var/www/html 中的现有数据?

Kubernetes hasn't mechanism like docker-compose to share some folder as volume. Kubernetes 没有像 docker-compose 这样的机制来共享某个文件夹作为卷。 But you can create Persistent Volume (PV) and Persistent Volume Claim (PVC) and share your data between containers.但是您可以创建 Persistent Volume (PV) 和 Persistent Volume Claim (PVC) 并在容器之间共享您的数据。 It is described in documentation Examples from Docs:它在来自 Docs 的文档示例中进行了描述:

YAML for creating PV用于创建 PV 的 YAML

kind: PersistentVolume
apiVersion: v1
metadata:
  name: task-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: "/mnt/data"

Than you make PVC from this volume比你用这卷做 PVC

YAML: YAML:

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: task-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Gi

Your YAML for deployment will looks like:您用于部署的 YAML 将如下所示:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: serviceability
spec:
  replicas: 1
  template:
    metadata:
     spec:
      containers:
      - name: my-stuff-php
        image: our-php-service-in-fpm-container:latest
        ports:
        - containerPort: 9000
          name: transport
          protocol: TCP
        volumeMounts:
        - name: name: task-pv-storage
          mountPath: /var/www/html
      - name: my-stuff-nginx
        image: nginx:latest
        ports:
        - containerPort: 80
          name: http
          protocol: TCP
        volumeMounts:
        - name: name: task-pv-storage
          mountPath: /var/www/html
      volumes:
      - name: task-pv-storage
        ersistentVolumeClaim:
        claimName: task-pv-claim

As a result you will have volume with data which you share between two container in pod.因此,您将拥有包含在 pod 中的两个容器之间共享的数据的卷。

Ran in the same situation.跑到同样的情况。 This is an example , Attach Handlers to Container Lifecycle Events to your php-fpm image, my-stuff-php, in your case:这是一个示例将容器生命周期事件的处理程序附加到您的 php-fpm 图像 my-stuff-php,在您的情况下:

lifecycle:
    postStart:
      exec:
        command: ["/bin/sh", "-c", "cp -r /app/. /var/www/html"]
  1. Build your php-fpm with code at /app directory.使用/app目录中的代码构建您的 php-fpm。
  2. Create the shared volume with emptyDir , which is also the nginx document root.使用emptyDir创建共享卷,它也是 nginx 文档根目录。
  3. Use lifecycle.postStart.exec.command as example above to copy source code to that shared volume.使用lifecycle.postStart.exec.command作为上面的示例将源代码复制到该共享卷。

But not sure if it's the best approach.但不确定这是否是最好的方法。 Another way is to combine nginx and php-fpm in a single image.另一种方法是将 nginx 和 php-fpm 组合在一个图像中。

it look like you need a initContainer to host your source code,看起来您需要一个 initContainer 来托管您的源代码,

then make a postStart to copy the code to a empty dir share volume,然后做一个 postStart 将代码复制到一个空的目录共享卷,

therefore both php-fpm and nginx will share this volume因此 php-fpm 和 nginx 都会共享这个卷

in this case if your application will write file during runtime,在这种情况下,如果您的应用程序将在运行时写入文件,

both nginx and php-fpm can read the file too nginx 和 php-fpm 也可以读取文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM