service - pod traffic - selector

7/1/2020

I created 1) pod p1; service p1-s1; curl output - pod1 - Works! 2) pod p2, service p2-s2 curl output - pod2 - Works! 3) I created a combined service in the selector i chose both the labels app=p1 and app=p2 and observed that the last selector is picked and the traffic doesn't route to the first one. is it expected behavior?

if not how can i redirect traffic?

$ kubectl get pods --show-labels
NAME                      READY   STATUS    RESTARTS   AGE    LABELS
p1                        1/1     Running   0          24s    app=p1
p2                        1/1     Running   0          21s    app=p2

$ kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                         AGE
p1-s1        NodePort    10.105.49.230    <none>        8888:30778/TCP                  45s
p2-s2        NodePort    10.96.194.44     <none>        9999:32386/TCP                  42s

$ curl 172.17.0.2:30778
 "Pod1" 



$ curl 172.17.0.2:32386
 "Pod2"

combined service

selector: app: p1 app: p2 ports: - name: p1 port: 8888 targetPort: 3000 - name: p2 port: 9999 targetPort: 3000

$ kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE combined NodePort 10.107.182.9 <none> 8888:30430/TCP,9999:30014/TCP 39s

$ curl 172.17.0.2:30430 "Pod2"

$ curl 172.17.0.2:30014 "Pod2"

-- mgs
kubernetes
minikube

1 Answer

7/2/2020

".. because the next key overwrites the previous key with the same name", The service creates only one endpoint at the end of it based on the last selector. Hence the traffic is redirected to the pod last key-value mentioned. Read the trail for details. Thank you @zerkms for the clarity.

-- mgs
Source: StackOverflow