It's about open source softwares

Wednesday, May 27, 2015

Docker on Windows

      Day by day, Docker becoming very famous as it changes the process of building, deployment, shipping of applications with the help of operating system level virtualization technology called containers

Docker, What it is? 


      Docker is an open source projects that automates the deployment of applications inside containers. It uses operating system virtualization technology. It uses linux kernel features like resource isolation CGROUPS and kernel namespaces.

      Docker container is a virtual environment running on host operating system similar to virtual machines. The main difference between traditional virtual machine and container is that they all shares host machine kernel instead of having separate one for each container. Because container doesn't have their own kernel its very lightweight virtual machines.

This post describe the installation process of docker on windows using Boot2Docker.
   

Boot2Docker


Docker uses Linux kernel specific features. So that we required lightweight virtual machine to run docker on windows. Boot2Docker is nothing but lightweight linux distribution which is used for docker container on windows.
It runs in approx. 27MB RAM and boot in approx. 5 Sec.
Boot2Docker internals
Here, we are going to use docker on windows operating system, but internally it running docker engine on linux virtual machine(Boot2Docker). On Docker server we build, run, stop or download containers. We are only accessing or communicating with docker server using windows docker clients.

Installation


1. Download latest installer of boot2docker for windows from https://github.com/boot2docker/windows-installer/releases/latest

2. Click and run installer

3. Installer installs windows docker client, Boot2Docker management tools and ISO, virtualBox, MSYS-git Unix tools.
    If virtual box and MSYS-git unix tools were already installed on your machine then installer doesn't install it.  

4. Select check box for desktop icon creation and adding docker executable to environment variables (PATH) 


5. Complete the installation by clicking finish button


6. Start boot2docker by clicking desktop shortcut or using program files -> Boot2Docker for windows.
    This will starts the docker engine and set the environment variables. 
    You can check the docker version by running "docker version" command. 



You can also see Boot2docker VM running in VirtualBox. 


Run "hello-world" docker container


Boot2Docker installer provides us "hello-world" image which only prints the hello message when we run container to verify the installation. You can run your first container simply typing "docker run" command
$ docker run hello-world


You have successfully installed the boot2docker on windows and ready to use docker. 

Using docker from powershell or windows cmd


For using docker from powershell or CMD, we have to add the .ssh in its PATH variables and set some docker variables like DOCKER_HOST, DOCKER_CERT_PATH and DOCKER_TLS_VERIFY



Useful Resource 

NOTE:
Recently, Microsoft announces new container technology as well as nano servers for azure cloud and windows servers to run cloud applications and containers.


Share:

Thursday, May 21, 2015

Provision SQL server with powershell on azure

In this post I am explaining steps to provision SQL server on Azure cloud using powershell. Am also going to explain how to access newly provisioned SQL server using SQL Server Management Studio(SSMS). 

Provisioning SQL Server


Provisioning of SQL server mainly consists of below high level steps:
  • Configure connection between workstation and Azure account
  • Create SQL server VM in Azure
  • Add endpoint to SQL Server to listen on the TCP protocol
  • Add firewall rule to SQL server
  • Enable MIXED mode authentication and restart SQL Server instance
  • Create a SQL Server administrator login

Configure connection between workstation and Azure account


Firstly, we have to set a connection from workstation to Azure account by configuring the credentials and subscriptions. Refer https://msdn.microsoft.com/en-us/library/dn385850%28v=nav.70%29.aspx for creating and importing a publish settings file.  
Following code configures the workstation, along with newly created storage account.
# Parameters used for script
$serviceName="SQLServer"
$password="P@ssw0rd"
$StorageAccName = $serviceName -replace '-' | %{"${_}strg"}

# Add publish setting file
$PublishFile = "<Publish Setting File>"
Import-AzurePublishSettingsFile $PublishFile

# create storage account 
if (!(Test-AzureName -Storage $StorageAccName))
{  
    Write-Host "Creating Storage Account $StorageAccName"
    New-AzureStorageAccount -StorageAccountName $StorageAccName -Location "East US"
} 

# If you have multiple subscriptions in Azure account then select one of the as default  
Set-AzureSubscription -SubscriptionName "<subscription Name>" -CurrentStorageAccount $StorageAccName
Get-AzureSubscription

Create SQL server VM in Azure


