01. Week1 실습자료 - chuirang/DevOps GitHub Wiki
- 개발 환경 구성
- Sample 프로그램 개발
- 이미지 빌드 및 업로드
- K8S 배포
- K8S 모니터링
-
CTEK 접속확인 (172.16.3.141)
-
https://github.com/ 가입 (Source Code Management)
-
https://hub.docker.com/ 가입 (Image Repository)
-
Code-server (Web-based IDE) 실행 후 Chrome으로 접속 bind-addr 을 0.0.0.0:개인포트 로 수정
wonkilee.lee@u2004-master:~$ vi ~/.config/code-server/config.yaml bind-addr: 0.0.0.0:18086 auth: password password: 6f773b84a11843c35974eaef cert: false wonkilee.lee@u2004-master:~$ code-server [2021-07-21T08:00:28.456Z] info code-server 3.11.0 4e8cd09ef0412dfc7b148b7639a692e20e4fd6dd [2021-07-21T08:00:28.462Z] info Using user-data-dir ~/.local/share/code-server [2021-07-21T08:00:28.492Z] info Using config file ~/.config/code-server/config.yaml [2021-07-21T08:00:28.493Z] info HTTP server listening on http://0.0.0.0:18086 [2021-07-21T08:00:28.493Z] info - Authentication is enabled [2021-07-21T08:00:28.494Z] info - Using password from ~/.config/code-server/config.yaml [2021-07-21T08:00:28.494Z] info - Not serving HTTPS
개인 홈디렉터리에 실습 폴더를 만들고 vi app.py
에서 아래의 코드를 입력
import os
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
target = os.environ.get('TARGET', 'World')
return 'Hello {}!\n'.format(target)
if __name__ == "__main__":
app.run(debug=True,host='0.0.0.0',port=int(os.environ.get('PORT’, 18180)))
*18180 -> 본인 code-server port + 100 로 변경
-
Python 3.7 설치 (실습 환경에는 설치가 되어 있음)
-
Virtual environment 환경 구성(python)
wonkilee.lee@u2004-master:~$ mkdir ~/01_SimpleApp wonkilee.lee@u2004-master:~$ cd ~/01_SimpleApp wonkilee.lee@u2004-master:~/01_SimpleApp/$ python3 -m venv venv wonkilee.lee@u2004-master:~/01_SimpleApp/$ cd venv/bin wonkilee.lee@u2004-master:~/01_SimpleApp/venv/bin$ source activate (venv) wonkilee.lee@u2004-master:~/01_SimpleApp/venv/bin$ cd ../..
-
Flask 모듈(필요 라이브러리) 설치
(venv) wonkilee.lee@u2004-master:~/01_SimpleApp$ pip install Flask
-
App 실행
(venv) wonkilee.lee@u2004-master:~/01_SimpleApp$ python app.py * Serving Flask app 'app' (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. WARNING: This is a development server. Do not use it in a production deployment. * Running on http://172.16.3.141:18180/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! * Debugger PIN: 120-020-657 172.16.3.78 - - [21/Jul/2021 08:13:20] "GET / HTTP/1.1" 200 -
-
실행확인 - curl 또는 Web Browser 로 실행
app.py 가 있는 위치에서 vi Dockerfile
로 작성
FROM python:3.8-slim
ENV PYTHONUNBUFFERED True
ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . ./
RUN pip install Flask gunicorn
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 app:app
-
이미지 빌드
docker build -t {docker.io ID}/sampleapp:v1 .
-
이미지 빌드 확인 – 로컬
docker images
-
이미지 저장소 로그인
$ docker login
-
이미지 업로드
$ docker push {docker.io ID}/sampleapp:v1
-
이미지 업로드 확인 - docker.io
빌드한 도커 이미지를 K8S 환경으로 배포한다
vi deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: sampleapp
namespace: {본인namespace}
labels:
app: sampleapp
spec:
replicas: 1
selector:
matchLabels:
app: sampleapp-container
template:
metadata:
labels:
app: sampleapp-container
spec:
containers:
- name: sampleapp
image: {본인docker.io ID}/sampleapp:v1
ports:
- containerPort: {본인로컬테스트포트}
vi service.yaml
apiVersion: v1
kind: Service
metadata:
name: sampleapp-container-svc
namespace: {본인namespace}
labels:
app: sampleapp-container
spec:
ports:
- name: http
port: {본인로컬테스트포트}
targetPort: {본인로컬테스트포트}
selector:
app: sampleapp-container
type: LoadBalancer
배포된 K8S 환경의 도커 이미지(App) 를 확인하고 모니터링한다.
$ kubectl get deploy
$ kubectl get svc
$ kubectl get po
$ kubectl describge po <pod_name>
$ kubectl logs <pod_name>