需求

实际应用中,一个系统往往是包含前后端的,通常前端使用Vue,后端使用Springboot。而之前我们只是在K8S中配置过后端Springboot项目,现在我们需要将完整的系统部署到K8S集群中,通过本次部署可以具体分析如何部署,为日后上线生产环境做好充足的准备。

前端

前端如果使用Vue开发,需要将打包后的dist放到Web容器的root目录下,在此我们使用Deployent来部署Nginx pod。

1.root目录

Nginx镜像默认配置文件中指定的root目录为/usr/share/nginx/html,我们可以使用默认根目录也可以自定义,但是要保证集群中node节点能共享此目录,因此需要将root目录进行持久化并以共享的方式挂在,在此我们使用简单的NFS。

# Master节点
mkdir /App/nfs/nginx/htdocs
chmod 777 /App/nfs/nginx/htdocs
cd /App/nfs/nginx/htdocs
# 将dist 放到hello.test.cn 目录下
mkdir hello.test.cn
mv dist .
# htdocs目录将通过NFS挂载到Nginx pod的自定义站点root目录/mnt上。
/mnt/hello.test.cn/dist

2.Nginx配置文件

Nginx配置文件默认放在/etc/nginx/conf.d中,站点配置文件通过ConfigMap进行定义。

apiVersion: v1
kind: ConfigMap
metadata:name: config-nginx-hello-test-cnnamespace: testlabels:app: config-nginx-hello-test-cn
data:hello.test.cn.conf: |-server {listen       80;server_name hello.test.cn;location / {root /mnt/hello.test.cn/dist;index index.html index.htm;try_files $uri $uri/ /index.html;}}

3.Nginx pod

Nginx定义为Deployment类型的工作负载,其中自定义站点root目录及具体的站点配置文件分别以NFSConfigMap形式进行挂载。

apiVersion: apps/v1
kind: Deployment
metadata:name: web-nginxnamespace: test
spec:selector:matchLabels:app: nginxtemplate:metadata:name: web-nginxlabels:app: nginxspec:containers:- name: nginximage: nginximagePullPolicy: IfNotPresentvolumeMounts:- name: htdocsmountPath: /mnt- name: config-nginx-hello-test.cnmountPath: /etc/nginx/conf.d/hello.test.cn.confsubPath: hello.test.cn.confports:- containerPort: 80volumes:#挂在nfs- name: htdocsnfs:path: /App/nfs/htdocsserver: 192.168.3.217#挂在configmap- name: config-nginx-hello-test-cnconfigMap:name: config-nginx-hello-test-cndefaultMode: 0640

注意:hello.test.cn.conf一定要挂载为subPath,否则将会变成一个目录。

4.Service

前端的Service比较简单,部署类型为NodePort。

apiVersion: v1
kind: Service
metadata:name: web-helloworldnamespace: test
spec:type: NodePortselector:app: nginxports:- port: 80targetPort: 80

后端

后端Springboot部署,详细配置说明可参见K8S部署Springboot项目一文,在此我们不做具体解释。

# 1.Deployment
apiVersion: apps/v1
kind: Deployment
metadata:name: api-helloworldnamespace: test
spec:replicas: 1selector:matchLabels:app: helloworldtemplate:metadata:name: helloworldlabels:app: helloworldspec:hostAliases:- ip: "10.11.10.11"hostnames:- "api1.test.cn"- "api2.test.cn"- ip: "10.11.10.12"hostnames:- "api3.test.cn"containers:- name: helloworldenv:- name: JAVA_OPTSvalue: "-Xmx128m -Xms128m -Dspring.profiles.active=test"image: harbor.test.cn/helloworld/helloworld:1311c4520122dfa67bb60e0103c9519fcb370e50imagePullPolicy: IfNotPresentlivenessProbe:httpGet:path: /apiport: 8080initialDelaySeconds: 200timeoutSeconds: 5readinessProbe:httpGet:path: /apiport: 8080initialDelaySeconds: 180timeoutSeconds: 5ports:- containerPort: 8080resources:limits:#cpu: "0.5"memory: "500Mi"requests:#cpu: "0.5"memory: "500Mi"volumeMounts:- name: logdirmountPath: /logs- name: localtimemountPath: /etc/localtime- name: timezonemountPath: /etc/timezoneimagePullSecrets:- name: harborvolumes:- name: logdiremptyDir: {}- name: localtimehostPath:path: /etc/localtime- name: timezonehostPath:path: /etc/timezone# 2.Service
apiVersion: v1
kind: Service
metadata:name: api-hellworldnamespace: test
spec:type: NodePortselector:app: helloworldports:- port: 8080targetPort: 8080

