In the case that an app and database are in different containers/pods (each a separate deployment yaml) how do you get them to communicate?
For example, a Django app requires the host name of the database in it's config/environment variables (along with the database name and a few other things) in order to connect to the database.
You should be able to specify the service as follows (assuming the database has a service called db-service in the default namespace):
Inside Django app demployment.yaml file:
env:
- name: SQL_HOST
value: "db-service.default"
or you could do some hackery with the HOSTNAME for the database container (if it's similar to the app name) for example:
Inside Django app demployment.yaml file:
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: SQL_HOST
value: $(POD_NAME)-db
Inside Postgres demployment.yaml file:
spec:
replicas: 1
selector:
matchLabels:
app: sitename-db-container
template:
metadata:
labels:
app: sitename-db-container
spec:
hostname: sitename-db
But what happens when you have multiple deployments inside a service for the same app (each having their app - database container pair)? How will the service know which app pod will communicate with what database pod? Does there now need to be a separate service for every app and database deployment?