0

SQL Saturday Cork 2018

This coming weekend (9th of June) is SQL Saturday Cork!

I’ve been really looking forward to this SQL Sat as there’s only one in Ireland each year. I’m not from Ireland but have been here for over 4 years now and the User Group in Dublin is where I first started speaking.

I won best lighting talk at SQL Saturday Dublin in 2016 and that pretty much convinced me that I could go on and present full length sessions. So I owe a lot to the event in Ireland and consider it my “home” event.

It’s the first time that the event has been held in Cork and it looks to be a cracker. The venue is at University College Cork and there’s a great line-up of speakers (myself included 🙂 )

There’s also two fantastic pre-cons on the Friday before: –

Move your database to the Cloud
by Jose Manuel Jurado & Roberto Cavalcanti

50 Things All SQL Server Developers Need to Know
by Kevin Kline

So if you’re based in Ireland (or even if you’re not) I highly recommend that you come for a weekend of fantastic SQL Server training.

Hope to see you there!

0

Deploying Azure Container Instances

In a previous post I went through how to Push an image to the Azure Container Registry

Now let’s look at using that image to create an Azure Container Image instance.

Azure Container Instances (ACI) are Microsoft’s serverless container technology. They allow us to spin up a container without having to manage the underlying infrastructure (VMs etc). Let’s run through spinning up an ACI now.

First off, I’ll be using the image I pushed up in my previous post. If you haven’t run through doing that, the link is here

OK, so let’s log in to Azure (using the Azure-CLI on Windows Subsystem for Linux): –

az login

In order to store credentials that can be used to access our Azure Container Registry and pull the container image, we first need to create a key vault: –

az keyvault create --resource-group apcontainers1 --name apkeyvault1

Now that the vault is created, we create a service principal and store its credentials in the vault: –

az keyvault secret set \
  --vault-name apkeyvault1 \
  --name ApContainerRegistry01-pull-pwd \
  --value $(az ad sp create-for-rbac \
                --name ApContainerRegistry01-pull \
                --scopes $(az acr show --name ApContainerRegistry01 --query id --output tsv) \
                --role reader \
                --query password \
                --output tsv)

Then we grab the service principal’s appId which will be the username passed to the Azure Container Registry: –

