Skip to content

Latest commit

 

History

History
56 lines (48 loc) · 2.05 KB

02-09-env-vars.md

File metadata and controls

56 lines (48 loc) · 2.05 KB

Lesson 9 - Change Environment Variables For A Deployment

You almost have a fully working application but now the routes to the API server have changed for your Kubernetes deployment. Thankfully, in one of the earlier lessons, you stored this variable so that it could be stored inside an environment variable. Also, your server will now look for environment variables and update the config.js file accordingly. This should let you easily change the URL for the API server location.

In your deployment.yaml file, in the containers section, you will add a new env property. This will contain an array with all the environment variables that you want to add to the container that runs in this pod. Environment variables are objects with a name and value properties.

spec:
  template:
    ...
    spec:
      containers:
      - name: server
        ...
        env:
        - name: BASE_URL
          value: api

Your new deployment.yaml file should now include the environment variable and look like this.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: k8scourse-front
  labels:
    app: k8scourse-demo
    role: frontend
spec:
  template:
    metadata:
      labels:
        app: k8scourse-demo
        role: frontend
    spec:
      containers:
      - name: server
        image: joellord/k8scourse-front
        env:
        - name: BASE_URL
          value: api
  selector:
    matchLabels:
      role: frontend
  replicas: 2

Now you will need to apply the changes to this deployment and restart the pods. You can apply the new deployment with the apply command again. This will update the deployment and will automatically restart the pods since it will detect that those need to be changed. If you do a get command to find all the front end pods, you should see that they’ve been recently restarted.

kubectl apply -f ./deployment.yaml 
kubectl get pods -l role=frontend

Now go back to the application in the browser and you will see it running again. Well, almost. You still haven’t connected a database yet but you’re getting closer.