Azure IaaS

Azure 虚拟机使用指引

临时盘

Azure 的虚机默认没有开通虚拟内存(Windows)或交换文件(Linux),但是每个虚机自带一块临时盘正好用于存放这类临时数据。

Linux

Linux 虚机在临时盘上开启交换文件通过 /etc/waagent.conf 来配置:

# 格式化并挂载临时盘
ResourceDisk.Format=y

# 在临时盘上激活 Swap 文件
ResourceDisk.EnableSwap=y

# 指定 swap 分区的大小为 4GB
ResourceDisk.SwapSizeMB=4096

修改配置后重启服务器,可以用 free 命令确认修改效果。

VPN

相关路径

%APPDATA%\Microsoft\Network\Connections\Cm\ VPN 配置存放文件夹

macOS/Linux 下自签证书

# macOS
brew install strongswan

# Ubuntu
sudo apt-get install strongswan-ikev2 strongswan-plugin-eap-tlssudo libstrongswan-extra-plugins

COMPANY_NAME=<Company Name>
ipsec pki --gen --outform pem > "${COMPANY_NAME}ServerKey.pem"
ipsec pki --self --in `echo $COMPANY_NAME`ServerKey.pem --dn "CN=${COMPANY_NAME}ServerCert" --ca --outform pem --not-after "31.12.9999 23:59:59" --dateform "%d.%m.%Y %T"> ${COMPANY_NAME}ServerCert.pem

ipsec pki --gen --outform pem >  "${COMPANY_NAME}ClientKey.pem"
# Mac
ipsec pki --pub --in "${COMPANY_NAME}ClientKey.pem" | ipsec pki --issue --cacert "${COMPANY_NAME}ServerCert.pem" --cakey "${COMPANY_NAME}ServerKey.pem" --dn "CN=${COMPANY_NAME}ClientCert" --san "${COMPANY_NAME}" --flag clientAuth --outform pem --not-after "`date -v+6m +'%d.%m.%y %T'`" > "${COMPANY_NAME}ClientCert.pem"

# Ubuntu
ipsec pki --pub --in "${COMPANY_NAME}ClientKey.pem" | ipsec pki --issue --cacert "${COMPANY_NAME}ServerCert.pem" --cakey "${COMPANY_NAME}ServerKey.pem" --dn "CN=${COMPANY_NAME}ClientCert" --san "${COMPANY_NAME}" --flag clientAuth --outform pem --not-after "`date --date='+6 month' +'%d.%m.%y %T'`" > "${COMPANY_NAME}ClientCert.pem"

PASSWORD=`openssl rand -base64 15`

openssl pkcs12 -in "${COMPANY_NAME}ClientCert.pem" -inkey "${COMPANY_NAME}ClientKey.pem" -certfile "${COMPANY_NAME}ServerCert.pem" -export -out "${COMPANY_NAME}ClientCert.p12" -password "pass:${PASSWORD}"

# 转 base64 上传到 Azure Portal
openssl x509 -in `echo $COMPANY_NAME`ServerCert.pem -outform der | base64 -w 0

echo ${PASSWORD}

Windows 自签证书

#

$current = Get-Date
Get-ChildItem -Path "Cert:\CurrentUser\My"
$cert = Get-ChildItem -Path "Cert:\CurrentUser\My\XXXXXXXXXXXXXXXXXXXX"

常见错误

Error 798 无 VPN 证书

VPN 证书导入后,VPN 客户端依旧报错无法找到证书。

解决方案

导入 VPN 证书时去掉私钥强保护选项:

Error 812

错误信息

Paging
Next
Prev

The connection was prevented because of a policy configured on your RAS/VPN server. Specifically, the authentication method used by the server to verify your username and password may not match the authentication method configured in your connection profile. Please contact the Administrator of the RAS server and notify them of this error. (Error 812) For customized troubleshooting information for this connection, click Help.

由于 RAS/VPN 服务器上配置的某个策略,连接被阻止。特别是,服务器用于验证用户名和密码的身份验证方法可能与连接配置文件中所配置的身份验证方法不匹配。请与 RAS 服务器的管理员联系并将此错误通知给他们。 (错误 812) 有关自定义疑难解答信息,请单击“帮助”。

错误原因

2018年7月1号开始,Azure VPN 需要 TLS 1.2 支持方可使用。Windows 7 系列内核需要手动安装 TLS 支持。

解决方案

尝试以下步骤解决此类问题:

  • 安装以下两个补丁:

    https://www.catalog.update.microsoft.com/search.aspx?q=kb2977292

    https://www.catalog.update.microsoft.com/search.aspx?q=kb3140245

  • 以管理员方式在命令行下运行以下命令后重启电脑。
reg add HKLM\SYSTEM\CurrentControlSet\Services\RasMan\PPP\EAP\13 /v TlsVersion /t REG_DWORD /d 0xfc0
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\WinHttp" /v DefaultSecureProtocols /t REG_DWORD /d 0xaa0
if %PROCESSOR_ARCHITECTURE% EQU AMD64 reg add "HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Internet Settings\WinHttp" /v DefaultSecureProtocols /t REG_DWORD /d 0xaa0

http://crl3.digicert.com/ssca-sha2-g1.crl

http://crl4.digicert.com/ssca-sha2-g1.crl

Azure File 文件名乱码

Azure File 服务可用作集群 NAS 盘,有时不同 Linux 虚机下挂载会出现上传文件之文件名乱码。原因是 Linux 挂载 CIFS 时有时默认编码为 iso8859-1,有时为 utf8,挂载时显式统一指定为 utf8 编码可解决该问题:,iocharset=utf8

创建虚机

复用既存非托管磁盘

删除虚机后重建,对于非托管磁盘,可以利用 Azure CLI 通过 Storage 里未删除的磁盘直接重建虚拟机。

az vm create -g '<资源组名称>' `
             -n '<虚拟机名称>' `
             --use-unmanaged-disk `
             --attach-os-disk "<系统盘 URL>" `
             --attach-data-disks "<数据盘 URL>" `
             --os-type linux `
             --size Standard_D1_v2 `
             --availability-set "<可用集名称>" `
             --nics "<网卡名称>" 

深度监控

配置监控数据存储可以获取虚机内部信息:内存、磁盘使用率等。

更新管理

# 删除 Log Analytics workspaces 客户端
wget https://raw.githubusercontent.com/Microsoft/OMS-Agent-for-Linux/master/installer/scripts/onboard_agent.sh && sh onboard_agent.sh --purge

常用工具

名称 网址
PowerShell https://github.com/PowerShell/PowerShell
Azure CLI https://docs.microsoft.com/en-us/cli/azure/install-azure-cli

PowerShell

安装

# MacOS
brew cask install powershell

使用 PowerShell 管理 Azure 虚拟机前,需要安装 Azure 模块:

# 安装 Azure PowerShell 模块
Install-Module -Name Az -AllowClobber

# 更新 Azure PowerShell 模块
Update-Module -Name Az

使用 PowerShell 管理虚机前,需要初始化 Azure 模块:

az cloud list --output table

# 显示所有订阅
az account list --output table

# 列出可用区域
az account list-locations --output table

# 显示东区可用虚机机型
az vm list-sizes --location 'China East' --output table

Azure CLI

az cloud set --name AzureChinaCloud
az login

# 巡查 VM 配置信息
az vm show -g RESOURCE_GROUP -d -n VM_NAME

Enterprise Linux、CentOS、RedHat

Author: njun
njun's picture
Updated: 2020/12/21