<p>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.</p>

<pre><code>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
</code></pre>

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

<p>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.</p>

<p>Thanks for any help and advice!</p>

<p><strong>Edit:</strong> </p>

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

<p>Below is how the <code>kubectl describe service worker</code> gives. As the <code>Endpoints</code> show the pods are picked up.</p>

<pre><code>Name:                     worker
Namespace:                default
Annotations:              &lt;none&gt;
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:                   &lt;none&gt;
</code></pre>

OrlandoL
<p>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:</p>

<pre><code>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
</code></pre>

<p>Then your service to match this would be:</p>

<pre><code>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
</code></pre>
