使用 S2I 构建云原生应用

1. S2I 能解决什么问题

上图是 S2I 的工作流,S2I CLI 依赖于 Docker 环境。主要分为两步:

  • 通过 BASE IMAGE 、S2I Scripts 构建应用的基础镜像
  • 将源码移动到应用的基础镜像,仅添加一层文件,得到最终的镜像文件
  • 打补丁,如果所依赖的镜像需要安全补丁,S2I 允许你一次性重新构建所有镜像。
  • 高效,在构建过程中,S2I 不允许运行任意的 yum install 命令,以防止降低开发迭代速度
  • 生态,S2I 鼓励一个共享镜像生态。从而你的应用可以实现最佳实践。
  •  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
    
    • 查看镜像
    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. 参考

    • https://github.com/openshift/source-to-image
    • https://github.com/sclorg/s2i-python-container
    • https://www.openshift.com/blog/create-s2i-builder-image