惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LINUX DO - 最新话题
A
Arctic Wolf
I
Intezer
V
Vulnerabilities – Threatpost
C
Cisco Blogs
MyScale Blog
MyScale Blog
NISL@THU
NISL@THU
Y
Y Combinator Blog
C
CERT Recently Published Vulnerability Notes
P
Privacy International News Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
酷 壳 – CoolShell
酷 壳 – CoolShell
Recorded Future
Recorded Future
云风的 BLOG
云风的 BLOG
S
SegmentFault 最新的问题
Microsoft Security Blog
Microsoft Security Blog
L
LangChain Blog
博客园 - 聂微东
博客园 - 叶小钗
F
Fortinet All Blogs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Recent Announcements
Recent Announcements
C
Cyber Attacks, Cyber Crime and Cyber Security
Latest news
Latest news
Simon Willison's Weblog
Simon Willison's Weblog
P
Palo Alto Networks Blog
S
Schneier on Security
C
Cybersecurity and Infrastructure Security Agency CISA
V
V2EX
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The Hacker News
The Hacker News
博客园 - 司徒正美
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
L
LINUX DO - 热门话题
罗磊的独立博客
K
Kaspersky official blog
Last Week in AI
Last Week in AI
Know Your Adversary
Know Your Adversary
小众软件
小众软件
Stack Overflow Blog
Stack Overflow Blog
T
Threat Research - Cisco Blogs
D
DataBreaches.Net
Scott Helme
Scott Helme
P
Proofpoint News Feed
P
Privacy & Cybersecurity Law Blog
P
Proofpoint News Feed
博客园 - 三生石上(FineUI控件)
Hugging Face - Blog
Hugging Face - Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
F
Full Disclosure

陈少文的网站

巨变与机遇的未来十年 Kubernetes 平台管理软件压力测试方案 使用镜像部署 Hexo 静态页面 终于等到你 - GitHub 镜像仓库服务(ghcr.io) 一起来学 Go --(6)Interface 一起来学 Go --(5)Goroutine 和 Channel 什么是函数式编程 如何在 Kubernetes 集群集成 Kata 柯里化与偏函数 使用 PyGithub 自动创建 Label 软件产品是团队能力的输出 Helm 2 、Helm 3 比较 IoT 变现 Kubernetes 中的 DNS 服务 国内的 Helm 镜像源 Harbor 使用自签证书支持 Https 访问 DevOps 工具链之 Prow 如何使用 kfctl 安装 Kubeflow VS Code 无法下载 Go 插件的工具包 工程师更应具有服务精神 你不知道的 Docker 使用技巧 使用 Docker 运行 Tensorflow 论中国 什么是左移 如何清空 Git 仓库全部历史记录 一禅小和尚 有风吹过厨房 时间的玫瑰 如何在 CentOS 安装 GPU 驱动 开发 Tips(19) 使用 Velero 备份 Kubernetes 集群 Kubernetes Cheat Sheet 开发 Tips(18) 如何构建一个 Java 工程 开发 Tips(17) KubeSpray 安装 Kubernetes 报错 ip in ansible_all_ipv4_addresses 基于 Kubernetes 和 Jenkins 搭建自动化测试系统 在 Kubernetes 上动态创建 Jenkins Slave 使用 Jenkins 进行服务拨测 开发 Tips(16) Kubernetes 签发 Ingress 证书及日常故障运维 Kubernetes 中 Deployment 的基本操作 Kubernetes 中的证书 如何使用 KubeBuilder 开发一个 Operator Kubernetes 1.6.0 安装问题汇总 镜像管理工具 -- Harbor 开发 Tips(15) Docker 如何拉取镜像 开发 Tips(14) 使用 Helm 安装 harbor 开发 Tips(13) 使用 S2I 构建云原生应用 在 Kubernetes 中使用 emptyDir、hostPath、localVolume 开发 Tips(12) 开发 Tips(11) 代码质量分析工具 SonarQube 使用 Kubeadm 安装 Kubernetes 集群 一起来学 Go --(4)常用函数 Kubernetes 中的 Ceph Kubernetes 之 Volumes Kubernetes 之 Labels、Selectors 开发 Tips(10) 开源正在重构商业模式 Kubernetes 之网络 Kubernetes 之 API 使用 Helm 和 Operator 快速部署 Prometheus Kubernetes 复杂有状态应用管理框架 -- Operator Kubernetes 的包管理器 -- Helm 一起来学 Go --(3)Go Modules 如何一步一步地优化博客方案 kubectl 实用指南 Kubernetes 中的基本概念 搭建远程 Kubernetes 开发环境 大公司和小公司的 ToB 思路 开发 Tips(9) Go 入门指南 一起来学 Go --(2)数据与逻辑结构 如何预防 Web 富文本中的 XSS 攻击 django-xss-cleaner 云工作时代 一起来学 Go --(1)背景与特点 SaaS 开发团队的不同阶段 你不知道的 Git 使用技巧 输出既服务 微服务设计 继续奔跑 开发 Tips(8) 从账户安全到二次验证 Django 性能之数据库查询优化 Django 性能之分库分表 敏捷开发之研发流程 打造一致性的团队 开发 Tips(7) Pytest 进阶学习之 Mock PaaS 部署之 buildpack Go 开发配置 领域输出才是 PaaS 的核心竞争力 Pytest 入门学习 开发 Tips(6) 如何使用 Jenkins、Docker、GitLab 搭建 Django 自动化部署流程
老树开新花 - Cloud Native Buildpacks
微信公众号 · 2021-02-05 · via 陈少文的网站

