티스토리 뷰

Cloud/Cloud Native

python application deploy with minikube

jacobbaek Jacob_baek 2019. 9. 28. 23:03

Docker image 생성 및 deployment 생성

아래와 같이 Dockerfile을 생성하고 Build하여 image로 생성한다.

jacob@jacob-laptop:~/workspaces/hellopy$ cat Dockerfile 
FROM python:3.7.4
EXPOSE 8000
CMD python -m SimpleHTTPServer
jacob@jacob-laptop:~/workspaces/hellopy$ docker build -t hello-py:v1 .
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM python:3.7.4
 ---> 02d2bb146b3b
Step 2/3 : EXPOSE 8000
 ---> Using cache
 ---> b72c3a572cbd
Step 3/3 : CMD python -m SimpleHTTPServer
 ---> Using cache
 ---> 8ed128fb4a3e
Successfully built 8ed128fb4a3e
Successfully tagged hello-py:v1

생성된 Docker image를 사용하영 kubectl로 deployment를 생성한다.

jacob@jacob-laptop:~/workspaces/hellopy$ kubectl run hello-py --image=hello-py:v1 --port=8000 --image-pull-policy=Never
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/hello-py created

실제 생성된 pod를 확인한다.

jacob@jacob-laptop:~/workspaces/hellopy$ kubectl get pods --context=minikube
NAME                                READY   STATUS             RESTARTS   AGE
hello-py-5844b45b8b-thcth           0/1     CrashLoopBackOff   8          18m
nginx-deployment-54f57cf6bf-hhwlv   1/1     Running            0          32m
nginx-deployment-54f57cf6bf-v94k8   1/1     Running            0          32m

CrashLoopBackOff 현상확인

확인시 pod에 CrashLoopBackOff문제가 있음을 확인할 수 있다. 이를 좀더 명확히 확인하고자 log를 확인한다.

jacob@jacob-laptop:~/workspaces/hellopy$ kubectl logs hello-py-5844b45b8b-thcth
/usr/local/bin/python: No module named SimpleHTTPServer

application 문제해결 및 deployment 업데이트

확인된 로그상에서 SimpleHTTPServer Module을 찾을수 없음을 말하고 있다.

python 3.X대 부터는 SimpleHTTPServer module 대신 http.server를 사용할 수 있다.

Dockerfile을 수정하여 다시 deployment를 생성해보자.

jacob@jacob-laptop:~/workspaces/hellopy$ cat Dockerfile 
FROM python:3.7.4
EXPOSE 8000
CMD python3 -m http.server
jacob@jacob-laptop:~/workspaces/hellopy$ docker build -t hello-py:v2 .
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM python:3.7.4
 ---> 02d2bb146b3b
Step 2/3 : EXPOSE 8000
 ---> Using cache
 ---> b72c3a572cbd
Step 3/3 : CMD python3 -m http.server
 ---> Running in 4cacb174f630
Removing intermediate container 4cacb174f630
 ---> 4ac44ef8d878
Successfully built 4ac44ef8d878
Successfully tagged hello-py:v2

다시 build된 이미지를 기존 deployment내 image에 업데이트 한다.

jacob@jacob-laptop:~/workspaces/hellopy$ kubectl set image deployment/hello-py hello-py=hello-py:v2
deployment.apps/hello-py image updated

이후 다시 pod를 확인해보면 다음과 같이 다시 정상 deploy되었음을 확인할 수 있다.

jacob@jacob-laptop:~/workspaces/hellopy$ kubectl get pods
NAME                                READY   STATUS        RESTARTS   AGE
hello-py-5844b45b8b-thcth           0/1     Terminating   11         32m
hello-py-7bb87fd7bf-4vcck           1/1     Running       0          8s
nginx-deployment-54f57cf6bf-hhwlv   1/1     Running       0          46m
nginx-deployment-54f57cf6bf-v94k8   1/1     Running       0          46m
jacob@jacob-laptop:~/workspaces/hellopy$ kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
hello-py-7bb87fd7bf-4vcck           1/1     Running   0          12s
nginx-deployment-54f57cf6bf-hhwlv   1/1     Running   0          46m
nginx-deployment-54f57cf6bf-v94k8   1/1     Running   0          46m

마지막으로 minikube를 통해 웹접속을 수행해보자.

jacob@jacob-laptop:~/workspaces/hellopy$ minikube service hello-py
|-----------|----------|-------------|-----------------------------|
| NAMESPACE |   NAME   | TARGET PORT |             URL             |
|-----------|----------|-------------|-----------------------------|
| default   | hello-py |             | http://192.168.99.102:30234 |
|-----------|----------|-------------|-----------------------------|
🎉  Opening kubernetes service  default/hello-py in default browser...

browser가 출력되고 정상적으로 웹접속이 이루어짐을 확인할 수 있다.

참고사이트

https://medium.com/humanscape-tech/kubernetes-%EB%8F%84%EC%9E%85-%EC%A0%84-minikube-%EC%82%AC%EC%9A%A9%EA%B8%B0-2eb2b6d8e444

 

 

 

 

'Cloud > Cloud Native' 카테고리의 다른 글

minikube start in WSL2  (0) 2019.12.07
kubernetes troubleshooting  (0) 2019.11.14
Multi configured kubectl  (0) 2019.10.28
python application deploy with minikube  (0) 2019.09.28
Docker in centos  (0) 2019.06.07
application deploy using minikube  (0) 2017.09.04
댓글
댓글쓰기 폼