在前面的教程中,你已學(xué)習(xí)如何通過 SSH 連接到虛擬機 (VM) 并手動安裝 NGINX。 若要以快速一致的方式創(chuàng)建 VM,通常需要某種形式的自動化。 在首次啟動 VM 時實現(xiàn)自定義的常見方法是使用cloud-init。 本教程介紹如何執(zhí)行下列操作:
創(chuàng)建 cloud-init 配置文件
創(chuàng)建使用 cloud-init 文件的 VM
在創(chuàng)建 VM 后,查看正在運行的 Node.js 應(yīng)用
使用 Key Vault 安全地存儲證書
使用 cloud-init 自動進行 NGINX 的安全部署
Note
在 Azure 中國區(qū)使用 Azure CLI 2.0 之前,請先運行az cloud set -n AzureChinaCloud來改變云環(huán)境。如果想切回國際版 Azure,請再次運行az cloud set -n AzureCloud。
如果選擇在本地安裝并使用 CLI,本教程要求運行 Azure CLI 2.0.4 或更高版本。 運行az --version即可查找版本。 如果需要進行安裝或升級,請參閱安裝 Azure CLI 2.0。
Cloud-init 概述
Cloud-init是一種廣泛使用的方法,用于在首次啟動 Linux VM 時對其進行自定義。 可使用 cloud-init 來安裝程序包和寫入文件,或者配置用戶和安全性。 在初始啟動期間運行 cloud-init 時,無需額外的步驟和代理即可應(yīng)用配置。
Cloud-init 還支持不同的發(fā)行版。 例如,不需使用 apt-get install 或 yum install 來安裝包, 而是可定義要安裝的程序包的列表。 Cloud-init 將對所選發(fā)行版自動使用本機包管理工具。
我們正在與合作伙伴協(xié)作,將 cloud-init 納入用戶向 Azure 提供的映像中并使其在映像中正常運行。 下表概述了 cloud-init 當前在 Azure 平臺映像上的可用性:
別名發(fā)布者產(chǎn)品SKUVersion
UbuntuLTSCanonicalUbuntuServer16.04-LTS最新
UbuntuLTSCanonicalUbuntuServer14.04.5-LTS最新
CoreOSCoreOSCoreOSStablelatest
創(chuàng)建 cloud-init 配置文件
若要運行 cloud-init,請創(chuàng)建一個 VM,以便安裝 NGINX 并運行簡單的“Hello World”Node.js 應(yīng)用。 以下 cloud-init 配置會安裝所需的程序包,創(chuàng)建 Node.js 應(yīng)用,然后初始化并啟動該應(yīng)用。
在當前 shell 中,創(chuàng)建名為“cloud-init.txt”的文件并粘貼下面的配置。 可使用任何想要使用的編輯器。 請確保已正確復(fù)制整個 cloud-init 文件,尤其是第一行:
yaml復(fù)制
#cloud-configpackage_upgrade:truepackages:-nginx-nodejs-npmwrite_files:- owner:www-data:www-data- path:/etc/nginx/sites-available/defaultcontent:|
server {
listen 80;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}- owner:azureuser:azureuser- path:/home/azureuser/myapp/index.jscontent:|
var express = require('express')
var app = express()
var os = require('os');
app.get('/', function (req, res) {
res.send('Hello World from host ' + os.hostname() + '!')
})
app.listen(3000, function () {
console.log('Hello world app listening on port 3000!')
})runcmd:-servicenginxrestart-cd"/home/azureuser/myapp"-npminit-npminstallexpress-y-nodejsindex.js
有關(guān) cloud-init 配置選項的詳細信息,請參閱cloud-init 配置示例。
創(chuàng)建虛擬機
使用az group create創(chuàng)建資源組,然后才能創(chuàng)建 VM。 以下示例在“chinaeast”位置創(chuàng)建名為“myResourceGroupAutomate”的資源組:
Azure CLI復(fù)制
azgroupcreate--namemyResourceGroupAutomate--locationchinaeast
現(xiàn)在,請使用az vm create創(chuàng)建 VM。 使用--custom-data參數(shù)傳入 cloud-init 配置文件。 如果未將 cloud-init.txt 配置文件保存在現(xiàn)有工作目錄中,請?zhí)峁┰撐募耐暾窂健?以下示例創(chuàng)建名為“myAutomatedVM”的 VM:
Azure CLI復(fù)制
azvmcreate\--resource-groupmyResourceGroupAutomate \--namemyVM \--imageUbuntuLTS \--admin-usernameazureuser \--generate-ssh-keys\--custom-datacloud-init.txt
創(chuàng)建 VM、安裝程序包和啟動應(yīng)用需耗時幾分鐘。 在 Azure CLI 返回提示之后,仍然存在繼續(xù)運行的后臺任務(wù)。 可能還需等待幾分鐘才能訪問應(yīng)用。 創(chuàng)建 VM 后,請記下 Azure CLI 顯示的publicIpAddress。 此地址用于通過 Web 瀏覽器訪問 Node.js 應(yīng)用。
若要使 VM 能使用 Web 流量,請通過az vm open-port從 Internet 打開端口 80:
Azure CLI復(fù)制
azvmopen-port--port80--resource-groupmyResourceGroupAutomate--namemyVM
測試 Web 應(yīng)用
現(xiàn)在可以打開 Web 瀏覽器,在地址欄中輸入“http://”。 在 VM 創(chuàng)建過程中提供自己的公共 IP 地址。 Node.js 應(yīng)用如下例所示:
注入 Key Vault 中的證書
此可選部分展示了如何在 Azure Key Vault 中安全存儲證書,并在 VM 部署期間將其注入。 此過程可確保首次啟動時將最新的證書注入到 VM 中,不必使用包含內(nèi)嵌證書的自定義映像。 在該過程中,證書永遠不會離開 Azure 平臺,也不會在腳本、命令行歷史記錄或模板中公開。
Azure Key Vault 保護加密密鑰和機密,例如證書或密碼。 Key Vault 有助于簡化密鑰管理過程,讓你能夠持續(xù)掌控用于數(shù)據(jù)訪問和加密的密鑰。 此方案介紹了一些用于證書創(chuàng)建和使用的 Key Vault 概念,但未詳述如何使用 Key Vault。
以下步驟演示如何:
創(chuàng)建 Azure Key Vault
生成證書或?qū)⑵渖蟼鞯?Key Vault
利用要注入到 VM 的證書創(chuàng)建密鑰
創(chuàng)建 VM 并注入證書
創(chuàng)建 Azure Key Vault
首先,使用az keyvault create創(chuàng)建 Key Vault,并在部署 VM 時啟用該 Key Vault。 每個 Key Vault 均需具備唯一名稱且全部小寫。 將下例中的 mykeyvault 替換為自己唯一的 Key Vault 名稱:
Azure CLI復(fù)制
keyvault_name=mykeyvaultazkeyvaultcreate\--resource-groupmyResourceGroupAutomate \--name$keyvault_name\--enabled-for-deployment
生成證書并將其存儲在 Key Vault 中
為供生產(chǎn)使用,應(yīng)通過az keyvault certificate import導(dǎo)入由受信任的提供程序簽名的有效證書。 在本教程中,以下示例顯示了如何使用az keyvault certificate create生成使用默認證書策略的自簽名證書:
Azure CLI復(fù)制
azkeyvaultcertificatecreate\--vault-name$keyvault_name\--namemycert \--policy"$(az keyvault certificate get-default-policy)"
準備用于 VM 的證書
若要在 VM 創(chuàng)建過程中使用該證書,請使用az keyvault secret list-versions獲取證書的 ID。 VM 需要特定格式的證書才能在啟動時將其注入,因此請使用az vm format-secret轉(zhuǎn)換該證書。 以下示例將這些命令的輸出分配給變量,以便在后續(xù)步驟中使用:
Azure CLI復(fù)制
secret=$(azkeyvaultsecretlist-versions\--vault-name$keyvault_name\--namemycert \--query"[?attributes.enabled].id"--outputtsv)vm_secret=$(azvmformat-secret--secret"$secret")
創(chuàng)建 cloud-init 配置以保護 NGINX
創(chuàng)建 VM 時,證書和密鑰都將存儲在受保護的 /var/lib/waagent/ 目錄中。 要將證書自動添加到 VM 并配置 NGINX,可使用上一示例中已更新的 cloud-init 配置。
創(chuàng)建名為“cloud-init-secured.txt”的文件并粘貼下面的配置。 同樣,請確保已正確復(fù)制整個 cloud-init 文件,尤其是第一行:
yaml復(fù)制
#cloud-configpackage_upgrade:truepackages:-nginx-nodejs-npmwrite_files:- owner:www-data:www-data- path:/etc/nginx/sites-available/defaultcontent:|
server {
listen 80;
listen 443 ssl;
ssl_certificate /etc/nginx/ssl/mycert.cert;
ssl_certificate_key /etc/nginx/ssl/mycert.prv;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}- owner:azureuser:azureuser- path:/home/azureuser/myapp/index.jscontent:|
var express = require('express')
var app = express()
var os = require('os');
app.get('/', function (req, res) {
res.send('Hello World from host ' + os.hostname() + '!')
})
app.listen(3000, function () {
console.log('Hello world app listening on port 3000!')
})runcmd:-secretsname=$(find/var/lib/waagent/-name"*.prv"| cut -c -57)
- mkdir /etc/nginx/ssl
- cp $secretsname.crt /etc/nginx/ssl/mycert.cert
- cp $secretsname.prv /etc/nginx/ssl/mycert.prv
- service nginx restart
- cd "/home/azureuser/myapp"
- npm init
- npm install express -y
- nodejs index.js
創(chuàng)建安全的 VM
現(xiàn)在,請使用az vm create創(chuàng)建 VM。 使用--secrets參數(shù)注入 Key Vault 中的證書數(shù)據(jù)。 與上個示例一樣,使用--custom-data參數(shù)傳入 cloud-init 配置:
Azure CLI復(fù)制
azvmcreate\--resource-groupmyResourceGroupAutomate \--namemyVMSecured \--imageUbuntuLTS \--admin-usernameazureuser \--generate-ssh-keys\--custom-datacloud-init-secured.txt \--secrets"$vm_secret"
創(chuàng)建 VM、安裝程序包和啟動應(yīng)用需耗時幾分鐘。 在 Azure CLI 返回提示之后,仍然存在繼續(xù)運行的后臺任務(wù)。 可能還需等待幾分鐘才能訪問應(yīng)用。 創(chuàng)建 VM 后,請記下 Azure CLI 顯示的publicIpAddress。 此地址用于通過 Web 瀏覽器訪問 Node.js 應(yīng)用。
若要使 VM 能使用安全的 Web 流量,請通過az vm open-port從 Internet 打開端口 443:
Azure CLI復(fù)制
azvmopen-port \--resource-groupmyResourceGroupAutomate \--namemyVMSecured \--port443
測試 Web 應(yīng)用是否安全
現(xiàn)在可以打開 Web 瀏覽器,在地址欄中輸入“https://”。 在 VM 創(chuàng)建過程中提供自己的公共 IP 地址。 若使用自簽名的證書,請接受安全警告:
隨即顯示受保護的 NGINX 站點和 Node.js 應(yīng)用,如下例所示:
后續(xù)步驟
在本教程中,你使用 cloud-init 在首次啟動時配置了 VM。 你已了解如何:
創(chuàng)建 cloud-init 配置文件
創(chuàng)建使用 cloud-init 文件的 VM
在創(chuàng)建 VM 后,查看正在運行的 Node.js 應(yīng)用
使用 Key Vault 安全地存儲證書
使用 cloud-init 自動進行 NGINX 的安全部署
轉(zhuǎn)到下一教程,了解如何創(chuàng)建自定義 VM 映像。
立即訪問http://market.azure.cn