Please enable Javascript to view the contents

1. Buildpack 老树开新花

Buildpacks 项目最早是由 Heroku 在 2011 年发起, 被以 Cloud Foundry 为代表的 PaaS 平台广泛采用。在之前的文档 《PaaS 部署之 buildpack》 中, 我演示了如何将一个 Django 应用部署到 Heroku 上。

Buildpacks 不足的是产出包是 Droplet 格式, 不能直接适配容器平台。

在 2018 年 1 月, Pivotal 和 Heroku 发起了一个项目 Cloud Native Buildpacks(简称, CNB) , 并在同年十月份加入 CNCF 。这个项目的目标就是实现一个统一的应用打包生态系统。

简单点就是, 之前打包出来是 Droplet, 现在是 OCI 容器镜像, 可以部署在任意兼容 OCI 镜像的容器平台。工作原理如下:

  1. 探测。根据源码内容, 自动探测、匹配 buildpacks 。
  2. 分析。在历史构建中, 查找缓存。
  3. 构建。将应用的源码, 创建为可运行的包
  4. 导出 OCI 格式的容器镜像。

2. Cloud Native Buildpacks 适合哪些人

Cloud Native Buildpacks 非常适合需要频繁打包应用的场景, 而且应用的种类比较多。

  • aPaaS 平台

aPaaS 是提供运行时的平台, 经常需要部署各类应用。使用 Cloud Native Buildpacks 能规范打包流程, 快速将源码转换为镜像。

  • FaaS 平台

FaaS 平台提供函数的运行环境。在以容器为核心的基础设施上, FaaS 将用户代码打包为镜像, 再创建容器运行。而 Cloud Native Buildpacks 既能探测用户代码的编程语言、框架, 还能快速编译成 OCI 镜像, 正是 FaaS 之所需。Google Cloud Platform 的 FaaS 采用的就是 Buildpacks。

  • 开发者

并不是每个开发者都适合使用 Cloud Native Buildpacks, 学习和维护也需要成本。如果你有很多应用需要维护和发布, 那么 Cloud Native Buildpacks 将拯救你, 起码可以少写很多 Dockerfile。

3. 与同类应用打包工具的比较

关于 S2I, 我之前写过一篇文档, 使用 S2I 构建云原生应用。我负责研发的一款产品中, 采用的就是 S2I 对源码进行打包成镜像。

而 Buildpacks 是我上一份工作中接触到的技术。当时我开发 SaaS , 需要借助 Buildpacks 打包应用。

这是来自 Buildpacks 社区的同类工具对比:

Logoopenshiftjibko
Product NameCloud Native Buildpackssource-to-image (s2i)Jibko
Advanced CachingYesYesNoNo
Bill-of-MaterialsYesNoNoNo
Modular / PluggableYesNoN/A †N/A †
Multi-languageYesYesNoNo
Multi-processYesNoNoNo
Minimal app imageYesYes ‡YesYes
RebasingYesNoNoNo
ReproducibilityYesNoYesYes
ReusabilityYesYesN/A †N/A †
Integrations_ Azure
_ CircleCI
_ GitLab
_ Google
_ Heroku
_ Spring Boot
_ Tekton
_ …
* OpenShift_ Gradle
_ Maven
GovernanceCNCFRed HatGoogleGoogle

从生态和功能上看, Buildpacks 比 S2I 更好。

4. 在 Docker 环境下打包应用

需要提前安装 Git 环境, 在 Docker 环境下操作。

4.1 安装 Pack 命令工具

Pack 是 Cloud Native Buildpacks 提供用于使用 buildpacks 的工具。这里以 Linux 系统为例进行安装:

1
(curl -sSL "https://github.com/buildpacks/pack/releases/download/v0.17.0/pack-v0.17.0-linux.tgz" | sudo tar -C /usr/local/bin/ --no-same-owner -xzv pack)

查看 pack 的版本

1
2
3
pack version

0.17.0+git-d9cb4e7.build-2045

4.2 打包一个应用

  • 克隆代码
1
git clone https://github.com/shaowenchen/devops-python-sample
  • 进入代码目录
1
cd devops-python-sample/src
  • 查看代码目录
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
tree

.
|-- manage.py
|-- project
|   |-- app1
|   |   |-- admin.py
|   |   |-- apps.py
|   |   |-- __init__.py
|   |   |-- migrations
|   |   |   `-- __init__.py
|   |   |-- models.py
|   |   |-- tests.py
|   |   `-- views.py
|   |-- __init__.py
|   |-- settings.py
|   |-- tests.py
|   |-- urls.py
|   `-- wsgi.py
`-- requirements.txt

