Service
In short, there is three things you need to know:
- Containers in the same component will be started within a Kubernetes pod which allows the containers of same component to communicate via
localhost
. - If you want containers to communicate across components, you need to define services for these components.
- If you want to connect a domain to a service, you need to configure an ingress for this service.
Define services for your components
You can define a service for a component by configuring the service
section of this component within your devspace.yaml
.
deployments:
- name: backend-api
component:
containers:
- image: "dscr.io/username/nodejs-app"
service:
ports:
- port: 80
containerPort: 3000
- name: database
component:
containers:
- image: "dscr.io/username/mysql"
service:
ports:
- port: 3306
The example above would define two services:
- Service
backend-api
which forwards all traffic frombackend-api:80
(cluster-internal DNS address) to the componentbackend-api
on port3000
- Service
database
which forwards all traffic fromdatabase:3306
(cluster-internal DNS address) to the componentdatabase
on port3306
With the above services, it would be possible that our containers within the backend-api
component could connect to the MySQL server running in database
with this connection string: mysql://USERNAME:PASSWORD@database:3306/DB_NAME
Service names must be unique across all components. If you do not specify a name for the service, it will have the same name as the component. Service names can be seen as cluster-internal domains that allow containers to access containers from other components.
View the specification for services
name: [a-z0-9-]{1,253} # Name of the service (used for cluster-internal DNS, default: component name)
type: ClusterIP # Type of the service (default: ClusterIP, only ClusterIP is supported)
ports:
- port: [number] # External port exposed by the service
containerPort: [number] # Port of the container that the service redirects traffic to (default: value of port option)
externalIPs:
- 123.45.67.890 # ExternalIP to expose the service on (discouraged)
FAQ
How do I create high-availability services?
If you want fault-tolerance for your services, you can define that your components run in a replicated way. Generally, incoming traffic for a service will be forwarded to a randomly selected replica of the service's component. However, if one of the components become unhealthy, Kubernetes will automatically forward traffic to the other available replicas. To allow Kubernetes to know which of your containers are unhealthy, you need to define health checks
How should containers within the same component communicate?
DevSpace automatically defines a pod for each of your components, i.e. all containers that you define in the same components in your chart/values.yaml
will be in the same pod and can communicate via localhost
.
How do should containers communicate across different components?
If you want a container A to access a container B running inside another component, you should define a service pointing to container B.
What are pods?
Pods are groups of containers which share the same network stack. That means that containers within the same pod can communicate via localhost
. It also means that two containers cannot use the same port for an application, i.e. if one containers starts an application on port 3000, all other containers within the same pod cannot use this port anymore.
Each pod within your Space will get a cluster-internal IP address of the format 10.X.X.X
.
What are services?
Services are used for inter-pod communication. Each service within your Space will get a cluster-internal IP address of the format 10.X.X.X
which can be used to connect to the service. However, you should not connect directly to this IP address. Instead, you should connect to the DNS name of this service which is simply the name of the service.
Altough you can directly use the IP addresses of your containers/pods or of your services for internal communication, you should use the (DNS) name of a service instead because the IP addresses might change.