一、系统环境
系统 | centos7 |
k8s | v1.24 |
containerd | v1.7.16 |
etcd | v3.5.0 |
二、镜像生成工具准备
nerdctl | v1.7.6 |
buildkit | v0.13.2 |
1 nerdctl安装
下载:
wget -c https://github.com/containerd/nerdctl/releases/download/v1.7.6/nerdctl-full-1.7.6-linux-amd64.tar.gz
解压:
tar -zxf nerdctl-full-1.7.6-linux-amd64.tar.gz -C /usr/local/nerdctl
配置:
将/usr/local/nerdctl/bin加入path
vi /etc/profile
source /etc/profile
2 buildkit安装
下载:
wget https://github.com/moby/buildkit/releases/download/v0.11.6/buildkit-v0.11.6.linux-amd64.tar.gz
解压:
tar -xf buildkit-v0.13.2.linux-amd64.tar.gz -C /usr/local/buildkit
配置:
将/usr/local/buildkit/bin加入path
vi /etc/profile
source /etc/profile
服务配置:
cat <<EOF > /usr/lib/systemd/system/buildkitd.service
[Unit]
Description=buildkitd
After=network.target
[Service]
ExecStart=/usr/local/buildkit/bin/buildkitd
[Install]
WantedBy=multi-user.target
EOF
# 重新加载Unit file
systemctl daemon-reload
# 启动服务
systemctl start buildkitd
# 开机自启动
systemctl enable buildkitd
需要特俗网络配置代理:
vi /usr/lib/systemd/system/buildkitd.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=http://127.0.0.1:10809"
Environment="HTTPS_PROXY=http://127.0.0.1:10809"
Environment="NO_PROXY=localhost,127.0.0.1,::1,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16"
sudo systemctl daemon-reload
sudo systemctl restart buildkit
三、项目配置
1 go后台程序配置
1.1 项目根目录下Dockerfile文件
#使用官方的Golang映像创建构建工件。
FROM golang:1.21
# 设置代理
ENV HTTP_PROXY=http://127.0.0.1:10809
ENV HTTPS_PROXY=http://127.0.0.1:10809
ENV NO_PROXY=localhost,127.0.0.1
# 在容器内设置当前工作目录
WORKDIR /app
# 复制go mod和sum文件
COPY go.mod go.sum ./
# 下载所有依赖项。依赖项将被缓存。装好就走。Sum文件不会被更改
RUN go mod download
# 将源代码从当前目录复制到容器内的工作目录
COPY . .
# 构建Go应用程序
RUN CGO_ENABLED=0 GOOS=linux go build -o main .
# 设置代理
ENV HTTP_PROXY=http://127.0.0.1:10809
ENV HTTPS_PROXY=http://127.0.0.1:10809
ENV NO_PROXY=localhost,127.0.0.1
# 安装ffmpeg
RUN apt-get update && apt-get install -y ffmpeg
# 设置时区,解决时区问题
RUN echo "Asia/Shanghai" > /etc/timezone;
ENV LANG C.UTF-8
# 将端口8080暴露给外部世界
EXPOSE 8080
# 命令运行可执行文件
CMD ["./main", "-profile=prod"]
1.2 编译
[root@k8s-master01 zhiqu]# nerdctl --debug build -t leellun/zhiqu ./
DEBU[0000] Choosing the buildkit host "buildkit-default/buildkitd.sock", candidates=[buildkit-default/buildkitd.sock buildkit/buildkitd.sock] (in "/run/")
DEBU[0000] Choosing the buildkit host "buildkit/buildkitd.sock", candidates=[buildkit-default/buildkitd.sock buildkit/buildkitd.sock] (in "/run/")
DEBU[0000] Chosen buildkit host "unix:///run/buildkit/buildkitd.sock"
DEBU[0000] worker labels: map[org.mobyproject.buildkit.worker.executor:oci org.mobyproject.buildkit.worker.hostname:k8s-master01 org.mobyproject.buildkit.worker.network:host org.mobyproject.buildkit.worker.oci.process-mode:sandbox org.mobyproject.buildkit.worker.selinux.enabled:false org.mobyproject.buildkit.worker.snapshotter:native]
DEBU[0000] running /usr/local/buildkit/bin/buildctl [--addr=unix:///run/buildkit/buildkitd.sock build --progress=auto --frontend=dockerfile.v0 --local=context=./ --output=type=docker,name=docker.io/leellun/zhiqu:latest --local=dockerfile=/root/zhiqu --opt=filename=Dockerfile]
[+] Building 222.2s (11/11)
[+] Building 222.4s (11/11) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 686B 0.0s
=> [internal] load metadata for docker.io/library/golang:1.21 2.5s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [1/6] FROM docker.io/library/golang:1.21@sha256:a8edec58ba598e2f1259f4ec4ca1b06358468214225e73d7c841ab0980c12367 0.0s
=> => resolve docker.io/library/golang:1.21@sha256:a8edec58ba598e2f1259f4ec4ca1b06358468214225e73d7c841ab0980c12367 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 87.11kB 0.0s
=> CACHED [2/6] WORKDIR /app 0.0s
=> [3/6] COPY go.mod go.sum ./ 3.6s
=> [4/6] RUN go mod download 115.1s
=> [5/6] COPY . . 6.0s
=> [6/6] RUN CGO_ENABLED=0 GOOS=linux go build -o main . 43.7s
=> exporting to docker image format 51.0s
=> => exporting layers 32.6s
=> => exporting manifest sha256:a2b6adb815f33f4a93e4c5ea19ecdfa4c34e56365c9339d2839adf260abcfce2 0.0s
=> => exporting config sha256:4a200525a4604ce63db8fa0abe08a45886a7f4ea96f86ae02819401d3091083b 0.0s
=> => sending tarball 18.3s
Loaded image: docker.io/leellun/zhiqu:latest
1.3 将镜像推送到容器镜像仓库
登录 Docker Hub:
[root@k8s-master01 zhiqu]# nerdctl login
Enter Username: leellun
Enter Password:
WARNING: Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
给镜像打标签:
nerdctl tag leellun/zhiqu:latest your_dockerhub_username/zhiqu:latest
推送镜像:
nerdctl push your_dockerhub_username/zhiqu:latest
由于我的dockerhub的用户名就是leellun,就不用打标签了
[root@k8s-master01 zhiqu]# nerdctl push leellun/zhiqu:latest
INFO[0000] pushing as a reduced-platform image (application/vnd.docker.distribution.manifest.v2+json, sha256:a2b6adb815f33f4a93e4c5ea19ecdfa4c34e56365c9339d2839adf260abcfce2)
manifest-sha256:a2b6adb815f33f4a93e4c5ea19ecdfa4c34e56365c9339d2839adf260abcfce2: done |++++++++++++++++++++++++++++++++++++++|
config-sha256:4a200525a4604ce63db8fa0abe08a45886a7f4ea96f86ae02819401d3091083b: done |++++++++++++++++++++++++++++++++++++++|
elapsed: 198.9s total: 7.3 Ki (37.0 B/s)
1.4 k8s配置
在项目根目录下创建 deployment.yaml
:
apiVersion: apps/v1
kind: Deployment
metadata:
name: zhiqu
namespace: newlandp
spec:
replicas: 1
selector:
matchLabels:
app: zhiqu
template:
metadata:
labels:
app: zhiqu
spec:
containers:
- name: zhiqu
image: leellun/zhiqu:latest
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: zhiqu-service
namespace: newlandp
spec:
type: NodePort
ports:
- port: 8080
targetPort: 8080
selector:
app: zhiqu
1.5 部署到 Kubernetes 集群
kubectl apply -f deployment.yaml
1.6 测试部署环境
[root@k8s-master01 ~]# curl http://192.168.10.10:31734/api/video/videos/stat/23
{"code":200,"msg":"操作成功","data":{"video_id":23,"views":4,"danmu_count":0,"thumb_count":0,"collect_count":0,"comment_count":0,"thumb_status":0,"collect_status":0}}
2 web前端
2.1 Dockerfile文件
# 使用官方的Node.js镜像作为基础镜像
FROM node:18-alpine
# 设置工作目录
WORKDIR /app
# 复制 package.json 和 package-lock.json
COPY package*.json ./
# 安装依赖关系
RUN yarn install
# 复制应用程序的其余部分
COPY . .
# 构建Next.js应用程序
RUN yarn build
# 暴露端口
EXPOSE 3000
# 启动程序
CMD ["yarn", "start"]
2.2 编译
nerdctl build -t leellun/zhiqu-web ./
2.3 推送镜像
nerdctl push leellun/zhiqu-web
2.4 k8s配置
apiVersion: apps/v1
kind: Deployment
metadata:
name: zhiqu-web
spec:
replicas: 3
selector:
matchLabels:
app: zhiqu-web
template:
metadata:
labels:
app: zhiqu-web
spec:
containers:
- name: zhiqu-web
image: leellun/zhiqu-web:latest
ports:
- containerPort: 3000
---
apiVersion: v1
kind: Service
metadata:
name: zhiqu-web-service
spec:
selector:
app: zhiqu-web
ports:
- protocol: TCP
port: 80
targetPort: 3000
type: NodePort
2.5 部署k8s
kubectl apply -f zhiqu-web.yaml
3 web管理
3.1 Dockerfile文件
# 使用官方的Node.js镜像作为基础镜像
FROM node:18-alpine
# 设置工作目录
WORKDIR /app
#复制 package.json 和 package-lock.json
COPY package.json yarn.lock ./
# 安装依赖关系
RUN yarn install
# 复制应用程序的其余部分
COPY . .
# 构建Next.js应用程序
RUN yarn build
# nginx镜像
FROM nginx:alpine
# 将构建好的React.js文件复制到Nginx的HTML目录中
COPY --from=0 /app/dist /usr/share/nginx/html
# 复制nginx配置文件到系统
COPY nginx.conf /etc/nginx/nginx.conf
# 暴露端口
EXPOSE 80
# 开启 nginx
CMD ["nginx", "-g", "daemon off;"]
nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream api_backend {
server zhiqu-service.newlandp:8080;
}
server {
listen 80;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /api {
proxy_pass http://api_backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Cookie $http_cookie;
}
}
}
3.2 编译
nerdctl build -t leellun/zhiqu-admin ./
3.3 推送镜像
nerdctl push leellun/zhiqu-admin
3.4 k8s配置
apiVersion: apps/v1
kind: Deployment
metadata:
name: zhiqu-admin
namespace: newlandp
spec:
replicas: 1
selector:
matchLabels:
app: zhiqu-admin
template:
metadata:
labels:
app: zhiqu-admin
spec:
containers:
- name: zhiqu-admin
image: leellun/zhiqu-admin:latest
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: zhiqu-admin-service
namespace: newlandp
spec:
selector:
app: zhiqu-admin
ports:
- protocol: TCP
port: 80
targetPort: 80
type: NodePort
3.5 部署
kubectl apply -f zhiqu-admin.yaml
四、界面预览
还没有开发完成,不过核心功能现在还是有的