注意:我们在此暂时取消CPU 资源限制,因为Springboot在启动时耗费的CPU资源较多,导致进程启动慢,在测试时可以临时关闭。

Ingress

Ingress 作为访问前后端的入口,我们在此将其分离出来单独讲解。

在Ingress中我们通过访问路由将前后端分离:

  • / 访问前端静态文件;
  • /api 访问后端接口;

1.http访问

apiVersion: extensions/v1beta1
kind: Ingress
metadata:name: ingress-hello.test.cnnamespace: test
spec:rules:- host: hello.test.cnhttp:paths:- path: /apipathType: Prefixbackend:serviceName: api-hellworldservicePort: 8080- path: /pathType: Prefixbackend:serviceName: web-helloworldservicePort: 80

2.https访问

# 1.从证书创建secret
# kubectl create secret tls tls-secret-test-cn --key test.cn.key --cert test.cn.pem -n test# 2.Ingress配置
apiVersion: extensions/v1beta1
kind: Ingress
metadata:name: ingress-hello.test.cnnamespace: test
spec:rules:- host: hello.test.cnhttp:paths:- path: /apipathType: Prefixbackend:serviceName: api-helloworldservicePort: 8080- path: /pathType: Prefixbackend:serviceName: web-nginxservicePort: 80tls:- hosts:- hello.test.cnsecretName: tls-secret-test-cn

注意:配置https,默认访问http会强制转换成https访问。
在此我们可以通过ssl-redirect设置为"false"(默认为true)来解决,将其加入到ingress-nginx的全局配置文件。

# vim global_configmap.yaml
# ingress-nginx 全局配置文件
apiVersion: v1
kind: ConfigMap
metadata:name: nginx-configurationnamespace: ingress-nginxlabels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginx
data:proxy-connect-timeout: "300"proxy-read-timeout: "300"proxy-send-timeout: "300"proxy-body-size: "200m"ssl-redirect: "false"# 应用后,nginx会自动reload生效
# kubectl apply -f global_configmap.yaml

此时既可以通过http访问,也可以通过https访问。

配置文件合并

最好我们将以上配置合并为一个配置文件hello.test.cn.yaml,方便使用。

