一、制作镜像:
1、创建nginx配置文件default.conf
server {listen 80;server_name localhost; # 修改为docker服务宿主机的iplocation / {root /usr/share/nginx/html;index index.html index.htm;try_files $uri $uri/ /index.html =404;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}
}
root /usr/share/nginx/html:这个目录和下面创建的Dockerfile中目录要保持一致
2、创建Dockerfile
FROM nginx:1.20.2MAINTAINER testRUN rm /etc/nginx/conf.d/default.confADD default.conf /etc/nginx/conf.d/COPY dist/ /usr/share/nginx/html
- FROM nginx -- 打包容器的底层来刚才先拉取的nginx
- MAINTAINER beizhu:备注
- RUN rm /usr/local/nginx/conf/nginx.conf.default:删除目录下的nginx.conf.default 文件
- ADD nginx.conf.default /usr/local/nginx/conf/:将default.conf复制到/usr/local/nginx/conf/下,用本地的default.conf配置来替换nginx镜像里的默认配置
- COPY dist/ /usr/local/nginx/html/:将项目根目录下dist文件夹(构建之后才会生成)下的所有文件复制到镜像/usr/local/nginx/html/目录下上一步root地址
3、准备静态模板
4、打包docker镜像image并推送
docker build -f Dockerfile -t [dockerhub用户名]/vue-pro:1.0 .
docker push [dockerhub用户名]/vue-pro:1.0
如果是用的阿里云镜像,请参照阿里云镜像推送方式。
二、K8S部署:
1、编写 K8S yaml 文件
vi deploy-ui.yaml
apiVersion: v1
kind: Namespace
metadata:name: shop-ui---apiVersion: apps/v1
kind: Deployment
metadata:name: manager-uinamespace: shop-ui
spec:replicas: 1selector:matchLabels:app: manager-uitemplate:metadata:labels:app: manager-uispec:containers:- name: manager-uiimage: 192.168.1.48/shop-ui/vue-pro:1.0ports:- name: httpcontainerPort: 80
---apiVersion: v1
kind: Service
metadata:name: manager-ui-servicenamespace: shop-ui
spec:selector:app: manager-uiports:- name: httpprotocol: TCPport: 80targetPort: 80nodePort: 30001type: NodePort
2、部署服务
kubectl apply -f deploy-ui.yaml