Skip to content

Instantly share code, notes, and snippets.

@rosenhouse
Last active May 13, 2021 22:31
Show Gist options
  • Save rosenhouse/1233e9223015dd61c70e35b6cff958a7 to your computer and use it in GitHub Desktop.
Save rosenhouse/1233e9223015dd61c70e35b6cff958a7 to your computer and use it in GitHub Desktop.
kubernetes per-port backend selection example
# shows how kubernetes kubeproxy can select backends based on which port of a multi-port service is being accessed
# nginx is only listening on port 80
# kuard is only listening on port 8080
# the partial-ports service exposes both 80 and 8080
#
# kubectl -n foo run -it --rm --restart=Never --image=curlimages/curl:7.76.1 test1 -- curl partial-ports:80
# and
# kubectl -n foo run -it --rm --restart=Never --image=curlimages/curl:7.76.1 test1 -- curl partial-ports:8080
# both succeed 100% of the time
---
apiVersion: v1
kind: Namespace
metadata:
name: foo
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-80
namespace: foo
spec:
replicas: 1
selector:
matchLabels:
app: nginx-80
template:
metadata:
labels:
app: nginx-80
spec:
containers:
- name: nginx
image: library/nginx:1.19.10
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-80
namespace: foo
spec:
selector:
app: nginx-80
ports:
- port: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: kuard-8080
namespace: foo
spec:
replicas: 1
selector:
matchLabels:
app: kuard-8080
template:
metadata:
labels:
app: kuard-8080
spec:
containers:
- name: kuard
image: gcr.io/kuar-demo/kuard-amd64:blue
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: kuard-8080
namespace: foo
spec:
selector:
app: kuard-8080
ports:
- port: 8080
---
apiVersion: v1
kind: Service
metadata:
name: partial-ports
namespace: foo
spec:
selector: {}
ports:
- port: 80
name: nginx-80
- port: 8080
name: kuard-8080
---
apiVersion: discovery.k8s.io/v1beta1
kind: EndpointSlice
metadata:
labels:
kubernetes.io/service-name: partial-ports
name: partial-ports-nginx-80
namespace: foo
addressType: IPv4
endpoints:
- addresses:
- 10.244.0.6 # ip of the nginx pod, hand-crafted at run-time
ports:
- name: nginx-80
port: 80
protocol: TCP
---
apiVersion: discovery.k8s.io/v1beta1
kind: EndpointSlice
metadata:
labels:
kubernetes.io/service-name: partial-ports
name: partial-ports-kuard-8080
namespace: foo
addressType: IPv4
endpoints:
- addresses:
- 10.244.0.7 # ip of the kuard pod, hand-crafted at run-time
ports:
- name: kuard-8080
port: 8080
protocol: TCP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment