Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I specify service before deployment in a single Kubernetes configuration file?

I'm trying to understand why kubernetes docs recommend to specify service before deployment in one configuration file:

The resources will be created in the order they appear in the file. Therefore, it’s best to specify the service first, since that will ensure the scheduler can spread the pods associated with the service as they are created by the controller(s), such as Deployment.

Does it mean spread pods between kubernetes cluster nodes?

I tested with the following configuration where a deployment is located before a service and pods are distributed between nods without any issues.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: incorrect-order
  namespace: test
spec:
  selector:
    matchLabels:
      app: incorrect-order
  replicas: 2
  template:
    metadata:
      labels:
        app: incorrect-order
    spec:
      containers:
      - name: incorrect-order
        image: nginx
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: incorrect-order
  namespace: test
  labels:
    app: incorrect-order
spec:
  type: NodePort
  ports:
  - port: 80
  selector:
    app: incorrect-order

Another explanation is that some environment variables with service URL will not be set for pods in this case. However it also works ok in case a configuration is inside one file like the example above.

Could you please explain why it is better to specify service before the deployment in case of one configuration file? Or may be it is some outdated recommendation.

like image 532
yuppie-flu Avatar asked May 18 '18 10:05

yuppie-flu


People also ask

What is the role of a service in an Kubernetes deployment?

A Kubernetes service is a logical abstraction for a deployed group of pods in a cluster (which all perform the same function). Since pods are ephemeral, a service enables a group of pods, which provide specific functions (web services, image processing, etc.) to be assigned a name and unique IP address (clusterIP).

Why do we need service in Kubernetes?

A Service routes traffic across a set of Pods. Services are the abstraction that allows pods to die and replicate in Kubernetes without impacting your application. Discovery and routing among dependent Pods (such as the frontend and backend components in an application) are handled by Kubernetes Services.

Which service is default when we expose the deployment in Kubernetes?

The default protocol for Services is TCP; you can also use any other supported protocol. As many Services need to expose more than one port, Kubernetes supports multiple port definitions on a Service object. Each port definition can have the same protocol , or a different one.

What is the purpose of Kubernetes configuration file?

object configuration file / configuration file: A file that defines the configuration for a Kubernetes object. This topic shows how to pass configuration files to kubectl apply . Configuration files are typically stored in source control, such as Git.


1 Answers

If you use DNS as service discovery, the order of creation doesn't matter.

In case of Environment Vars (the second way K8S offers service discovery) the order matters, because once that vars are passed to the starting pod, they cannot be modified later if the service definition changes.

So if your service is deployed before you start your pod, the service envvars are injected inside the linked pod.

If you create a Pod/Deployment resource with labels, this resource will be exposed through a service once this last is created (with proper selector to indicate what resource to expose).

like image 152
Nicola Ben Avatar answered Oct 04 '22 00:10

Nicola Ben