추론 로그를 기록합니다.
- Python 3.8 이상
- Docker
- Kubernetes 클러스터 또는 minikube
이 프로그램은 Kubernetes 클러스터 또는 minikube 가 필요합니다. Kubernetes 클러스터는 독자적으로 구축하거나, 각 클라우드 매니지드 서비스(GCP GKE、AWS EKS、MS Azure AKS 등)를 이용해 주십시오. GCP GKE 클러스터로 가동을 확인했습니다.
- 현재 디렉토리
$ pwd
~/ml-system-in-actions/chapter5_operations/prediction_log_pattern
- Docker 이미지 빌드
$ make build_all
# 실행 커맨드
# docker build \
# -t shibui/ml-system-in-actions:prediction_log_pattern_api_0.0.1 \
# -f Dockerfile.api \
# .
# docker build \
# -t shibui/ml-system-in-actions:prediction_log_pattern_client_0.0.1 \
# -f Dockerfile.client \
# .
- Kubernetes 로 각 서비스 기동
$ make deploy
# 실행 커맨드
# kubectl apply -f manifests/namespace.yml
# kubectl apply -f manifests/
# 디플로이먼트 확인
$ kubectl -n prediction-log get all
# 출력
# NAME READY STATUS RESTARTS AGE
# pod/api-85d44df447-2v95h 2/2 Running 0 67s
# pod/api-85d44df447-2xhrn 2/2 Running 0 67s
# pod/api-85d44df447-xwfbn 2/2 Running 0 67s
# pod/client 1/1 Running 0 67s
# NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
# service/api ClusterIP 10.4.7.145 <none> 8000/TCP 67s
# NAME READY UP-TO-DATE AVAILABLE AGE
# deployment.apps/api 3/3 3 3 67s
# NAME DESIRED CURRENT READY AGE
# replicaset.apps/api-85d44df447 3 3 3 67s
- 기동한 API 에 요청
# 기동한 service/api 에 포트 포워딩
$ kubectl -n prediction-log port-forward service/api 8000:8000 &
# 헬스 체크
$ curl localhost:8000/health
# 출력
# {
# "health":"ok"
# }
# 메타 데이터
$ curl localhost:8000/metadata
# 출력
# {
# "data_type": "float32",
# "data_structure": "(1,4)",
# "data_sample": [
# [
# 5.1,
# 3.5,
# 1.4,
# 0.2
# ]
# ],
# "prediction_type": "float32",
# "prediction_structure": "(1,3)",
# "prediction_sample": [
# 0.97093159,
# 0.01558308,
# 0.01348537
# ],
# "outlier_type": "bool, float32",
# "outlier_structure": "(1,2)",
# "outlier_sample": [
# false,
# 0.4
# ]
# }
# 라벨 목록
$ curl localhost:8000/label
# 출력
# {
# "0": "setosa",
# "1": "versicolor",
# "2": "virginica"
# }
# 테스트 데이터로 추론 요청
$ curl localhost:8000/predict/test
# 출력
# {
# "job_id": "ee1b0d",
# "prediction": [
# 0.9709315896034241,
# 0.015583082102239132,
# 0.013485366478562355
# ],
# "is_outlier": false,
# "outlier_score": 0.1915884017944336
# }
# 추론 요청
$ curl \
-X POST \
-H "Content-Type: application/json" \
-d '{"data": [[6.7, 3.0, 5.2, 2.3]]}' \
localhost:8000/predict
# 출력
# {
# "job_id": "1934ee",
# "prediction": [
# 0.009793723002076149,
# 0.009877714328467846,
# 0.9803286194801331
# ],
# "is_outlier": false,
# "outlier_score": 0.44043588638305664
# }
# 로그 확인
$ kubectl -n prediction-log logs deployment.apps/api api
# 출력
# [2021-02-06 08:39:49] [INFO] [10] [src.app.routers.routers] [_predict] [81] execute: [d7a0a7]
# [2021-02-06 08:39:49] [INFO] [10] [src.ml.prediction] [predict] [47] predict proba [0.00979372 0.00987771 0.98032862]
# [2021-02-06 08:39:49] [INFO] [10] [src.ml.outlier_detection] [predict] [38] outlier score 0.44043588638305664
# [2021-02-06 08:39:49] [INFO] [10] [src.app.routers.routers] [wrapper] [33] [/predict] [d7a0a7] [1.0488033294677734 ms] [data=[[6.7, 3.0, 5.2, 2.3]]] [[0.009793723002076149, 0.009877714328467846, 0.9803286194801331]] [False] [0.44043588638305664]
# [2021-02-06 08:39:49] [INFO] [10] [uvicorn.access] [send] [458] 127.0.0.1:33446 - "POST /predict HTTP/1.1" 200
- 서비스 삭제
$ kubectl delete ns prediction-log
# 출력
# namespace "prediction-log" deleted