×
Community Blog How to Deploy a SQL Server Container in Alibaba Container Service Kubernetes

How to Deploy a SQL Server Container in Alibaba Container Service Kubernetes

In this tutorial, we are going to connect and deploy Kubernetes resources through the Alibaba Cloud console.

By Nadaraj Prabhu, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud's incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.

In this tutorial, we will connect and deploy Kubernetes resources through the Alibaba Cloud console. First, we'll see how we can connect a local machine to a Kubernetes cluster.

Prerequisites

Install a Kubernetes Cluster on Alibaba Cloud.

Connecting to Kubernetes Cluster

To manage the Kubernetes cluster, you need to connect to your cluster in using Cloud Shell CLI 2.0. It will help you execute "kubectl" command from local machine to "Kubernetes Services" which is hosted on Alibaba Cloud.

Step 1: To install "kubectl" on local machine, go to Alibaba Cloud Console Portal Link. select you "cluster" and click on "Manage" of your Kubernetes cluster.

1

2

Step 2: Scroll down and navigate to "Cluster Resource" Section and copy the key (kube config code).

3

Note:

  1. These commands will not work in cloud shell and must be running on your local machine.
  2. If you are planning to access the Kubernetes cluster using Public IP then copy Internet Access Code.
  3. If you have a VPN setup for this cluster then you can use VPN Access

Step 3: Go to your local machine, If you do not already have kubectl installed in your CLI. Open PowerShell/ CMD (as administrator) and install Chocolatey. Run the following command:

Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

Note: I'm installing using Chocolatey method, you choose method your choice Link

4

Step 4: Install "kubectl" Run the below command:

choco install kubernetes-cli

To check "kubectl" version, type the command below:

kubectl version

5

Step 5: Change the path of to your directory and create a "config file" store connection Link, follow the commands below:

Below command will show your home and you need to keep the config fie there.

echo $HOME

cd C:\

mkdir C:\Kubelet\ .kube

cd C:\Kubelet\ .kube

New-Item config -type file

6

Step 6: Now go back to the cluster, browse for "KubeConfig", copy the value and paste it in on local "config" file.

7

8

Note: The config file on the mentioned path is used as the authentication for Kubernetes Service to communicate with your local machine.

Step 7: Now type the below command to check whether you are connected you Kubernetes cluster.

kubectl cluster-info 

9

You can leverage Cloud Shell's functionality to create CusterRole, service, applications, pods, etc from local machine. In next topic will see how Cloud Shell help us create "secret" key which can store sensitive information. Like SQL Server "sa" Password etc.

Deploy a SQL Server Using Kubernetes Web UI Management Interface

You can configure a SQL Server instance on container using Kubernetes Service, with persistent storage for high availability (HA). The solution also provides high resilience. If the SQL Server instance fails, Kubernetes automatically re-creates it in a new pod. Kubernetes also provides resiliency against a node failure. In this demo we are going to deploy the resources though Kubernetes dashboard in Alibaba Cloud.

Step 1: Create an SA password.

Kubernetes can manage sensitive configuration information, like passwords as secrets.

The following command creates a password for the SA account:

kubectl create secret generic mssql --from-literal=SA_PASSWORD="Pass@123db" 

10

Note: You can also create and verify from console under "secret" by opening "dashboard" of your "Kubernetes Cluster".

To createSecret on Kubernetes Services, open select dashboard of your Kubernetes cluster.

11

12

Step 2: Create a persistent storage:

Create a manifest/ docker deployment script to define the storage class and the persistent volume claim, which specifies the storage provisioner, parameters, and reclaim policy.

Note: Make sure you replace "storage class" in the below script. You can get your storage classes name as shown below

13

Below mentioned code represents an Alibaba disk with 30GB storage of type standard SSD.

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: mssql-data
spec:
  storageClassName: alicloud-disk-ssd #Change the Storage Class value
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 30Gi

Step 3: To deploy the Persistent Volume Claim on Kubernetes Services, open select dashboard of your Kubernetes cluster.

14

Step 4: Click on "Create" and paste the script, or you can save the above script as yaml file and upload the script. Link 1

15

Step 5: Once the deployment is successful, navigate to "Persistent Volumes Claims" blade and you can see the disk which is being created.

16

Step 6: Create a SQL Server which uses the persistent storage to store the database:

Create a manifest/ docker deployment script which describe the container based on the SQL Server mssql-server-linux Docker image (make sure it fetches password from the secret created).

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: mssql-deployment
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: mssql
    spec:
      terminationGracePeriodSeconds: 10
      containers:
      - name: mssql
        image: microsoft/mssql-server-linux
        ports:
        - containerPort: 1433
        env:
        - name: ACCEPT_EULA
          value: "Y"
        - name: SA_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mssql
              key: SA_PASSWORD 
        volumeMounts:
        - name: mssql-data
          mountPath: /var/opt/mssql
      volumes:
      - name: mssql-data
        persistentVolumeClaim:
          claimName: mssql-data
---
apiVersion: v1
kind: Service
metadata:
  name: mssql-deployment
spec:
  selector:
    app: mssql
  ports:
    - protocol: TCP
      port: 1433
      targetPort: 1433
  type: LoadBalancer

Step 7: To deploy the SQL Services on a container of Kubernetes Services, go to your Kubernetes cluster and select "dashboard"

17

Step 8: click on "Create" and paste the script, or you can save the above script as yaml file and upload the script. Link 2

18

Step 9: To check the deployment status, go to "Deployments" blade and check for the deployment name, if deployment is successful you should see a green check as shown below.

19

Step 10: Go to Services blade to check your SQL Service and collect the Public IP.

20

Step 11: Now connect to SQL Server using SQL Server Management Tool (SSMS).

21

Now you have deployed SQL Server on kubernetes container with persistent storage and load balancer. SQL Server "SA" login password is stored in a secret vault, which makes it easy for you to swap whenever necessary. For monitoring purpose, navigate to "Overview" blade and check the status and heath of the nodes, services and pods.

22

References:

1 1 1
Share on

Alibaba Clouder

2,605 posts | 747 followers

You may also like

Comments

Dikky Ryan Pratama June 23, 2023 at 8:46 am

awesome!