Showing posts with label az. Show all posts
Showing posts with label az. Show all posts

Az-204 - Prep - How To - Creating a container and access its service in Azure environment with ACR

Hello Team, 

Below commands can be used to create a container with image from ACR and to access the container service.


az login \
--username <> \
--password <> \

az group list | select -p name

# Deploying a container from public registry
az container create \
--resource-group "1-028b2503-playground-sandbox" \
--name psdemo-hello-world-cli-010 \
--dns-name-label psdemo-hello-world-cli-010 \
--image mcr.microsoft.com/azuredocs/aci-helloworld \
--ports 80

# show the container info
az container show --resource-group "1-5e3dbe31-playground-sandbox" \
--name "psdemo-hello-world-cli-009"

# Retrieve the container URL to access it over internet
URL=$(az container show --resource-group '1-5e3dbe31-playground-sandbox' \
--name 'psdemo-hello-world-cli-009' --query ipAddress.fqdn | tr -d '"')
echo "https://$URL"

# Demo 1 - Deploy container from azure container registry
## STEP 0 SET ENVIRONMENT VARIABLES
ACR_NAME="grsr18"
ACR_REGISTRY_ID=$(az acr show --name $ACR_NAME --query id --output tsv)
ACR_LOGINSERVER=$(az acr show --name $ACR_NAME --query loginServer --output tsv)

echo "ACR ID: $ACR_REGISTRY_ID"
echo "ACR_LOGINSERVER $ACR_LOGINSERVER"

