3

我使用 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"

在我的pre-install-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 denied (SQL: select * from information_schema.tables where table_schema = forge and table_name = migrations and table_type = 'BASE TABLE')

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

4

1 回答 1

1

好的,我再次尝试在项目中将 Confirmap 挂载为 .env 文件。但是这次我也提供了一个 subPath 定义。现在它可以工作并且“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"

subPath 定义是 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"
于 2019-12-11T12:23:32.037 回答