如何在 Kubernetes 中共享每个容器的卷?
how to sharing volume each containers in Kubernetes?
例如,运行 Drupal 应用程序使用 nginx:stable-alpine + drupal:8.6-fpm-alpine。
- nginx 容器需要共享
/var/www/html
来自 drupal 容器的静态内容。
- 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
我阅读了 volume、pv、pvc 文档。
但没有找到任何关于如何将容器内的目录公开为卷的解决方案。
有什么想法吗?
查看 hostPath,它将允许您将本地文件夹用作持久存储。 https://kubernetes.io/docs/concepts/storage/volumes/#hostpath
根据您的 kubernetes 集群为其他类型 pv/pvc 设置的方式,存在一些学习曲线和配置差异。
例如,运行 Drupal 应用程序使用 nginx:stable-alpine + drupal:8.6-fpm-alpine。
- nginx 容器需要共享
/var/www/html
来自 drupal 容器的静态内容。 - 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
我阅读了 volume、pv、pvc 文档。 但没有找到任何关于如何将容器内的目录公开为卷的解决方案。
有什么想法吗?
查看 hostPath,它将允许您将本地文件夹用作持久存储。 https://kubernetes.io/docs/concepts/storage/volumes/#hostpath
根据您的 kubernetes 集群为其他类型 pv/pvc 设置的方式,存在一些学习曲线和配置差异。