registry 封装容器部署环境测试
封装打包镜像
dockerfile
# 阶段 1:构建阶段(使用多阶段构建以减少最终镜像大小)
FROM golang:1.22-alpine AS builder
# 安装构建所需工具
RUN #apk add --no-cache git
# 设置工作目录
WORKDIR /app
# 将 go.mod 和 go.sum 复制到容器中
COPY go.mod ./
# 下载依赖
RUN go mod download
# 将项目代码复制到工作目录中
COPY . .
# 编译 Go 程序(静态编译)
RUN CGO_ENABLED=0 GOOS=linux go build -o registry ./cmd/registry# 阶段 2:运行阶段
FROM alpine
# 安装运行时工具(用于调试或检查)
RUN apk add --no-cache bash ca-certificates
# 设置工作目录
WORKDIR /app
# 从构建阶段复制编译好的二进制文件和必要资源
COPY --from=builder /app/registry /app/registry
COPY --from=builder /app/tmp/data /app/tmp/data
# 暴露服务端口
EXPOSE 8081
# 启动应用程序
CMD ["/app/registry"]
因为本地的 alpine 镜像是 1.21 版本的,封装时发现一些使用的依赖需要 1.22 版本以上兼容,所以需要更新镜像版本
封装镜像完成
部署测试发现环境崩溃了。。。。
因为服务器环境更新了,IP 分配也更改了,SSH 链接的公钥发生变化,所以需要更新本地的公钥然后重新 SSH 链接
ssh-keygen -f "/home/public/.ssh/known_hosts" -R "172.110.0.109"
部署测试
部署环境下载镜像
sudo ctr -n k8s.io image pull registry.cn-hangzhou.aliyuncs.com/leung_qw/registry:v1.0.0
部署文件
apiVersion: apps/v1
kind: Deployment
metadata:name: registry-deploymentnamespace: testlabels:app: web
spec:replicas: 1 # 部署 1 个副本selector:matchLabels:app: webtemplate:metadata:labels:app: webspec:containers:- name: registryimage: registry.cn-hangzhou.aliyuncs.com/leung_qw/registry:v1.0.0ports:- containerPort: 8081
---
apiVersion: v1
kind: Service
metadata:labels:app: webname: registry-svcnamespace: test
spec:selector:app: webports:- protocol: TCPport: 3001 # Service 端口targetPort: 8081 # 目标 Pod 的端口type: ClusterIP # 仅在集群内部访问,如需外部访问可改为 NodePort 或 LoadBalancer
接口测试
测试发现的一些问题:
将镜像部署成服务时,每次访问服务接口,会有时断时续的链接问题,但是对于镜像本身的 pod 接口则没有这个问题
接口测试命令:
/upload 接口 (这里引号是需要的,否则如果有多个参数,bash 会把连接符 & 识别为后台执行符号)
curl -X POST “10.110.59.35:3001/upload?fileName=1.txt” --data-binary @1.txt -H "Content-Type: application/octet-stream"curl -X POST “10.110.59.35:3001/upload?fileName=1.txt&tag=v1.2.0” --data-binary @1.txt -H "Content-Type: application/octet-stream"
/query/exits 接口
curl "10.110.59.35:3001/query/exits?filename=1.txt&tag=v1.4"/query/list 接口
curl "10.110.59.35:3001/query/list"/delete 接口
curl "10.244.169.129:8081/delete?filename=1.txt"
/download 接口
curl "10.244.169.129:8081/download?filename=1.txt&tag=v1.4"
/forward 接口
curl -X POST "10.244.167.144:8081/forward?target=http://100.1.255.252:3001&filename=1.txt" --data-binary @test-broker.txt -H "Content-Type: application/octet-stream"
性能测试
使用时间戳来记录衡量性能
echo "Request sent at: $(date +%s) ($(date -d @$(date +%s) '+%Y-%m-%d %H:%M:%S'))" && curl "10.244.169.129:8081/query/exits?filename=1.txt&tag=v1.4"
这里要同步宿主机和容器的时间同步,因为容器镜像中封装的是 Linux 轻量版的 alpine,安装时区数据包,并设置时区
apk add tzdataln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtimedate
但是实际测试发现,部署后更改容器内部镜像时,容器接口接收到文件后记录的时间,依然是更改之前的时间,所以这里需要在部署时就同步时间,更新部署配置文件
apiVersion: apps/v1
kind: Deployment
metadata:name: registry-deploymentnamespace: testlabels:app: web
spec:replicas: 1 # 部署 1 个副本selector:matchLabels:app: webtemplate:metadata:labels:app: webspec:containers:- name: registryimage: registry.cn-hangzhou.aliyuncs.com/leung_qw/registry:v1.0.0ports:- containerPort: 8081volumeMounts:- name: time-zonemountPath: /etc/localtime # 将容器的本地时间与宿主机同步readOnly: truevolumes:- name: time-zonehostPath:path: /usr/share/zoneinfo/Asia/Shanghai # 设置时区为 Asia/Shanghaitype: File
---
apiVersion: v1
kind: Service
metadata:labels:app: webname: registry-svcnamespace: test
spec:selector:app: webports:- protocol: TCPport: 3001 # Service 端口targetPort: 8081 # 目标 Pod 的端口type: ClusterIP
定义一个 大小为 500 MB 的文件,
# 查看文件详细信息
stat test.txt
# 查看文件信息
ls -l test.txt
# 定义指定大小的文件
truncate -s 500M test.txt
# 查看宿主机内存等性能信息
free -h
这边测试发现,上传 upload 接口上传 500 MB大小的文件,耗时为 5s
echo "Request sent at: $(date +%s) ($(date -d @$(date +%s) '+%Y-%m-%d %H:%M:%S'))" && curl -X POST "1
0.244.167.144:8081/forward?target=http://100.1.255.252:3001&filename=broker.txt" --data-binary @broker.txt -H "Content-Type: app
lication/octet-stream"echo "Request sent at: $(date +%s) ($(date -d @$(date +%s) '+%Y-%m-%d %H:%M:%S'))" && curl "100.1.255.252:3001/query/exits?filename=broker.txt"
跨域测试大概为 6s