阿里云服务器 ECS 开放端口

开放端口设置

配置路径

配置规则

授权策略 优先级 协议类型 端口范围 授权对象 描述
允许 1 自定义 TCP 目的:5000/5000 源:0.0.0.0/0 Flask
允许 1 自定义 TCP 目的:8000/8000 源:0.0.0.0/0 FastAPI

问题

今天通过上面的配置后,发现不能访问,上网搜索了半天,看到有说需要配置 iptables ,于是开始了一顿搜索和学习(Linux上iptables基础应用),进行了如下配置,发现还是访问不了。

iptables -A INPUT -p tcp --dport 5000 -j ACCEPT
iptables -A INPUT -p tcp --dport 8000 -j ACCEPT

解决

突然想到了 Web 应用服务器的配置 --host,我这里用的是 uvicorn,默认使用的 127.0.0.1,这样只能接收本机的访问,需要接收其它主机的访问就需要配置为 0.0.0.0。

Web 应用服务器的配置

  • uvicorn --host 0.0.0.0
  • gunicorn --bind 0.0.0.0

最后测试了一下,发现根本不需要使用 iptables 配置。

参考资料

命令 nc

捕获 HTTP 请求的内容

  1. 监听端口,用于捕获数据。
nc -l port 
  1. 发送 HTTP 请求。
curl http://ip:port/

GET 请求

curl http://127.0.0.1:8000/
GET /?name=wjj HTTP/1.1
Host: 127.0.0.1:8000
User-Agent: curl/7.61.1
Accept: */*

POST JSON 请求

curl --location --request POST 'http://127.0.0.1:8000/users_by_json' \
--header 'Content-Type: application/json' \
--data-raw '{
    "name": "wjj",
    "age": 40
}'
POST /users_by_json HTTP/1.1
Host: 127.0.0.1:8000
User-Agent: curl/7.61.1
Accept: */*
Content-Type: application/json
Content-Length: 36

{
    "name": "wjj",
    "age": 40
}

基于健康码识别的 FastAPI 同步和异步函数的基准测试

健康码识别服务使用了 FastAPI 进行开发的,本周主要工作是为了对健康码识别的服务进行性能调优。接口函数使用了 async 关键字,但是内部的实现并没有使用 await。由于改写成异步代码需要时间,这里并没有改写代码,只是删除了 async 关键字。部署服务使用了 uvicorn 和 gunicorn+uvicorn 两种方法。

基准测试工具使用的是 ab

测试流程

生成测试数据

准备测试图片 health.jpg

echo -n '{"base64": "' > health.json
base64 -w0 health.jpg >> health.json
echo -n '"}' >> health.json

部署服务

uvicorn

docker run --runtime=nvidia --rm -it -e NVIDIA_VISIBLE_DEVICES=2 -p 20001:8000 \
    -v $(pwd):/health_code_service --name=health-uvicorn  health-code-service \
    uvicorn controller:app --host 0.0.0.0 --workers 1
  • workers 并发进程数

HTTP 基准测试工具

wrk

wrk 使用的是 HTTP/1.1

安装

需要从 GitHub 上克隆代码自己编译,编译前需要安装 git, gcc。

git clone https://github.com/wg/wrk.git
cd wrk
#使用多线程(机器的处理器核数)加速编译,
make -j $(nproc)
cp wrk /usr/local/bin/

测试

10 个线程,保持打开 100 个并发连接,持续 10 秒。

wrk -t10 -c100 -d10 http://www.baidu.com/
Running 10s test @ http://www.baidu.com/
  10 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   138.46ms  219.05ms   1.89s    92.11%
    Req/Sec   128.07     79.83   700.00     94.40%
  12776 requests in 10.02s, 128.22MB read
  Socket errors: connect 0, read 57, write 0, timeout 3
Requests/sec:   1275.30
Transfer/sec:     12.80MB

打印详细的延时分布信息 wrk -c10 -t4 --latenc

阿里云 yum 安装应用报 404 错误

今天登录阿里云安装应用出现 404

