쿠버네티스를 통해 django와 Flask를 실행해보자 !!
- 사전 진행해야 하는것
- pyenv 설치, pyenv를 통한 가상 환경 3.11.6 구축
PostgreSQL 확인
https://www.postgresql.org/download/linux/ubuntu/
sudo systemctl status postgresql.service
## active인거 확인
Django - Nginx 이미지 생성
Django 프로젝트 생성
# ~/work/ch10/ex01/myDjango04
pyenv activate py3_11_6
django-admin startproject myapp
# ~/work/ch10/ex01/myDjango04/myapp/myapp/settings.py
# django가 로컬에 설치한 PostgreSQL을 바라볼 수 있도록 설정 변경
...
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': '1234',
'HOST': '192.168.64.8', # myserver01 - master
'PORT': '5432',
}
}
...
Django - requirements 및 Dockerfile 작성
# ~/work/ch10/ex01/myDjango04/requirements.txt
django==4.2.7
gunicorn==20.1.0 # gurnicorn을 통해 nginx와 django와 연동
psycopg2==2.9.9
# ~/work/ch10/ex01/myDjango04/Dockerfile
FROM python:3.11.6
WORKDIR /usr/src/app
COPY . .
RUN python -m pip install --upgrade pip
RUN pip install -r requirements.txt
WORKDIR ./myapp
CMD gunicorn --bind 0.0.0.0:8000 myapp.wsgi:application
EXPOSE 8000
Django - Docker build & DockerHub에 push
# ~/work/ch10/ex01/myDjango04
ls
## Dockerfile myapp requirements.txt
docker build . -t <repository>/mydjango_ch10:0.2
docker push <repository>/mydjango_ch10:0.2
Nginx 이미지 생성
# ~/work/ch10/ex01/myNginx04d/default.conf
server{
listen 80;
server_name localhost;
location /{
proxy_pass http://127.0.0.1:8000;
}
}
# ~/work/ch10/ex01/myNginx04d/Dockerfile
FROM nginx:1.25.3
RUN rm /etc/nginx/conf.d/default.conf
COPY default.conf /etc/nginx/conf.d
CMD ["nginx", "-g", "daemon off;"]
Nginx - Docker build & DockerHub에 push
# ~/work/ch10/ex01/myNginx04d
ls
## default.conf Dockerfile
docker build . -t <repository>/mynginxd_ch10:0.3
docker push <repository>/mynginxd_ch10:0.3
Flask - Nginx 이미지 생성
Flask 프로젝트 생성
# ~/work/ch10
mkdir ex02
cp -r ex01 ex02
cd ex02
mv ex01 myFlask02
cd myFlask02
Flask - requirements 및 Dockerfile 작성
# ~/work/ch10/ex02/myFlask02/requirements.txt
flask==3.0.0
gunicorn==20.1.0
# ~/work/ch10/ex02/myFlask02/Dockerfile
FROM python:3.11.6
WORKDIR /usr/src/app
COPY . .
RUN python -m pip install --upgrade pip
RUN pip install -r requirements.txt
WORKDIR ./myapp
CMD gunicorn --bind 0.0.0.0:8001 main:app
EXPOSE 8001
Flask - Docker build & DockerHub에 push
# ~/work/ch10/ex02/myFlask02
ls
## Dockerfile myapp requirements.txt
docker build . -t <repository>/myflask_ch10:0.2
docker push <repository>/myflask_ch10:0.2
Nginx 이미지 생성
# ~/work/ch10/ex02/myNginx02f/default.conf
server{
listen 80;
server_name localhost;
location /{
proxy_pass http://127.0.0.1:8001;
}
}
# ~/work/ch10/ex02/myNginx02f/Dockerfile
FROM nginx:1.25.3
RUN rm /etc/nginx/conf.d/default.conf
COPY default.conf /etc/nginx/conf.d
CMD ["nginx", "-g", "daemon off;"]
Nginx - Docker build & DockerHub에 push
# ~/work/ch10/ex02/myNginx02f
ls
## default.conf Dockerfile
docker build . -t <repository>/mynginxf_ch10:0.3
docker push <repository>/mynginxf_ch10:0.3
Django deployment, service 생성
# ~/work/ch10/ex03/django-deploy.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploy-django
spec:
replicas: 3
selector:
matchLabels:
app.kubernetes.io/name: web-deploy
template:
metadata:
labels:
app.kubernetes.io/name: web-deploy
spec:
containers:
- name: nginx-d
image: hyesushim/mynginxd_ch10:0.3
ports:
- containerPort: 80
- name: django-web
image: hyesushim/mydjango_ch10:0.2
ports:
- containerPort: 8000
# ~/work/ch10/ex03/django-service.yml
apiVersion: v1
kind: Service
metadata:
name: django-service
spec:
selector:
app.kubernetes.io/name: web-deploy
type: ClusterIP
ports:
- protocol: TCP
port: 80
targetPort: 80
Flask deployment, service 생성
# ~/work/ch10/ex03/flask-deploy.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploy-flask
spec:
replicas: 3
selector:
matchLabels:
app.kubernetes.io/name: flask-web-deploy
template:
metadata:
labels:
app.kubernetes.io/name: flask-web-deploy
spec:
containers:
- name: nginx-f
image: hyesushim/mynginxf_ch10:0.3
ports:
- containerPort: 80
- name: flask-web
image: hyesushim/myflask_ch10:0.2
ports:
- containerPort: 8001
# ~/work/ch10/ex03/flask-service.yml
apiVersion: v1
kind: Service
metadata:
name: flask-service
spec:
selector:
app.kubernetes.io/name: flask-web-deploy
type: ClusterIP
ports:
- protocol: TCP
port: 80
targetPort: 80
Ingress 파일 생성 및 배포
/test01 ⇒ django 서비스 → django deployment
/test02 ⇒ flask 서비스 → flask deployment
# ~/work/ch10/ex03/django-flask-ingress.yml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: django-flask-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
ingressClassName: nginx
rules:
- http:
paths:
- path: /test01(/|$)(.*)
pathType: Prefix
backend:
service:
name: django-service
port:
number: 80
- path: /test02(/|$)(.*)
pathType: Prefix
backend:
service:
name: flask-service
port:
number: 80
배포
k apply -f django-deployment.yml
k apply -f django-service.yml
k apply -f flask-deployment.yml
k apply -f flask-service.yml
k get all
k apply -f django-flask-ingress.yml
k get ingress
## NAME CLASS HOSTS ADDRESS PORTS AGE
## django-flask-ingress nginx * 192.168.64.9 80 5h41m
k port-forward -n mynginx service/nginx-ingress-controller-1730253917 2000:80 --address 0.0.0.0
# 호스트 머신
curl http://192.168.64.100/test01 # ingress-controller의 외부IP에 직접 접근
curl http://192.168.64.100/test02
curl http://192.168.64.8:2000/test01 # 포트포워딩
curl http://192.168.64.8:2000/test02
'Cloud' 카테고리의 다른 글
[CI/CD] Github Actions와 ArgoCD를 활용한 CI/CD (0) | 2024.11.05 |
---|---|
[K8s] Kubernetes Ingress 및 Helm (2) | 2024.11.05 |
[K8s] Kubernetes Service (1) | 2024.11.05 |
[K8s] kubectl과 Workload (0) | 2024.10.25 |
[K8s] K8s 구조 및 실습 환경 구축 (0) | 2024.10.01 |