使用 "envFrom" 和 "configMapRef" (kustomize/kubernetes/laravel) 时,环境变量在 pod/container 中不可用

Environment variables not available within pod/container when using "envFrom" and "configMapRef" (kustomize/kubernetes/laravel)

我将 kustomize 用作独立的二进制文件(与 Homebrew 一起安装)并具有以下 base/kustomization.yaml 文件:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
commonLabels:
  app: myservice
  app.kubernetes.io/name: myservice
  service: myservicename
  vendor: mycompany
  app.kubernetes.io/version: "0.1.0"
namePrefix: myservice-
namespace: mynamespace
commonAnnotations:
  supportMail: support@sagich.net
resources:
- pre-install-job.yaml
- cronjob.yaml
- service.yaml
- api.yaml
- worker.yaml
configMapGenerator:
- files:
  - env.properties
  name: configmap
secretGenerator:
- envs:
  - env.properties
  name: env-file-secret

env.properties 文件如下所示:

APP_NAME="vendor-service-whatever"
APP_ENV="production"
APP_KEY="base64:onKQb...LtU="
APP_DEBUG="false"
APP_URL="https://myamazingsite.com"
LOG_CHANNEL="stderr"
DB_CONNECTION="mysql"
DB_HOST="mariadb-slave"
DB_DATABASE="incredibledb"

在我的预安装-job.yaml配置中,我声明了一个这样的容器:

containers:
    - name: "pre-install-job"
      image: "registry.myamazingdomain.com/vendor/whatever/service:latest"
      imagePullPolicy: "Always"
      args:
        - /bin/bash
        - -c
        - php artisan migrate --force -v;
      envFrom:
        - configMapRef:
            name: "configmap"

问题:环境变量在 pod/container 中不可用。我还尝试将 configmap 作为 .env 文件安装在容器中,但也没有成功。也许有人可以为我指出正确的方向。

产生的错误是:

Illuminate\Database\QueryException : SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = forge and table_name = migrations and table_type = 'BASE TABLE')

因为使用了默认值而不是环境变量中定义的值。

好的,我再次尝试将 confirmap 挂载为项目中的 .env 文件。但是这次我也提供了一个子路径定义。现在可以使用了,"php artisan migrate" 命令运行成功。

  volumes:
    - name: "env-volume"
      configMap:
        name: "configmap"
  containers:
    - name: "pre-install-job"
      image: "registry.myamazingdomain.com/vendor/whatever/service:latest"
      imagePullPolicy: "Always"
      args:
        - /bin/bash
        - -c
        - php artisan migrate --force -v;
      volumeMounts:
        - name: "env-volume"
          mountPath: "/var/www/html/app/.env"
          subPath: "env.properties"

子路径定义是 configMapGenerator 的一部分:

configMapGenerator:
  - name: configmap
    files:
      - env.properties

例如:

$ kubectl -n mynamespace get configmap blablubb-configmap-8c4m97m94g -o yaml

apiVersion: v1
data:
  env.properties: |
    APP_NAME="vendor-service-whatever"
    APP_ENV="production"
    APP_KEY="base64:onKQb...LtU="
    APP_DEBUG="false"