After configuring the workstation, next step will be creating a SQL server using powershell scripts. Below code snippet gets the latest SQL server 2012 image and creates SQL server VM in Azure.  
    
    # Function to get latest image by name  
    function GetLatestImage
    {
       param($imageFamily)
       $images = Get-AzureVMImage | where { $_.ImageFamily -eq $imageFamily } | Sort-Object -Descending -Property PublishedDate
       return $images[0].ImageName
    }

    # parameters
    $Image = (GetLatestImage "SQL Server 2012 SP2 Enterprise on Windows Server 2012") 
    $UserName = "$serviceName-admin"
    $VMName = $serviceName

    # create Azure service   
    if (!(Test-AzureName -Service $serviceName))
    {   Write-Host "Creating $serviceName service"
        New-AzureService -ServiceName $serviceName -Label $VMName -Location "East US"
    }

    # create VM
    Write-Host "Creating SQL server machine $VMName"
    New-AzureVMConfig -Name $VMName -InstanceSize Large -ImageName $Image | 
    Add-AzureProvisioningConfig -Windows -Password $password -AdminUsername $UserName | 
    New-AzureVM –ServiceName $serviceName -WaitForBoot

Add endpoint to SQL Server to listen on the TCP protocol and start VM


Once machine got provisioned in Azure we have to add or update endpoints so that we can communicate with SQL server machine present on Azure cloud. Windows server by default have endpoints for remote desktop and powershell. We have to add endpoint which connects to default database port(1433). 
# Update vm to add endpoints
Get-AzureVM -Name $VMName -ServiceName $serviceName |
Add-AzureEndpoint -Protocol tcp -LocalPort 1433 -PublicPort 1433 -Name 'MSSQL'|
Update-AzureVM

Write-Host "Starting $VMName"
Start-AzureVM -ServiceName $serviceName -Name $VMName

Install certificates required for login in to SQL VM


For executing command on Azure windows machine which will configure our SQL server, we have to install certificates on the machine. Copy the contents from http://pshscripts.blogspot.in/2015/02/install-rmazurevmcert.html and paste it in "Install-WinRmAzureVMCert.ps1" file. Save this file in the same directory where our SQL provision script is present.
# Encrypt credentials 
$secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
$AdminCredential = New-Object System.Management.Automation.PSCredential ($UserName, $secpasswd)

# Bypass security policies 
set-ExecutionPolicy -Scope CurrentUser Bypass -Force

# Install certificates
$ScriptPath = Split-Path $MyInvocation.InvocationName
Invoke-Expression "$ScriptPath\Install-WinRmAzureVMCert.ps1 -SubscriptionName '<Subscription Name>' -CloudServiceName $serviceName -VMName "$VMName"

# Get URI for SQL VM
$uri = Get-AzureWinRMUri –Service $serviceName –Name $VMName    

After installing certificates, we are going to invoke commands on newly created SQL instance. You can perform the following steps in single or multiple commands invocation.


Add Firewall rule


Invoke command on SQL server which adds the firewall rule to open default database port 1433.
Invoke-Command –ConnectionUri $uri –Credential $AdminCredential -ArgumentList $UserName, $password –ScriptBlock {
    param($UserName, $password) 
    
    Set-ExecutionPolicy Bypass
    
    # Add Firewall rule
    Write-Host "Adding firewall rule"
    netsh advfirewall firewall add rule name="SQL Server (TCP-In)" program='C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Binn\sqlservr.exe' dir=in protocol=TCP localport=1433 action=allow    
}

Configuring SQL server TCP port and Restart SQL server


