Okteto: 간단한 Dockerfile로 작업

9147 단어 kubernetesokteto
개발자 환경
전통적인 개발에는 코드 커밋, 리포지토리로 푸시, CI 파이프라인 시작, 전달 및 CD 사례를 통해 클러스터로 푸시가 포함됩니다. 하루에 여러 번 수행해야 하고 점점 더 많은 개발자가 믹스에 추가됨에 따라 작업이 부담스러워집니다.
Dev Environments는 편집기에서 코드(또는 변경 사항)를 저장할 때 변경 사항을 실시간으로 미리 볼 수 있도록 하여 이 문제를 해결하려고 합니다. 이 작업은 스크립트와 래퍼로 수행할 수 있지만 구성 및 유지에 많은 오버헤드가 발생합니다. Okteto cli는 잘 정의된 흐름으로 이러한 부담을 덜어주는 CLI 세트를 제공하려고 합니다.


CLI 설치



❯ brew install okteto
...
❯ okteto version
okteto version 1.14.9




kubernetes 컨텍스트에 okteto cli 구성



❯ kubectl ctx # from krew ctx plugin
default

❯ okteto context use default
 ✓  Using context default @ default
 i  Run 'okteto context update-kubeconfig' to update your kubectl credentials

❯ okteto context update-kubeconfig
 i  Updated kubernetes context 'default/default' in '[/Users/nashok/k3s.conf]'

❯ okteto context list
Name       Namespace  Builder  Registry
default *  default    docker   -




개발자 워크플로
  • 샘플 Python 애플리케이션

  • cat app.py
    import datetime
    from flask import Flask, jsonify
    
    app = Flask('example')
    now = datetime.datetime.now()
    
    @app.route('/', methods=['GET'])
    def root():
      return jsonify({'message': 'hello world'})
    
    @app.route('/healthz', methods=['GET'])
    def health():
      return jsonify({'message': f'up since {now}'})
    
    if __name__ == '__main__':
      app.run(host='0.0.0.0', port=8080, debug=True)cat Dockerfile
    FROM python:3.8-slim-buster
    
    WORKDIR /python-docker
    
    COPY requirements.txt requirements.txt
    RUN pip3 install -r requirements.txt
    
    EXPOSE 8080
    
    COPY . .
    
    CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"]
    
    ❯ tree -L 1
    .
    ├── Dockerfile 
    ├── app.py
    ├── requirements.txt
    


  • 달리다 okteto init

  • ❯ okteto init
     i  Using default @ default as context
     ✓  Okteto manifest (okteto.yml) deploy and build configured successfully
     ?  Do you want to launch your development environment? [y/n]: n
    
    ❯ tree -L 1
    .
    ├── Dockerfile
    ├── app.py
    ├── okteto.yml # gets created
    ├── requirements.txt
    
    


  • 이미지 빌드 및 푸시

  • grep image: okteto.yml
        image: ashoka007/okteto-test:0.0.1
    
    ❯ okteto build
     i  Building image for service 'python'
     i  Building the image 'ashoka007/okteto-test:0.0.1' using your local docker daemon
    [+] Building 4.2s (11/11) FINISHED
     => [internal] load build definition from Dockerfile                                                                             => => transferring dockerfile: 255B
     ...
    0.0.1: digest: sha256:dfd2fa6109e7d6cfade3c6eda90319b0f59816877faf4a23e18bb6cf6a50bc2f size: 2207
     ✓  Image 'ashoka007/okteto-test:0.0.1' successfully pushed
    


  • 이미지 배포

  • ❯ okteto deploy
     i  Using default @ default as context
     i  Images were already built. To rebuild your images run 'okteto build' or 'okteto deploy --build'
     i  Running kubectl apply -f kubernetes.yaml
    deployment.apps/okteto-test-dep created
    service/okteto-test-svc created
    There are no available endpoints for 'python'
     ✓  Development environment 'python' successfully deployed
     i  Run 'okteto up' to activate your development container
    


  • 동기화 시작

  • ❯ okteto up
     i  Using default @ kind-macbook as context
     i  Development environment 'docker' already deployed.
     i  To redeploy your development environment run 'okteto deploy' or 'okteto up [devName] --deploy'
     i  Images were already built. To rebuild your images run 'okteto build' or 'okteto deploy --build'
     ✓  Images successfully pulled
     ✓  Files synchronized
        Context:   kind-macbook
        Namespace: default
        Name:      test-app
        Forward:   8080 -> 8080
                   9229 -> 9229
    root@test-app-okteto-6bb5c444cf-txvxt:/python-docker# 
    


  • DEV 모드에서 서비스를 시작하고 작업을 테스트합니다.

  • root@test-app-okteto-6bb5c444cf-txvxt:/python-docker# python app.py
     * Serving Flask app 'example' (lazy loading)
     * Environment: production
       WARNING: This is a development server. Do not use it in a production deployment.
       Use a production WSGI server instead.
     * Debug mode: on
     * Running on all addresses (0.0.0.0)
       WARNING: This is a development server. Do not use it in a production deployment.
     * Running on http://127.0.0.1:8080
     * Running on http://10.244.1.16:8080 (Press CTRL+C to quit)
     * Restarting with stat
     * Debugger is active!
     * Debugger PIN: 111-486-135
    127.0.0.1 - - [03/Aug/2022 05:26:16] "GET / HTTP/1.1" 200 -
    
    # test from localhost
    ❯ curl localhost:8080
    {
      "message": "hello from app again @ 2022-08-03 05:26:02.759327"
    }
    


  • DEV 포드와 localhost 간의 양방향 동기화를 확인합니다.

  • # localhosttouch test.txt
    ❯ ls
    Dockerfile       app.py           okteto.yaml      requirements.txt test.txt
    
    # inside the pod
    root@test-app-okteto-6bb5c444cf-txvxt:/python-docker# ls
    Dockerfile  app.py  okteto.yaml  requirements.txt  test.txt
    


  • 정리

  • root@test-app-okteto-6bb5c444cf-txvxt:/python-docker# exit
    exit
    
    # localhost
    ❯ okteto down
     i  Using default @ kind-macbook as context
     ✓  Development container 'test-app' deactivated
    
    

    좋은 웹페이지 즐겨찾기