[Cloud Foundry] cf dev python 기본 예제

cf dev를 이용해 Cloud Foundry의 CFAR(Cloud Foundry Application Runtime)에 파이썬 앱을 올려보자.


환경

  • Ubuntu 18.04
  • Baremetal Server


목표

  • Cloud Foundry에서 CFAR(Cloud Foundry Application Runtime)의 기본을 실습해보기 위해 cf dev를 이용해 파이썬 어플리케이션을 올려보자.
  • 해당 포스트는 실습 위주이며 참고자료에 있는 내용을 토대로 작성하였습니다.


Cloud Foundry

Cloud Foundry

  • Cloud Foundry: Cloud Foundry Foundation에 의해서 운영되는 멀티클라우드 오픈소스 platform as a service (PaaS).
  • 많은 프로젝트들이 들어가 있으며 배포, 관리 및 앱 환경 구축에 이르기까지 다양한 서비스를 제공한다.
  • 예시) BOSH 그리고 CFAR

CFAR(Cloud Foundry Application Runtime)

  • CFAR: Cloud Foundry Application Runtime (CFAR)은 개발자가 코드에만 신경쓸 수 있게 해주는 코드 중심 플랫폼입니다. 어떤 언어나 프레임워크인지 상관없이 코드를 받아서 어떤 클라우드에서라도 실행할 수 있게 해줍니다.
  • 코드를 작성하고 설정 파일을 추가한 다음에 CFAR에 업로드하면 앱 실행환경을 만들 수 있습니다.

CF Dev

  • CF Dev: CF Dev는 개발자 컴퓨터에 하이퍼바이저와 BOSH Director를 이용해 Cloud Foundry를 사용할 수 있도록 고안된 배포판입니다.
  • CF Dev를 이용하면 개인의 컴퓨터에서 간단하게 CFAR의 기능을 이용해 앱을 구축할 수 있습니다.
  • 본 글에서는 간단한 파이썬 플라스크(Flask) 앱을 만들어볼 예정입니다.


CF Dev

CF Dev 설치

  • QEMU와 libvirt 설치
sudo apt install qemu-kvm libvirt-bin libvirt-doc -y
  • CF CLI 설치
# ...first add the Cloud Foundry Foundation public key and package repository to your system
wget -q -O - https://packages.cloudfoundry.org/debian/cli.cloudfoundry.org.key | sudo apt-key add -
echo "deb https://packages.cloudfoundry.org/debian stable main" | sudo tee /etc/apt/sources.list.d/cloudfoundry-cli.list
# ...then, update your local package index, then finally install the cf CLI
sudo apt-get update -y
sudo apt-get install cf-cli -y
  • CF Dev plugin 설치
cf install-plugin -r CF-Community cfdev


CF Dev 환경 구축

  • cf dev start
cf dev start
  • 결과 화면
Downloading Resources...
Progress: |====================>| 100.0%
Setting State...

CF Dev collects anonymous usage data to help us improve your user experience. We intend to share these anonymous usage analytics with user community by publishing quarterly reports at :

https://github.com/pivotal-cf/cfdev/wiki/Telemetry

Are you ok with CF Dev periodically capturing anonymized telemetry [y/N]?> y
WARNING: CF Dev requires 8192 MB of RAM to run. This machine may not have enough free RAM.
Creating the VM...
Starting the VM...
Fetching VM Address...
Waiting for the VM...
Deploying the BOSH Director...
Deploying CF...
  Done (11m24s)

 	  ██████╗███████╗██████╗ ███████╗██╗   ██╗
 	 ██╔════╝██╔════╝██╔══██╗██╔════╝██║   ██║
 	 ██║     █████╗  ██║  ██║█████╗  ██║   ██║
 	 ██║     ██╔══╝  ██║  ██║██╔══╝  ╚██╗ ██╔╝
 	 ╚██████╗██║     ██████╔╝███████╗ ╚████╔╝
 	  ╚═════╝╚═╝     ╚═════╝ ╚══════╝  ╚═══╝
 	             is now running!

 	To begin using CF Dev, please run:
 	    cf login -a https://api.dev.cfdev.sh --skip-ssl-validation

 	Admin user => Email: admin / Password: admin
 	Regular user => Email: user / Password: pass

 	To deploy a particular service, please run:
 	    cf dev deploy-service <service-name> [Available services: mysql]


로그인

  • Email: user / Password: pass
cf login -a https://api.dev.cfdev.sh --skip-ssl-validation
  • 결과 화면
API endpoint: https://api.dev.cfdev.sh

Email: user

Password:
Authenticating...
OK

Targeted org cfdev-org

Targeted space cfdev-space



API endpoint:   https://api.dev.cfdev.sh (API version: 3.76.0)
User:           user
Org:            cfdev-org
Space:          cfdev-space


어플리케이션 파일 작성

  • 4개의 파일이 필요합니다.
  • 폴더 생성
mkdir flask_test_cf
cd flask_test_cf

app.py

from flask import Flask
import os

app = Flask(__name__)

@app.route('/')
def hello_world():
    return "Hello World!"

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=int(os.getenv("PORT", 5001)))

Procfile

  • CFAR에서 실행할 명령어
web: python app.py

requirements.txt

  • pip freeze 명령어를 이용해서 생성 가능
pip freeze > requirements.txt
Click==7.0
Flask==1.1.1
itsdangerous==1.1.0
Jinja2==2.11.1
MarkupSafe==1.1.1
Werkzeug==1.0.0

runtime.txt

  • 사용할 파이썬 버전 명시
python-3.6.9


작성한 파일 확인

$ ls
app.py  Procfile  requirements.txt  runtime.txt


