Sunday, May 14, 2017

Kubernetes basics : windows cluster in azure

Kubernetes:Container orchestration engine 


1.     Pods: Its a collection of containers or processes. Containers within a pod share an IP address and port space and can talk to each other through localhost. Its the basic unit of deployment in kubernetes as in a container is the basic unit of deployment in Docker. Each Pod as unique IP Address
a.      Ex: Container for sync files and another container for serving the files can be part of single pod
b.     It can contain only container

2.     Replication controller or Replication Sets: This make sure the specified number of pods are running at any one time and also make sure the system is in the desired state.

Ex: if one of the pod is down. controller will create a new pod and the pods are load balanced  

3.     Deployments: It contains the details of the pods and the replication controller It will deploy the replica sets which will deploy pods and in turn deploy container.

4.     Services: Its a group of pods that can be accessed as a single unit. If one of the pod goes down. a new a pod is created and user has to contact only the service and not to the pods directly

5.     Persistent Volumes: for users and administrators that abstracts details of how storage is provided from how it is consumed

6.     Config Maps: Applications require configuration via some combination of config files, command line arguments and environment variables


Creating kubernetes windows cluster in azure 

Install AzureCLI-2.0

Create a resource group
az group create --name=myKubernetesResourceGroup --location=westus

Create the Cluster
az acs create --orchestrator-type=kubernetes --resource-group myKubernetesResourceGroup --name=myKubernetesClusterName --dns-prefix=myPrefix --agent-count=2 --generate-ssh-keys --windows --admin-
username girish --admin-password basava123#

Install kubectl 
az acs kubernetes install-cli

Get the config file
az acs kubernetes get-credentials --resource-group=myKubernetesResourceGroup --name=myKubernetesClusterName



Create a Json file for creating a Pod

{

 "apiVersion": "v1",

 "kind": "Pod",

 "metadata": {

   "name": "iis",

   "labels": {

     "name": "iis"

   }

 },

 "spec": {

   "containers": [

     {

       "name": "iis",

       "image": "microsoft/iis",

       "ports": [

         {

         "containerPort": 80

         }

       ]

     }

   ],

   "nodeSelector": {

     "beta.kubernetes.io/os": "windows"

   }

 }

}


Create a pods

   kubectl apply -f C:\Users\girish\Desktop\iis.json


Get the Pods

kubectl get pods

To expose the container to the world,Run the below command which will create a service and a azure load balancer

kubectl expose pods iis --port=80 --type=LoadBalancer

To see the service

kubectl get svc

Sunday, May 7, 2017

VSTS: Get the team current sprint name from REST API

The following powershell script will get the current sprint name of the team in a project, we can use the same or other REST API in VSTS pipeline.



Param
(
   [string] $vstsAccount = "girishgoudar",
   [string] $projectName = "PritiX.Assignment.Exam.Demo",
   [string] $teamName = "PritiX.Assignment.Exam.Demo%20Team",
   [string] $token = "icww3qhmyrcbdgs3zeuwgcss24pk5tbsuni3zmkfhxugdkv3i63q" #personal Access token PAT
 
)

$results = @{}

if(!$vstsAccount.ToLower().Contains(".visualstudio.com")) {$vstsAccount+=".visualstudio.com"}
if(!$vstsAccount.ToLower().StartsWith("https://")) {$vstsAccount = "https://"+$vstsAccount}
if(!$vstsAccount.EndsWith("/")) {$vstsAccount+="/"}

# Only current iteration of the team

$uri = "$($vstsAccount)DefaultCollection/$($projectName)/$($teamName)/_apis/work/TeamSettings/Iterations?`$timeframe=current&api-version=2.0"

# Calls VSTS REST API

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
$results = Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

# Test for results
if ($results.count -eq 0)
{
     throw "Results not found"
}

Write-Host $results.value[0].name
Write-Host $results.value[0].url

Saturday, May 6, 2017

Create pester test to check the status of the web site

The following powershell script will run the pester test and the test will pass when the website is up and running and fail when the web site state is other than started

We can also include this test as part of the pipeline in VSTS and publish the test results 


Param(
        [string]$computerName,
        [string]$webSiteName
    )
    describe ‘Check Web Site Status’ {
    
    #$secpasswd = ConvertTo-SecureString “PlainTextPassword” -AsPlainText -Force
    #$mycreds = New-Object System.Management.Automation.PSCredential (“username”, $secpasswd)
    $credentials = Get-Credential
     it ‘Checks if the web site is in started state after deployment’ {
        
        Invoke-Command -ComputerName $computerName -ScriptBlock {
            Import-Module WebAdministration;
            $webSite = Get-Website -Name $webSiteName
            if($webSite.state -eq "Started")
            {
                return $true
            }
            else
            {
                return $false
            }
        } -Credential $credentials | Should Be $true

     }
    
 }