当前位置: 首页> 娱乐> 明星 > 网站建设价格山东济南兴田德润什么活动_物联网名词解释_百度快照怎么优化排名_搜索引擎yandex入口

网站建设价格山东济南兴田德润什么活动_物联网名词解释_百度快照怎么优化排名_搜索引擎yandex入口

时间:2025/7/9 8:20:52来源:https://blog.csdn.net/u014007760/article/details/139587248 浏览次数:0次
网站建设价格山东济南兴田德润什么活动_物联网名词解释_百度快照怎么优化排名_搜索引擎yandex入口

作为一个后端 偶尔会承担一些运维工作 此次经历了发布vue项目的过程 因网上资料混乱 做个记录

首先要编写对应的Dockerfile (花了一些时间弄清楚[为什么需要用nginx作为基础镜像 而不是node.js])

Dockerfile

FROM nginx:1.16.0WORKDIR /frontCOPY dist /front/COPY nginx.conf /etc/nginx/conf.d/default.confEXPOSE 80

比如后端springboot项目 肯定用例如 java8作为基础镜像 那vue为什么不用同理的运行环境node.js来构建
先来看node.js的构建命令

cnpm install
node --max_old_space_size=4096 node_modules/@vue/cli-service/bin/vue-cli-service.js build

后来查阅资料 才了解 执行node build命令后 前端vue项目会被构建成一个纯静态资源放在dist目录
静态资源就不需要启动命令(类似npm run serve)来运行 可以直接静态访问。所以,只需要把静态访问的路径交由nginx管理即可

nginx.conf

log_format  spockportallog  '$remote_addr - $remote_user [$time_local] "$request" ''$status $bytes_sent $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for" ''$upstream_addr $host $sent_http_x_reqid $upstream_response_time $request_time';server {listen 80; //ngnix的默认端口 通过访问安装了ngnix的服务器ip:80 可以访问nginx服务access_log  /var/log/nginx/access.log  main;error_log   /var/log/nginx/error.log;# Make site accessible from http://localhost/client_max_body_size 1024m;proxy_send_timeout 500s;proxy_read_timeout 500s;location @rewrites {rewrite ^(.+)$ /index.html last;}location / {add_header Access-Control-Allow-Origin *;add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';if_modified_since off;expires off;etag off;root /front/; //这里很关键 /front文件夹存放的是vue build后的静态文件(Dockerfile... COPY dist /front/) 通过这句 ngnix和vue项目关联起来 这样 访问80端口 ngnix就会把请求转发到 /front/index.htmlindex index.html index.htm;try_files $uri $uri/ @rewrites;}//这里是配置请求后端接口的反向代理 效果例如 www.test.com/api/t01-->http://XXX.XXX.XX.XX:30960/t01location /api/{proxy_pass http://XXX.XXX.XX.XX:30960/; //后端的服务地址proxy_set_header Host $host;proxy_set_header Referer $http_referer;}}

vue项目中 要配置对应的api基础路径

.env

NODE_ENV=production
VUE_APP_PLATFORM_NAME=demo vue项目
VUE_APP_API_BASE_URL=/api

创建axios实例时配置前缀

const request = axios.create({// API 请求的默认前缀baseURL:process.env.VUE_APP_API_BASE_URL,timeout: 30000 // 请求超时时间
})

接下来就是在项目根目录创建manifests文件夹 里面有3个文件
1.image仓库 密钥
imageSecret.yaml

kind: Secret
apiVersion: v1
metadata:name: imageSecretnamespace: ${namespace}annotations:kubesphere.io/creator: admin
data:.dockerconfigjson: >-XXXXXXXXXXXXXXX
type: kubernetes.io/dockerconfigjson

2.k8s deployment文件
frontend-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:labels:app: front-uatname: front-uat
spec:replicas: 1selector:matchLabels:app: front-uatversion: front-uatstrategy:type: Recreatetemplate:metadata:labels:app: front-uatversion: front-uatspec:imagePullSecrets:- name: imageSecretcontainers:- image: ${image}name: frontendports:- containerPort: 80name: nginx-portresources:limits:cpu: 2000mmemory: 2000Mirequests:cpu: 500mmemory: 1000Mi

3.nodePort service 和 ingress
frontend-service.yaml

apiVersion: v1
kind: Service
metadata:labels:app: front-uatname: front-uat
spec:type: NodePortports:- name: "front-uat"port: 80targetPort: 80selector:app: front-uatversion: front-uat
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:name: front-uatannotations:nginx.ingress.kubernetes.io/rewrite-target: /
spec:rules:- host: ${host}.demo.comhttp:paths:- path: /backend:serviceName: front-uatservicePort: 80

一共在根目录添加了2个文件 1个文件夹
Dockerfile nginx.conf manifests(imageSecret.yaml frontend-deployment.yaml frontend-service.yaml)

流水线主要有2个步骤
1.镜像构建并推送到阿里云buildkit
2.kubectl发布到阿里云k8s集群

ps: Dockerfile中 是可以用 node.js作为基础镜像的 那就是另外一种构建方法了 不使用node build命令
而是直接在Dockerfile中使用CMD npm run serve。

关键字:网站建设价格山东济南兴田德润什么活动_物联网名词解释_百度快照怎么优化排名_搜索引擎yandex入口

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: