I have created a custom SQL Server Database Container Image. I can run that as a container on my local machine (Windows 10 with Docker for Windows). After running that container, I can successfully connect to it from SQL Server Management Studio by using server name as 127.0.0.1:1433.
I am trying to run that same container as a StatefulSet in Kubernetes in Azure.
Below are Kubernetes resources.
Secret (For SQL Server SA password)
apiVersion: v1
kind: Secret
metadata:
name: sqlsecret
type: Opaque
data:
sapassword: base64encodedvalue
Storage Class (StatefulSet - External Storage)
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: azurestorageclass
provisioner: kubernetes.io/azure-disk
parameters:
skuName: Standard_LRS
location: southeastasia
storageAccount: myaccount
External Service (Tried with Headless Service as well, but I need to connect externally from pods also)
apiVersion: v1
kind: Service
metadata:
name: sqlservice
labels:
name: sqlservice
spec:
ports:
- port: 1433
targetPort: 1433
externalIPs:
- Kubernetes-Cluster-LB-Public-IP-Address-From-Azure-Portal
selector:
role: sqlservice
and finally StatefulSet.
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: sqlserverstatefulset
spec:
serviceName: "sqlservice"
replicas: 1
template:
metadata:
labels:
role: sqlservice
spec:
terminationGracePeriodSeconds: 10
containers:
- name: sqlinux
image: custom-image-from-docker-hub
env:
- name: SA_PASSWORD
valueFrom:
secretKeyRef:
name: sqlsecret
key: sapassword
- name: ACCEPT_EULA
value: "Y"
ports:
- containerPort: 1433
volumeMounts:
- name: sql-persistent-storage
mountPath: /data/db
volumeClaimTemplates:
- metadata:
name: sql-persistent-storage
annotations:
volume.beta.kubernetes.io/storage-class: "azurestorageclass"
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 100Gi
I run this command to provision these resources from my local machine (with context pointing to Azure cluster).
kubectl create -f c:\sqlkube.yaml
All my resources are provisioned successfully. All green, nothing red in Kubernetes dashboard (kubctl proxy
).
I have also set a Health Probe and Load Balancing Rule for Azure Load Balancer for port 1433.
However, when I enter Public IP address of LB : 1433 as Server Name in SQL Server Management Studio, I get following error.
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 0 - The wait operation timed out.) (Microsoft SQL Server, Error: 258)
What am I missing?
Service needs to be changed to type: LoadBalancer
. If you already have provisioned a static IP in same resource group as k8s cluster, you can specify that as loadBalancerIP: WW.XX.YY.ZZ
. If commented, service will provision a new IP address. Use kubectl describe service sqlservice
to find out IP address or use k8s dashboard.
apiVersion: v1
kind: Service
metadata:
name: sqlservice
labels:
app: sqlservice
spec:
type: LoadBalancer
#loadBalancerIP: 52.187.173.125
ports:
- port: 1433
targetPort: 1433
selector:
app: sqlinux