Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions edgemesh_HTTP_communication_demo/README.md
Original file line number Diff line number Diff line change
@@ -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

<img src="images/check_pod.png">

4. check service

<img src="images/check_service.png">

5. access cloud pod

<img src="images/access.png">

6. check logs

<img src="images/check_log.png">
45 changes: 45 additions & 0 deletions edgemesh_HTTP_communication_demo/cloud/demo_cloud.yaml
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions edgemesh_HTTP_communication_demo/cloud/dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM python:3.8
WORKDIR /cloud_test
ADD . .
RUN pip3 install -r requirements.txt
CMD ["python3", "main.py"]
44 changes: 44 additions & 0 deletions edgemesh_HTTP_communication_demo/cloud/main.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 2 additions & 0 deletions edgemesh_HTTP_communication_demo/cloud/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requests==2.25.1
tornado==6.2
44 changes: 44 additions & 0 deletions edgemesh_HTTP_communication_demo/edge/demo_edge.yaml
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions edgemesh_HTTP_communication_demo/edge/dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM python:3.8
WORKDIR /edge_test
ADD . .
RUN pip3 install -r requirements.txt
CMD ["python3", "main.py"]
41 changes: 41 additions & 0 deletions edgemesh_HTTP_communication_demo/edge/main.py
Original file line number Diff line number Diff line change
@@ -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()
1 change: 1 addition & 0 deletions edgemesh_HTTP_communication_demo/edge/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tornado==6.2
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.