redhat-scholars / kubernetes-tutorial

Kubernetes Tutorial for https://dn.dev/master
https://redhat-scholars.github.io/kubernetes-tutorial/
Apache License 2.0
361 stars 194 forks source link

Improving set env section #12

Open kameshsampath opened 4 years ago

kameshsampath commented 4 years ago

Improve the set env section with examples by adding examples to show variable interpolation in the command and args attributes of the deployment YAML. The args in command and args need to follow the variable substitution pattern like $(ENV_VAR_NAME).

I feel we need to call this out in the tutorial as developers will follow the natural instinct of using $ENV_VAR_NAME pattern.

kameshsampath commented 4 years ago

Take an example of running psql to load DB schema:

  template:
    spec:
      volumes:
        - name: leaderboard-schema
          configMap:
            name: leaderboard-schema
            items:
              - key: schema.sql
                path: schemas/schema.sql
      containers:
        - name: pgsql12-client
          image: quay.io/rhdevelopers/openshift-pgsql12-primary:centos7-clients
          imagePullPolicy: Always
          env:
            - name: PGHOST
              value: postgresql
            - name: PGDATABASE
              valueFrom:
                secretKeyRef:
                  name: openshift-pgsql12-primary-secret
                  key: database-db
            - name: PGUSER
              valueFrom:
                secretKeyRef:
                  name: openshift-pgsql12-primary-secret
                  key: database-username
            - name: PGPASSWORD
              valueFrom:
                secretKeyRef:
                  name: openshift-pgsql12-primary-secret
                  key: database-username-password
          command: ["/usr/pgsql-12/bin/psql"]
          args:
            [
              "--dbname=$(PGDATABASE)",
              "--file=/opt/sql/schemas/schema.sql",
              "--no-password",
            ]
          volumeMounts:
            - name: leaderboard-schema
              mountPath: /opt/sql

for variable PGDATABASE to be interpolated in command args we need to use the syntax "--dbname=$(PGDATABASE)"