12

Running SQL Server in Kubernetes on Azure Kubernetes Services (AKS)

I’ve previously blogged about running SQL Server in ACS but Microsoft has now released a new version still called Azure Container Services (AKS instead of ACS however) but now specifically tailored to building Kubernetes clusters.


EDIT – Azure Container Services (AKS) has been renamed to Azure Kubernetes Services. Blog title has been updated


There are some differences to the original ACS (making the process simpler) so let’s run through setting up a Kubernetes cluster running SQL Server in AKS.

Ok, first thing to do is install the CLI (I’m going to work from a Bash shell on my desktop): –

echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ wheezy main" | \
     sudo tee /etc/apt/sources.list.d/azure-cli.list
	 
	 
sudo apt-key adv --keyserver packages.microsoft.com --recv-keys 52E16F86FEE04B979B07E28DB02C46DF417A0893
sudo apt-get install apt-transport-https
sudo apt-get update && sudo apt-get install azure-cli

Check the version of the CLI installed (make sure it’s at least version 2.0.20): –

az --version

Then login to Azure in the shell (and follow the instructions): –

az login

As AKS is still in preview a flag needs to be enabled on your Azure subscription.
To do this run: –

az provider register -n Microsoft.ContainerService

You can check that the flag has been successfully enabled by running: –

az provider show -n Microsoft.ContainerService

Cool. Now we’re good to go with setting up a Kubernetes cluster! Same as the original ACS, a resource group needs to be created to hold all the objects in the cloud: –

az group create --name ApResourceGroup1 --location ukwest

And now the cluster can be created. I’m going to create a two node cluster by running: –

az aks create --resource-group ApResourceGroup1 --name mySQLK8sCluster1 --node-count 2 --generate-ssh-keys


EDIT: updated agent-code to node-count has this switch seems to have changed since I wrote this post

What’s cool about this is the amount of objects it’s creating in the background: –

All that from one line of code!

Once that’s complete, Kubectl needs to be installed locally to manage the cluster: –

az aks install-cli

And then I need to connect my local shell to the cluster: –

az aks get-credentials --resource-group ApResourceGroup1 --name mySQLK8sCluster1

Ok, let’s check the nodes in the cluster: –

kubectl get nodes

Awesome, I have two nodes up and running in my cluster!

Next thing to do is spin up SQL Server in a container within the cluster. To do this I’m going to build it from a yaml file: –

nano sqlserver.yml

And drop the following into it: –

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: sqlserver
  labels:
    app: sqlserver
spec:
  replicas: 1
  template:
    metadata:
      labels:
        name: sqlserver
    spec:
      containers:
      - name: sqlserver1
        image: microsoft/mssql-server-linux:latest
        ports:
        - containerPort: 1433
        env:
        - name: SA_PASSWORD
          value: "Testing1122"
        - name: ACCEPT_EULA
          value: "Y"
---
apiVersion: v1
kind: Service
metadata:
  name: sqlserver-service
spec:
  ports:
  - name: sqlserver
    port: 1433
    targetPort: 1433
  selector:
    name: sqlserver
  type: LoadBalancer

This will spin up a container within the cluster (as a deployment) and create a load balanced service with an external IP so that I can connect to SQL Server from my desktop. So now run: –

kubectl create -f sqlserver.yml

Once that’s complete we can run some commands to view the objects created. To check the SQL Server container created: –

kubectl get pods

To check on the deployment:-

kubectl get deployments

And finally, to check on the service: –

kubectl get service

Once the service has an external IP, I can use that to connect to SQL Server within my Kubernetes cluster in AKS!

How awesome is that! Microsoft have made this a nice and simple way of getting into running Kubernetes in Azure. I’m going to play around with this some more 🙂

Last thing, to remove all the objects built in this demo you just need to run: –

az group delete --name ApResourceGroup1

Thanks for reading!

6

Blogging for four years!

I find it absolutely amazing that I’ve been writing this blog for four years. My first ever post My 5 pieces of advice for new DBAs was published way back in November 2013.

I started out writing one post per month, just on areas that I was working on or topics that I found interesting. Over the last year I ramped that up to one technical post a week, one editorial, and on Fridays a post linking to articles I’d been reading. The difference that’s made has been phenomenal!


(hits on my blog by year)

Ok, obviously just publishing more posts a week wouldn’t cause that increase in hits. 2017 has been a really exciting year for me as I found a topic that I really enjoy writing about and have started regularly presenting session about it (give you three guesses what that topic is 🙂 )

I’m really proud of what I’ve accomplished this year and 2018 looks to be even more exciting. I’m working on a couple of new sessions and there’s a load of great events coming up.

It still blows my mind that when I started out presenting it was just to see if I could do it (just like starting this blog was) and now I’m working on different presenting techniques to deliver the best session that I can.

Hope you all had an amazing 2017, bring 2018 on!

Thank you for reading!

0

Friday Reading 2017-10-27

