I’m trying to figure out how can i execute SQL commands on MSSQL server which running in kubernetes. I tride init container and postStart hook but cant seem to find an answer.
When im using PowerShell, with this command i can excecute SQL commands on the SQL server. But cant do that from the yaml pipeline itself. kubectl exec -it podname -- /opt/mssql-tools/bin/sqlcmd -S localhost -U username -P "password" -Q "SELECT @@VERSION;"
Using the yaml pipeline often gives this error:
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2749.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..
Yaml which start a simple SQL server:
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: ${LOWERASSET}
labels:
app: ${ASSET}
spec:
replicas: 1
serviceName: ${LOWERASSET}
selector:
matchLabels:
app: ${ASSET}
template:
metadata:
labels:
app: ${ASSET}
spec:
securityContext:
fsGroup: 10001
containers:
- name: ${LOWERASSET}
image: ${IMAGE}
securityContext:
runAsUser: 10001
resources:
requests:
cpu: "200m"
memory: "2Gi"
limits:
cpu: "500m"
memory: "4Gi"
ports:
- containerPort: 1433
name: tcpsql
env:
- name: MSSQL_PID
value: Developer
- name: ACCEPT_EULA
value: "Y"
- name: ENABLE_HADR
value: "1"
- name: AGENT_ENABLED
value: "1"
- name: SA_PASSWORD
valueFrom:
secretKeyRef:
name: secret
key: pwd
volumeMounts:
- name: ${LOWERASSET}
mountPath: "/var/opt/mssql"
volumeClaimTemplates:
- metadata:
name: ${LOWERASSET}
labels:
app: ${ASSET}
backup: "${BACKUP}"
spec:
storageClassName: encrypted-standard
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 8Gi
---
kind: Service
apiVersion: v1
metadata:
name: ${LOWERASSET}
labels:
app: ${ASSET}
spec:
type: ClusterIP
selector:
app: ${ASSET}
ports:
- name: tcpsql
protocol: TCP
port: 1433
**Here is my latest try. **
apiVersion: v1
kind: ConfigMap
metadata:
name: sql-init-script
namespace: namespace
data:
init.sql: |
some sql code
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: sql-server
namespace: namespace
labels:
app: sql-server
spec:
replicas: 1
selector:
matchLabels:
app: sql-server
template:
metadata:
labels:
app: sql-server
spec:
containers:
- name: sql-server
image: ${IMAGE}
ports:
- containerPort: 1433
env:
- name: ACCEPT_EULA
value: "Y"
- name: SA_PASSWORD
valueFrom:
secretKeyRef:
name: secret
key: pwd
readinessProbe:
exec:
command:
- /opt/mssql-tools/bin/sqlcmd
- -Q
- "SELECT 1"
initialDelaySeconds: 30
periodSeconds: 10
---
apiVersion: v1
kind: Service
metadata:
name: sql-server
namespace: namespace
spec:
ports:
- port: 1433
targetPort: 1433
selector:
app: sql-server
type: ClusterIP
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: ${LOWERASSET}
namespace: namespace
labels:
app: ${ASSET}
spec:
replicas: 1
serviceName: ${LOWERASSET}
selector:
matchLabels:
app: ${ASSET}
template:
metadata:
labels:
app: ${ASSET}
spec:
securityContext:
fsGroup: 10001
initContainers:
- name: init-sql
image: ${IMAGE}
resources:
requests:
cpu: "200m"
memory: "2Gi"
limits:
cpu: "500m"
memory: "4Gi"
command:
- /bin/bash
args:
- -c
- |
echo "Starting SQL Server readiness check"
until /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P "$SA_PASSWORD" -Q "SELECT 1"; do
echo "Waiting for SQL Server to be ready"
sleep 5
done
echo "SQL Server is ready."
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P "$SA_PASSWORD" -i /mnt/init/init.sql
env:
- name: SA_PASSWORD
valueFrom:
secretKeyRef:
name: sql-server
key: pwd
volumeMounts:
- name: init-script
mountPath: /mnt/init
containers:
- name: ${LOWERASSET}
image: ${IMAGE}
resources:
requests:
cpu: "200m"
memory: "2Gi"
limits:
cpu: "500m"
memory: "4Gi"
ports:
- containerPort: 1433
name: tcpsql
volumeMounts:
- name: ${LOWERASSET}
mountPath: "/var/opt/mssql"
volumes:
- name: init-script
configMap:
name: sql-init-script
volumeClaimTemplates:
- metadata:
name: ${LOWERASSET}
labels:
app: ${ASSET}
backup: "${BACKUP}"
spec:
storageClassName: encrypted-standard
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 8Gi
---
apiVersion: v1
kind: Service
metadata:
name: ${LOWERASSET}
namespace: namespace
labels:
app: ${ASSET}
spec:
type: ClusterIP
selector:
app: ${ASSET}
ports:
- name: tcpsql
protocol: TCP
port: 1433
Tried initcontainer, postStart hook.
Both came with the error mentioned above
Snooker is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.