forked from mercybassey/django-optl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployment.yaml
207 lines (193 loc) · 5.3 KB
/
deployment.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
apiVersion: v1
kind: Service
metadata:
name: postgresql
spec:
selector:
app: postgresql
ports:
- port: 5432
targetPort: 5432
---
apiVersion: v1
kind: Service
metadata:
name: notes-app
spec:
selector:
app: notes-app
ports:
- port: 8000
targetPort: 8000
type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: postgresql
spec:
replicas: 1
selector:
matchLabels:
app: postgresql
template:
metadata:
labels:
app: postgresql
annotations:
# Add OpenTelemetry specific annotations
instrumentation.opentelemetry.io/inject-python: "true"
spec:
containers:
- name: postgresql
image: dalareo/notes-db:latest
# always pull
imagePullPolicy: Always
ports:
- containerPort: 5432
env:
# Comprehensive OpenTelemetry Environment Variables
- name: OTEL_SERVICE_NAME
value: postgresql-service
- name: OTEL_RESOURCE_ATTRIBUTES
value: "service.name=postgresql-service,service.type=database,db.system=postgresql,db.connection_string=postgresql://django@notes:5432/notes"
- name: OTEL_TRACES_EXPORTER
value: otlp
- name: OTEL_EXPORTER_OTLP_ENDPOINT
value: "http://grafana-k8s-monitoring-alloy.grafana.svc.cluster.local:4317"
- name: OTEL_TRACE_SAMPLING_RATE
value: "1.0"
# Database Configuration
- name: POSTGRES_DB
value: notes
- name: POSTGRES_USER
value: django
- name: POSTGRES_PASSWORD
value: "1234"
- name: POSTGRES_MULTIPLE_DATABASES
value: notes,django
# Debugging and Logging
- name: PYTHONUNBUFFERED
value: "1"
- name: PYTHONPATH
value: "/root/.local/lib/python3.x/site-packages"
volumeMounts:
- name: initdb
mountPath: /docker-entrypoint-initdb.d
readinessProbe:
exec:
command:
- pg_isready
- -U
- django
initialDelaySeconds: 15
periodSeconds: 10
volumes:
- name: initdb
configMap:
name: postgresql-init
defaultMode: 0755
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: notes-app
spec:
replicas: 1
selector:
matchLabels:
app: notes-app
template:
metadata:
labels:
app: notes-app
annotations:
# Add OpenTelemetry specific annotations
instrumentation.opentelemetry.io/inject-python: "true"
spec:
containers:
- name: notes-app
image: dalareo/notes-app:latest
#always pull
imagePullPolicy: Always
ports:
- containerPort: 8000
env:
# Database Connection
- name: DB_NAME
value: django
- name: DB_USER
value: django
- name: DB_PASSWORD
value: "1234"
- name: DB_HOST
value: postgresql
- name: DB_PORT
value: "5432"
# Django and OpenTelemetry Configuration
- name: DJANGO_SETTINGS_MODULE
value: django_project.settings
- name: DJANGO_LOG_LEVEL
value: INFO
- name: ALLOWED_HOSTS
value: "*"
- name: DEBUG
value: "True"
# OpenTelemetry Environment Variables
- name: OTEL_SERVICE_NAME
value: notes-web-service
- name: OTEL_EXPORTER_OTLP_ENDPOINT
value: "http://grafana-k8s-monitoring-alloy.grafana.svc.cluster.local:4317"
- name: OTEL_TRACES_SAMPLER
value: "always_on"
- name: OTEL_TRACES_EXPORTER
value: "otlp"
- name: OTEL_PYTHON_LOG_CORRELATION
value: "true"
command: ["/bin/sh", "-c"]
args:
- |
# Wait for database to be ready
until pg_isready -h postgresql -p 5432 -U django; do
echo "Waiting for PostgreSQL to be ready..."
sleep 5
done
# Rest of your startup script
pip install --user psycopg2-binary
python manage.py makemigrations
python manage.py migrate
python manage.py runserver 0.0.0.0:8000
readinessProbe:
httpGet:
path: /
port: 8000
initialDelaySeconds: 30
periodSeconds: 10
---
apiVersion: v1
kind: ConfigMap
metadata:
name: postgresql-init
data:
create-multiple-databases.sh: |
#!/bin/bash
set -e
set -u
function create_user_and_database() {
local database=$1
echo "Creating database '$database'"
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" postgres <<-EOSQL
SELECT 'CREATE DATABASE $database'
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = '$database')\gexec
GRANT ALL PRIVILEGES ON DATABASE $database TO $POSTGRES_USER;
EOSQL
}
if [ -n "$POSTGRES_MULTIPLE_DATABASES" ]; then
echo "Multiple database creation requested: $POSTGRES_MULTIPLE_DATABASES"
for db in $(echo $POSTGRES_MULTIPLE_DATABASES | tr ',' ' '); do
create_user_and_database $db
done
echo "Multiple databases created"
fi
echo "Generating sample trace"
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" -c "SELECT 1" postgres