2

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: ''
cjones
  • 8,384
  • 17
  • 81
  • 175
  • How do you run `npx sequelize-cli` command and how do you run `index.js`? How do you define environment variables? If environment variables are defined - they would have been used. – zerkms Oct 19 '19 at 23:10
  • I'm just running `npx sequelize-cli` from an open terminal. The `index.js` is started with `skaffold`. I've posted how the env variables are declared in the k8s manifest. – cjones Oct 19 '19 at 23:50
  • Can you try exec into the container and check `env` to determine if this is kubernetes or the JS framework? – Dandy Oct 20 '19 at 00:15
  • Doing a `kubectl exec server-deployment-656bfc8cd6-lgt8g env` lists the env variables. However just running `env` from where I am trying to run `npx sequelize-cli`. I figure what is happening is that just running `npx sequelize-cli` from the terminal is outside the context of the running k8s-skaffold stuff. I need to figure out how to run `npx sequelize-cli` within the running k8s environment. Not quite sure how to do that. – cjones Oct 20 '19 at 00:38

1 Answers1

1

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" 
cjones
  • 8,384
  • 17
  • 81
  • 175