az keyvault secret set \
    --vault-name apkeyvault1 \
    --name ApContainerRegistry01-pull-usr \
    --value $(az ad sp show --id http://ApContainerRegistry01-pull --query appId --output tsv)

Great stuff. Now let’s confirm the repositories in our Azure Container Registry: –

az acr repository list --name apcontainerregistry01 --output table

We have the image that was pushed up to the ACR in my last post, so let’s deploy that to an Azure Container Instance: –

az container create \
    --resource-group apcontainers1 \
    --image apcontainerregistry01.azurecr.io/sqlserverlinuxagent:latest \
    --registry-login-server apcontainerregistry01.azurecr.io \
    --registry-username $(az keyvault secret show --vault-name apkeyvault1 -n ApContainerRegistry01-pull-usr --query value -o tsv) \
    --registry-password $(az keyvault secret show --vault-name apkeyvault1 -n ApContainerRegistry01-pull-pwd --query value -o tsv) \
    --name testcontainer1 \
    --cpu 2 --memory 4 \
    --environment-variables ACCEPT_EULA=Y SA_PASSWORD=Testing1122 \
    --ip-address public \
    --ports 1433

The code should be fairly self explanatory. I’m using the username and password created earlier to access the ACR and am then spin up a container from the sqlserverlinuxagent:latest image. The container has 2 CPUs and 4GB of memory available to it and it will be listening on a public IP address on port 1433 (be very careful with this).

At the time of writing, the only option available for ip-address is public, hopefully further options will be available soon. I will update this blog if/when that happens.

OK, let’s grab the container details: –

az container show --name testcontainer1 --resource-group apcontainers1

Once the provisioning state is succeeded and there’s an IP address, we are good to go.

If you want to view the logs of the container: –

az container logs --name testcontainer1 --resource-group apcontainers1

And we can also remote into the container: –

az container exec --resource-group apcontainers1 --name testcontainer1 --exec-command bash

Finally, to clean-up (delete the container): –

az container delete --name testcontainer1 --resource-group apcontainers1

So, that’s how to deploy a custom container image from the Azure Container Registry to an Azure Container Instance.

Thanks for reading!

0

Comparing two SQL instances

Last week I was working on a couple of SQL instances that contained databases which were part of an Always On availability group, with one server being the primary and the other the secondary.

I needed to make sure that the secondary had all the databases that were on the primary (darn you auto seeding!!). The problem was that these instances had over 200 databases which meant checking them was no simple task.

Now I know there’s a few ways to do this but the method I used is a simple & quick way of comparing databases on two SQL instances using the powershell cmdlet

Compare-Object

What this does is pretty much what it says on the tin. The cmdlet takes two objects and compares them based on a input property (in this case it’ll be database names).

Here’s the code: –

$InstanceA = ''
$InstanceB = ''

$SqlQuery = 'SELECT name FROM sys.databases'

$DatabasesA = Invoke-SqlCmd2 -sqlinstance $InstanceA -query $SqlQuery

$DatabasesB = Invoke-SqlCmd2 -SqlInstance $InstanceB -query $SqlQuery

Compare-Object -ReferenceObject $DatabasesA -DifferenceObject $DatabasesB -Property "name"

Let’s run a quick test to show the output. Say I have two SQL instances with the following databases: –

If I run the script, I will get the following: –

This is telling me that Databases C & G exist in Instance B but not Instance A and that Databases E & I exist in Instance A but not in Instance B.

The script runs very quickly (even with a large amount of dbs) and gives a nice, easy to understand output that allowed me to work out which databases needed to be reseeded in my AG.

Of course, the $SqlQuery variable can be changed so that it can return other properties of the instances for comparison so this really is a nice way to compare two SQL instances.

Thanks for reading!

2

Pushing SQL Server images to the Azure Container Registry

The Azure Container Registry is an online repository for storing Docker images (think the Docker Hub).

What’s cool about this is that we can store our images in the same data centre as our deployments, so spinning up containers from the images should be pretty quick. Let’s run through setting up a Registry and pushing an image to it.

But first things first, a quick terminology reminder 🙂

Registry – this is a remote service that will store all our images
Repository – this is a collection of images

Cool, let’s run through setting up a Registry and pushing an image to it. I’ll be using the Azure-CLI and VS Code with the Azure-CLI plugin. However, I’ll be using a powershell terminal within VS Code. This is because I want to access Docker on my Windows 10 machine, so that I can push an image up to the ACR.

First thing to do is check that the azure-cli is installed: –

az --version

N.B. – You can install it from here if you don’t already have it

Then we need to log in to azure: –

az login

N.B. – You can specify a username and password with this command HOWEVER it doesn’t work for accounts with 2 factor authentication (I mean…really)

Anyway…now we can create a resource group for our registry: –

az group create --name apcontainers1 --location westus2

Then we can create the registry: –

az acr create --resource-group apcontainers1 --name ApContainerRegistry01 --sku Basic

I’m setting this up with the Basic SKU (as this is a demo). You can read more about the Registry SKU levels here

In order to be able to push to the registry, we need to log in: –

az acr login --name ApContainerRegistry01

And we also need to get the login server of the registry: –

az acr list --resource-group apcontainers1 --query "[].{acrLoginServer:loginServer}" --output table

N.B. – save the output of this command.

OK, now let’s look locally for an image that we want to push to our ACR: –

docker images

I’m going to push my custom dbafromthecold/sqlserverlinuxagent image. It’s a public image so if you want to use it, just run: –

docker pull dbafromthecold/sqlserverlinuxagent:latest

So similar to pushing to the Docker hub, we need to tag the image with the login server name that we retrieved a couple of commands ago and the name of the image: –

docker tag dbafromthecold/sqlserverlinuxagent apcontainerregistry01.azurecr.io/sqlserverlinuxagent:latest

We can see the new tag locally by running: –

docker images

Cool! Ok, so now we can push to the ACR: –

docker push apcontainerregistry01.azurecr.io/sqlserverlinuxagent:latest

And then confirm that the image is there: –

az acr repository list --name apcontainerregistry01 --output table

What this has done is create a repository called sqlserverlinuxagent with our image tagged underneath it. To see the image run: –

az acr repository show-tags --name apcontainerregistry01 --repository sqlserverlinuxagent

So we have a repository called sqlserverlinuxagent with one image tagged as latest underneath it.

Awesome, now that the image is there we can use it to deploy an Azure Container Instance. I’ll cover how to do that in my next post 🙂

To clean up, we delete the repository: –

az acr repository delete --name ApContainerRegistry01 --repository sqlserverlinuxagent

Oh, if you want to delete the registry…

az acr delete --name apcontainerregistry01

And a more nuclear option (which will delete the resource group): –

az group delete --name apcontainers1

Thanks for reading!

0

Loopback available for Windows Containers

The April 2018 update for Windows brought a few cool things but the best one (imho) is that now we can now connect to Windows containers locally using ‘localhost’ and the port specified upon container runtime.

Let’s have a look at how this works.

First, spin up a container:-

docker run -d -p 15789:1433 `
    --env ACCEPT_EULA=Y `
        --env SA_PASSWORD=Testing1122 `
            --name testcontainer `
                microsoft/mssql-server-windows-developer:latest

Previously, if we wanted to connect to the container locally, we would have had to grab its Private IP by running: –

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' testcontainer

Not any more though! We can now use ‘localhost’ and the port number.

This update was available for a while in the Windows 10 insiders track but it’s now gone GA. Pretty cool as this functionality has been around for Linux containers since, well, forever. 🙂

Thanks for reading!