1项目准备
1.1项目执行npm build命令构建生产环境包
1.2将生产环境包与dockerfile文件放在同一级目录下
1项目准备
1.1项目执行npm build命令构建生产环境包
1.2将生产环境包与dockerfile文件放在同一级目录下
2编写dockerfile文件
# 使用官方 Nginx 镜像作为基础镜像
FROM nginx:latest
# 添加你的自定义 Nginx 配置文件
COPY nginx.conf /etc/nginx/nginx.conf
# 如果有其他静态文件或站点内容,也可以进行复制
COPY /dist /etc/mlgdzc_qd/dist
COPY /nginxconf/ /etc/mlgdzc_qd/nginxconf
# 暴露 Nginx 监听的端口(默认为 80)
EXPOSE 8080
# 可以添加其他自定义操作,例如设置环境变量等
# 启动 Nginx 服务器
CMD ["nginx", "-g", "daemon off;"]
编写ngxin配置文件
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;
include /etc/mlgdzc_qd/nginxconf/*;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
localhost.conf
server {
listen 8080;
server_name localhost;
charset utf-8;
location / {
root /etc/mlgdzc_qd/dist;
try_files $uri $uri/ /index.html;
}
}
3构建Docker镜像
进入到dockerfile文件目录下
执行docker build -t 镜像名称:版本号 -f dockerfile文件名构建镜像
docker images命令查看镜像是否构建成功
4编写K8syaml文件
deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mlgdzc-qd-deploy
namespace: default
spec:
replicas: 2
selector:
matchLabels:
app: mlgdzc-qd
release: v1
template:
metadata:
labels:
app: mlgdzc-qd
release: v1
spec:
containers:
- name: mlgdzc-qd
image: mlgdzc_qd:latest
imagePullPolicy: IfNotPresent
service.yaml
apiVersion: v1
kind: Service
metadata:
name: mlgdzc-qd-nodeport
namespace: default
spec:
type: NodePort
selector:
app: mlgdzc-qd
release: v1
ports:
- name: httpd
port: 8080
targetPort: 8080
nodePort: 30000
进入到yaml文件目录下
执行kubectl apply -f deployment.yaml kubectl apply -f service.yaml
执行命令
kubectl get deployment,pods,svc
pod运行状态Running,正常
访问本机项目地址
项目访问正常,登陆正常,部署完成
省略了MySQL数据库部署