3 directories, 14 files

这是一个非常普通的 Django 项目结构。

  • 查看推荐的 Builder

Builder 定义如何将源码转换为镜像的步骤, 如果有自己熟悉的 Builder 可以直接使用, 也可以使用 Pack 推荐的 Builder 。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
pack builder suggest

Suggested builders:
	Google:                gcr.io/buildpacks/builder:v1      Ubuntu 18 base image with buildpacks for .NET, Go, Java, Node.js, and Python
	Heroku:                heroku/buildpacks:18              heroku-18 base image with buildpacks for Ruby, Java, Node.js, Python, Golang, & PHP
	Heroku:                heroku/buildpacks:20              heroku-20 base image with buildpacks for Ruby, Java, Node.js, Python, Golang, & PHP
	Paketo Buildpacks:     paketobuildpacks/builder:base     Ubuntu bionic base image with buildpacks for Java, .NET Core, NodeJS, Go, Ruby, NGINX and Procfile
	Paketo Buildpacks:     paketobuildpacks/builder:full     Ubuntu bionic base image with buildpacks for Java, .NET Core, NodeJS, Go, PHP, Ruby, Apache HTTPD, NGINX and Procfile
	Paketo Buildpacks:     paketobuildpacks/builder:tiny     Tiny base image (bionic build image, distroless-like run image) with buildpacks for Java Native Image and Go

Tip: Learn more about a specific builder with:
	pack inspect-builder <builder-image>
  • 进行构建, 这里采用 heroku/buildpacks:20 进行构建
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
pack build python-sample --builder heroku/buildpacks:20

20: Pulling from heroku/buildpacks
Digest: sha256:128508a60a25ecac8ed16b4507747cceaf627e2617b2e898d98ac303411a010f
Status: Image is up to date for heroku/buildpacks:20
20: Pulling from heroku/pack
Digest: sha256:6f7301c933d3d0d777c3f5b99b5cc9d8e5ee7304e69b5f05989e19e5c4206a95
Status: Image is up to date for heroku/pack:20
===> DETECTING
======== Output: heroku/ruby@0.0.1 ========
no
err:  heroku/ruby@0.0.1 (1)
heroku/python   0.3
heroku/procfile 0.6
===> ANALYZING
Previous image with name "python-sample" not found
===> RESTORING
===> BUILDING
-----> Installing python-3.6.12
-----> Installing pip 20.1.1, setuptools 47.1.1 and wheel 0.34.2
-----> Installing SQLite3
-----> Installing requirements with pip
 !     Your Django version is nearing the end of its community support.
 !     Upgrade to continue to receive security updates and for the best experience with Django.
 !     For more information, check out https://www.djangoproject.com/download/#supported-versions
       Collecting Django==1.11.29
         Downloading Django-1.11.29-py2.py3-none-any.whl (6.9 MB)
       Collecting coverage==4.5.4
         Downloading coverage-4.5.4-cp36-cp36m-manylinux1_x86_64.whl (205 kB)
       Collecting pytz
         Downloading pytz-2021.1-py2.py3-none-any.whl (510 kB)
       Installing collected packages: pytz, Django, coverage
       Successfully installed Django-1.11.29 coverage-4.5.4 pytz-2021.1
-----> $ python manage.py collectstatic --noinput
       61 static files copied to '/workspace/static'.

-----> Discovering process types
       Procfile declares types     -> (none)
===> EXPORTING
Adding layer 'heroku/python:profile'
Adding 1/1 app layer(s)
Adding layer 'launcher'
Adding layer 'config'
Adding label 'io.buildpacks.lifecycle.metadata'
Adding label 'io.buildpacks.build.metadata'
Adding label 'io.buildpacks.project.metadata'
Warning: default process type 'web' not present in list []
*** Images (1de89df98f60):
      python-sample
Adding cache layer 'heroku/python:shim'
Successfully built image python-sample
  • 查看镜像
1
2
3
docker images |grep python-sample

python-sample                     latest              39a646c853c2        41 years ago        756MB

41 years ago 应该是有 Bug, 镜像有点大。第二次构建时, 并没有看到明显提速, 看来得用自定义 Builder 才能展示上述表格中的优势。

  • 创建容器试试
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
docker run --rm -p 8000:8000 python-sample  python manage.py runserver 0.0.0.0:8000

Performing system checks...

System check identified no issues (0 silenced).

You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
February 04, 2021 - 08:33:02
Django version 1.11.29, using settings 'project.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
  • 在 8000 端口访问页面。

5. 总结

本文主要是体验了一下 Buildpacks 在云原生背景下的进化版本 Cloud Native Buildpacks 。

Buildpacks 与 S2I 一样, 支持开发者自定义 Builder, 也支持用户在源码仓库中自定义一些配置, 比如指定源码目录、指定解释器版本等。PaaS、FaaS 类的平台自己维护一套类似的工具, 不如直接采用并贡献这些开源项目。

6. 参考


微信公众号