问题

yum install install httpd-tools -y
CentOS-8 - AppStream                                                                                                                                                                      9.6 kB/s | 2.3 kB     00:00    
Errors during downloading metadata for repository 'AppStream':
  - Status code: 404 for http://mirrors.cloud.aliyuncs.com/centos/8/AppStream/x86_64/os/repodata/repomd.xml (IP: 100.100.2.148)
错误:为 repo 'AppStream' 下载元数据失败 : Cannot download repomd.xml: Cannot download repodata/repomd.xml: All mirrors were tried

解决方案 cd /etc/yum.repos.d/ find . -name '.repo' -exec sed -i 's/mirrors.cloud.aliyuncs.com/mirrors.aliyun.com/g' {} + find . -name '.

Python 自动生成周报

安装依赖库

pip install typer python-docx

编写生成个人周报的应用 制作个人周报模版 个人周报-{}-{}.xlsx 编写应用 weekly.py import shutil import openpyxl import typer from datetime import datetime, timedelta def get_weekly_info(): now = datetime.now() # 工作表的标题,格式: yymmdd(周一)-yymmdd(周五) monday = now-timedelta(now.weekday()) friday = monday+timedelta(4) sheet_title = '{}-{}'.format(monday.strftime('%y%m%d'), friday.strftime('%y%m%d')) # 报告日期,格式: yyyy/mm/dd(周四) report_date = now.strftime("%Y/%m/%d") # 计划的开始时间与结束时间 base_date = datetime(1899, 12, 30, 0, 0) work_tasks_begin_time = (monday-base_date).

Python办公自动化套件

操作 Word 文档

安装依赖库 python-docx

pip install python-docx

示例 from docx import Document from docx.shared import Inches document = Document() document.add_heading('Document Title', 0) p = document.add_paragraph('A plain paragraph having some ') p.add_run('bold').bold = True p.add_run(' and some ') p.add_run('italic.').italic = True document.add_heading('Heading, level 1', level=1) document.add_paragraph('Intense quote', style='Intense Quote') document.

Dockerfile 实践

系统

指定本地时区

FROM ubuntu:20.04
LABEL maintainer="wang-junjian@qq.com"

ARG TIME_ZONE=Asia/Shanghai
RUN DEBIAN_FRONTEND=noninteractive apt-get install tzdata -y && \
    ln -fs /usr/share/zoneinfo/$TIME_ZONE /etc/localtime && \
    echo $TIME_ZONE > /etc/timezone

Python 开发环境 构建 Python, OpenCV 开发环境的镜像 FROM ubuntu:20.04 LABEL maintainer="wang-junjian@qq.com" # 设置 apt 源(阿里云),设置完必须 update。 RUN sed -i 's/archive.ubuntu.com/mirrors.aliyun.

构建基于PaddlePaddle开发服务镜像

构建镜像

FROM paddlepaddle/paddle:2.2.2-gpu-cuda10.2-cudnn7
LABEL maintainer="wang-junjian@qq.com"

RUN apt-get update && apt-get install libjpeg-dev zlib1g-dev -y

RUN pip install -i https://mirrors.aliyun.com/pypi/simple/ \
    numpy fastapi paddleocr opencv-python

EXPOSE 20000

WORKDIR /inference-serving
ADD . ./

CMD ["python", "app.py"]

官方推荐:非安培架构的GPU,推荐使用CUDA10.2,性能更优。

自己构建 paddlepaddle 镜像

通过官方的 Docker Hub 没有找到 runtime 版本,想着节省几个G的空间,于是考虑自己来构建。

我删库了, rm -rf *

今天,我“删库”了......

/data$ ll logs/
rm -rf *

我在根目录查看子目录的信息,确认是想删除的数据,然后顺手执行了 rm -rf * ,杯具产生了......

拼命补救,没成功......

查看目录或文件的 inode id

$ ls -id /
2 /

$ ls -id /usr/
28966913 /usr/

$ ls -id /usr/bin/bash
28967390 /usr/bin/bash

参考资料

