Search code examples
kubernetesenvironment-variablessequelize.jsskaffold

Kubernetes env variables not working in sequelize-cli


I'm trying to run the following command:

npx sequelize-cli db:migrate

sequelize-cli uses a ./config/config.js file that contains the following:

module.exports = {
  username: process.env.PGUSER,
  host: process.env.PGHOST,
  database: process.env.PGDATABASE,
  password: process.env.PGPASSWORD,
  port: process.env.PGPORT,
};

If you console.log() all of thee process.env.<var>, it all comes back undefined.

However, if go into the index.js where the Express app resides and console.log the same thing, it comes back with the expected values.

I have Kubernete running with skaffold.yaml and minikube during all of this.

Is there a way to get this working without creating a .env just to run these commands?

server-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: server-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      component: server
  template:
    metadata:
      labels:
        component: server
    spec:
      containers:
        - name: server
          image: sockpuppet/server
          ports:
            - containerPort: 5000
          env:
            - name: PGUSER
              value: postgres
            - name: PGHOST
              value: postgres-cluster-ip-service
            - name: PGPORT
              value: '5432'
            - name: PGDATABASE
              value: postgres
            - name: PGPASSWORD
              value: ''

Solution

  • Well, it isn't pretty, but this is the best I could figure out. Hopefully someone has a better answer...

    Since this is a deployment that consists of three replicas, it produces three pods. Have to get the id for one first:

    kubectl get pod
    

    Once I have that I can do the following:

    kubectl exec -it server-deployment-84cf685559-gwkvt -- npx sequelize-cli db:migrate
    

    That works, but kind of messy.

    Came across this link that be more efficient especially if just making an alias:

    kubectl exec -it $(kubectl get pods -o name | grep -m1 INSERT_DEPLOYMENT_NAME_HERE | cut -d'/' -f 2) INSERT_YOUR_COMMAND_HERE
    
    kubectl exec -it $(kubectl get pods -o name | grep -m1 server-deployment | cut -d'/' -f 2) "npx sequelize-cli db:migrate"