4

Connecting a powershell session to a container

One of the questions that I was asked at SQL Saturday Iceland was “how can I view the filesystem within a container?”.

This is a great question as one of the things that people find off-putting about containers is their opaqueness. It’s not obvious where everything lives within the container or how we can view the files within it.

Thankfully there’s a simple docker command that allows us to open a powershell session within a container, that command is docker exec.

Let’s run through an example. Firstly I’m going to spin up a new container running an instance of SQL Server: –

docker run -d -p 15777:1433 --env ACCEPT_EULA=Y --env sa_password=Testing11@@ --name testcontainer microsoft/mssql-server-windows

Then verify that the container is running: –

docker ps

Now, to open up the powershell session inside run: –

docker exec -it testcontainer powershell

N.B. – the -i flag keeps the powershell session open, otherwise it will immediately exit. The -t flag creates a pseudo terminal, you don’t have to include it tbh but the examples I’ve seen online always include it.

Ok, we know have a powershell session within the container and we can execute commands as normal, for example: –

get-psdrive

This will allow us to explore what the filesystem is within the container and run powershell commands as needed. In the screenshot above it’s interesting to see the space available/used on the C: drive. Have a dig around within your containers and let me know if you find anything interesting 🙂

Thanks for reading!

0

Monday Coffee 2017-04-03

Last week Pass announced changes to how speakers are going to be selected for their annual conference (you can read the announcement here.

There’s been a bit of a debate about the changes but as far as I can tell, these seem to be for the better (right?).

Inviting speakers with a proven track record of delivering high-quality presentations can only be a good thing and guaranteeing that a (albeit) small number of new speakers will be there will hopefully prevent presenting at Pass Summit becoming only available to a certain circle of people.

I’ve only been to the event once and I have to say, I found quite a few of the sessions there to be below-par. Don’t get me wrong, I also saw a few great sessions but if these changes improve the overall quality then I say that they are a good thing. Of course, it’s all about the execution so only time will tell if these make the expected improvements but I think Pass are doing this right.

They’ve asked for feedback here and have organised two online meetings so that we, as the community, have the chance to speak directly to them and voice any concerns that we may have.

Have a good week.

0

Friday Reading 2017-03-31

SQLBits is only a week away! But before that I’ve been reading…

Backing up SQL Server on Linux using Ola Hallengrens Maintenance Solution
Rob goes through how to use Ola Hallengren’s scripts to back up databases in SQL Server on Linux (now that the agent is supported)

Setting the default backup directory for SQL Server on Linux
Slava Murygin shows us three steps to set the default backup directory for an instance of SQL Server running on Linux

Top 5 Questions about Basic Availability Groups
Pieter Vanhove goes through 5 common questions about AGs.

What the heck is a DTU?
Good post explaining what the performance metric for Azure SQL DB is

Have a good weekend!

1

Using SQL Client Aliases

SQLServerCentral.com recently published my case study on how my company implemented Windows Containers running SQL Server to streamline our QA setup process. If you haven’t seen it, it’s here.

One of the problems that we ran into when moving to using containers was how to get the applications to connect. Let me explain the situation.

The applications in our production environment use DNS CNAME aliases that reference the production SQL instance’s IP address. In our old QA environment, the applications and SQL instance lived on the same virtual server so the DNS aliases were overwritten by host file entries that would point to 127.0.0.1.

This caused us a problem when moving to containers as the containers were on a separate server listening on a custom tcp port. Port numbers cannot be specified in DNS aliases or host file entries and we couldn’t update the application string (one of the pre-requisites of the project) so we were pretty stuck until we realised that we could use SQL client aliases.

The client aliases would allow us to replicate the DNS aliases that we had in production and we wouldn’t have to change the app connection string.

Setting up aliases is simple enough if you have the SQL configuration manager installed. Simply navigate down to the SQL Native Client section within the config manager, right click and fill out the name, server IP and port number: –

N.B. – I always do both 64 and 32-bit sections to be safe

But we didn’t want to install the config manager on the QA servers and also, that’s a very manual process. So we scripted it out.

First thing to do was get the SQL Native Client installed on all the VMs. The way we did this was to install it on one and then create a VM template that all the other servers were built from. The SQL Native Client installer can be downloaded from here. (that’s for SQL 2012 btw as that’s the version of SQL that we worked with).

Once that was done we then ran a powershell script to create the aliases directly in the registry of the server. Here’s a sample of the script: –

# First of all create the registry keys to hold the aliases
New-Item -Path "HKLM:\Software\Microsoft\MSSQLServer\Client\" -Name ConnectTo -Force -ErrorAction Stop | Out-Null
New-Item -Path "HKLM:\Software\Wow6432Node\Microsoft\MSSQLServer\Client\" -Name ConnectTo -Force -ErrorAction Stop  | Out-Null

# Then create the aliases
$ServerName = "192.168.1.7";
$dbPort     = "10010";
$TCPAlias   = "DBMSSOCN," + $ServerName + "," + $dbPort

$Aliases = @("Alias1","Alias2")

foreach($Alias in $Aliases)
{
    New-ItemProperty -Path "HKLM:\Software\Microsoft\MSSQLServer\Client\ConnectTo\" -Name $Alias -PropertyType String -Value $TCPAlias -ErrorAction Stop | Out-Null
    New-ItemProperty -Path "HKLM:\Software\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo\" -Name $Alias -PropertyType String -Value $TCPAlias -ErrorAction Stop | Out-Null
}

Obviously we wrapped the script up in a load of try…catch blocks to make sure that if the keys were already there then it would drop & re-create but the core of what it does is above. What the script above will do is create two aliases that map to server 192.168.1.7 on port 10010: –

This allowed us to move the apps from using local instances of SQL to using containers listening on a custom port on a remote server without having to change anything in the app configuration. This reduced our server setup time significantly and freed up resources on the app server. Also, we could control what container the apps were pointing to via the registry entries! Very handy.

Thanks for reading, let me know if you have any questions.

0

Monday Coffee 2017-03-27

Hey all, I’m back after attending SQL Saturday in Iceland. This week’s going to be busy!

I’ve said before that SQL Saturday Iceland was the first time that I presented a full hour session. It seemed to go well although having it start at 14:50 wasn’t the best as I was kinda nervous so I stressed out for most of the day! But done now and I was pretty pleased with it as I finished on time whilst getting all my material in and I answered all the questions.

I don’t really want to write an “Advice for first time presenters” post as there are plenty of them out there but I will say a couple of things that really helped me. First one, don’t wing it. I made notes on each of the slides that I had in my deck and worked through them one by one to make sure I got everything in that I wanted to say. I then grabbed a meeting room after hours in work and went through it over and over, making sure that it flowed nicely and that I got used to referring to slides whilst talking.

Also, my demos would have required a lot of typing and parts that required things like server bounces which would have taken too long to do live so I invested in decent video editing software and it paid for itself imho. The demos worked well and I was able to talk whilst they were playing, something that I don’t think that I’d have been able to do if I was doing everything live.

So all in all, really chuffed that I’ve done it and it’s now onto SQL Bits!

Final word, Iceland is an absolute amazing country. I hired a car and went driving round for a couple of days and the scenery is nothing sort of spectacular. My advice, if you have an chance, go.

Have a good week!