diff --git a/k8s/configmap.yaml b/k8s/app-config.yaml similarity index 100% rename from k8s/configmap.yaml rename to k8s/app-config.yaml diff --git a/k8s/app.yaml b/k8s/app.yaml index d9d34e5..e837fa2 100644 --- a/k8s/app.yaml +++ b/k8s/app.yaml @@ -18,7 +18,7 @@ spec: spec: containers: - name: app-container - image: varenikvkusny/fastapi-with-bot:1.0 + image: varenikvkusny/fastapi-with-bot:1.2 resources: requests: memory: "64Mi" @@ -64,6 +64,9 @@ kind: Service metadata: name: app-service namespace: fastapi-with-bot + annotations: + prometheus.io/scrape: "true" + prometheus.io/port: "8000" spec: selector: app: app diff --git a/k8s/bot.yaml b/k8s/bot.yaml index 2cd10b7..0f8a08d 100644 --- a/k8s/bot.yaml +++ b/k8s/bot.yaml @@ -14,7 +14,7 @@ spec: spec: containers: - name: app-container - image: varenikvkusny/fastapi-with-bot:1.0 + image: varenikvkusny/fastapi-with-bot:1.2 command: ["python", "-m", "src.tg_bot.main_bot"] diff --git a/k8s/grafana-deployment.yaml b/k8s/grafana-deployment.yaml new file mode 100644 index 0000000..43451fb --- /dev/null +++ b/k8s/grafana-deployment.yaml @@ -0,0 +1,40 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: grafana-deployment + namespace: fastapi-with-bot +spec: + replicas: 1 + selector: + matchLabels: + app: grafana + template: + metadata: + labels: + app: grafana + spec: + containers: + - name: grafana + image: grafana/grafana:latest + resources: + requests: + memory: "64Mi" + cpu: "50m" + limits: + memory: "256Mi" + cpu: "250m" + ports: + - containerPort: 3000 + +--- +apiVersion: v1 +kind: Service +metadata: + name: grafana-service + namespace: fastapi-with-bot +spec: + selector: + app: grafana + ports: + - port: 3000 + targetPort: 3000 diff --git a/k8s/migration-job.yaml b/k8s/migration-job.yaml index f7c91b7..4d03ae8 100644 --- a/k8s/migration-job.yaml +++ b/k8s/migration-job.yaml @@ -24,7 +24,7 @@ spec: containers: - name: migration-container - image: varenikvkusny/fastapi-with-bot:1.0 + image: varenikvkusny/fastapi-with-bot:1.2 command: ["alembic", "upgrade", "head"] envFrom: diff --git a/k8s/prometheus-config.yaml b/k8s/prometheus-config.yaml new file mode 100644 index 0000000..d94e22b --- /dev/null +++ b/k8s/prometheus-config.yaml @@ -0,0 +1,17 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: prometheus-config + namespace: fastapi-with-bot +data: + prometheus.yml: | + global: + scrape_interval: 15s + scrape_configs: + - job_name: 'kubernetes-services' + kubernetes_sd_configs: + - role: service + relabel_configs: + - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scrape] + action: keep + regex: true diff --git a/k8s/prometheus-deployment.yaml b/k8s/prometheus-deployment.yaml new file mode 100644 index 0000000..c2e7a36 --- /dev/null +++ b/k8s/prometheus-deployment.yaml @@ -0,0 +1,51 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: prometheus-deployment + namespace: fastapi-with-bot +spec: + replicas: 1 + selector: + matchLabels: + app: prometheus + template: + metadata: + labels: + app: prometheus + spec: + serviceAccountName: prometheus + containers: + - name: prometheus-container + image: prom/prometheus:latest + resources: + requests: + memory: "64Mi" + cpu: "50m" + limits: + memory: "256Mi" + cpu: "250m" + ports: + - containerPort: 9090 + args: + - "--config.file=/etc/prometheus/prometheus.yml" + volumeMounts: + - name: prometheus-volume + mountPath: /etc/prometheus/ + + volumes: + - name: prometheus-volume + configMap: + name: prometheus-config + +--- +apiVersion: v1 +kind: Service +metadata: + name: prometheus-service + namespace: fastapi-with-bot +spec: + selector: + app: prometheus + ports: + - port: 9090 + targetPort: 9090 diff --git a/k8s/prometheus-rbac.yaml b/k8s/prometheus-rbac.yaml new file mode 100644 index 0000000..d5a3a5a --- /dev/null +++ b/k8s/prometheus-rbac.yaml @@ -0,0 +1,33 @@ +apiVersion: v1 +kind: ServiceAccount +metadata: + name: prometheus + namespace: fastapi-with-bot + +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: prometheus +rules: + - apiGroups: [""] + resources: + - nodes + - pods + - services + - endpoints + verbs: ["get", "list", "watch"] + +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + name: prometheus +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: prometheus +subjects: + - kind: ServiceAccount + name: prometheus + namespace: fastapi-with-bot diff --git a/requirements.txt b/requirements.txt index cac8b58..cd626da 100644 Binary files a/requirements.txt and b/requirements.txt differ diff --git a/src/backend/main.py b/src/backend/main.py index 2c24d60..e2aa452 100644 --- a/src/backend/main.py +++ b/src/backend/main.py @@ -2,6 +2,7 @@ import redis.asyncio as redis from fastapi import FastAPI from contextlib import asynccontextmanager +from prometheus_fastapi_instrumentator import Instrumentator from .routers import auth, posts from . import clients from .config import get_settings @@ -10,21 +11,26 @@ logging.basicConfig(level=logging.INFO) + @asynccontextmanager async def lifespan(app: FastAPI): - logging.info('Приложение запускается') - - logging.info('Инициализирую redis_client') + logging.info("Приложение запускается") - clients.redis_client = redis.from_url(settings.redis_url, encoding='utf-8', decode_responses=True) + logging.info("Инициализирую redis_client") + clients.redis_client = redis.from_url( + settings.redis_url, encoding="utf-8", decode_responses=True + ) yield - logging.info('Приложение остановлено') + logging.info("Приложение остановлено") app = FastAPI(lifespan=lifespan) +instrumentator = Instrumentator().instrument(app) +instrumentator.expose(app) + app.include_router(auth.router) -app.include_router(posts.router) \ No newline at end of file +app.include_router(posts.router)