"istio-init" required to run before additional initContainers

1/20/2019

using a standard istio deployment in a kubernetes cluster I am trying to add an initContainer to my pod deployment, which does additional database setup.

Using the cluster IP of the database doesn't work either. But I can connect to the database from my computer using port-forwarding.

This container is fairly simple:

    spec:
      initContainers:
        - name: create-database
          image: tmaier/postgresql-client
          args:
            - sh
            - -c
            - |
              psql "postgresql://$DB_USER:$DB_PASSWORD@db-host:5432" -c "CREATE DATABASE fusionauth ENCODING 'UTF-8' LC_CTYPE 'en_US.UTF-8' LC_COLLATE 'en_US.UTF-8' TEMPLATE template0"
              psql "postgresql://$DB_USER:$DB_PASSWORD@db-host:5432" -c "CREATE ROLE user WITH LOGIN PASSWORD 'password';"
              psql "postgresql://$DB_USER:$DB_PASSWORD@db-host:5432" -c "GRANT ALL PRIVILEGES ON DATABASE fusionauth TO user; ALTER DATABASE fusionauth OWNER TO user;"

This kubernetes initContainer according to what I can see runs before the "istio-init" container. Is that the reason why it cannot resolve the db-host:5432 to the ip of the pod running the postgres service?

The error message in the init-container is:

psql: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

The same command from fully initialized pod works just fine.

-- Janos Veres
istio
kubernetes

1 Answer

1/21/2019

You can't access services inside the mesh without the Envoy sidecar, your init container runs alone with no sidecars. In order to reach the DB service from an init container you need to expose the DB with a ClusterIP service that has a different name to the Istio Virtual Service of that DB.

You could create a service named db-direct like:

apiVersion: v1
kind: Service
metadata:
  name: db-direct
  labels:
    app: db
spec:
  type: ClusterIP
  selector:
    app: db
  ports:
    - name: db
      port: 5432
      protocol: TCP
      targetPort: 5432

And in your init container use db-direct:5432.

-- Stefan P.
Source: StackOverflow