【Kubernetes系列】K8s下部署前端Vue应用


一、编写 Dockerfile 文件

vi Dockerfile
FROM node:10.19.0 as build-stage
WORKDIR /app
ENV NODE_ENV uat
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

# production stage
FROM nginx:stable-alpine as production-stage
COPY --from=build-stage /app/dist /usr/share/nginx/html
COPY ./nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

二、编写 nginx 配置文件

vi nginx.conf
#这个文件给docker用的
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    client_max_body_size 10m;
    gzip on;
    gzip_min_length  5k;
    gzip_buffers     4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 4;
    gzip_types       text/plain application/x-javascript application/javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
    gzip_vary on;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root /usr/share/nginx/html;
            try_files $uri $uri/ /index.html $uri/ =404;
            index  index.html index.htm;
        }
    }
}

三、编写打包上传私有仓库 shell

vi docker-build.sh
#代码目录
code_path=$PWD
REGISTRY_PATH='192.168.1.48/shop-ui'
BUILD_VERSION=4.2.3.7

read -p "请输入仓库地址": REGISTRY_PATH

echo $REGISTRY_PATH

docker build -t $REGISTRY_PATH/manager-ui:$BUILD_VERSION ${code_path}/manager
docker push $REGISTRY_PATH/manager-ui:$BUILD_VERSION

四、编写 K8s yaml 文件

vi deploy-ui.yaml
apiVersion: v1
kind: Namespace
metadata:
  name: shop-ui

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: manager-ui
  namespace: shop-ui
spec:
  replicas: 1
  selector:
    matchLabels:
      app: manager-ui
  template:
    metadata:
      labels:
        app: manager-ui
    spec:
      containers:
      - name: manager-ui
        image: 192.168.1.48/shop-ui/manager-ui:4.2.3.7
        ports:
        - name: http
          containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: manager-ui-service
  namespace: shop-ui
spec:
  selector:
    app: manager-ui
  ports:
    - name: http
      protocol: TCP
      port: 80
      targetPort: 80
      nodePort: 10003
  type: NodePort

五、部署服务

kubectl apply -f deploy-ui.yaml
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奔跑吧邓邓子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值