MinIO Quickstart

使用容器的方式部署 MinIO

Standalone

docker run --rm \
  -p 9000:9000 \
  -p 9001:9001 \
  --name minio \
  -v /data/minio/data:/data \
  -e "MINIO_ROOT_USER=admin" \
  -e "MINIO_ROOT_PASSWORD=12345678" \
  minio/minio server /data --console-address ":9001"
API: http://172.17.0.2:9000  http://127.0.0.1:9000 

Console: http://172.17.0.2:9001 http://127.0.0.1:9001 

Documentation: https://docs.min.io

单机分布式 # 创建4个目录 mkdir -p /data/minio/erasure-code/data{1..

Json Formatter

test.json

{ "stuff": { "that": [1,2,3], "isin": true, "json": "end"}}

jq

在命令行运行

jq . <<< '{ "stuff": { "that": [1,2,3], "isin": true, "json": "end"}}'
jq . test.json

在vim的命令模式下运行

%!jq .

python json.tool

在命令行运行

python -m json.tool <<< '{ "stuff": { "that": [1,2,3], "isin": true, "json": "end"}}'
python -m json.tool test.json

在vim的命令模式下运行

%!python -m json.tool

在线格式化 Format JSON JSON Formatter JSON Formatter, Validator, Vi

命令tr

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            126G     0  126G   0% /dev
tmpfs            26G  4.0M   26G   1% /run
/dev/sda2       548G   50G  471G  10% /
tmpfs           126G     0  126G   0% /sys/fs/cgroup
/dev/sda1       511M  7.9M  504M   2% /boot/efi
/dev/sdb1       2.0T  4.7G  1.9T   1% /data

字母小写转大写

$ df -h | tr [:lower:] [:upper:]
FILESYSTEM      SIZE  USED AVAIL USE% MOUNTED ON
UDEV            126G     0  126G   0% /DEV
TMPFS            26G  4.0M   26G   1% /RUN
/DEV/SDA2       548G   50G  471G  10% /
TMPFS           126G     0  126G   0% /SYS/FS/CGROUP
/DEV/SDA1       511M  7.9M  504M   2% /BOOT/EFI
/DEV/SDB1       2.0T  4.7G  1.9T   1% /DATA
$ df -h | tr a-z A-Z

字母大写转小写

$ df -h | tr [:upper:] [:lower:]
$ df -h | tr A-Z a-z

OpenResty 内执行 Lua 脚本

OpenResty 是一款基于 NGINX 和 LuaJIT 的 Web 平台。

拉取 OpenResty 镜像

  • Ubuntu
sudo docker pull openresty/openresty:xenial
  • CentOS
sudo docker pull openresty/openresty:centos

