2 different services for same DaemonSet K8s

523 Views Asked by At

I was wondering if there is a way to create a service for a pod on a specific node. For example:

Lets say I have a cluster with 4 worker nodes [compute-0 ... compute-3].

Nodes "compute-0" and "compute-1" have a label "app=firstApplication"

Nodes "compute-2" and "compute-3" have a different label "app=secondApplication"

I have a single Daemonset running across all 4 nodes.

I want to create 2 services, one for each couple of nodes. Is this possible somehow?

Thanks!

EDIT

The reason for what we are trying to do is that we have an Openshift4.6 cluster, and for security reasons we have VXLAN port blocked off between 2 groups of nodes. When pods try to resolve DNS queries using the default dns service (172.30.0.10), sometimes they access the dns pods on the blocked off nodes.

2

There are 2 best solutions below

7
On

No - this is not possible! Since services are referencing their Pods by Labels and all Pods in a DaemonSet are labelled the same, you can't do that. Of course, you could label your Pods after creation, but since this would be lost after recreation of the DaemonSet, I would not go down that route.

You could split your DaemonSet into parts and use Node Selectors or Affinity to control the distribution of Pods over Nodes.

If you specify a .spec.template.spec.nodeSelector, then the DaemonSet controller will create Pods on nodes which match that node selector. Likewise if you specify a .spec.template.spec.affinity, then DaemonSet controller will create Pods on nodes which match that node affinity.

That way, each DaemonSet can have its own Service.

5
On

You just need to patch existing pods. Add those label in your pods. May be you need to handle another operator. The job of the operator is to get the pods first. Then check if the desire label exist or not . If not exist patch the label of the pod. this is just like kubectl patch. With the help of kubeclient just update the label if the label is not exist in the pods. do some research about kubeclient. There are also an example sample-controller in kubernetes. Here is the link :

ref

if there are some extra label in pod just add them in selector.

---
kind: Service
apiVersion: v1
metadata:
  name: first-svc
  labels:
    app: firstApplication
spec:
  selector:
    app: firstApplication
  ports:
    - name: http 
      port: 8080
      targetPort: 8080

---
kind: Service
apiVersion: v1
metadata:
  name: second-svc
  labels:
    app: secondApplication
spec:
  selector:
    app: secondApplication
  ports:
    - name: http 
      port: 8080
      targetPort: 8080
---