Another solution to the one suggested in above answer is to make use of Headless Service and some reverse-proxy/load balancer like HAProxy(I used it).
So you can have a headless service or change the existing service which is exposing your pods like below. It is the configuration clusterIP:None which will be responsible for creating a headless service.
apiVersion: v1
kind: Service
metadata:
name: some-service
namespace: dev
labels:
app: <labels here>
spec:
ports:
- port: <port>
targetPort: <tport>
selector:
app: <selectors here>
clusterIP: None
Then you can deploy HAProxy like below
apiVersion: apps/v1
kind: Deployment
metadata:
name: haproxy-headless
namespace: dev
labels:
app: haproxy-headless
spec:
replicas: 1
selector:
matchLabels:
app: haproxy-headless
template:
metadata:
labels:
app: haproxy-headless
spec:
containers:
- name: haproxy-headless
image: haproxy:1.9
ports:
- containerPort: 8888
name: management
- containerPort: 8085
name: http
volumeMounts:
- name: haproxy-config
mountPath: "/usr/local/etc/haproxy/haproxy.cfg"
volumes:
- name: haproxy-config
configMap:
name: haproxy-config
ConfigMap for Haproxy:
apiVersion: v1
kind: ConfigMap
metadata:
name: haproxy-config
namespace: dev
data:
haproxyconfig.cfg: |
defaults
mode tcp
log global
option httplog
retries 5
timeout connect 10s
timeout client 300s
timeout server 300s
resolvers test_resolver
nameserver dns1 <your dns server address>
resolve_retries 30
timeout retry 2s
hold valid 10s
accepted_payload_size 8192
frontend stats
mode http
bind :8888
stats enable
stats uri /
stats refresh 15s
frontend test_fe
bind :8085
timeout client 60s
default_backend test_be
backend test_be
balance leastconn
server-template srv 7 <service-name>.<your namespace>.svc.cluster.local:6311 check resolvers test_resolver
Important thing here to understand how we configure our HAProxy here server-template is being used for service discovery using the DNS of K8s and you can use balance leastconn to make sure the least used server is used.
In the end a service would be required for the HAProxy
apiVersion: v1
kind: Service
metadata:
name: haproxy-service
namespace: dev
spec:
selector:
app: haproxy-headless
ports:
- name: be
protocol: TCP
port: 6311
targetPort: 8085
- name: mngmnt
protocol: TCP
port: 8888
targetPort: 8888
type: <Type as per requirement>
Now use the above service whenever you want to access the app. Maybe you can read about how to use HAProxy in k8s for load balancing it basically doing service discovery + load balancing.
Maybe a few configurations shown in the yaml are not correct or mismatched but i hope concept is clear about how to achieve this.