How to select a specific pod for a service in Kubernetes
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:
You can map NodeIP:NodePort with PodIP:PodPort. Your pod is running on some Node(Instance/VM).
Assign Label to your nodes ,
Write a service for your pod , for example
service.yaml:
apiVersion: v1kind: Servicemetadata: name: mysql-service labels: label: mysql-servicespec: 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
- 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.