diff --git a/edgemesh_HTTP_communication_demo/README.md b/edgemesh_HTTP_communication_demo/README.md new file mode 100644 index 00000000..18f09e1b --- /dev/null +++ b/edgemesh_HTTP_communication_demo/README.md @@ -0,0 +1,38 @@ +# Pod_communication_in_HTTP_through_EdgeMesh + +## Description + +- cloud pod can get random number created by edge pod through communicating in HTTP + +## Environment prepare + +- KubeEdge,k8s ,node,cloud and so on +- In cloud and edge, you should prepare for go version = 1.17 +- In cloud and edge, you should ensure edgemesh can run effectively + +## Deployment application + +1. Create image and upload it to the corresponding device + +2. prepare for cloud_Pod and edge_Pod + + ```sh + kubectl apply -f demo_edge.yaml + kubectl apply -f demo_cloud.yaml + ``` + +3. check pod + + + +4. check service + + + +5. access cloud pod + + + +6. check logs + + \ No newline at end of file diff --git a/edgemesh_HTTP_communication_demo/cloud/demo_cloud.yaml b/edgemesh_HTTP_communication_demo/cloud/demo_cloud.yaml new file mode 100644 index 00000000..3ca65029 --- /dev/null +++ b/edgemesh_HTTP_communication_demo/cloud/demo_cloud.yaml @@ -0,0 +1,45 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: demo-cloud + labels: + app: demo-cloud +spec: + replicas: 1 + selector: + matchLabels: + app: demo-cloud + template: + metadata: + labels: + app: demo-cloud + spec: + affinity: + nodeAffinity: + requiredDuringSchedulingIgnoredDuringExecution: + nodeSelectorTerms: + - matchExpressions: + - key: node-role.kubernetes.io/edge + operator: DoesNotExist + - key: node-role.kubernetes.io/agent + operator: DoesNotExist + containers: + - name: demo-cloud + image: cloud_demo:v1 + imagePullPolicy: IfNotPresent + ports: + - containerPort: 8001 +--- +apiVersion: v1 +kind: Service +metadata: + name: demo-cloud +spec: + type: NodePort + ports: + - port: 8001 + nodePort: 8001 + targetPort: 8001 + protocol: TCP + selector: + app: demo-cloud \ No newline at end of file diff --git a/edgemesh_HTTP_communication_demo/cloud/dockerfile b/edgemesh_HTTP_communication_demo/cloud/dockerfile new file mode 100644 index 00000000..9f49ac05 --- /dev/null +++ b/edgemesh_HTTP_communication_demo/cloud/dockerfile @@ -0,0 +1,5 @@ +FROM python:3.8 +WORKDIR /cloud_test +ADD . . +RUN pip3 install -r requirements.txt +CMD ["python3", "main.py"] diff --git a/edgemesh_HTTP_communication_demo/cloud/main.py b/edgemesh_HTTP_communication_demo/cloud/main.py new file mode 100644 index 00000000..98a792e8 --- /dev/null +++ b/edgemesh_HTTP_communication_demo/cloud/main.py @@ -0,0 +1,44 @@ +import tornado.ioloop +import tornado.httpserver +import tornado.web +import tornado.options +from tornado.options import define, options +import requests +import logging +import json +logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s') +log = logging.getLogger(__name__) +define("port", type=int, default=8001, help="run on the given port") +# 创建请求处理器 +# 当处理请求时会进行实例化并调用HTTP请求对应的方法 + + +class IndexHandler(tornado.web.RequestHandler): + def get(self): + url = "http://demo-edge:8000/" + res = requests.get(url=url) + res = json.loads(res.text) + number = res.get('number') + logging.info(number) + self.write({'number':number}) + self.finish() + +# 创建路由表 +urls = [(r"/", IndexHandler) + ] + +# 定义服务器 +def main(): + # 解析命令行参数 + tornado.options.parse_command_line() + # 创建应用实例 + app = tornado.web.Application(urls) + # 监听端口 + app.listen(options.port) + # 创建IOLoop实例并启动 + tornado.ioloop.IOLoop.current().start() + +# 应用运行入口,解析命令行参数 +if __name__ == "__main__": + # 启动服务器 + main() diff --git a/edgemesh_HTTP_communication_demo/cloud/requirements.txt b/edgemesh_HTTP_communication_demo/cloud/requirements.txt new file mode 100644 index 00000000..729fdbda --- /dev/null +++ b/edgemesh_HTTP_communication_demo/cloud/requirements.txt @@ -0,0 +1,2 @@ +requests==2.25.1 +tornado==6.2 diff --git a/edgemesh_HTTP_communication_demo/edge/demo_edge.yaml b/edgemesh_HTTP_communication_demo/edge/demo_edge.yaml new file mode 100644 index 00000000..5d8dd8a9 --- /dev/null +++ b/edgemesh_HTTP_communication_demo/edge/demo_edge.yaml @@ -0,0 +1,44 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: demo-edge + labels: + app: demo-edge +spec: + replicas: 1 + selector: + matchLabels: + app: demo-edge + template: + metadata: + labels: + app: demo-edge + spec: + affinity: + nodeAffinity: + requiredDuringSchedulingIgnoredDuringExecution: + nodeSelectorTerms: + - matchExpressions: + - key: node-role.kubernetes.io/edge + operator: Exists + - key: node-role.kubernetes.io/agent + operator: Exists + containers: + - name: demo-edge + image: demo:v1 + imagePullPolicy: IfNotPresent + ports: + - containerPort: 8000 +--- +apiVersion: v1 +kind: Service +metadata: + name: demo-edge +spec: + selector: + app: demo-edge + ports: + - name: http-0 + port: 8000 + protocol: TCP + targetPort: 8000 diff --git a/edgemesh_HTTP_communication_demo/edge/dockerfile b/edgemesh_HTTP_communication_demo/edge/dockerfile new file mode 100644 index 00000000..538d863e --- /dev/null +++ b/edgemesh_HTTP_communication_demo/edge/dockerfile @@ -0,0 +1,5 @@ +FROM python:3.8 +WORKDIR /edge_test +ADD . . +RUN pip3 install -r requirements.txt +CMD ["python3", "main.py"] diff --git a/edgemesh_HTTP_communication_demo/edge/main.py b/edgemesh_HTTP_communication_demo/edge/main.py new file mode 100644 index 00000000..fd5ec374 --- /dev/null +++ b/edgemesh_HTTP_communication_demo/edge/main.py @@ -0,0 +1,41 @@ +import tornado.ioloop +import tornado.httpserver +import tornado.web +import tornado.options +from tornado.options import define, options +import random +import logging +logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s') +log = logging.getLogger(__name__) +define("port", type=int, default=8000, help="run on the given port") +# 创建请求处理器 +# 当处理请求时会进行实例化并调用HTTP请求对应的方法 + + +class IndexHandler(tornado.web.RequestHandler): + def get(self): + number = random.randint(1, 100) + logging.info('number is ' + str(number)) + data = {'code':200, 'number':number} + self.write(data) + self.finish() + +# 创建路由表 +urls = [(r"/", IndexHandler) + ] + +# 定义服务器 +def main(): + # 解析命令行参数 + tornado.options.parse_command_line() + # 创建应用实例 + app = tornado.web.Application(urls) + # 监听端口 + app.listen(options.port) + # 创建IOLoop实例并启动 + tornado.ioloop.IOLoop.current().start() + +# 应用运行入口,解析命令行参数 +if __name__ == "__main__": + # 启动服务器 + main() diff --git a/edgemesh_HTTP_communication_demo/edge/requirements.txt b/edgemesh_HTTP_communication_demo/edge/requirements.txt new file mode 100644 index 00000000..e9eefea6 --- /dev/null +++ b/edgemesh_HTTP_communication_demo/edge/requirements.txt @@ -0,0 +1 @@ +tornado==6.2 diff --git a/edgemesh_HTTP_communication_demo/images/access.png b/edgemesh_HTTP_communication_demo/images/access.png new file mode 100644 index 00000000..8fb8b29b Binary files /dev/null and b/edgemesh_HTTP_communication_demo/images/access.png differ diff --git a/edgemesh_HTTP_communication_demo/images/check_log.png b/edgemesh_HTTP_communication_demo/images/check_log.png new file mode 100644 index 00000000..c2d51544 Binary files /dev/null and b/edgemesh_HTTP_communication_demo/images/check_log.png differ diff --git a/edgemesh_HTTP_communication_demo/images/check_pod.png b/edgemesh_HTTP_communication_demo/images/check_pod.png new file mode 100644 index 00000000..ed1fac41 Binary files /dev/null and b/edgemesh_HTTP_communication_demo/images/check_pod.png differ diff --git a/edgemesh_HTTP_communication_demo/images/check_service.png b/edgemesh_HTTP_communication_demo/images/check_service.png new file mode 100644 index 00000000..7e4e8110 Binary files /dev/null and b/edgemesh_HTTP_communication_demo/images/check_service.png differ