---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: sqlserver-pvc
  namespace: academia
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: local-path
  resources:
    requests:
      storage: 2Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: sqlserver
  namespace: academia
  labels:
    app: sqlserver
spec:
  replicas: 1
  selector:
    matchLabels:
      app: sqlserver
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: sqlserver
    spec:
      nodeSelector:
        kubernetes.io/hostname: hp62a
      containers:
        - name: sqlserver
          image: andresgarcia0313/mssql-express-lowram:latest
          ports:
            - containerPort: 1433
              name: sql
          env:
            - name: MSSQL_SA_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: student-secrets
                  key: db-password
            - name: MSSQL_PID
              value: "Express"
            - name: MSSQL_MEMORY_LIMIT_MB
              value: "512"
          volumeMounts:
            - name: sqlserver-data
              mountPath: /var/opt/mssql
          resources:
            requests:
              memory: "512Mi"
              cpu: "250m"
            limits:
              memory: "768Mi"
              cpu: "1000m"
          livenessProbe:
            exec:
              command:
                - /opt/mssql-tools/bin/sqlcmd
                - -S
                - localhost
                - -U
                - sa
                - -P
                - $(MSSQL_SA_PASSWORD)
                - -Q
                - SELECT 1
            initialDelaySeconds: 60
            periodSeconds: 30
            timeoutSeconds: 10
            failureThreshold: 5
          readinessProbe:
            tcpSocket:
              port: 1433
            initialDelaySeconds: 30
            periodSeconds: 15
            timeoutSeconds: 10
      volumes:
        - name: sqlserver-data
          persistentVolumeClaim:
            claimName: sqlserver-pvc
---
apiVersion: v1
kind: Service
metadata:
  name: sqlserver
  namespace: academia
spec:
  selector:
    app: sqlserver
  ports:
    - port: 1433
      targetPort: 1433
      name: sql
  type: ClusterIP
