⎈ k8s knowledge compiler

Service [page]deterministic

Expose an application running in your cluster behind a single outward-facing endpoint, even when the workload is split across multiple backends.

conceptsnetworking

[definition:service]

A key aim of Services in Kubernetes is that you don't need to modify your existing application to use an unfamiliar service discovery mechanism. You can run code in Pods, whether this is a code designed for a cloud-native world, or an older app you've containerized. You use a Service to make that set of Pods available on the network so that clients can interact with it.

If you use a [deployment](#gloss:deployment) to run your app, that Deployment can create and destroy Pods dynamically. From one moment to the next, you don't know how many of those Pods are working and healthy; you might not even know what those healthy Pods are named. Kubernetes [Pods](#gloss:pod) are created and destroyed to match the desired state of your cluster. Pods are ephemeral resources (you should not expect that an individual Pod is reliable and durable).

Each Pod gets its own IP address (Kubernetes expects network plugins to ensure this). For a given Deployment in your cluster, the set of Pods running in one moment in time could be different from the set of Pods running that application a moment later.

This leads to a problem: if some set of Pods (call them "backends") provides functionality to other Pods (call them "frontends") inside your cluster, how do the frontends find out and keep track of which IP address to connect to, so that the frontend can use the backend part of the workload?

Enter _Services_.

## Services in Kubernetes

The Service API, part of Kubernetes, is an abstraction to help you expose groups of Pods over a network. Each Service object defines a logical set of endpoints (usually these endpoints are Pods) along with a policy about how to make those pods accessible.

For example, consider a stateless image-processing backend which is running with 3 replicas. Those replicas are fungible—frontends do not care which backend they use. While the actual Pods that compose the backend set may change, the frontend clients should not need to be aware of that, nor should they need to keep track of the set of backends themselves.

The Service abstraction enables this decoupling.

The set of Pods targeted by a Service is usually determined by a [selector](#gloss:selector) that you define. To learn about other ways to define Service endpoints, see [Services _without_ selectors](#services-without-selectors).

If your workload speaks HTTP, you might choose to use an [Ingress](/docs/concepts/services-networking/ingress/) to control how web traffic reaches that workload. Ingress is not a Service type, but it acts as the entry point for your cluster. An Ingress lets you consolidate your routing rules into a single resource, so that you can expose multiple components of your workload, running separately in your cluster, behind a single listener.

The [Gateway](https://gateway-api.sigs.k8s.io/#what-is-the-gateway-api) API for Kubernetes provides extra capabilities beyond Ingress and Service. You can add Gateway to your cluster - it is a family of extension APIs, implemented using [CustomResourceDefinitions](#gloss:CustomResourceDefinition) - and then use these to configure access to network services that are running in your cluster.

### Cloud-native service discovery

If you're able to use Kubernetes APIs for service discovery in your application, you can query the [API server](#gloss:kube-apiserver) for matching EndpointSlices. Kubernetes updates the EndpointSlices for a Service whenever the set of Pods in a Service changes.

For non-native applications, Kubernetes offers ways to place a network port or load balancer in between your application and the backend Pods.

Either way, your workload can use these [service discovery](#discovering-services) mechanisms to find the target it wants to connect to.

## Defining a Service

A Service is an [object](#gloss:object) (the same way that a Pod or a ConfigMap is an object). You can create, view or modify Service definitions using the Kubernetes API. Usually you use a …(trimmed)

Sources

concepts/services-networking/service.md · docService

Related (25)

references Deploymentdeployment conf=1
references PodPods conf=1
references Selectorselector conf=1
references CustomResourceDefinitionCustomResourceDefinitions conf=1
references API serverAPI server conf=1
references Objectobject conf=1
references EndpointSliceEndpointSlices conf=1
references Namespacenamespace conf=1
references Labellabel conf=1
references Annotationannotation conf=1
defines Service conf=1
part_of Services in Kubernetesdescribes conf=1
part_of Defining a Servicedescribes conf=1
part_of Headless Servicesdescribes conf=1
part_of Discovering servicesdescribes conf=1
part_of Virtual IP addressing mechanismdescribes conf=1
part_of External IPsdescribes conf=1
part_of API Objectdescribes conf=1
part_of {{% heading "whatsnext" %}}describes conf=1
part_of Cloud-native service discoverydescribes conf=1
part_of Port definitions {#field-spec-ports}describes conf=1
part_of Services without selectorsdescribes conf=1
part_of EndpointSlicesdescribes conf=1
part_of Endpoints (deprecated) {#endpoints}describes conf=1

← all Docs