Wednesday, June 21, 2017

Azure CLI Commands to create windows Kubernetes cluster and run windows container using json file


Refer the previous two blogs for details of the Kubernetes and the commands mentioned here



1.    az group create --name=myKubernetesWindowsResourceGroup --location="Southeast Asia"

2.    az acs create --orchestrator-type=kubernetes --resource-group myKubernetesWindowsResourceGroup --name=myKubernetesClusterName --dns-prefix=mydnsPrefix --agent-count=2 --generate-ssh-keys --windows --admin-username girish --admin-password basava123#

3.    az acs kubernetes install-cli

4.    PS C:\Program Files (x86)> az acs kubernetes get-credentials --resource-group=myKubernetesWindowsResourceGroup --name=myKubernetesWindowsCluster

5.    PS C:\Program Files (x86)>  .\kubectl.exe get pods

6.    PS C:\Program Files (x86)> .\kubectl.exe  create secret docker-registry myregistrykey --docker-server=https://acrgirish.
azurecr.io --docker-username=ACRgirish --docker-password=K7/n0/8r9zt9AIg+JCftoC6Dyzrg=4rf --docker-email=ANY_EMAIL_ADDRESS
7.    PS C:\Program Files (x86)> .\kubectl.exe create -f C:\Users\aditi\Desktop\K8\Deployment.json

8.    PS C:\Program Files (x86)> .\kubectl.exe create -f C:\Users\aditi\Desktop\K8\Service.json




Service.json file
{
    "apiVersion": "v1",
    "kind": "Service",
    "metadata": {
        "name": "musicstoreapp"
    },
    "spec": {
        "selector": {
            "app": "musicstoreapp"
        },
"type": "LoadBalancer",
        "ports": [{
            "port": 80
        }]
    }
}

Deployment.json file
{
    "apiVersion": "extensions/v1beta1",
    "kind": "Deployment",
    "metadata": {
        "name": "musicstoreapp"
    },
    "spec": {
        "replicas": 2,
        "template": {
            "metadata": {
                "labels": {
                    "app": "musicstoreapp"
                }
            },
            "spec": {
                "containers": [{
                    "name": "musicstore",
                    
                    "image": "acrgirish.azurecr.io/mvcmusicstore",
                    "ports": [{
                        "containerPort": 80
                    }]
                }],
                "imagePullSecrets": [{
                        "name": "myregistrykey"
                }]
            }
        }
    }

Creating the Kuberenetes cluster on Azure and running dotnet core containers

Install the azure cli 2.0 and run the following commands in prowershell

az login  # login to azure

az group create --name="LinuxACSResource" --location="Southeast Asia" # Create a resource group

az acs create --orchestrator-type=kubernetes --resource-group "LinuxACSResource" --name="cluster
name" --dns-prefix="dnsprefix" --generate-ssh-keys # Create a Kubernetes cluster

az acs kubernetes install-cli # Install kube ctl

az acs kubernetes get-credentials --resource-group="LinuxACSResource" --name="clustername"
#  Get the kubernetes config file

PS C:\Program Files (x86)> .\kubectl.exe get nodes #  Get the kubernetes nodes

PS C:\Program Files (x86)> .\kubectl.exe run nginx --image nginx #  Run the Nginx image

PS C:\Program Files (x86)> .\kubectl.exe get pods #  Get the Pods

PS C:\Program Files (x86)> .\kubectl.exe expose deployments nginx --port=80 --type=LoadBalancer #  Run a service

PS C:\Program Files (x86)> .\kubectl.exe get svc #  Get a service

Run the dotnet core container

docker login

docker login acrgirish.azurecr.io -u ACRgirish -p K7/n0/8r9zt9AIg+JCftoC6Dyzrg=4rf

docker tag corewebapp:dev acrgirish.azurecr.io/app

docker push acrgirish.azurecr.io/app

PS C:\Program Files (x86)> .\kubectl.exe create -f C:\Users\aditi\Desktop\K8\Deployment.yaml

PS C:\Program Files (x86)> .\kubectl.exe get pods

PS C:\Program Files (x86)> .\kubectl.exe create -f C:\Users\aditi\Desktop\K8\Service.yaml


//dockerfile

FROM microsoft/aspnetcore:1.1
ARG source
WORKDIR /app
EXPOSE 80
COPY ${source:-/bin/Release/netcoreapp1.1/publish} .
ENTRYPOINT ["dotnet", "corewebapp.dll"]


//deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: dotnetcoreapp
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: dotnetcoreapp
    spec:
      containers:
      - name: coreapp
        image: acrgirish.azurecr.io/app
        ports:
        - containerPort: 8080
      imagePullSecrets:
      - name: myregistrykey


//service.yaml

apiVersion: v1
kind: Service
metadata:
   namedotnetcoreapp
spec:
  selector:
    appdotnetcoreapp
  typeLoadBalancer
  ports:
    - port: 80

kubectl create secret docker-registry myregistrykey --docker-server=https://myregistry.azurecr.io --docker-username=ACR_USERNAME --docker-password=ACR_PASSWORD --docker-email=ANY_EMAIL_ADDRESS

Tuesday, June 20, 2017

Powershell script to perform action on remote computer

Powershell script to stop-website on a remote computer


$securepassword=Convertto-SecureString –String $(adminpassword) –AsPlainText –force
$mycredentials=New-object System.Management.Automation.PSCredential $(adminuser),$securepassword
Invoke-Command -ComputerName "computername" -ScriptBlock {
            Import-module WebAdministration;
            Stop-Website "TestCD"
} -Credential $mycredentials