kore web 项目的单一二进程应用构建

发布时间 2023-11-29 10:46:17作者: 荣锋亮

kore 对于开发的web 应用支持基于模块模式的(编译为共享库)以及单一二进制文件(all-in-one)

参考使用

  • 核心配置
    就是对于通过kodev创建的项目中构建配置添加 single_binary=yes
    参考配置
 
# hello build config
# You can switch flavors using: kodev flavor [newflavor]
 
# Set to yes if you wish to produce a single binary instead
# of a dynamic library. If you set this to yes you must also
# set kore_source together with kore_flavor.
#single_binary=no
#kore_source=/home/joris/src/kore
#kore_flavor=
single_binary=yes
# The flags below are shared between flavors
cflags=-Wall -Wmissing-declarations -Wshadow
cflags=-Wstrict-prototypes -Wmissing-prototypes
cflags=-Wpointer-arith -Wcast-qual -Wsign-compare
 
cxxflags=-Wall -Wmissing-declarations -Wshadow
cxxflags=-Wpointer-arith -Wcast-qual -Wsign-compare
 
# Mime types for assets served via the builtin asset_serve_*
#mime_add=txt:text/plain; charset=utf-8
#mime_add=png:image/png
#mime_add=html:text/html; charset=utf-8
 
dev {
    # These flags are added to the shared ones when
    # you build the "dev" flavor.
    cflags=-g
    cxxflags=-g
}
 
#prod {
#    You can specify additional flags here which are only
#    included if you build with the "prod" flavor.
#}
  • docker 构建
FROM kore/kodev:kodev as base
 
WORKDIR /hello
 
COPY . /hello
 
RUN kodev build
 
RUN ls -sailh /hello/
 
# 使用了kore 的基础镜像,其他的会有问题,除非自己完整构建kodev 以及kore 基础镜像,因为单一二进制文件是后台进程
# 所以使用runit 进行进程管理
FROM kore/kore:4.2.3
 
RUN apk add  --no-cache runit
 
COPY --from=base /hello/hello /app/hello
 
COPY hello.runit /etc/service/hello/run
 
RUN chmod +x /etc/service/hello/run
 
EXPOSE 8888
 
RUN  which runsvdir
 
ENTRYPOINT ["/sbin/runsvdir", "-P", "/etc/service"]

hello.runit

 
#!/bin/sh
exec /app/hello

压测

使用loadtest 工具

  • 命令
loadtest -n 10000  http://localhost:8888

说明

kore 单一二进制文件的模式是挺方便,但是目前来说它是在构建时将一些公共配置以及运行时配置直接也打包到应用中了
此种方法有好处也有不太方便的地方,就是如果希望重新配置就得重新构建,而且目前基于容器构建需要使用官方的基础
镜像(除非自己重新构建一个)否则会有兼容的问题, 同时似乎应该是可以支持前台进程的,但是在尝试构建的时候木有
成功,完整代码我已经push github 了

参考资料

https://docs.kore.io/4.2.0/applications/building.html
https://github.com/rongfengliang/kore_learning
https://github.com/alexfernandez/loadtest