查看镜像的标签 $ sudo docker inspect openresty/openresty:centos | jq '.[].Config.Labels' { "maintainer": "Evan Wies <evan@neomantra.net>", "org.label-schema.build-date": "20210915", "org.label-schema.license": "GPLv2", "org.label-schema.name": "CentOS Base Image", "org.label-schema.schema-version": "1.0", "org.label-schema.

逻辑卷管理(Logical Volume Management)

查看 LVM 的相关命令

man lvm
lvm --help

查看系统的块设备 $ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT loop0 7:0 0 43.4M 1 loop /snap/snapd/14549 loop1 7:1 0 43.3M 1 loop /snap/snapd/14295 loop2 7:2 0 76.3M 1 loop /snap/lxd/22407 loop3 7:3 0 61.9M 1 loop /snap/core20/1328 loop4 7:4 0 55.5M 1 loop /snap/core18/2253 loop5 7:5 0 55.5M 1 loop /snap/core18/2284 loop7 7:7 0 94.5M 1 loop /snap/go/9028 loop8 7:8 0 94.5M 1 loop /snap/go/8839 loop9 7:9 0 76.3M 1 loop /snap/lxd/22358 loop10 7:10 0 61.9M 1 loop /snap/core20/1270 sda 8:0 0 558.9G 0 disk ├─sda1 8:1 0 512M 0 part /boot/efi └─sda2 8:2 0 558.

构建基于 ONNXRuntime 的推理服务

构建 ONNXRuntime-GPU 镜像

编写 requirements.txt

$ vim requirements.txt
flask
connexion[swagger-ui]
connexion
gunicorn
numpy
opencv-python
scikit-image
psutil
pynvml
onnxruntime-gpu

编写 Dockerfile 需要带 cudnn 库的 CUDA 作为基镜像 $ vim Dockerfile FROM nvidia/cuda:11.4.0-cudnn8-runtime-ubuntu20.04 LABEL maintainer="wang-junjian@qq.com" RUN rm /etc/apt/sources.list.d/cuda.list /etc/apt/sources.list.d/nvidia-ml.list && \ sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.

在Linux上安装CUDA Toolkit

安装 CUDA Toolkit

下载

wget https://developer.download.nvidia.com/compute/cuda/11.6.0/local_installers/cuda_11.6.0_510.39.01_linux.run

安装 $ sudo sh cuda_11.5.1_495.29.05_linux.run =========== = Summary = =========== Driver: Installed Toolkit: Installed in /usr/local/cuda-11.5/ Samples: Installed in /home/lnsoft/, but missing recommended libraries Please make sure that - PATH includes /usr/local/cuda-11.5/bin - LD_LIBRARY_PATH includes /usr/local/cuda-11.5/lib64, or, add /usr/local/cuda-11.5/lib64 to /etc/ld.so.

GaiaGPU: 在容器云中共享GPU

容器技术由于其轻量级和可伸缩的优势而被广泛使用。GPU也因为其强大的并行计算能力被用于应用程序加速。在云计算环境下,容器可能需要一块或者多块GPU计算卡来满足应程序的资源需求,但另一方面,容器独占GPU计算卡常常会带来资源利用率低的问题。因此,对于云计算资源提供商而言,如何解决在多个容器之间共享GPU计算卡是一个很有吸引力的问题。本文中我们提出了一种称为GaiaGPU的方法,用于在容器间共享GPU存储和GPU的计算资源。GaiaGPU会将物理GPU计算卡分割为多个虚拟GPU并且将虚拟GPU按需分配给容器。同时我们采用了弹性资源分配和动态资源分配的方法来提高资源利用率。实验结果表明GaiaGPU平均仅带来1.015%的性能损耗并且能够高效的为容器分配和隔离GPU资源。

编译 GaiaGPU 服务

配置 git 加速

$ git config --global url."https://github.com.cnpmjs.org".insteadOf "https://github.com"

$ vim /etc/profile
export GOPROXY=https://goproxy.cn,direct
export GO111MODULE=on
$ source /etc/profile

vCUDA Controller $ git clon

安装Go

安装

到 https://golang.google.cn/dl/ 下载对应操作系统的安装包。

Mac

wget https://golang.google.cn/dl/go1.17.7.darwin-amd64.pkg

Linux

wget https://golang.google.cn/dl/go1.17.7.linux-amd64.tar.gz
sudo tar -C /usr/local/ -xzf go1.17.7.linux-amd64.tar.gz

Ubuntu

sudo apt install golang

配置

$ sudo vim /etc/profile
PATH=$PATH:/usr/local/go/bin

#Go代理配置(解决墙的问题)
export GOPROXY=https://goproxy.cn,direct
export GO111MODULE=on
$ source /etc/profile

查看 Go 版本信息

$ go version
go version go1.17.7 linux/amd64

参考资料

使用终端浏览Markdown和HTML

浏览Markdown

sudo apt install lynx
sudo apt install pandoc

pandoc README.md | lynx -stdin
sudo pip install grip
sudo apt install lynx

grip -b README.md
lynx http://localhost:6419/
sudo apt install pandoc
pandoc README.md -t plain | less

浏览HTML

sudo apt install w3m

w3m index.html
sudo apt install lynx
sudo apt install pandoc

pandoc index.html | lynx -stdin
sudo pip install grip

grip -b index.html

参考资料