Configure default SQL server instance to listen on port (1433) using TCP/IP protocol. After configuration restart the server.
Invoke-Command –ConnectionUri $uri –Credential $AdminCredential -ArgumentList $UserName, $password –ScriptBlock {
    param($UserName, $password) 
    
    Set-ExecutionPolicy Bypass
     
    Import-Module sqlps -DisableNameChecking

    # Configure the SQL Server TCP/IP protocol for the port that was configured in the endpoint
    Write-Host "Configuring SQL server TCP port"
    $TCPPort = "1433"
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement") | out-null
    $MachineObject = New-Object ('Microsoft.SqlServer.Management.Smo.WMI.ManagedComputer') .
    $tcp = $MachineObject.GetSMOObject("ManagedComputer[@Name='" + (Get-Item env:\computername).Value + "']/ServerInstance[@Name='MSSQLSERVER']/ServerProtocol[@Name='Tcp']")
    if ($tcp.IsEnabled -ne "True")
    {
        $tcp.IsEnabled = $true
        $tcp.alter()
        $MachineObject.GetSMOObject($tcp.urn.Value + "/IPAddress[@Name='IPAll']").IPAddressProperties[1].Value = $TCPPort
        $tcp.alter()
    }
    else
    {
        $MachineObject.GetSMOObject($tcp.urn.Value + "/IPAddress[@Name='IPAll']").IPAddressProperties[1].Value = $TCPPort
        $tcp.alter()
    }

    # Stop and start the SQL Server instance
    Write-Host "Restarting SQL server" 
    $Mssqlserver = $MachineObject.Services['MSSQLSERVER']
    $Mssqlserver.Stop()
    start-sleep -s 60
    $Mssqlserver.Start()
    start-sleep -s 60
} 


Enable MIXED mode authentication on SQL Server instance


We didn't setup any domain controller in Azure environment. So we are not able to connect SQL server from other machine using windows authentication mode. For connecting on premise installation we need SQL server configured in mixed mode(SQL server Authentication). 
    
    Invoke-Command –ConnectionUri $uri –Credential $AdminCredential -ArgumentList $UserName, $password –ScriptBlock {
        param($UserName, $password) 
    
        Set-ExecutionPolicy Bypass
     
        Import-Module sqlps -DisableNameChecking
        
        # Enabled MIXED mode authentication for the SQL Server instance
        Write-Host "Enabling mixed mode authentication"
        $LoginObject = New-Object ('Microsoft.SqlServer.Management.Smo.Server') $env:COMPUTERNAME
        $LoginObject.Settings.LoginMode = [Microsoft.SqlServer.Management.SMO.ServerLoginMode]::Mixed
        $LoginObject.Settings.Alter()

        # Stop and start the SQL Server instance 
        Write-Host "Restarting SQL server"
        $Mssqlserver.Stop()
        start-sleep -s 60
        $Mssqlserver.Start()
        start-sleep -s 60
    }

Create a SQL Server administrator login


At last, create a SQL authenticated user which is used to login into SQL instance from SQL Server Management Studio. 
      Username : "SQLServer-admin" 
      Password : "P@ssw0rd"
