- nginx 部署vue 项目结构如下:
- 将本地vue 编译后的dist copy到容器目录覆盖掉源文件
- 将本地vue nginx.conf copy到容器目录覆盖掉源文件
- dist.tar.gz window系统使用7-Zip软件,先压缩tar,在tar的基础上压缩成gz 文件,上传linux 可以直接使用tar -zxvf 解压缩
- Dockerfile 文件:
#下载最新nginx
FROM nginx
MAINTAINER zr
#vue dist包上传到容器nginx默认目录
ADD dist.tar.gz /usr/share/nginx/html
#复制本地nginx配置文件覆盖容器nginx默认配置文件
COPY nginx.conf /etc/nginx/nginx.conf
#暴漏容器端口
EXPOSE 8088
#指定容器时区
RUN rm -f /etc/localtime \
&& ln -sv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone
#启动nginx容器服务
ENTRYPOINT nginx -g "daemon off;"
- nginx.conf 文件:
#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;
#gzip on;
server {
listen 8088; #端口号,自定义
server_name 127.0.0.1;
root /usr/share/nginx/html/;
index index.html;
location / {
try_files $uri $uri/ @router;
index index.html index.htm index.php;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET';
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
location @router {
rewrite ^.*$ /index.html last;
}
location /prod-api/ {
rewrite ^/prod-api/(.*)$ /$1 break;
proxy_pass http://k8s-realize-master-service.realize:8080/; #访问后端的地址
}
}
}
- k8s-vue-demo.yaml 文件:
apiVersion: v1
kind: Service
metadata:
name: $IMG_NAME
namespace: realize
labels:
app: $IMG_NAME
spec:
type: NodePort
ports:
- port: 8088
nodePort: 30088 #service对外开放端口
selector:
app: $IMG_NAME
---
apiVersion: apps/v1
kind: Deployment #对象类型
metadata:
name: $IMG_NAME #名称
namespace: realize
labels:
app: $IMG_NAME #标注
spec:
replicas: 1 #运行容器的副本数,修改这里可以快速修改分布式节点数量
selector:
matchLabels:
app: $IMG_NAME
template:
metadata:
labels:
app: $IMG_NAME
spec:
containers: #docker容器的配置
- name: $IMG_NAME
image: rz-dt-image-server-registry.cn-shanghai.cr.aliyuncs.com/rz-dt/$IMG_NAME:$IMG_TAG # pull镜像的地址 ip:prot/dir/images:tag
imagePullPolicy: Always #pull镜像时机,
ports:
- containerPort: 8088 #容器对外开放端口,需与springboot配置文件一致
volumeMounts:
- name: time-config
mountPath: /etc/localtime
readOnly: true
#从私有仓库拉取镜像凭证
imagePullSecrets:
- name: rz-dt-miyue
volumes:
- name: time-config
hostPath:
path: /usr/share/zoneinfo/Asia/Shanghai
- 一键部署k8s脚本 deploy.sh
echo "开始制作镜像..."
image_tag=`date +%Y-%m-%d-%H-%M-%S` #_%H%M
image_name=k8s-vue-demo
echo "当前时间版本号:$image_tag"
docker build -t ${image_name}:${image_tag} .
echo "制作镜像成功!"
echo "登录"
sudo docker login --username=aliyun0398513152 rz-dt-image-server-registry.cn-shanghai.cr.aliyuncs.com --password=1234!@#$qwerQWER
echo "镜像最新版本提交"
docker tag ${image_name}:${image_tag} rz-dt-image-server-registry.cn-shanghai.cr.aliyuncs.com/rz-dt/${image_name}:${image_tag}
docker push rz-dt-image-server-registry.cn-shanghai.cr.aliyuncs.com/rz-dt/${image_name}:${image_tag}
echo "k8s一键部署"
export IMG_TAG=${image_tag}
export IMG_NAME=${image_name}
envsubst < ${image_name}.yaml | kubectl apply -f -