You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
FROM node:latest as builder
WORKDIR /data/project
COPY ./ ./
RUN npm install && npm run build
FROM alpine
WORKDIR /project/dist
COPY --from=builder /data/project/dist ./
docker build -t lqshow/app-test .
Step 2: Create configmap for Nginx configuration
将 nginx 的配置通过 Configmap 方式注入到容器
kind: ConfigMapapiVersion: v1metadata:
name: nginx-configdata:
nginx.conf: | user nginx; worker_processes 1; error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/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 /var/log/nginx/access.log main; sendfile on; keepalive_timeout 65; # Load config files from the /etc/nginx/conf.d directory # The default server is in conf.d/default.conf include /etc/nginx/conf.d/*.conf; }default.conf: | server { listen 80; server_name localhost; gzip on; gzip_comp_level 9; gzip_vary on; gzip_static on; gzip_types text/plain application/x-javascript text/css application/xml application/json application/javascript application/x-httpd-php image/jpeg image/gif image/png image/svg+xml xml/svg; # Set nginx to serve files from the shared volume root /usr/share/nginx/data/project; location / { try_files $uri /index.html; } }
Overview
目前所有前端产品的部署流程是这样的,首先由前端项目打包提供纯静态文件,然后基于 nginx 实现静态网页的部署。
现在的做法是将静态文件和 nginx 定制在一个镜像内,由于打包需要 nodejs 的环境,所以最终生产的镜像根据依赖的不同一般都达到了1G 以上。
目前升级 app 的内容,哪怕只更改一个 app 的配置或者调整 nginx 的配置,都需要重新制作一个新的镜像发布,这样的流程非常麻烦。即使事先将配置做成卷映射,但是镜像依然巨大。
Sidecar pattern
sidecar 指的就是我们可以在一个 Pod 中,启动一个或多个辅助容器,来完成一些独立于主进程(主容器)之外的工作。
它主要利用在同一 Pod 中的容器可以共享存储空间的能力。
Frontend Deployment
根据 Sidecar 的容器设计模式,我们可以很容易想到,这里 nginx 应该作为主容器,前端项目作为辅助容器,只需负责提供静态文件即可。
这样部署有以下3个好处
Step 1: Frontend Dockerfile
通过多阶段构建生产 mini 镜像
docker build -t lqshow/app-test .
Step 2: Create configmap for Nginx configuration
将 nginx 的配置通过 Configmap 方式注入到容器
Step 3: Create deployment
在这个 Pod 模板中,定义了两个容器
Reference
The text was updated successfully, but these errors were encountered: