How do I create a LoadBalancer service over Pods created by a ReplicaSet/Deployment

9/18/2019

I'm using a ReplicaSet to manage my pods and I try to expose these pods with a service. The Pods created by a ReplicaSet have randomized names.

NAME                   READY   STATUS    RESTARTS   AGE
master                 2/2     Running   0          20m
worker-4szkz           2/2     Running   0          21m
worker-hwnzt           2/2     Running   0          21m

I try to expose these Pods with a Service, since some policies restrict me to use hostNetwork=true. I'm able to expose them by creating a NodePort service for each Pod with kubectl expose pod worker-xxxxx --type=NodePort.

This is clearly not a flexible way. I wonder how to create a Service (LoadBalancer type maybe?) to access to all the replicas dynamically in my ReplicaSet. If that comes with a Deployment that would be perfect too.

Thanks for any help and advice!

Edit:

I put a label on my ReplicaSet and a NodePort type Service called worker selecting that label. But I'm not able to ping worker in any of my pods. What's the correct way of doing this?

Below is how the kubectl describe service worker gives. As the Endpoints show the pods are picked up.

Name:                     worker
Namespace:                default
Annotations:              <none>
Selector:                 tag=worker
Type:                     NodePort
IP:                       10.106.45.174
Port:                     port1  29999/TCP
TargetPort:               29999/TCP
NodePort:                 port1  31934/TCP
Endpoints:                10.32.0.3:29999,10.40.0.2:29999
Port:                     port2  29996/TCP
TargetPort:               29996/TCP
NodePort:                 port2  31881/TCP
Endpoints:                10.32.0.3:29996,10.40.0.2:29996
Port:                     port3  30001/TCP
TargetPort:               30001/TCP
NodePort:                 port3  31877/TCP
Endpoints:                10.32.0.3:30001,10.40.0.2:30001
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>
-- OrlandoL
kubernetes

1 Answer

9/19/2019

I Believe that you can optimize this a bit by using Deployments instead of ReplicaSets (This is now the standard way), i.e you could have a deployment as follows:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80

Then your service to match this would be:

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  # This is the important part as this is what is used to route to 
  # the pods created by your deployment
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
-- Spazzy757
Source: StackOverflow