kubectl -n magento logs magento-install-jssk6
我正在安装作业中获取Database found In ConfigModel.php line 166:Missing write permissions to the following paths: /var/www/html/pub/media
apiVersion: batch/v1
kind: Job
metadata:
name: magento-install
namespace: magento
spec:
template:
metadata:
name: install
labels:
app: magento-install
k8s-app: magento
spec:
containers:
- name: magento-setup
image: kiweeteam/magento2:vanilla-2.3.4-php7.3-fpm
command: ["/bin/sh"]
args:
- -c
- |
/bin/bash <<'EOF'
bin/install.sh
php bin/magento setup:perf:generate-fixtures setup/performance-toolkit/profiles/ce/small.xml
magerun index:list | awk '{print $2}' | tail -n+4 | xargs -I{} magerun index:set-mode schedule {}
magerun cache:flush
EOF
envFrom:
- configMapRef:
name: config
volumeMounts:
- mountPath: /var/www/html/pub/media
name: media
volumes:
- name: media
persistentVolumeClaim:
claimName: media
restartPolicy: OnFailure
当我试图更改权限时,我得到了chown:更改"/var/www/html/pub/media"的所有权:不允许操作
发生这种情况是因为您以www-data
用户身份运行chown
,而此目录的当前所有者是root
。
您可以使用以root
(id为0
的用户(身份运行的init容器来解决问题。下面您可以看到magento-install
Job
的修改版本,其中添加了init-container:
apiVersion: batch/v1
kind: Job
metadata:
name: magento-install
namespace: magento
spec:
template:
metadata:
name: install
labels:
app: magento-install
k8s-app: magento
spec:
initContainers:
- name: magento-chown
securityContext:
runAsUser: 0
image: kiweeteam/magento2:vanilla-2.3.4-php7.3-fpm
command: ['sh', '-c', 'chown -R www-data:www-data /var/www/html/pub/media']
volumeMounts:
- name: media
mountPath: "/var/www/html/pub/media"
containers:
- name: magento-setup
image: kiweeteam/magento2:vanilla-2.3.4-php7.3-fpm
command: ["/bin/sh"]
args:
- -c
- |
/bin/bash <<'EOF'
bin/install.sh
php bin/magento setup:perf:generate-fixtures setup/performance-toolkit/profiles/ce/small.xml
magerun index:list | awk '{print $2}' | tail -n+4 | xargs -I{} magerun index:set-mode schedule {}
magerun cache:flush
EOF
envFrom:
- configMapRef:
name: config
volumeMounts:
- mountPath: /var/www/html/pub/media
name: media
volumes:
- name: media
persistentVolumeClaim:
claimName: media
restartPolicy: OnFailure
一旦您使用连接到新创建的Pod
kubectl exec -ti -n magento magento-install-z66qg -- /bin/bash
您将看到/var/www/html/pub/media
目录的当前所有者不再是root
,而是www-data
用户:
www-data@magento-install-z66qg:~/html$ ls -ld /var/www/html/pub/media
drwxr-xr-x 3 www-data www-data 4096 Jul 27 18:45 /var/www/html/pub/media
我们可以进一步简化它。init容器甚至不需要使用kiweeteam/magento2:vanilla-2.3.4-php7.3-fpm
映像。它可能是一个基于busybox
的简单容器,默认情况下以root
运行,因此您可以省略前面示例中的安全上下文,initContainers
部分将如下所示:
initContainers:
- name: magento-chown
image: busybox
command: ['sh', '-c', 'chown -R www-data:www-data /var/www/html/pub/media']
volumeMounts:
- name: media
最终效果将完全相同。