Morning all! PASS Summit is coming next week but unfortunately I can’t make it this year. I hope everyone who is going has a blast, learns some cool stuff, and meets great people.

Instead I’ll be presenting at the Southampton SQL User Group next Wednesday. Looking forward to this as I love presenting about containers and have been writing new demos to show off 🙂

This week I have been reading/watching…

Understanding container standards
Video explaining all the components of containers and the standards that govern their interactions

Introducting the preview of Azure Container Services (AKS)
Azure blog announcing the preview of AKS, the new Azure managed Kubernetes service

Container Orchestration Simplified with Managed Kubernetes in Azure Container Service (AKS)
Here’s a 15 min channel 9 video giving a quick overview of AKS

Best Jenkins tutorials, videos, courses & books 2017
Nice summary of Jenkins tutorials

DockerCon Europe 2017 Videos
All the sessions from last week’s DockerCon

Have a good weekend!

3

Enabling the SQL Server Agent in Linux Containers

EDIT 3 – November 2019 – There is an environment variable that can now be used to enable the SQL Server Agent, MSSQL_AGENT_ENABLED

docker run -d -p 15789:1433 `
--env ACCEPT_EULA=Y `
--env SA_PASSWORD=Testing1122 `
--env MSSQL_AGENT_ENABLED=True `
--name testcontainer `
mcr.microsoft.com/mssql/server:2019-GDR1-ubuntu-16.04

A full list of environment variables available for SQL Server can be found here


EDIT 2 – May 2018 – Thanks to Jon & Russell’s comments for pointing out that as of SQL 2017 CU4 the Agent no longer needs to be installed separately, just enabled. I have updated the code here and on Github to reflect that change

There is also the option to enable the agent in the microsoft/mssql-server-linux:latest image, just run the line of code below to enable the agent from within the container.


EDIT – Feb 2017 – Justin Hartman (t) pointed out that the original code here no longer works but a fix has been provided by Microsoft. I’ve updated the image in DockerHub and the code here and in my Github repository


At SQL Saturday Holland a few weeks ago I was (surprise, surprise) chatting about containers and mentioned that I hadn’t been able to get the SQL Agent working.

Now, one of the benefits of attending SQL Saturdays is that you get to pick the brains of a lot of very clever people and luckily for me, Jan Van Humbeek (blog|twitter) was there.

Jan said that he had gotten the SQL Agent running in Linux containers so I asked if he could send on his code and he very kindly obliged.

So, the disclaimer for this blog post is that I didn’t write the code here, Jan did. All I’ve done is drop it into a dockerfile so that an image can be built. Thank you very much Jan!

Here’s the dockerfile to build an image with the Agent installed: –

# building from ubuntu
FROM ubuntu:16.04

# install curl & sudo & apt-transport-https
RUN apt-get update && apt-get install -y curl sudo && apt-get install -y apt-transport-https

# Import the public repository GPG keys
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -

# Register the Microsoft SQL Server Ubuntu repository
RUN curl https://packages.microsoft.com/config/ubuntu/16.04/mssql-server-2017.list | tee /etc/apt/sources.list.d/mssql-server.list

# update package list 
RUN apt-get update -y

# install sql server
RUN apt-get install -y mssql-server

# enable the agent
RUN sudo /opt/mssql/bin/mssql-conf set sqlagent.enabled true 

# start sql server 
CMD /opt/mssql/bin/sqlservr

N.B. – This dockerfile is also available on my Github

Ok, so I then dropped this docker into C:\docker\builds\linuxwithagent and built the image: –

docker build -t linuxwithagent C:\docker\builds\linuxwithagent

This does throw a few errors but will successfully build an image. A container can then be run: –

docker run -d -p 15789:1433 --env ACCEPT_EULA=Y --env SA_PASSWORD=Testing11@@ --name testcontainer linuxwithagent

Once the container is running, connect via SSMS and the agent will be available.

I’ve made this image available on the docker hub. You can pull it down by running: –

docker pull dbafromthecold/sqlserverlinuxagent:latest

Thanks for reading!

0

Event Organisers

Over the last few weeks I’ve spoken at a few conferences, the last of those being SQL Relay.

For those of you who aren’t familiar with SQL Relay, it’s unique in that it’s not one event. SQL Relay is five events starting on Monday in one location and then a different location in the UK every day until the Friday.

As I was speaking at four of those locations I got myself a place on a bus hired by the organisers to take them and other speakers to each of the event locations. This gave me an insight into the sheer amount of work that’s required to host such an event.

Hint, it’s a lot of work

Don’t get me wrong, I knew organising events is hard work but knowing and seeing are two completely different things! Holding one event is hard enough but five in a row? The organisers of SQL Relay are some of the most hardworking and motivated people I’ve ever met.

Best thing about them though? On the Friday, the last event, even though they were shattered beyond belief, they were smiling and joking with attendees.

So hat’s off to event organisers everywhere, you mad mad bunch 🙂

Have a good week!