apiVersion: v1
kind: ConfigMap
metadata:
name: rabbitmq-cm
data:
RABBITMQ_DEFAULT_VHOST: "my_vhost"
RABBITMQ_DEFAULT_USER: "user"
RABBITMQ_DEFAULT_PASS: "qwer1234"
apiVersion: apps/v1
kind: Deployment
metadata:
name: rabbitmq-deployment
spec:
selector:
matchLabels:
type: rabbitmq
replicas: 1
template:
metadata:
labels:
type: rabbitmq
spec:
containers:
- name: rabbitmq
image: rabbitmq:latest
envFrom:
- configMapRef:
name: rabbitmq-cm
rabbitmq-plugins enable rabbitmq_management
apiVersion: v1
kind: Service
metadata:
name: rabbitmq-svc
spec:
selector:
type: rabbitmq
ports:
- port: 5672
name: message-que
targetPort: 5672
- port: 15672
name: dashboard
targetPort: 15672
type: LoadBalancer
apiVersion: v1
kind: ConfigMap
metadata:
name: admin-db-cm
data:
MYSQL_ROOT_PASSWORD: 'qwer1234'
MYSQL_DATABASE: 'admin-db'
apiVersion: v1
kind: ConfigMap
metadata:
name: admin-cm
data:
DBNAME: "admin-db"
DBUSER: "root"
DBPASS: "qwer1234"
DBHOST: "127.0.0.1"
DBPORT: "3306"
RABBITMQURL: "amqp://user:qwer1234@rabbitmq-svc/my_vhost"
apiVersion: apps/v1
kind: Deployment
metadata:
name: admin-deployment
spec:
selector:
matchLabels:
type: admin
replicas: 1
template:
metadata:
labels:
type: admin
spec:
nodeSelector:
kubernetes.io/hostname: node-01
containers:
- name: admin-db
image: mysql:8.0.32-debian
volumeMounts:
- name: admin-db
mountPath: /var/lib/mysql
envFrom:
- configMapRef:
name: admin-db-cm
- name: admin
image: ddarahakit2023/admin:1.0
startupProbe:
tcpSocket:
port: 8000
initialDelaySeconds: 10
periodSeconds: 10
command: ["python", "manage.py", "runserver", "0.0.0.0:8000"]
volumeMounts:
- name: admin-path
mountPath: /app
envFrom:
- configMapRef:
name: admin-cm
- name: admin-consumer
image: ddarahakit2023/admin:1.0
command: ["python", "-u", "consumer.py"]
volumeMounts:
- name: admin-path
mountPath: /app
envFrom:
- configMapRef:
name: admin-cm
volumes:
- name : admin-path
hostPath:
path: /admin-app
type: DirectoryOrCreate
- name : admin-db
hostPath:
path: /admin-db
type: DirectoryOrCreate
import os
를 해줘야한다!vi /admin-app/consumer.py
params = pika.URLParameters(os.environ["RABBITMQURL"])
vi /admin-app/admin/settings.py
'NAME': os.environ["DBNAME"],
'USER': os.environ["DBUSER"],
'PASSWORD': os.environ["DBPASS"],
'HOST': os.environ["DBHOST"],
'PORT': os.environ["DBPORT"],
vi /admin-app/products/producer.py
params = pika.URLParameters(os.environ["RABBITMQURL"])
apiVersion: v1
kind: Service
metadata:
name: admin-svc
spec:
selector:
type: admin
ports:
- port: 8000
targetPort: 8000
type: LoadBalancer
apiVersion: v1
kind: ConfigMap
metadata:
name: main-db-cm
data:
MYSQL_ROOT_PASSWORD: 'qwer1234'
MYSQL_DATABASE: 'main-db'
apiVersion: v1
kind: ConfigMap
metadata:
name: main-cm
data:
DBNAME: "main-db"
DBUSER: "root"
DBPASS: "qwer1234"
DBHOST: "127.0.0.1"
DBPORT: "3306"
RABBITMQURL: "amqp://user:qwer1234@rabbitmq-svc/my_vhost"
MYSQLURL: "mysql://root:qwer1234@127.0.0.1/main-db"
ADMIN_SVC: "admin-svc"
apiVersion: apps/v1
kind: Deployment
metadata:
name: main-deployment
spec:
selector:
matchLabels:
type: main
replicas: 1
template:
metadata:
labels:
type: main
spec:
nodeSelector:
kubernetes.io/hostname: node-02
containers:
- name: main-db
image: mysql:8.0.32-debian
volumeMounts:
- name: main-db
mountPath: /var/lib/mysql
envFrom:
- configMapRef:
name: main-db-cm
- name: main
image: ddarahakit2023/main:1.3
startupProbe:
tcpSocket:
port: 5000
initialDelaySeconds: 10
periodSeconds: 10
command: ["python", "main.py"]
volumeMounts:
- name: main-path
mountPath: /app
envFrom:
- configMapRef:
name: main-cm
- name: main-consumer
image: ddarahakit2023/main:1.3
command: ["python", "-u", "consumer.py"]
volumeMounts:
- name: main-path
mountPath: /app
envFrom:
- configMapRef:
name: main-cm
volumes:
- name : main-path
hostPath:
path: /main-app
type: DirectoryOrCreate
- name : main-db
hostPath:
path: /main-db
type: DirectoryOrCreate
import os
를 해줘야한다!vi main-app/consumer.py
params = pika.URLParameters(os.environ["RABBITMQURL"])
vi main-app/main.py
app.config["SQLALCHEMY_DATABASE_URI"] = os.environ["MYSQLURL"]
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
req = requests.get('http://'+os.environ["ADMIN_SVC"]+':8000/api/user')
vi main-app/producer.py
params = pika.URLParameters(os.environ["RABBITMQURL"])
vi main-app/requirements.txt
Werkzeug==0.16.0
itsdangerous==2.0.1
Jinja2==3.0.3
apiVersion: v1
kind: Service
metadata:
name: main-svc
spec:
selector:
type: main
ports:
- port: 5000
targetPort: 5000
type: LoadBalancer
데이터를 넣어놔서 연결되어 있는 것을 확인 할 수 있다.admin에서 입력하기 전까지 원래는 아무것도 없다.
확인을 하고 싶지만 RabbiMQ 와 연결이 계속 끊기고 있다..
INSERT INTO products_user VALUES(1);