How to access Kubernetes container environment variables from Next.js application?

后端 未结 1 867
走了就别回头了
走了就别回头了 2020-12-22 01:34

In my next.config.js, I have a part that looks like this:

module.exports = {
  serverRuntimeConfig: { // Will only be available on the server side
    mySecr         


        
相关标签:
1条回答
  • 2020-12-22 02:11

    You can create a config-map and then mount it as a file in your deployment with your custom environment variables.

    apiVersion: extensions/v1beta1
    kind: Deployment
    metadata:
      annotations:
        deployment.kubernetes.io/revision: "38"
      creationTimestamp: xx
      generation: 40
      labels:
        app: appname
      name: appname
      namespace: development
      resourceVersion: xx
      selfLink: /apis/extensions/v1beta1/namespaces/development/deployments/appname
      uid: xxx
    spec:
      progressDeadlineSeconds: xx
      replicas: 1
      revisionHistoryLimit: 10
      selector:
        matchLabels:
          app: appname
          tier: sometier
      strategy:
        rollingUpdate:
          maxSurge: 1
          maxUnavailable: 1
        type: RollingUpdate
      template:
        metadata:
          creationTimestamp: null
          labels:
            app: appname
            tier: sometier
        spec:
          containers:
            - env:
                - name: NODE_ENV
                  value: development
                - name: PORT
                  value: "3000"
                - name: SOME_VAR
                  value: xxx
                - name: SOME_VAR
                  value: xxxx
              volumeMounts:
                - name: environment-variables
                  mountPath: "your/path/to/store/the/file"
                  readOnly: true
              image: someimage
              imagePullPolicy: Always
              name: appname
              readinessProbe:
                failureThreshold: 3
                httpGet:
                  path: /healthz
                  port: 3000
                  scheme: HTTP
                initialDelaySeconds: 5
                periodSeconds: 10
                successThreshold: 1
                timeoutSeconds: 1
              resources:
                requests:
                  cpu: 100m
                  memory: 100Mi
              terminationMessagePath: /dev/termination-log
              terminationMessagePolicy: File
          volumes:
            - name: environment-variables
              configMap:
                name: environment-variables
                items:
                  - key: .env
                    path: .env
          dnsPolicy: ClusterFirst
          restartPolicy: Always
          schedulerName: default-scheduler
          securityContext: {}
          terminationGracePeriodSeconds: 30
    status:
      availableReplicas: 1
      conditions:
        - lastTransitionTime: xxx
          lastUpdateTime: xxxx
          message: Deployment has minimum availability.
          reason: MinimumReplicasAvailable
          status: "True"
          type: Available
      observedGeneration: 40
      readyReplicas: 1
      replicas: 1
      updatedReplicas: 1
    

    I added the following configuration in your deployment file:

          volumeMounts:
            - name: environment-variables
              mountPath: "your/path/to/store/the/file"
              readOnly: true
      volumes:
        - name: environment-variables
          configMap:
            name: environment-variables
            items:
              - key: .env
                path: .env
    

    You can then create a config map with key ".env" with your environment variables on kubernetes.

    Configmap like this:

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: environment-variables
      namespace: your-namespace
    data:
      .env: |
        variable1: value1
        variable2: value2
    
    0 讨论(0)
提交回复
热议问题