Note: SQL Server Azure VM and database username and password are same in this tutorial. Here, I have append "-admin" to $serviceName variable. You can change it to whatever you want.
Invoke-Command –ConnectionUri $uri –Credential $AdminCredential -ArgumentList $UserName, $password –ScriptBlock {
    param($UserName, $password) 
  
    Set-ExecutionPolicy Bypass
     
    Import-Module sqlps -DisableNameChecking

    $DatabaseUsername = $UserName
    $DatabasePassword = $password

    # Create SQL admin Login
    Write-Host "Creating SQL admin Login"
    Invoke-SqlCmd -ServerInstance $env:COMPUTERNAME -Database "master" -Query `
    "
        USE [master]
        GO
        IF Not EXISTS (SELECT name FROM master.sys.server_principals WHERE name = '$DatabaseUsername')
        BEGIN
         CREATE LOGIN [$DatabaseUsername] WITH PASSWORD='$DatabasePassword'
         EXEC sp_addsrvrolemember '$DatabaseUsername', 'sysadmin'
            EXEC sp_addsrvrolemember '$DatabaseUsername', 'dbcreator'
         EXEC sp_addsrvrolemember '$DatabaseUsername', 'securityadmin'
        END
        "
}   

SQL server machine is now setup in mentioned Azure subscription.


Connect to SQL server through SQL SERVER MANAGEMENT STUDIO


Open SQL Server Management Studio and login to SQL server using "SQL Server Authentication" mode.

Login Details:
         Server Name: "<Service Name>.cloudapp.net"
         Port              : 1433
         Login            : "<Service Name>-admin"
         Password      : "P@ssw0rd"
  


I hope, you successfully created SQL server on your Azure subscription. Suggestions and comments about this post are always welcome.

Share:

Wednesday, May 6, 2015

Execute Bash commands with Powershell on Azure VM



Recently, I had to provision and configure Linux machine through powershell on Azure cloud. We can create the linux machine very easily using powershell script. But i am more interested in configuring linux machine through powershell. For this i have to somehow SSH into linux machine to execute the bash scripts and commands.

There is module based on SSH.NET library for powershell which do SSH in to linux machine and then executes commands through powershell commands.

In this post, i am writing about installation and usage of SSH-Session module of powershell.

Installation



2. Firstly unblock the zip file (go to file properties), then unzip and place unzipped folder in one of PowerShell modules folders. You can find the module folders in powershell using following command,

PS C:\> $env:PSModulePath -split ';'
C:\Users\rahul\Documents\WindowsPowerShell\Modules
C:\Program Files (x86)\WindowsPowerShell\Modules
C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ServiceManagement

3. After unzipping the folders in powershell module folder import SSH-Sessions module.
 
PS C:\> Import-Module SSH-Sessions
VERBOSE: Loading module from path
'C:\Users\rahul\Documents\WindowsPowerShell\Modules\SS
VERBOSE: Loading 'Assembly' from path
'C:\Users\rahul\Documents\WindowsPowerShell\Modules\SS
VERBOSE: Loading module from path
'C:\Users\rahul\Documents\WindowsPowerShell\Modules\SS
VERBOSE: Exporting function 'ConvertFrom-SecureToPlain'.
VERBOSE: Exporting function 'New-SshSession'.
VERBOSE: Exporting function 'Invoke-SshCommand'.
VERBOSE: Exporting function 'Enter-SshSession'.
VERBOSE: Exporting function 'Remove-SshSession'.
VERBOSE: Exporting function 'Get-SshSession'.
VERBOSE: Importing function 'ConvertFrom-SecureToPlain'.
VERBOSE: Importing function 'Enter-SshSession'.
VERBOSE: Importing function 'Get-SshSession'.
VERBOSE: Importing function 'Invoke-SshCommand'.
VERBOSE: Importing function 'New-SshSession'.
VERBOSE: Importing function 'Remove-SshSession'.

3. Confirm the installation

PS C:\> Get-Command -Module SSH-Sessions
CommandType     Name                                               ModuleName
-----------               ----                                                    ----------
Function               ConvertFrom-SecureToPlain             SSH-Sessions
Function               Enter-SshSession                               SSH-Sessions
Function               Get-SshSession                                  SSH-Sessions
Function               Invoke-SshCommand                        SSH-Sessions
Function               New-SshSession                                SSH-Sessions
Function               Remove-SshSession                          SSH-Sessions

Usage

  • First we have to setup a ssh session with linux server using New-SshSession command
PS C:\> New-SshSession -ComputerName rk-ubuntu.cloudapp.net -Username rahul -Password "test123"
Successfully connected to rk-ubuntu.cloudapp.net

  • Get the SSH sessions using Get-SshSession command
PS C:\> Get-SshSession
ComputerName                                                                                        Connected
------------                                                                                                  ---------
rk-ubuntu.cloudapp.net                                                                             True
  • Execute the commands using Invoke-SshCommand command. Here i am executing commands to see kernel information and get hostname of linux machine.
PS C:\> Invoke-SshCommand -ComputerName rk-ubuntu.cloudapp.net -Command 'uname -a' -q
Linux rk-ubuntu 2 3.2.0-69-virtual #103-Ubuntu SMP Tue Sep 2 05:21:29 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux 
PS C:\> Invoke-SshCommand -ComputerName rk-ubuntu.cloudapp.net -Command 'hostname' -q
rk-ubuntu

  • Interactive SSH session using Enter-SshSession command
  PS C:> Enter-SshSession -ComputerName rk-ubuntu.cloudapp.net
[rk-ubuntu] rahul # : cat /etc/*-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04.2 LTS"
NAME="Ubuntu"
VERSION="14.04.2 LTS, Trusty Tahr"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 14.04.2 LTS"
VERSION_ID="14.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
[rk-ubuntu] rahul # : exit

  • Remove SSH session using Remove-SshSession command 
PS C:\> Remove-SshSession -ComputerName rk-ubuntu.cloudapp.net 
PS C:\> Get-SshSession
No connections found

Hope this article is helpful for you guys. 
Comments and suggestions are welcome.


Share: