ToB企服应用市场:ToB评测及商务社交产业平台

标题: 26. 干货系列从零用Rust编写正反向代理,如何发布Rust项目到Docker [打印本页]

作者: 飞不高    时间: 2023-12-16 17:33
标题: 26. 干货系列从零用Rust编写正反向代理,如何发布Rust项目到Docker
wmproxy

wmproxy已用Rust实现http/https代理, socks5代理, 反向代理, 静态文件服务器,四层TCP/UDP转发,内网穿透,后续将实现websocket代理等,会将实现过程分享出来,感兴趣的可以一起造个轮子
项目地址

国内: https://gitee.com/tickbh/wmproxy
github: https://github.com/tickbh/wmproxy
容器化

现在服务器环境已经大部分转为了docker这类容器类的部署方式,因为容器化可以与宿主机隔离,又可以虚拟出统一的环境,保证程序在任何系统上表现是一样的。
我们需要将当前的Rust程序打包成docker的image然后发布到部署的整个过程。
初始化

  1. git clone https://github.com/tickbh/wmproxy.git
复制代码
  1. docker init
  2. Welcome to the Docker Init CLI!
  3. This utility will walk you through creating the following files with sensible defaults for your project:
  4.   - .dockerignore
  5.   - Dockerfile
  6.   - compose.yaml
  7. Let's get started!
  8. ? What application platform does your project use? Rust
  9. ? What version of Rust do you want to use? 1.71.1
  10. ? What port does your server listen on? 82
复制代码
  1. # 拉取了可以编译的RUST版本
  2. FROM rust:${RUST_VERSION}-slim-bullseye AS build
  3. ARG APP_NAME
  4. WORKDIR /app
  5. # 挂载相应的文件目录结构
  6. RUN --mount=type=bind,source=src,target=src \
  7.     --mount=type=bind,source=Cargo.toml,target=Cargo.toml \
  8.     --mount=type=bind,source=.cargo,target=.cargo \
  9.     --mount=type=bind,source=Cargo.lock,target=Cargo.lock \
  10.     --mount=type=cache,target=/app/target/ \
  11.     --mount=type=cache,target=/usr/local/cargo/registry/ \
  12.     <<EOF
  13. set -e
  14. cargo build --locked --release
  15. cp ./target/release/$APP_NAME /bin/wmproxy
  16. EOF
  17. # 用较小的发行版本做载体,保证较小的image
  18. # 目标image中不包含Rust环境
  19. FROM debian:bullseye-slim AS final
  20. ARG UID=10001
  21. RUN adduser \
  22.     --disabled-password \
  23.     --gecos "" \
  24.     --home "/nonexistent" \
  25.     --shell "/sbin/nologin" \
  26.     --no-create-home \
  27.     --uid "${UID}" \
  28.     appuser
  29. USER root
  30. # 拷贝编译好的可执行文件到目标系统,到时可直接执行目标程序
  31. COPY --from=build /bin/wmproxy /bin/
  32. # 拷贝相应的配置文件
  33. COPY config /etc/config
  34. EXPOSE 82:82 8837:8837 8090:8090
复制代码
  1. docker build --tag wmproxy .
复制代码
  1. /projects/foo/bar/baz/.cargo/config
  2. /projects/foo/bar/.cargo/config
  3. /projects/foo/.cargo/config
  4. /projects/.cargo/config
  5. /.cargo/config
  6. $HOME/.cargo/config
复制代码
通过netstat可以查看到
  1. [source.crates-io]
  2. replace-with = 'ustc'
  3. [source.ustc]
  4. registry = "git://mirrors.ustc.edu.cn/crates.io-index"
  5. [http]
  6. check-revoke = false
复制代码
查看当前服务器状态curl http://127.0.0.1:8837/now

可以正常的获取当前配置
8090为http代理,接下来测试代理是否工作
  1. ...
  2. => exporting to image                                                             0.1s
  3. => => exporting layers                                                            0.1s
  4. => => writing image sha256:ca62d7bf9c2684912b27994c2d09917a4836c0fd63867cc9765bf  0.0s
  5. => => naming to docker.io/library/wmproxy                                         0.0s
复制代码
接下来进行curl测试

可以通过代理正常访问,关掉docker就会返回错误。
发布image

  1. REPOSITORY                        TAG                                        IMAGE ID       CREATED         SIZE
  2. wmproxy                           latest                                     ca62d7bf9c26   2 minutes ago   101MB
复制代码
  1. docker run -p 82:82 -p 8090:8090 -p 127.0.0.1:8837:8837 --name proxy_bash -v $PWD/reverse.toml:/etc/config/reverse.toml:rw wmproxy /bin/./wmproxy -c /etc/config/reverse.toml
复制代码
如果安装desktop版会自动读取token
5. 接下来直接把目标镜像push
  1. TCP    0.0.0.0:82             0.0.0.0:0              LISTENING
  2. TCP    0.0.0.0:8090           0.0.0.0:0              LISTENING
  3. TCP    127.0.0.1:8837         0.0.0.0:0              LISTENING
复制代码
此时后台上可以查看到,表示已发表成功

  1. export http_proxy=http://127.0.0.1:8090/
复制代码
至此已经可以运行完毕
结语

容器化已经在服务器中属于密不可分的一环,包括k8s等编排技术也是基于容器化的基本上去大规模部署的,保证不会因为系统的差别而带来不一致的体验。
点击 [关注][在看][点赞] 是对作者最大的支持

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4