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

推荐订阅源

T
Troy Hunt's Blog
F
Fortinet All Blogs
D
DataBreaches.Net
Google DeepMind News
Google DeepMind News
Y
Y Combinator Blog
The Register - Security
The Register - Security
T
Tailwind CSS Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
V
Vulnerabilities – Threatpost
S
Securelist
S
SegmentFault 最新的问题
T
Threat Research - Cisco Blogs
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Privacy International News Feed
S
Schneier on Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LangChain Blog
GbyAI
GbyAI
Apple Machine Learning Research
Apple Machine Learning Research
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
美团技术团队
Cyberwarzone
Cyberwarzone
C
Cisco Blogs
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Google Online Security Blog
Google Online Security Blog
M
MIT News - Artificial intelligence
U
Unit 42
V
V2EX
C
CERT Recently Published Vulnerability Notes
云风的 BLOG
云风的 BLOG
B
Blog
博客园 - 叶小钗
Attack and Defense Labs
Attack and Defense Labs
Security Archives - TechRepublic
Security Archives - TechRepublic
aimingoo的专栏
aimingoo的专栏
Hacker News: Ask HN
Hacker News: Ask HN
博客园 - Franky
Engineering at Meta
Engineering at Meta
Schneier on Security
Schneier on Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
酷 壳 – CoolShell
酷 壳 – CoolShell
T
The Blog of Author Tim Ferriss
IT之家
IT之家
W
WeLiveSecurity
Cisco Talos Blog
Cisco Talos Blog
K
Kaspersky official blog
Martin Fowler
Martin Fowler
SecWiki News
SecWiki News

陈少文的网站

巨变与机遇的未来十年 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) 在 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 自动化部署流程 开发 Tips(5)
使用 S2I 构建云原生应用
微信公众号 · 2019-08-26 · via 陈少文的网站

Please enable Javascript to view the contents

1. S2I 能解决什么问题

可以预见的是,未来大量的应用会以容器的方式进行部署。

容器平台关注的是镜像和容器,应用开发关注的是业务代码,而最终代码需要以镜像的方式进行部署。从代码到镜像,就是 Source To Image ,即 S2I 。

在前面的文章 PaaS 部署之 buildpack 中,描述到一个应用运行起来之前,需要进行一些必要的配置。这些配置包括运行依赖、环境变量、服务地址等。

S2I 提供 buildpack 类似的功能,但 S2I 提供的方式更加通用化、更加容器化。

2. S2I 的特点

上图是 S2I 的工作流,S2I CLI 依赖于 Docker 环境。

主要分为两步:

  • 通过 BASE IMAGE 、S2I Scripts 构建应用的基础镜像
  • 将源码移动到应用的基础镜像,仅添加一层文件,得到最终的镜像文件

BASE IMAGE 是一些通用的镜像,例如,Python、CentOS、Nodejs 等。S2I Scripts 通常是一些固定的流程脚本,比如变更数据库、拷贝静态文件等。

S2I 的特点:

  • 速度,S2I 可以实现很复杂的操作逻辑,而不会创建新的镜像层,所以运行起来很快。
  • 打补丁,如果所依赖的镜像需要安全补丁,S2I 允许你一次性重新构建所有镜像。
  • 高效,在构建过程中,S2I 不允许运行任意的 yum install 命令,以防止降低开发迭代速度
  • 生态,S2I 鼓励一个共享镜像生态。从而你的应用可以实现最佳实践。

S2I 能够通过目录下的文件探测语言,如果有 Dockerfile 将会退化为非 S2I 的方式使用 Dockerfile 进行构建。

识别的语言探测的文件
javapom.xml
nodejsapp.json, package.json
perlcpanfile、index.pl
phpcomposer.json、index.php
pythonrequirements.txt、setup.py
rubyGemfile, Rakefile、config.ru
scalabuild.sbt
golangGodeps, main.go

3. S2I 与 buildpack 区别

buildpack 是 CloudFoundry 提供的一种打包应用的工具。buildpack 对应用及其依赖进行构建、打包和更新,让开发人员更加专注在业务逻辑实现。

buildpack 最终会得到一个 droplet。droplet 是一个应用的可运行实例,运行在 dea(droplet execution agent)上。

S2I 是 OpenShift 推出的一种基于容器的应用镜像构建工具。S2I 面向容器,试图解决的问题更为通用。

S2I 最终会得到一个镜像,可以部署在任意容器平台。

4. 尝试使用 S2I

操作之前,请确保本地有可用的 Docker 环境。

  1. 创建一个 Django 工程
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
pip install django==1.11
django-admin startproject django_example
echo django==1.11 > ./django_example/requirements.txt
tree -L 3
.
└── django_example
    ├── django_example
    │   ├── __init__.py
    │   ├── settings.py
    │   ├── urls.py
    │   └── wsgi.py
    ├── manage.py
    └── requirements.txt

添加 requirements.txt 是为了让脚本识别应用。

  1. 安装 S2I ,以 OS X 为例:
1
brew install source-to-image
  1. 构建应用镜像
  • build 命令格式
1
2
3
s2i build -h
Usage:
  s2i build <source> <image> [<tag>] [flags]
  • source ,构建源码。可以是本地仓库,也可以是远程仓库
  • image,基础镜像
  • tag,生成的镜像
  • flags,构建参数,比如添加证书、设置网络等
  • 开始构建应用

centos/python-35-centos7 是一个 Python 3.5 应用的 S2I 基础镜像,点击查看

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
s2i build django_example centos/python-35-centos7 hello-django
---> Installing application source ...
---> Installing dependencies ...
Collecting django==1.11 (from -r requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/47/a6/078ebcbd49b19e22fd560a2348cfc5cec9e5dcfe3c4fad8e
64c9865135bb/Django-1.11-py2.py3-none-any.whl (6.9MB)
Collecting pytz (from django==1.11->-r requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/87/76/46d697698a143e05f77bec5a526bf4e56a0be
61d63425b68f4ba553b51f2/pytz-2019.2-py2.py3-none-any.whl (508kB)
Installing collected packages: pytz, django
Successfully installed django-1.11 pytz-2019.2
You are using pip version 7.1.2, however version 19.2.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
---> Collecting Django static files ...
WARNING: could not run 'manage.py collectstatic'. To debug, run:
python ./manage.py collectstatic --noinput
Ignore this warning if you're not serving static files with Django.
Build completed successfully
  • 查看镜像
1
2
3
docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
hello-django               latest              042c6143ecf9        50 seconds ago      669MB
  • 运行应用
 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
docker run -p 8080:8080 hello-django
---> Migrating database ...
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying sessions.0001_initial... OK
---> Serving application with 'manage.py runserver' ...
WARNING: this is NOT a recommended way to run you application in production!
Consider using gunicorn or some other production web server.
Performing system checks...
System check identified no issues (0 silenced).
August 26, 2019 - 08:08:36
Django version 1.11, using settings 'django_example.settings'
Starting development server at http://0.0.0.0:8080/
Quit the server with CONTROL-C.
  • 查看应用

5. 参考


微信公众号