k8s部署短视频网站(后台+web前端+web管理)

一、系统环境

系统centos7
k8sv1.24
containerdv1.7.16
etcdv3.5.0

二、镜像生成工具准备

nerdctlv1.7.6
buildkitv0.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
        volumeMounts:
        - name: timezone
          mountPath: /etc/localtime
        ports:
        - containerPort: 8080
        env:
        - name: NO_PROXY
          value: "localhost,192.168.10.0/24,127.0.0.0/8,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16"
      volumes:
        - name: timezone
          hostPath:
            path: /etc/localtime
---
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

四、界面预览

还没有开发完成,不过核心功能现在还是有的

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Docker镜像的发布可以通过两种方式来实现。一种方式是将Docker镜像导出为tar包,然后在其他设备上导入该tar包。另一种方式是在Dockerhub上建立自己的Docker仓库,其他设备可以从该仓库下载并使用镜像。 对于将Docker镜像导出为tar包的方式,可以通过执行命令"docker save -o [导出文件名.tar [镜像名]"来将镜像保存为tar包。然后,可以通过将该tar包拷贝到其他设备上,并执行"docker load -i [导出文件名.tar]"命令来导入镜像。 另一种方式是在Dockerhub上建立自己的Docker仓库。可以通过在Dockerhub上创建一个帐号,并在该帐号下创建一个新的仓库来实现。然后,可以使用"docker tag [镜像名 [Dockerhub用户名/仓库名:标签]"命令将镜像标记为Dockerhub仓库的格式,并使用"docker push [Dockerhub用户名/仓库名:标签]"命令将镜像推送到Dockerhub仓库。 通过以上两种方式,可以实现Docker镜像的发布和在其他设备上的使用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Docker详解(九)——Docker镜像发布](https://blog.csdn.net/weixin_40228200/article/details/124188726)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值