apiVersion: v1
kind: ConfigMap
metadata:name: config-nginx-hello-test-cnnamespace: testlabels:app: config-nginx-hello-test-cn
data:hello.test.cn.conf: |-server {listen       80;server_name hello.test.cn;location / {root /mnt/hello.test.cn/dist;index index.html index.htm;try_files $uri $uri/ /index.html;}}---
apiVersion: apps/v1
kind: Deployment
metadata:name: web-nginxnamespace: test
spec:selector:matchLabels:app: nginxtemplate:metadata:name: web-nginxlabels:app: nginxspec:containers:- name: nginximage: nginximagePullPolicy: IfNotPresentvolumeMounts:- name: htdocsmountPath: /mnt- name: config-nginx-hello-test.cnmountPath: /etc/nginx/conf.d/hello.test.cn.confsubPath: hello.test.cn.confports:- containerPort: 80volumes:#挂在nfs- name: htdocsnfs:path: /App/nfs/htdocsserver: 192.168.3.217#挂在configmap- name: config-nginx-hello-test-cnconfigMap:name: config-nginx-hello-test-cndefaultMode: 0640---
apiVersion: apps/v1
kind: Deployment
metadata:name: api-helloworldnamespace: test
spec:replicas: 1selector:matchLabels:app: helloworldtemplate:metadata:name: helloworldlabels:app: helloworldspec:hostAliases:- ip: "10.11.10.11"hostnames:- "api1.test.cn"- "api2.test.cn"- ip: "10.11.10.12"hostnames:- "api3.test.cn"containers:- name: helloworldenv:- name: JAVA_OPTSvalue: "-Xmx128m -Xms128m -Dspring.profiles.active=test"image: harbor.test.cn/helloworld/helloworld:1311c4520122dfa67bb60e0103c9519fcb370e50imagePullPolicy: IfNotPresentlivenessProbe:httpGet:path: /apiport: 8080initialDelaySeconds: 200timeoutSeconds: 5readinessProbe:httpGet:path: /apiport: 8080initialDelaySeconds: 180timeoutSeconds: 5ports:- containerPort: 8080resources:limits:#cpu: "0.5"memory: "500Mi"requests:#cpu: "0.5"memory: "500Mi"volumeMounts:- name: logdirmountPath: /logs- name: localtimemountPath: /etc/localtime- name: timezonemountPath: /etc/timezoneimagePullSecrets:- name: harborvolumes:- name: logdiremptyDir: {}- name: localtimehostPath:path: /etc/localtime- name: timezonehostPath:path: /etc/timezone---
apiVersion: v1
kind: Service
metadata:name: web-helloworldnamespace: test
spec:type: NodePortselector:app: nginxports:- port: 80targetPort: 80---
apiVersion: v1
kind: Service
metadata:name: api-hellworldnamespace: test
spec:type: NodePortselector:app: helloworldports:- port: 8080targetPort: 8080---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:name: ingress-hello.test.cnnamespace: test
spec:rules:- host: hello.test.cnhttp:paths:- path: /apipathType: Prefixbackend:serviceName: api-helloworldservicePort: 8080- path: /pathType: Prefixbackend:serviceName: web-nginxservicePort: 80tls:- hosts:- hello.test.cnsecretName: tls-secret-test-cn

问题思考

以上虽然实现了一套完整的前后端系统在K8S中部署,但是我们还需思考以下几个问题:

1.共享Nginx pod

K8S集群内一个项目我们使用一个Nginx pod来提供静态文件的访问,如果是多个项目将会产生很多Nginx pod,因此是否可以考虑使用一个Nginx pod来运行所有项目的静态文件呢?

2.Nginx热更新

每个静态站点我们都是用ConfigMap生成一个Nginx 配置文件,如hello.test.cn.conf;如果所有项目都使用同一个Nginx pod,那么再次通过ConfigMap生成Nginx配置文件,Nginx pod不会自动reload,这就需要手动操作或有一套热更新机制。

