Reputation: 423
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?
Upvotes: 0
Views: 498
Reputation: 11
Expose backend as cluster Ip service (provide clusterIP: None in backend service definition file).
Now in frontend env var pass backend service name that you created
Upvotes: 0
Reputation: 11128
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?
What do you mean by "multiple deployments inside a service" ? In a Service
definition you are supposed to select only one set of Pods
, let's say managed by one specific Deployment
. As @Matt suggested, you should always create a service with a unique name for each pod/db you want to access. If you have Pods
dedicated to do specific tasks you deploy them separately (as separate Deployments
). They can even consist of just one Pod
if you don't need any redundancy. And basically you will always create a separate Service
( obviously with unique name as you cannot create more Services
using the same name ) for exposing different microservice ( represented by unique Deployment
). Note that if you don't create a Deployment
but a simple Pod
it won't be managed by any controller so if it crashes, nothing will take care of recreating it. So definitely you should always use Deployment
even to run a single Pod
representing your microservice.
Have you read this topic in official kubernetes documentation ? If you don't specify Service
type, by default it creates so called ClusterIP
Service which is basically what you need to expose your app
components internally (make them available for other app components in your cluster).
Upvotes: 1