这里使用大模型给随机生成一个python的http服务,用来进行验证
基于 http.server 的简单 HTTP 服务器及其插件打包成 Docker 镜像的步骤
1、创建项目目录
mkdir http-plugin-server
cd http-plugin-server
2、创建 HTTP 服务器文件
(1)创建一个名为 server.py 的文件,并添加以下内容:
# server.py
from http.server import BaseHTTPRequestHandler, HTTPServer
import os
import importlib.util
class PluginHTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
path = self.path.strip('/')
if path in self.plugins:
response = self.plugins[path]()
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(response.encode())
else:
self.send_response(404)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(b'Not Found')
plugins = {}
def load_plugins():
plugins_dir = 'plugins'
for file in os.listdir(plugins_dir):
if file.endswith('.py'):
module_name = file[:-3]
spec = importlib.util.spec_from_file_location(module_name, os.path.join(plugins_dir, file))
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
if hasattr(module, 'register_plugin'):
module.register_plugin(PluginHTTPRequestHandler.plugins)
if __name__ == '__main__':
load_plugins()
server_address = ('', 8000)
httpd = HTTPServer(server_address, PluginHTTPRequestHandler)
print(f'Starting server on port {server_address[1]}...')
httpd.serve_forever()
(2)创建插件
mkdir plugins
在 plugins 目录下创建一个名为 example_plugin.py 的文件,并添加以下内容:
# example_plugin.py
def register_plugin(plugin_dict):
plugin_dict['example'] = example_view
def example_view():
return 'This is an example plugin!'
(3)创建 Dockerfile:
在项目根目录下创建一个名为 Dockerfile 的文件,并添加以下内容:
# 使用官方的 Python:3.9-slim-buster 镜像作为基础镜像
FROM python:3.7-slim-buster
#拉取skywalking 对应的基础环境
FROM apache/skywalking-python:1.1.0-grpc-py3.10
# 设置工作目录
WORKDIR /app
# 复制当前目录下的所有文件到容器的工作目录
COPY . .
#配置skywalking 相关的环境变量
ENV SW_AGENT_NAME='test-chen'
ENV SW_AGENT_NAMESPACE='test-chen1'
ENV SW_AGENT_COLLECTOR_BACKEND_SERVICES='skywalking的IP+端口'
# 暴露端口
EXPOSE 8000
# 运行 HTTP 服务器
CMD ["python", "server.py"]
(4)构建 Docker 镜像
在包含 Dockerfile 的目录中,运行以下命令来构建镜像:
docker build -t my-http-plugin-server .
运行 Docker 容器:
docker run -p 8000:8000 my-http-plugin-server
【镜像构建案例】:
【数据验证】:
准一 sky walking地址:
http://skywalking的IP+端口/General-Service/Services
【skywalking链路追踪python程序】
在原本的基础镜像中,拉去对应的sky walking python环境镜像
FROM apache/skywalking-python:1.1.0-grpc-py3.10
#配置skywalking 相关的环境变量
#服务名称
ENV SW_AGENT_NAME='test-chen'
#ENV SW_AGENT_NAMESPACE='test-chen1'
# Sky walking服务的地址+端口
ENV SW_AGENT_COLLECTOR_BACKEND_SERVICES='skywalking的IP+端口'
官网链接:Apache SkyWalking Python Agent dockerfile and images | Apache SkyWalking
Apache SkyWalking Python Agent dockerfile and images
Docker images are not official ASF releases but provided for convenience. Recommended usage is always to build the source
This image hosts the SkyWalking Python agent package on top of official Python base images (full & slim) providing support from Python 3.7 - 3.11.
How to use this image
The images are hosted at Docker Hub.
The images come with protocol variants(gRPC, Kafka, HTTP) and base Python variants(Full, Slim).
Build your Python application image on top of this image
Start by pulling the skywalking-python
image as the base of your application image. Refer to Docker Hub for the list of tags available.
FROM apache/skywalking-python:1.1.0-grpc-py3.10
# ... build your Python application
You could start your Python application with CMD
. The Python image already sets an entry point ENTRYPOINT ["sw-python"]
.
For example - CMD ['run', '-p', 'gunicorn', 'app.wsgi']
-p
is always needed when using with Gunicorn/uWSGI -> This will be translated to sw-python run -p gunicorn app.wsgi
You don’t need to care about enabling the SkyWalking Python agent manually, it should be adopted and bootstrapped automatically through the sw-python
CLI.
Environment variables should be provided to customize the agent behavior.
Build an image from the dockerfile
Provide the following arguments to build your own image from the dockerfile.
BASE_PYTHON_IMAGE # the Python base image to build upon
SW_PYTHON_AGENT_VERSION # agent version to be pulled from PyPI
SW_PYTHON_AGENT_PROTOCOL # agent protocol - grpc/ http/ kafka