Skip to content

Commit b7220ce

Browse files
committed
fix for sql startup
1 parent 82113cb commit b7220ce

File tree

3 files changed

+66
-7
lines changed

3 files changed

+66
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ the release.
77

88
## Unreleased
99

10+
* [fraud-detection] Add automatic SQL Server database initialization for FraudDetection database
11+
- Added ConfigMap with database initialization script
12+
- Added postStart lifecycle hook to execute init script on SQL Server startup
13+
- Fixed connection string to use correct database name (FraudDetection)
1014
* [chore] Use pre-built nginx otel image
1115
([#2614](https://github.com/open-telemetry/opentelemetry-demo/pull/2614))
1216
* [grafana] Update grafana version to 12.2.0

kubernetes/opentelemetry-demo.yaml

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,20 @@ type: Opaque
346346
stringData:
347347
SA_PASSWORD: "ChangeMe_SuperStrong123!"
348348
# Optional: full connection string if you want to inject as a single env var.
349-
DB_CONNECTION_STRING: "Server=tcp:sql-server-fraud.sql.svc.cluster.local,1433;Database=MyDb;User Id=sa;Password=ChangeMe_SuperStrong123!;Encrypt=True;TrustServerCertificate=True"
349+
DB_CONNECTION_STRING: "Server=tcp:sql-server-fraud.sql.svc.cluster.local,1433;Database=FraudDetection;User Id=sa;Password=ChangeMe_SuperStrong123!;Encrypt=True;TrustServerCertificate=True"
350+
---
351+
apiVersion: v1
352+
kind: ConfigMap
353+
metadata:
354+
name: mssql-init-script
355+
namespace: sql
356+
data:
357+
init-db.sql: |
358+
IF NOT EXISTS (SELECT name FROM sys.databases WHERE name = 'FraudDetection')
359+
BEGIN
360+
CREATE DATABASE FraudDetection;
361+
END
362+
GO
350363
---
351364
apiVersion: v1
352365
kind: Service
@@ -382,10 +395,6 @@ spec:
382395
labels:
383396
app: sql-server-fraud
384397
spec:
385-
securityContext:
386-
fsGroup: 10001 # mssql user can write PV
387-
runAsUser: 10001 # run container as mssql user
388-
runAsGroup: 0
389398
containers:
390399
- name: mssql
391400
image: mcr.microsoft.com/mssql/server:2022-latest
@@ -406,6 +415,17 @@ spec:
406415
volumeMounts:
407416
- name: data
408417
mountPath: /var/opt/mssql
418+
- name: init-script
419+
mountPath: /docker-entrypoint-initdb.d
420+
lifecycle:
421+
postStart:
422+
exec:
423+
command:
424+
- /bin/bash
425+
- -c
426+
- |
427+
sleep 30
428+
/opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P "${SA_PASSWORD}" -C -i /docker-entrypoint-initdb.d/init-db.sql
409429
resources:
410430
requests:
411431
cpu: "250m"
@@ -423,6 +443,10 @@ spec:
423443
port: 1433
424444
initialDelaySeconds: 30
425445
periodSeconds: 20
446+
volumes:
447+
- name: init-script
448+
configMap:
449+
name: mssql-init-script
426450
volumeClaimTemplates:
427451
- metadata:
428452
name: data
@@ -940,8 +964,10 @@ spec:
940964
value: "true"
941965
- name: SPLUNK_METRICS_ENABLED
942966
value: "true"
943-
- name: SPLUNK_METRICS_ENDPOINT
944-
value: "http://$(NODE_IP):9943"
967+
- name: SPLUNK_SNAPSHOT_PROFILE_ENABLED
968+
value: "true"
969+
- name: SPLUNK_SNAPSHOT_SELECTION_PROBABILITY
970+
value: "0.1"
945971
- name: OTEL_TRACE_EXPORTER
946972
value: otlp
947973
- name: OTEL_TRACES_SAMPLER

src/fraud-detection/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,35 @@
33
This service receives new orders by a Kafka topic and returns cases which are
44
suspected of fraud.
55

6+
## Dependencies
7+
8+
This service requires:
9+
- **Kafka**: For receiving order events
10+
- **SQL Server**: For storing fraud detection data in the `FraudDetection` database
11+
12+
## Kubernetes Deployment
13+
14+
### SQL Server Database Initialization
15+
16+
The fraud detection service requires a SQL Server database named `FraudDetection`. The Kubernetes deployment includes automatic database initialization:
17+
18+
1. **ConfigMap** (`mssql-init-script`): Contains SQL script to create the database
19+
2. **StatefulSet** (`sql-server-fraud`): SQL Server with lifecycle hook that executes the init script
20+
3. **InitContainers**: The fraud detection deployment waits for both Kafka and SQL Server to be ready before starting
21+
22+
The database is automatically created when the SQL Server pod starts using a `postStart` lifecycle hook that:
23+
- Waits 30 seconds for SQL Server to be fully initialized
24+
- Executes the database creation script via `sqlcmd`
25+
26+
### Connection Details
27+
28+
The service connects to SQL Server using these environment variables:
29+
- `SQL_SERVER_HOST`: `sql-server-fraud.sql.svc.cluster.local`
30+
- `SQL_SERVER_PORT`: `1433`
31+
- `SQL_SERVER_DATABASE`: `FraudDetection`
32+
- `SQL_SERVER_USER`: `sa`
33+
- `SQL_SERVER_PASSWORD`: From `mssql-secrets` secret
34+
635
## Local Build
736

837
To build the protos and the service binary, run from the repo root:

0 commit comments

Comments
 (0)