<h1>The problem</h1>
<h2>Error photo</h2>
<p><a href="https://i.stack.imgur.com/4tpWA.png" rel="nofollow noreferrer">Flyway Job Error</a></p>
<p>I'm currently attempting to use flyway to perform migrations in postgres. I have a feeling the init scripts are unable to be found based on this error message.
Not too sure what I'm missing as the init scripts worked when using docker containers.</p>
<p>Any help would be appreciated for solving this issue.</p>
<h1>What I have tried:</h1>
<ol>
<li>So far I have tried playing with the connection string inside the job to see if it was a connection problem.</li>
<li>Then I tried using a config map with the init sql script inside to hopefully have the script visible for migrations. Didn't have too much luck with that approach.
I have attached the example that I found for this approach.
<a href="https://github.com/arkuksin/scs-db-versioning/blob/master/k8s/migration/migration_configmap.yml" rel="nofollow noreferrer">Migrations with ConfigMap for init sql scripts</a></li>
</ol>
<p>I have some more research links that I found but they didn't quite fit or work with my problem and weren't as noteworthy or were outdated.</p>
<ol>
<li><a href="https://blog.sebastian-daschner.com/entries/flyway-migrate-databases-managed-k8s" rel="nofollow noreferrer">Database migrations in Kubernetes with flyway video</a></li>
<li><a href="https://reflectoring.io/flyway-kubernetes-continuous-database-configuration/" rel="nofollow noreferrer">Continuous Database configuration with flyway</a></li>
</ol>
<h1>Code/Kubernetes setup</h1>
<p>What follows is my current setup:</p>
<p>Side note: I am using kind to pull my docker images locally and create my clusters, I will attach my docker compose and docker files at the end.</p>
<h2>Flyway Setup</h2>
<p>I have Flyway as a Job in Kubernetes with this structure:</p>
<h3>Flyway job file</h3>
<pre><code>apiVersion: batch/v1
kind: Job
metadata:
  name: flyway-migration-job
spec:
  template:
    spec:
      containers:
      - name: flyway
        image: flyway/flyway:6.3.1
        command: [&quot;flyway -url=jdbc:postgresql://postgres-master:5432/wallet -user=test_user -password=testuser1234  -connectRetries=60 migrate&quot;]
      restartPolicy: Never
  backoffLimit: 1
</code></pre>
<h2>Postgres Setup</h2>
<h3>Postgres Deployment file:</h3>
<p>This job is using postgres which I have created a deployment for using:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres-master
  labels:
    app: postgres-master
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres-master
  template:
    metadata:
      labels:
        app: postgres-master
    spec:
      containers:
        - name: postgres-master
          image: postgres:12
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 5432
          env:
            - name: POSTGRES_DB
              value: wallet
            - name: POSTGRES_USER
              value: test_user
            - name: POSTGRES_PASSWORD
              value: testuser1234
</code></pre>
<h3>Postgres service file</h3>
<p>I have then exposed the Postgres pod using this Postgres service:</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: postgres-master
  labels: 
    app: postgres-master
spec: 
  type: NodePort
  ports:
    - protocol: TCP
      port: 5432
      targetPort: 5432
  selector: 
    app: postgres-master
</code></pre>
<h2>Node setup</h2>
<p>I also have a node deployment which connects to the database pod and I have confirmed that I have been able to connect to the database using hostname postgres-master(pod name), the above login details and port 5432.</p>
<h3>Node deployment file</h3>
<p>I've attached the node deployment file for completeness but it works.</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: node-deployment
  labels:
    app: node-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: node-deployment
  template:
    metadata:
      labels:
        app: node-deployment
    spec:
      containers:
        - name: node
          image: wallet-api-20_web:latest
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 8080
          env:
            - name: PG_HOST
              value: &quot;postgres-master.default&quot;
</code></pre>
<h2>Docker setup</h2>
<h3>Docker compose</h3>
<p>Then I have my docker compose file which I use to build my images:
Side not - All of the docker images worked together including migrations before I started adding Kubernetes.</p>
<pre><code>version: '3.5'
services:
  web:
    build:
      dockerfile: ./Dockerfile
    container_name: wallet-api-web
    depends_on: 
      - flyway
    ports: 
      - &quot;8080:8080&quot;
  postgres-db:
    build:
      dockerfile: ./utils/db.Dockerfile
    container_name: postgres-db
    environment:
      POSTGRES_PASSWORD: testuser1234
      POSTGRES_USER: test_user
      POSTGRES_DB: wallet
    image: postgres:12
    ports:
      - &quot;5432:5432&quot;
    restart: always

  flyway:
    command: -url=jdbc:postgresql://db:5432/wallet -user=test_user -password=testuser1234  -connectRetries=60 migrate
    container_name: flyway-migrations
    depends_on:
      - db
    image: flyway/flyway:6.3.1
    volumes:
      - ${PWD}/migrations/sql:/flyway/sql
</code></pre>
<h3>Postgres Dockerfile</h3>
<p>Here is my dockerfile for postgres:</p>
<pre><code>FROM postgres:13

ENV POSTGRES_DB wallet
</code></pre>
<h3>Node dockerfile</h3>
<pre><code>FROM node:15
ENV NODE_ENV=production

WORKDIR /app

# Copying the package json first enables caching.
COPY [&quot;package.json&quot;, &quot;package-lock.json*&quot;, &quot;./&quot;]

RUN npm install --production

COPY . . 

RUN npm run build

ENTRYPOINT npm run start
</code></pre>
<h3>Flyway Migration scripts</h3>
<p>This is the only migration script I have and it was working before moving to Kubernetes.</p>
<pre><code>CREATE TABLE transaction_events (
    transaction_version int,
    transaction_type VARCHAR(200),
    coins int,
    transaction_id VARCHAR(500),
    wallet_id VARCHAR(500)
)
</code></pre>
<p>Any help would be appreciated, I have a feeling this might be something small I am missing.</p>
<p>If anymore information is required please let me know.</p>
<p>Feel free to add any questions as well and I will do my best to provide further context.</p>
<p>Thank you.</p>

<p>I suspect the way you are calling <code>command</code> is the issue. it needs to be <code>command: [&quot;flyway&quot;, &quot;-url=jdbc:postgresql://postgres-master:5432/wallet&quot;, &quot;-user=test_user&quot;, &quot;-password=testuser1234&quot;, &quot;-connectRetries=60 migrate&quot;]</code></p>
<p>Also, I think you do not need to use <code>command</code>. You can use <code>args</code> instead since you are use also the command specified in the image entrypoint.
<code>args: [&quot;-url=jdbc:postgresql://postgres-master:5432/wallet&quot;, &quot;-user=test_user&quot;, &quot;-password=testuser1234&quot;, &quot;-connectRetries=60 migrate&quot;]</code></p>
<p>both should work anyway</p>
