From 1357a13f3f692ac9ed41737084bc55db8bd8ea28 Mon Sep 17 00:00:00 2001 From: Robert-Rino Date: Tue, 24 Oct 2023 23:31:09 +0800 Subject: [PATCH 1/2] WIP --- k8s-udp-session-affanity/Dockerfile | 10 ++++++++++ k8s-udp-session-affanity/README.md | 13 ++++++++++++ k8s-udp-session-affanity/app.py | 21 ++++++++++++++++++++ k8s-udp-session-affanity/k8s/deployment.yaml | 0 k8s-udp-session-affanity/k8s/service.yaml | 0 5 files changed, 44 insertions(+) create mode 100644 k8s-udp-session-affanity/Dockerfile create mode 100644 k8s-udp-session-affanity/README.md create mode 100644 k8s-udp-session-affanity/app.py create mode 100644 k8s-udp-session-affanity/k8s/deployment.yaml create mode 100644 k8s-udp-session-affanity/k8s/service.yaml diff --git a/k8s-udp-session-affanity/Dockerfile b/k8s-udp-session-affanity/Dockerfile new file mode 100644 index 0000000..ee2df8d --- /dev/null +++ b/k8s-udp-session-affanity/Dockerfile @@ -0,0 +1,10 @@ +FROM python:3.11.6-alpine3.18 + +ENV HOST=0.0.0.0 +ENV PORT= + +WORKDIR /usr/src/app +COPY . . + +CMD ["python", "app.py"] + diff --git a/k8s-udp-session-affanity/README.md b/k8s-udp-session-affanity/README.md new file mode 100644 index 0000000..9c6206c --- /dev/null +++ b/k8s-udp-session-affanity/README.md @@ -0,0 +1,13 @@ +Test script + +```python + +import socket + +UDP_PORT = 60002 +UDP_IP = '127.0.0.1' +MESSAGE = 'ping' + +sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP +sock.sendto(bytes(MESSAGE, "utf-8"), (UDP_IP, UDP_PORT)) +``` diff --git a/k8s-udp-session-affanity/app.py b/k8s-udp-session-affanity/app.py new file mode 100644 index 0000000..8c91b5b --- /dev/null +++ b/k8s-udp-session-affanity/app.py @@ -0,0 +1,21 @@ +#!/usr/bin/python3 +import socket +import os + +def loop_on_socket(s): + while True: + d, addr = s.recvfrom(1500) + print(d, addr) + s.sendto("ECHO: ".encode('utf8')+d, addr) + +if __name__ == "__main__": + # Instead of setting HOST to "0.0.0.0", + # we set HOST to the Load Balancer IP + HOST = os.environ.get('HOST') or '0.0.0.0' + PORT = os.environ.get('PORT') or 60002 + sock = socket.socket(type=socket.SocketKind.SOCK_DGRAM) + sock.bind((HOST, PORT)) + loop_on_socket(sock) + +# 198.51.100.2 is the load balancer's IP address +# You can also use the DNS name of the load balancer's IP address diff --git a/k8s-udp-session-affanity/k8s/deployment.yaml b/k8s-udp-session-affanity/k8s/deployment.yaml new file mode 100644 index 0000000..e69de29 diff --git a/k8s-udp-session-affanity/k8s/service.yaml b/k8s-udp-session-affanity/k8s/service.yaml new file mode 100644 index 0000000..e69de29 From 6f893a606c8be790a532d07a10ee27e3c0f34b17 Mon Sep 17 00:00:00 2001 From: Robert-Rino Date: Sun, 26 Nov 2023 22:15:20 +0800 Subject: [PATCH 2/2] WIP --- k8s-udp-session-affanity/README.md | 22 ++++++++ k8s-udp-session-affanity/app.py | 44 +++++++++------ k8s-udp-session-affanity/client.py | 12 +++++ k8s-udp-session-affanity/k8s/deployment.yaml | 57 ++++++++++++++++++++ k8s-udp-session-affanity/k8s/service.yaml | 55 +++++++++++++++++++ 5 files changed, 173 insertions(+), 17 deletions(-) create mode 100644 k8s-udp-session-affanity/client.py diff --git a/k8s-udp-session-affanity/README.md b/k8s-udp-session-affanity/README.md index 9c6206c..711b9fe 100644 --- a/k8s-udp-session-affanity/README.md +++ b/k8s-udp-session-affanity/README.md @@ -1,3 +1,22 @@ +Build image + +```shell +DOCKER_DEFAULT_PLATFORM=linux/amd64 docker build -t a901002666/k8s-udp-session-affanity . +``` + +Run image + +```shell +docker run -it --rm a901002666/k8s-udp-session-affanity +``` + +Send udp pack +```shell +echo 'nino' | nc -4u -w1 localhost 10080 +``` + + + Test script ```python @@ -11,3 +30,6 @@ MESSAGE = 'ping' sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP sock.sendto(bytes(MESSAGE, "utf-8"), (UDP_IP, UDP_PORT)) ``` + + +echo 'nino' | nc -u -w1 104.199.252.119 10080 diff --git a/k8s-udp-session-affanity/app.py b/k8s-udp-session-affanity/app.py index 8c91b5b..3d963f2 100644 --- a/k8s-udp-session-affanity/app.py +++ b/k8s-udp-session-affanity/app.py @@ -1,21 +1,31 @@ -#!/usr/bin/python3 +# UDP echo server import socket -import os -def loop_on_socket(s): - while True: - d, addr = s.recvfrom(1500) - print(d, addr) - s.sendto("ECHO: ".encode('utf8')+d, addr) +HOST = "0.0.0.0" # Standard loopback interface address (localhost) +PORT = 10080 # Port to listen on (non-privileged ports are > 1023) -if __name__ == "__main__": - # Instead of setting HOST to "0.0.0.0", - # we set HOST to the Load Balancer IP - HOST = os.environ.get('HOST') or '0.0.0.0' - PORT = os.environ.get('PORT') or 60002 - sock = socket.socket(type=socket.SocketKind.SOCK_DGRAM) - sock.bind((HOST, PORT)) - loop_on_socket(sock) +s = socket.socket(socket.AF_INET , socket.SOCK_DGRAM) +s.bind((HOST, PORT)) +print("Server started ...192.168.225..242:2222") +print("Waiting for Client response...") +while True: + print(s.recvfrom(1024)) -# 198.51.100.2 is the load balancer's IP address -# You can also use the DNS name of the load balancer's IP address + +# # TCP echo server +# import socket + +# HOST = "0.0.0.0" # Standard loopback interface address (localhost) +# PORT = 1935 # Port to listen on (non-privileged ports are > 1023) + +# with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: +# s.bind((HOST, PORT)) +# s.listen() +# conn, addr = s.accept() +# with conn: +# print(f"Connected by {addr}") +# while True: +# data = conn.recv(1024) +# if not data: +# break +# conn.sendall(data) diff --git a/k8s-udp-session-affanity/client.py b/k8s-udp-session-affanity/client.py new file mode 100644 index 0000000..d0f02b9 --- /dev/null +++ b/k8s-udp-session-affanity/client.py @@ -0,0 +1,12 @@ +import socket + +#client program + +s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) + +while True: + ip ,port = input("Enter server ip address and port number :\n").split() + m = input("Enter data to send server: ") + res = s.sendto(m.encode(),("192.168.225.242",2222)) + if res: + print("\nsuccessfully send") diff --git a/k8s-udp-session-affanity/k8s/deployment.yaml b/k8s-udp-session-affanity/k8s/deployment.yaml index e69de29..a94e046 100644 --- a/k8s-udp-session-affanity/k8s/deployment.yaml +++ b/k8s-udp-session-affanity/k8s/deployment.yaml @@ -0,0 +1,57 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: udp-app-srt + labels: + app: ingest-srs-v5-srt +spec: + replicas: 1 + selector: + matchLabels: + app: ingest-srs-v5 + protocol: udp + template: + metadata: + labels: + app: ingest-srs-v5 + protocol: udp + spec: + containers: + - name: app + image: a901002666/k8s-udp-session-affanity:udp + # env: + # - name: LB_IP + # value: '10.8.7.210' + ports: + - name: srt + protocol: UDP + containerPort: 10080 + +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: udp-app-rtmp + labels: + app: ingest-srs-v5-rtmp +spec: + replicas: 1 + selector: + matchLabels: + app: ingest-srs-v5 + protocol: tcp + template: + metadata: + labels: + app: ingest-srs-v5 + protocol: tcp + spec: + containers: + - name: app + image: a901002666/k8s-udp-session-affanity:tcp + # env: + # - name: LB_IP + # value: '10.8.7.210' + ports: + - name: rtmp + containerPort: 1935 diff --git a/k8s-udp-session-affanity/k8s/service.yaml b/k8s-udp-session-affanity/k8s/service.yaml index e69de29..2f8e052 100644 --- a/k8s-udp-session-affanity/k8s/service.yaml +++ b/k8s-udp-session-affanity/k8s/service.yaml @@ -0,0 +1,55 @@ +apiVersion: v1 +kind: Service +metadata: + namespace: default + name: ingest-srs-v5 + labels: + app.kubernetes.io/name: livestream + # annotations: + # cloud.google.com/l4-rbs: "enabled" +spec: + type: NodePort + # externalTrafficPolicy: Cluster + selector: + app: ingest-srs-v5 + ports: + - name: rtmp + protocol: TCP + port: 1935 + targetPort: rtmp + - name: srt + protocol: UDP + port: 10080 + targetPort: srt + +--- + +apiVersion: v1 +kind: Service +metadata: + name: ingest-srs-v5-external-tcp +spec: + type: LoadBalancer + selector: + app.kubernetes.io/name: livestream + ports: + - port: 1935 + targetPort: 1935 + protocol: TCP + loadBalancerIP: "35.221.161.73" + +--- + +apiVersion: v1 +kind: Service +metadata: + name: ingest-srs-v5-external-udp +spec: + type: LoadBalancer + selector: + app.kubernetes.io/name: livestream + ports: + - port: 10080 + targetPort: 10080 + protocol: UDP + loadBalancerIP: "35.221.161.73"