cf push를 이용해 application 올리기

  • 프로젝트 디렉토리에서
# -m: memory size to use
# -b: buildpack option(we use python in this case)
# python-hello-world: app name, this will be subdomain name
cf push -m 128M -b python_buildpack python-hello-world
  • 결과
Pushing app python-hello-world to org cfdev-org / space cfdev-space as user...
Getting app info...
Creating app with these attributes...
+ name:         python-hello-world
  path:         /home/twpower/flask_test_cf
  buildpacks:
+   python_buildpack
+ memory:       128M
  routes:
+   python-hello-world.dev.cfdev.sh

Creating app python-hello-world...
Mapping routes...
Comparing local files to remote cache...
Packaging files to upload...
Uploading files...
 963 B / 963 B [=========================================================================] 100.00% 1s

Waiting for API to complete processing files...

Staging app and tracing logs...
   Downloading python_buildpack...
   Downloaded python_buildpack (5M)
   Cell 19330b69-fa32-43e6-9884-9e89fcbbbc40 creating container for instance 6616abe5-490e-4854-9ee6-8485ad258dd6
   Cell 19330b69-fa32-43e6-9884-9e89fcbbbc40 successfully created container for instance 6616abe5-490e-4854-9ee6-8485ad258dd6
   Downloading app package...
   Downloaded app package (963B)
   -----> Python Buildpack version 1.6.36
   -----> Supplying Python
   -----> Installing python 3.6.9
          Download [https://buildpacks.cloudfoundry.org/dependencies/python/python-3.6.9-linux-x64-cflinuxfs3-f30bc832.tgz]
   -----> Installing pip-pop 0.1.3
          Download [https://buildpacks.cloudfoundry.org/dependencies/manual-binaries/pip-pop/pip-pop-0.1.3-fc106ef6.tar.gz]
   -----> Running Pip Install
          Collecting Click==7.0 (from -r /tmp/app/requirements.txt (line 1))
            Downloading https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl (81kB)
          Collecting Flask==1.1.1 (from -r /tmp/app/requirements.txt (line 2))
            Downloading https://files.pythonhosted.org/packages/9b/93/628509b8d5dc749656a9641f4caf13540e2cdec85276964ff8f43bbb1d3b/Flask-1.1.1-py2.py3-none-any.whl (94kB)
          Collecting itsdangerous==1.1.0 (from -r /tmp/app/requirements.txt (line 3))
            Downloading https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
          Collecting Jinja2==2.11.1 (from -r /tmp/app/requirements.txt (line 4))
            Downloading https://files.pythonhosted.org/packages/27/24/4f35961e5c669e96f6559760042a55b9bcfcdb82b9bdb3c8753dbe042e35/Jinja2-2.11.1-py2.py3-none-any.whl (126kB)
          Collecting MarkupSafe==1.1.1 (from -r /tmp/app/requirements.txt (line 5))
            Downloading https://files.pythonhosted.org/packages/b2/5f/23e0023be6bb885d00ffbefad2942bc51a620328ee910f64abe5a8d18dd1/MarkupSafe-1.1.1-cp36-cp36m-manylinux1_x86_64.whl
          Collecting Werkzeug==1.0.0 (from -r /tmp/app/requirements.txt (line 6))
            Downloading https://files.pythonhosted.org/packages/ba/a5/d6f8a6e71f15364d35678a4ec8a0186f980b3bd2545f40ad51dd26a87fb1/Werkzeug-1.0.0-py2.py3-none-any.whl (298kB)
          Installing collected packages: Click, Werkzeug, itsdangerous, MarkupSafe, Jinja2, Flask
            The script flask is installed in '/tmp/contents020609285/deps/0/python/bin' which is not on PATH.
            Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
          Successfully installed Click-7.0 Flask-1.1.1 Jinja2-2.11.1 MarkupSafe-1.1.1 Werkzeug-1.0.0 itsdangerous-1.1.0
          You are using pip version 18.1, however version 20.0.2 is available.
          You should consider upgrading via the 'pip install --upgrade pip' command.
   Exit status 0
   Uploading droplet, build artifacts cache...
   Uploading droplet...
   Uploading build artifacts cache...
   Uploaded build artifacts cache (48.9M)
   Uploaded droplet (49.7M)
   Uploading complete
   Cell 19330b69-fa32-43e6-9884-9e89fcbbbc40 stopping instance 6616abe5-490e-4854-9ee6-8485ad258dd6
   Cell 19330b69-fa32-43e6-9884-9e89fcbbbc40 destroying container for instance 6616abe5-490e-4854-9ee6-8485ad258dd6

Waiting for app to start...
   Cell 19330b69-fa32-43e6-9884-9e89fcbbbc40 successfully destroyed container for instance 6616abe5-490e-4854-9ee6-8485ad258dd6

name:              python-hello-world
requested state:   started
routes:            python-hello-world.dev.cfdev.sh
last uploaded:     Mon 10 Feb 14:37:53 KST 2020
stack:             cflinuxfs3
buildpacks:        python

type:            web
instances:       1/1
memory usage:    128M
start command:   python app.py
     state     since                  cpu    memory           disk           details
#0   running   2020-02-10T05:35:17Z   0.0%   118.4K of 128M   177.7M of 1G


응답 확인

$ curl http://python-hello-world.dev.cfdev.sh
Hello World!


CF Dev 삭제

  • cf dev 중지
cf dev stop
  • cfdev plugin 삭제
cf uninstall-plugin cfdev
  • 디렉토리 삭제
rm -rf ~/.cfdev


이슈

  • 가상머신 환경에서 실행하면 virbr0가 활성화되지 않는 이슈가 있다.


참고자료