## STEP 1 create a service principal and get the password and ID,
## this will allow azure container instances to pull
SP_NAME=acr-service-principal
SP_PASSWD=$(az ad sp create-for-rbac \
--name http://$ACR_NAME-pull \
--scopes $ACR_REGISTRY_ID \
--role acrpull \
--query password \
--output tsv)
SP_APPID=$(az ad sp show --id http://$ACR_NAME-pull --query appId --output tsv)

echo "Servce Princial ID : $SP_APPID"
echo "Service Princial Password $SP_PASSWD"

## STEP 2
az container create \
--resource-group "1-5e3dbe31-playground-sandbox" \
--name "psdemo-webapp-cli" \
--dns-name-label psdemo-webapp-cli \
--ports 80 \
--image $ACR_LOGINSERVER/webappimage:v1 \
--registry-login-server $ACR_LOGINSERVER \
--registry-username $SP_APPID \
--registry-password $SP_PASSWD

# due to permission issues if you cant create service princial
# then you can use your acr login creds to pull image
az container create --resource-group "1-028b2503-playground-sandbox" \
--name "psdemo-webapp-cli" --dns-name-label "psdemo-webapp-cli" \
--ports 80 --image $ACR_LOGINSERVER/webappimage:v1 \
--registry-login-server $ACR_LOGINSERVER --registry-username "GRSR18" \
--registry-password "xx=xxxxx=xxxxx+xxxxx"

# get the container status
az container show --resource-group "1-028b2503-playground-sandbox" \
--name "psdemo-webapp-cli"
az container show --resource-group "1-028b2503-playground-sandbox" \
--name "psdemo-webapp-cli" --query instanceView.state

# get the FQDN to access
url=$(az container show --resource-group "1-028b2503-playground-sandbox" \
--name "psdemo-webapp-cli" --query ipAddress.fqdn | tr -d '"')
echo http://$url
curl http://$url

# Get contianer logs
az container logs --resource-group "1-028b2503-playground-sandbox" \
--name "psdemo-webapp-cli"

# Delete the running container
az container delete --resource-group "1-028b2503-playground-sandbox" \
--name "psdemo-webapp-cli" --yes

# Cleanup from our demos, all ACIs and ACR Deployed in the resource group
az group delete --name "1-028b2503-playground-sandbox" --yes
docker image rm grsr18.azureacr.ip/webappimage:v1
docker image rm webappimage:v1



Az-204 - Prep - How To - Creating Azure Container Registry And Upload/Push docker image into Azure Container Registry (ACR)

 

Hello Everyone, 

This post contains below list of topics

  1. How to create ACR ( Azure Container Registry )
  2. How to login into ACR
  3. How to push Docker images into ACR
  4. How to build docker images with ACR
Github link contains a sample dotnet webapp, which we can build using given instructions. I will share the Github link soon. But you can build your own container image and upload it ACR with same instructions as give below.
# Login
az login --username "xxxx" --password "xxxx"

# Get the resource group name
az group list

# if you want to set subscription
az account set --subscription "SUB NAme"

# create a rg
az group create \
--name psdemo-rg \
--location centralus

# or list existing with

az group list

# Set ACR name , globally unique
ACR_NAME="GRSR810"
az acr create \
--resource-group "1-028b2503-playground-sandbox" \
--name $ACR_NAME \
--sku Standard

# login into ACR
az acr login --name $ACR_NAME

# Get the login server which is used in the image tag
az acr show --name $ACR_NAME --query loginServer
az acr show --name $ACR_NAME --query loginServer --output tsv
ACR_LOGINSERVER=$(az acr show --name $ACR_NAME --query loginServer --output tsv)
echo $ACR_LOGINSERVER

Tag the container image using the login server name.
docker image ls webappimage:v1
docker tag webappimage:v1 $ACR_LOGINSERVER/webappimage:v1
docker image ls $ACR_LOGINSERVER/webappimage:v1
docker image ls

# Push the image into Docker registry
docker push $ACR_LOGINSERVER/webappimage:v1

# Get list of the repositories and images/tags from ACR
az acr repository list --name $ACR_NAME --output table
az acr repository show-tags --name $ACR_NAME --repository webappimage --output table

# By using tasks we can build ACR with tasks.
az acr build --image "webappimage:v1-acr-task" --registry $ACR_NAME .

# 2 images must be there now
az acr repository show-tags --name $ACR_NAME --repository webappimage --output table


Dockerfile

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1

RUN mkdir /app
WORKDIR /app

COPY ./webapp/bin/Release/netcoreapp3.1/publish ./
COPY ./config.sh ./

RUN bash config.sh

EXPOSE 80
ENTRYPOINT [ "dotnet", "webapp.dll" ]

Building dotnet Web application

ls -ls ./webapp

dotnet build ./webapp
dotnet run --project ./webapp
curl http://localhost:5000

# Publish local build
dotnet publish -c Release ./webapp

# build docker image
docker build -t webappimage:v1 .

# Run the container
docker run --name webapp --publish 8080:80 --detach webappimage:v1
curl http://localhost:8080

docker stop webapp
docker rm webapp

docker image ls webappimage:v1


Az-204 - Prep - How To - Creating VM Using Azure Powershell

 

Hello Everyone, 

Please find below commands to create VM using azure powershell

 

# Login
Connect-AzAccount -SubscriptionName "Demo Account"

# Set your subscription
Set-AzContext -SubscriptionName "Demo Account"

# Create a resource Group
New-AzResourceGroup -Name "psdemo-rg" -Location "CentralUS"

# Create a credential to use in VM Creation
$username = 'cloud_user_p_25e585de'
$password = ConvertTo-SecureString 'c6Wh3BrR6ArHzO&Cx4cA' -AsPlainText -Force
$WindowsCred = New-Object System.Management.Automation.PSCredential ($username, $password)

# Create Windows VM
New-AzVM `
-ResourceGroupName 'psdemo-rg' `
-Name 'psdemo-win-az4' `
-Image 'Win2019Datacenter'`
-Credential $WindowsCred `
-OpenPorts 3389 `
-AzJob

Get-AzResourceGroup
# 1-83eb7c26-playground-sandbox

New-AzVM `
-ResourceGroupName "1-83eb7c26-playground-sandbox" `
-Name "psdemo-win-az1" `
-Image "Win2019Datacenter" `
-Credential $WindowsCred `
-OpenPorts 3389

New-AzVm `
-ResourceGroupName "1-83eb7c26-playground-sandbox" `
-Name "aznewvm23" `
-Location 'eastus' `
-VirtualNetworkName "mynewazvm23" `
-SubnetName "default" `
-SecurityGroupName "mynewvmNSG23" `
-PublicIpAddressName "mypublicip23" `
-OpenPorts 80,3389 `
-AsJob

Get-Job 1

$j = Get-Job -Name "Long Running*"
$j

"""
PS my_learning/az204_prep> $j = Get-Job -Name 'Long Running*'

PS my_learning/az204_prep> $j


Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
1 Long Running O… AzureLongRunni… Failed True localhost New-AzVM
2 Long Running O… AzureLongRunni… Failed True localhost New-AzVM
3 Long Running O… AzureLongRunni… Completed True localhost New-AzVM
"""

Get-AzVM -ResourceGroupName 1-83eb7c26-playground-sandbox

# Get the Public IP Address
Get-AzPublicIpAddress `
-ResourceGroupName "1-83eb7c26-playground-sandbox"`
-Name "psdemo-win-az" | Select-Object IpAddress

# Remove resource group after demo
Remove-AzResourceGroup -Name '1-83eb7c26-playground-sandbox'

Az-204 - Prep - How To - Creating VM Using Azure CLI - Bash

Hello Everyone, 

Please find below commands to create VM using azure cli `az`

# login and set a subscription
az login
az account set --subscription "Demo Account"

# List all created RGs
az group list --output table

# create a RG
az group create \
--name "psdemo-rg" \
--location "centralus"

# Creating a Windows VM
az vm create \
--resource-group "1-718a377a-playground-sandbox" \
--name "psdemo-win-cli" \
--image "win2019datacenter" \
--admin-username "demoadmin" \
--admin-password "password@123" \
--public-ip-sku "Standard"

# Open RDP Port for remote access (( Might open already))
az vm open-port \
--resource-group "1-718a377a-playground-sandbox" \
--name "psdemo-win-cli" \
--port "3389"

# get the Public IP Address for remote access
az vm list-ip-addresses \
--resource-group "1-718a377a-playground-sandbox" \
--name "psdemo-win-cli" \
--output table


# Creating a Linux VM
az vm create \
--resource-group "1-718a377a-playground-sandbox" \
--name "psdemo-linux-cli" \
--image "UbuntuLTS" \
--admin-username "demoadmin" \
--authentication "ssh" \
--ssh-key-value ~/.ssh/id_rsa.pub \
--public-ip-sku "Standard"

# open port 22 ((Might open already ))
az vm open-port \
--resource-group "1-718a377a-playground-sandbox" \
--name "psdemo-linux-cli" \
--port 22

# get the public IP Address
az vm list-ip-addresses \
--resource-group "1-718a377a-playground-sandbox" \
--name "psdemo-linux-cli" \
--output table

# verify login
ssh demoadmin@52.165.189.38


# Cleanup the resources
az group delete --name "1-718a377a-playground-sandbox"