How to select a specific pod for a service in Kubernetes
Asked Answered
A

2

13

I have a kubernetes cluster of 3 hosts where each Host has a unique id label. On this cluster, there is a software that has 3 instances (replicas).

Each replica requires to talk to all other replicas. In addition, there is a service that contains all pods so that this application is permanently available.

So I have:

Instance1 (with labels run: theTool,instanceid: 1)
Instance2 (with labels run: theTool,instanceid: 2)
Instance3 (with labels run: theTool,instanceid: 3)

and

Service1 (selecting pods with label instanceid=1)
Service2 (selecting pods with label instanceid=2)
Service3 (selecting pods with label instanceid=3)
Service (selecting pods with label run=theTool)

This approach works but have I cannot scale or use the rolling-update feature.

I would like to define a deployment with 3 replicas, where each replicate gets a unique generic label (for instance the replica-id like 1/3, 2/3 and so on).

Within the services, I could use the selector to fetch this label which will exist even after an update.

Another solution might be to select the pod/deployment, depending on the host where it is running on. I could use a DaemonSet or just a pod/deployment with affinity to ensure that each host has an exact one replica of my deployment.

But I didn't know how to select a pod based on a host label where it runs on.

Using the hostname is not an option as hostnames will change in different environments.

I have searched the docs but didn't find anything matching this use case. Hopefully, someone here has an idea how to solve this.

Accumulate answered 15/12, 2016 at 14:29 Comment(0)
C
13

The feature you're looking for is called StatefulSets, which just launched to beta with Kubernetes 1.5 (note that it was previously available in alpha under a different name, PetSets).

In a StatefulSet, each replica has a unique name that is persisted across restarts. In your example, these would be instance-1, instance-2, instance-3. Since the instance names are persisted (even if the pod is recreated on another node), you don't need a service-per-instance.

The documentation has more details:

Coreligionist answered 15/12, 2016 at 18:42 Comment(2)
I successfully created a StatefulSet but after that i discovered that it is currently not possible to resolve the ip for a pod by name.Accumulate
Ok, found the issue. The service MUST be headless. Then it is possible to resolve the pody by using <petname>.<service>.<namespace>.svc.<cluster_domain>Accumulate
P
0

You can map NodeIP:NodePort with PodIP:PodPort. Your pod is running on some Node(Instance/VM).

  1. Assign Label to your nodes ,

    http://kubernetes.io/docs/user-guide/node-selection/

  2. Write a service for your pod , for example

service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: mysql-service
  labels:
    label: mysql-service
spec:
  type: NodePort
  ports:
  - port: 3306 #Port on which your service is running
    nodePort: 32001 # Node port on which you can access it statically
    targetPort: 3306
    protocol: TCP
    name: http

  selector:
    name: mysql-selector   #bind pod here
  1. Add node selector (in spec field) to your deployment.yaml

deployment.yaml:

spec:
  nodeSelector:
    nodename: mysqlnode #labelkey=labelname assigned in first step

With this you will be able to access your pod service with Nodeip:Nodeport. If I labeled node 10.11.20.177 with ,

nodename=mysqlnode

I will add in node selector ,

nodeSelector:
  nodename : mysqlnode 

I specified in service nodePort so now I can access pod service (Which is running in container)

10.11.20.177:32001

But this node should be in same network so it can access pod. For outside access make 32001 accessible publicaly with firewall configuration. It is static forever. Label will take care of your dynamic pod ips.

Pains answered 15/12, 2016 at 15:16 Comment(1)
The problem with this approach is that I need to know the IPs or resolveable dns names of the other pods while starting in order for them to sync. They need to be aware of each other.Accumulate

© 2022 - 2024 — McMap. All rights reserved.