一套包含完整前后端的系统如何在K8S中部署?相关推荐

  1. 洗车店小程序管理系统源码 含完整前后端+搭建教程

    分享一个微信洗车源码,洗车店小程序管理系统源码,代码包含完整前后端+详细安装部署搭建教程. 系统特色功能一览: 1.所有页面支持自定义配色.自定义图标.内容,产品图片等: 2.适用于平台和连锁门店: ...

  2. 开源智能电子名片系统源码 含小程序完整前后端+搭建教程

    分享一个开源版智能电子名片小程序系统源码,系统为平台版,基于微信,代码包含了完整前后端+数据库+搭建教程.一张名片打通6大系统:名片+商城+官网+AI雷达+CRM+客服.   系统特色功能一览: 1. ...

  3. 全新开源版知识付费系统源码 支持多终端合一 含完整前后端+搭建教程

    分享一个全新的开源版知识付费系统源码,支持多终端合一管理,含微信小程序端.微信公众号端.H5端.PC端,含完整前后端.数据库代码包和搭建教程. 系统特色功能一览: 1.支持视频课程.音频课程.图文课程 ...

  4. 汽车4s店新车二手车租卖小程序源码系统 汽车行业通用版 含完整前后端+搭建教程

    分享一个汽车行业通用版系统源码,汽车4s店新车二手车租卖小程序源码系统,含完整前后端+搭建教程. 系统支持新车二手车的汽车在线展示.在线预约.购车计算器.贷款计算器,门店介绍,新闻资讯,汽车详情介绍等 ...

  5. 基于SpringBoot+Mybatis开发的前后端ERP系统Saas平台

    源码介绍 基于SpringBoot+Mybatis开发的前后端ERP系统Saas平台 ,专注于中小微企业的ERP软件.进销存系统,是一套基于SpringBoot2.2.0, Mybatis, JWT, ...

  6. 最新开源微信小程序一键开发平台源码 支持15大功能模块+完整前后端+搭建教程

    分享一个开源微信小程序一键开发综合平台源码,系统支持15大小程序功能模块,涉及各行各业,含完整前后端+详细搭建部署教程. 系统特色功能一览: 1.全新重构升级功能后端文件和前端文件: 2.整套源码已经 ...

  7. 社区拼团商城小程序源码+完整前后端+安装部署教程

    分享一个最新的社区拼团商城小程序源码+完整前后端+安装部署教程,功能支持界面diy+团长+供应商+拼团+秒杀+优惠券+菜谱+积分+群接龙+充值+预售+配送等,整合线下社区资源,模式丰富. 系统有总后台 ...

  8. vue 后台返回的文件流进行预览_vue实现下载文件流完整前后端代码

    这篇文章主要为大家详细介绍了vue实现下载文件流完整前后端代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 使用Vue时,我们前端如何处理后端返回的文件流 首先后端返 ...

  9. 同城信息本地生活服务小程序源码+完整前后端+搭建教程

    分享一个微同城本地生活服务类的小程序源码,含完整前后端程序包和搭建教程,核心功能有:电商商城.新闻资讯.外卖配送.服务预约.招聘求职.社区论坛等,为本地用户提供个性化.便捷化.多元化服务. 小程序源码 ...

  10. 开源版小程序开发一键生成平台源码 完整前后端+搭建教程

    分享一个一键生成微信小程序的源码,含15大功能模块,像微同城.电商类.在线报名.社区团购.外卖点餐.AI智能名片等小程序都有,根据需求可任意调用,自由DIY,开发属于你自己的小程序,源码开源可二开,含 ...

最新文章

  1. BZOJ 3585: mex( 离线 + 线段树 )
  2. samba srver on centos-7
  3. 堆排序示例-java
  4. 【linux系统】maven安装
  5. 转:如何用C#语言构造蜘蛛程序
  6. c语言大数相加怎么写,大数相加
  7. snake4444勒索病毒成功处理教程方法工具达康解密金蝶/用友数据库sql后缀snake4444...
  8. centos下apache安装
  9. 深入理解Java虚拟机——类加载机制
  10. php对象转换为字符串,php – 类的对象..无法转换为字符串
  11. 纠正网上Mac 上使用Hbuilder运行夜神游模拟器,Hbuilder找不到模拟器
  12. numpy中的argpartition
  13. 建立RHELAS4下面的Yum本地源
  14. html页面显示代码插件,客户端显示web网页支持html5的第三方内核插件
  15. 自由网络-去中心化分布式网络
  16. 直播入门(二)从视频采集到客户端播放
  17. kinect fusion 3D重建基本算法
  18. 张雨潇 计算机科学,三亚考生本科第二批录取名单[一]
  19. VLD的安装使用及其问题
  20. 用Python实现单词尾缀的分析及提取

热门文章

  1. 提取swf素材_swf素材提取工具
  2. java递归算法的实例最细讲解
  3. 中国联通骨干网网络介绍
  4. echarts引入china报错(The GeoJSON of the map must be provided)
  5. 软件那么多,恢复数据还靠它
  6. 第03讲 struts2中ActionSupport引入
  7. jsp网上零食销售网站系统
  8. UT斯达康首家手机网上专卖店落户淘宝网
  9. Protel 99 SE 的坑
  10. 快看这里,如何卸载windows11自动更新下载的文件还你C盘空间