How to create LoadBalancer service on Pod created by ReplicaSet/Deployment


orlando

I'm using a ReplicaSet to manage my pods and I'm trying to expose these pods using a service. Pods created by a ReplicaSet have random 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

Since some policy restricts my usage, I try to expose these pods through a service hostNetwork=true. I can expose them by creating a service NodePortfor each pod kubectl expose pod worker-xxxxx --type=NodePort.

This is obviously not a flexible approach. I don't know how to create a service (maybe of type LoadBalancer?) to dynamically access all replicas in my ReplicaSet. It would also be perfect if it came with a Deployment.

Thanks for your help and advice!

edit:

I put a label on the ReplicaSet and a service called select the workerlabel on a service of type NodePort . But I can't get into ping workerany of the pods. What is the correct way to do it?

Here's how kubectl describe service workerto give. The pods are picked up as Endpointsshown.

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>
Spazzy757

I believe you can optimize this a bit by using Deployments instead of ReplicaSets (which is now the standard approach), i.e. you can deploy like this:

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 matching service will 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

Related


Kubernetes pod not created for service

Boban I have a jenkins-agent Kubernetes service. But as I can see, no pod is created for the service. How to initiate pod creation from existing service? This is what my service configuration looks like: Name: ci-jenkins-agent Namespace: c

How to use existing LoadBalancer in Azure AKS service?

dbourcet I created AKS cluster using Terraform. I want the cluster to have a LoadBalancer and a static public IP, and I want them to be pre-existing in my Ingress Controller/LoadBalancer service definition because I don't want the Kubernetes manifest to dynami

How to use existing LoadBalancer in Azure AKS service?

dbourcet I created AKS cluster using Terraform. I want the cluster to have a LoadBalancer and a static public IP, and I want them to be pre-existing in my Ingress Controller/LoadBalancer service definition because I don't want the Kubernetes manifest to dynami

How to use existing LoadBalancer in Azure AKS service?

dbourcet I created AKS cluster using Terraform. I want the cluster to have a LoadBalancer and a static public IP, and I want them to be pre-existing in my Ingress Controller/LoadBalancer service definition because I don't want the Kubernetes manifest to dynami

kubernetes LoadBalancer service

Kaidu Trying to teach myself how to use Kubernetes, and ran into some problems. I was able to setup a cluster, deploy an nginx image, and then access nginx using a service of type NodePort (after adding the port to the node's security group inbound rules). My

kubernetes LoadBalancer service

Kaidu Trying to teach myself how to use Kubernetes, and ran into some problems. I was able to setup a cluster, deploy an nginx image, and then access nginx using a service of type NodePort (after adding the port to the node's security group inbound rules). My