Execute SQL commands in k8s

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

New contributor

Snooker is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật