Skip to content

Commit

Permalink
vL3 load balancer example (#9830)
Browse files Browse the repository at this point in the history
* vL3 load balancer example

Signed-off-by: Artem Glazychev <[email protected]>

* Ref

Signed-off-by: Artem Glazychev <[email protected]>

---------

Signed-off-by: Artem Glazychev <[email protected]>
  • Loading branch information
glazychev-art authored Sep 26, 2023
1 parent 565e732 commit d601478
Show file tree
Hide file tree
Showing 12 changed files with 276 additions and 0 deletions.
6 changes: 6 additions & 0 deletions apps/vl3-lb/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
- lb.yaml
55 changes: 55 additions & 0 deletions apps/vl3-lb/lb.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: vl3-lb
spec:
selector:
matchLabels:
app: vl3-lb
template:
metadata:
labels:
app: vl3-lb
"spiffe.io/spiffe-id": "true"
spec:
containers:
- name: lb
image: ghcr.io/networkservicemesh/ci/cmd-lb-vl3-vpp:cd64380
imagePullPolicy: IfNotPresent
env:
- name: SPIFFE_ENDPOINT_SOCKET
value: unix:///run/spire/sockets/agent.sock
- name: NSM_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAME
value: "$(NSM_NAME)"
- name: NSM_PORT
value: "80"
- name: NSM_LOG_LEVEL
value: TRACE
volumeMounts:
- name: spire-agent-socket
mountPath: /run/spire/sockets
readOnly: true
- name: nsm-socket
mountPath: /var/lib/networkservicemesh
readOnly: true
resources:
requests:
cpu: 150m
memory: 100Mi
limits:
cpu: 300m
memory: 300Mi
volumes:
- name: spire-agent-socket
hostPath:
path: /run/spire/sockets
type: Directory
- name: nsm-socket
hostPath:
path: /var/lib/networkservicemesh
type: DirectoryOrCreate
79 changes: 79 additions & 0 deletions examples/features/vl3-lb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# vL3 Load Balancer

This example shows what is a vL3 Load Balancer (LB) and how it works.

## Description

We all know and use Kubernetes Services in our work.
Service is a method for exposing a network application that is running as one or more Pods in your cluster. It distributes traffic across a set of selected Pods.

To define a Kubernetes Service:
```
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app.kubernetes.io/name: MyApp
ports:
- protocol: TCP
port: 80
targetPort: 9376
```
Now you can reach the service by its name.

It turned out that we can use a similar mechanism in our vL3 networks - we can use a **vL3 Load Balancer**.
Being on the same vL3 network, clients can behave like pods - they can communicate with each other. Using selectors, we can combine some of them to implement a service.
vL3-LB will help us with this. We will call these clients that implement the service as _"real servers"_.

So, vL3-LB has the following parameters:

| NSM env | Description |
|--------------------|--------------------------------------------------------------------|
| `NSM_SELECTOR` | labels that group the vL3 clients we need to implement any service |
| `NSM_PROTOCOL` | TCP or UDP IP protocol |
| `NSM_PORT` | LB port |
| `NSM_TARGET_PORT` | real server port |

vL3-LB is a vL3-network client that monitors the real servers using `NSM_SELECTOR`. It has its own DNS name that we can use.<br />

**Example of monitoring:**

![NSM vL3 Diagram](./diagram1.svg "vL3-LB monitoring")
<br />_Please note: The network configuration is given as an example. It can have a different number of elements._
<br /><br />
In the current example, we want to get a _finance_ service for processing **http requests**. <br />
After calling the command `$:curl finance:8080`, the DNS name is converted to the IP address of the Load Balancer. <br />
When the http request reaches the balancer, it converts the destination address to the address of the real server.

**Example of the data path:**<br />

![NSM vL3 Diagram](./diagram2.svg "vL3-LB data path")
<br />_Please note: The IP addresses are given as an example, they may change from run to run._

## Run

Deploy the vL3 network service, vL3-NSE, vL3-LB, finance-servers and finance-client (the last 3 are actually clients of the vL3 network) (see `kustomization.yaml`):
```bash
kubectl apply -k https://github.com/networkservicemesh/deployments-k8s/examples/features/vl3-lb?ref=fea72f963b344a0acdae636ceec6ea9f25fbfffb
```

Wait for vL3-clients to be ready:
```bash
kubectl wait --for=condition=ready --timeout=2m pod -l type=vl3-client -n ns-vl3-lb
```

Send an http-request from the finance-client:
```bash
kubectl exec deployments/finance-client -n ns-vl3-lb -- curl -s finance:8080 | grep "Hello! I'm finance-server"
```
In the response you will see the name of the real server that performed the processing.
If you run the command above many times, you will see that load balancing occurs and the responses are returned by different handlers.

## Cleanup

To clean up the example just follow the next command:
```bash
kubectl delete ns ns-vl3-lb
```
4 changes: 4 additions & 0 deletions examples/features/vl3-lb/diagram1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions examples/features/vl3-lb/diagram2.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions examples/features/vl3-lb/finance-client.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: finance-client
spec:
replicas: 1
selector:
matchLabels:
app: finance-client
template:
metadata:
labels:
app: finance-client
type: vl3-client
annotations:
networkservicemesh.io: kernel://vl3-lb/nsm-1
spec:
containers:
- name: nettools
image: travelping/nettools:1.10.1
imagePullPolicy: IfNotPresent
# simple `sleep` command would work
# but we need `trap` to be able to delete pods quckly
command: ["/bin/sh", "-c", "trap : TERM INT; sleep infinity & wait"]
24 changes: 24 additions & 0 deletions examples/features/vl3-lb/finance-lb-patch.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: vl3-lb
spec:
template:
metadata:
labels:
type: vl3-client
spec:
containers:
- name: lb
env:
- name: POD_NAME
value: "finance"
- name: NSM_PORT
value: "8080"
- name: NSM_TARGET_PORT
value: "80"
- name: NSM_SELECTOR
value: "app:finance"
- name: NSM_NETWORK_SERVICES
value: "memif://vl3-lb/nsm-1"
31 changes: 31 additions & 0 deletions examples/features/vl3-lb/finance-server.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: finance-server
spec:
replicas: 3
selector:
matchLabels:
app: finance-server
template:
metadata:
labels:
app: finance-server
type: vl3-client
annotations:
networkservicemesh.io: kernel://vl3-lb/nsm-1?app=finance
spec:
containers:
- name: nginx
image: hashicorp/http-echo:alpine
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
args:
- "-text=Hello! I'm $(POD_NAME)"
- -listen=:80
ports:
- containerPort: 80
18 changes: 18 additions & 0 deletions examples/features/vl3-lb/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

namespace: ns-vl3-lb

resources:
- ns-vl3-lb.yaml
- netsvc.yaml
- finance-client.yaml
- finance-server.yaml
- ../../../apps/nse-vl3-vpp
- ../../../apps/vl3-ipam
- ../../../apps/vl3-lb

patchesStrategicMerge:
- nse-patch.yaml
- finance-lb-patch.yaml
7 changes: 7 additions & 0 deletions examples/features/vl3-lb/netsvc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
apiVersion: networkservicemesh.io/v1
kind: NetworkService
metadata:
name: vl3-lb
spec:
payload: IP
5 changes: 5 additions & 0 deletions examples/features/vl3-lb/ns-vl3-lb.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
apiVersion: v1
kind: Namespace
metadata:
name: ns-vl3-lb
18 changes: 18 additions & 0 deletions examples/features/vl3-lb/nse-patch.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nse-vl3-vpp
labels:
app: nse-vl3-vpp
spec:
replicas: 1
template:
spec:
containers:
- name: nse
env:
- name: NSM_SERVICE_NAMES
value: "vl3-lb"
- name: NSM_REGISTER_SERVICE
value: "false"

0 comments on commit d601478

Please sign in to comment.