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
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
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
3 开始配置docker
项目根目录下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 .
# 将端口8080暴露给外部世界
EXPOSE 8080
# 命令运行可执行文件
CMD ["./main"]
4 编译
nerdctl build -t leellun/zhiqu ./
执行:
[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
5. 将镜像推送到容器镜像仓库
登录 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)
6 k8s配置
在项目根目录下创建 deployment.yaml
:
apiVersion: apps/v1
kind: Deployment
metadata:
name: go-app
spec:
replicas: 3
selector:
matchLabels:
app: go-app
template:
metadata:
labels:
app: go-app
spec:
containers:
- name: go-app
image: yourusername/go-app:latest
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: go-app-service
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 8080
selector:
app: go-app
7 部署到 Kubernetes 集群
kubectl apply -f deployment.yaml