本文說明如何使用 Azure CLI 建立和刪除 Azure Redis 快取實例。 本文也會示範如何使用 Azure CLI 來取得快取詳細數據,包括布建狀態、主機名、埠和密鑰。
先決條件
- 如果您沒有 Azure 帳戶,請在開始之前建立 免費帳戶 。
在 Azure Cloud Shell 中使用 Bash 環境。 如需詳細資訊,請參閱開始使用 Azure Cloud Shell。
若要在本地執行 CLI 參考命令,請安裝 Azure CLI。 如果您正在 Windows 或 macOS 上執行,請考慮在 Docker 容器中執行 Azure CLI。 如需詳細資訊,請參閱〈如何在 Docker 容器中執行 Azure CLI〉。
如果您使用的是本機安裝,請使用 az login 命令,透過 Azure CLI 來登入。 若要完成驗證程式,請遵循終端機中顯示的步驟。 如需其他登入選項,請參閱 使用 Azure CLI 向 Azure 進行驗證。
出現提示時,請在第一次使用時安裝 Azure CLI 延伸模組。 如需擴充功能的詳細資訊,請參閱 使用和管理 Azure CLI 的擴充功能。
執行 az version 以尋找已安裝的版本和相依程式庫。 若要升級至最新版本,請執行 az upgrade。
- 確定您已使用要在其中建立快取的訂用帳戶登入 Azure。 若要使用與您登入的訂用帳戶不同的訂用帳戶,請執行
az account set -s <subscriptionId>,將<subscriptionId>替換成您要使用的訂用帳戶識別碼。
備註
Azure Managed Redis 使用 Azure CLI az redisenterprise 命令。
redisenterprise Azure CLI 2.61.0 版或更高版本的擴充功能會在您第一次執行az redisenterprise命令時提示您安裝。
Azure Cache for Redis 會使用 az redisenterprise 企業層的命令,以及適用於基本、標準和進階層的 az redis 命令。 您可以使用下列腳本來建立和管理 Azure 受控 Redis 或 Azure Cache for Redis Enterprise。 針對 Azure Cache for Redis 基本、標準和進階,請使用 Azure Cache for Redis 腳本。
建立 Azure 管理型 Redis 快取
若要使用 Azure CLI 建立 Azure 受控 Redis 快取,則需要 name、location、resourceGroup 和 sku 參數。 其他參數是選擇性的,而且有預設值。
您可以使用本節中的 Azure CLI 腳本來建立具有預設設定的 Azure 受控 Redis 快取。 您也可以使用下列其他方法來建立快取:
快取 name 必須是在 Azure 區域中唯一的 1-63 個字元字串。 名稱只能包含數位、字母和連字元,必須以數位或字母開頭和結尾,而且不能包含連續連字元。
location 應該是靠近使用您的快取的其他服務的 Azure 區域。
選擇具有快取的適當功能和效能的 SKU。
Microsoft Entra 驗證預設會對所有新快取啟用,且是為了安全考量的建議做法。
這很重要
盡可能使用 Microsoft Entra ID 搭配受控識別,以授權對快取的要求。 使用 Microsoft Entra ID 和受控識別的授權可提供更佳的安全性,而且比共用存取密鑰授權更容易使用。 如需有關使用受控式身分識別結合快取的詳細資訊,請參閱 使用 Microsoft Entra 搭配 Azure 受控 Redis 進行快取認證。
所有新快取預設會啟用傳輸層安全性 (TLS) 1.2-1.3 加密。 您可以在建立快取期間或之後啟用非 TLS 埠和連線,但基於安全性考慮,不建議停用 TLS。
下列腳本會設定變數,然後使用 az group create 和 az redisenterprise create 命令來建立具有 Azure 受控 Redis 平衡 B1 SKU 快取的資源群組。
# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="East US"
resourceGroup="redis-cache-rg-$randomIdentifier"
tag="create-manage-cache"
cache="redis-cache-$randomIdentifier"
sku="Balanced_B1"
# Create a resource group
echo "Creating $resourceGroup in "$location"..."
az group create --resource-group $resourceGroup --location "$location" --tags $tag
# Create a Balanced B1 Azure Managed Redis cache
echo "Creating $cache"
az redisenterprise create --name $cache --resource-group $resourceGroup --location "$location" --sku $sku
取得 Azure 受控 Redis 快取的詳細數據
下列腳本會使用 az redisenterprise show 和 az redisenterprise 資料庫 list-keys 命令來取得並顯示上述快取的名稱、主機名、埠和密鑰詳細數據。
這很重要
只有在啟用快取的存取金鑰時,此 list-keys 作業才能運作。 此命令的輸出可能會藉由顯示秘密來危害安全性,而且可能會觸發敏感性資訊警告。 如需詳細資訊,請參閱 使用 Azure CLI 來管理敏感性資訊。
# Get details of an Azure Managed Redis cache
echo "Showing details of $cache"
az redisenterprise show --name "$cache" --resource-group $resourceGroup
# Retrieve the hostname and ports for an Azure Redis Cache instance
redis=($(az redisenterprise show --name "$cache" --resource-group $resourceGroup --query [hostName,enableNonSslPort,port,sslPort] --output tsv))
# Retrieve the keys for an Azure Redis Cache instance
keys=($(az redisenterprise database list-keys --cluster-name "$cache" --resource-group $resourceGroup --query [primaryKey,secondaryKey] --output tsv))
# Display the retrieved hostname, keys, and ports
echo "Hostname:" ${redis[0]}
echo "Non SSL Port:" ${redis[2]}
echo "Non SSL Port Enabled:" ${redis[1]}
echo "SSL Port:" ${redis[3]}
echo "Primary Key:" ${keys[0]}
echo "Secondary Key:" ${keys[1]}
清理資源
下列腳本會使用 az group delete 和 az redisenterprise delete 命令來刪除上述快取,然後刪除其資源群組。
# Delete a redis cache
echo "Deleting $cache"
az redisenterprise delete --name "$cache" --resource-group $resourceGroup -y
# echo "Deleting all resources"
az group delete --resource-group $resourceGroup -y
這很重要
使用這些腳本,透過 Azure CLI az redis 命令來建立和管理 Azure Cache for Redis 基本、標準和進階層。
Azure Cache for Redis 企業層和 Azure Managed Redis 會使用 az redisenterprise 命令。
redisenterprise Azure CLI 2.61.0 版或更高版本的擴充功能會在您第一次執行az redisenterprise命令時提示您安裝。
若要建立和管理 Azure Cache for Redis 企業層快取,請使用 Azure Managed Redis 指令碼。
建立 Azure Cache for Redis 快取
您可以使用下列 Azure CLI 腳本來建立 Azure Cache for Redis 基本、標準或進階層快取。 若要建立和管理 Azure Cache for Redis 企業層快取,請使用 Azure Managed Redis 指令碼。
若要使用 Azure CLI 建立 Azure Cache for Redis 基本、標準或進階快取, name則需要 、 location、 resourceGroup、 sku和 size 參數。 其他參數是選擇性的,而且有預設值。
您可以使用本節中的 Azure CLI 腳本,建立具有預設設定的 Azure Cache for Redis 基本快取。 您也可以使用下列其他方法來建立快取:
快取 name 必須是在 Azure 區域中唯一的 1-63 個字元字串。 名稱只能包含數位、字母和連字元,必須以數位或字母開頭和結尾,而且不能包含連續連字元。
location 應該是靠近使用您的快取的其他服務的 Azure 區域。
選擇具有適當功能和效能的 SKU 和 size,以滿足您的快取需求。
所有新快取預設會啟用傳輸層安全性 (TLS) 1.2-1.3 加密。 您可以在建立快取期間或之後啟用非 TLS 埠和連線,但基於安全性考慮,不建議停用 TLS。
這很重要
Microsoft 建議使用 Entra 身分驗證來提高安全性。 您可以在建立快取期間或之後啟用Microsoft Entra Authentication。
盡可能使用 Microsoft Entra ID 搭配受控識別,以授權對快取的要求。 使用 Microsoft Entra ID 和受控識別的授權可提供更佳的安全性,而且比共用存取密鑰授權更容易使用。 如需搭配快取使用受控識別的詳細資訊,請參閱 使用 Microsoft Entra ID 進行快取驗證。
下列腳本會使用 az group create 和 az redis create 命令,在其中建立具有 Azure Cache for Redis Basic C0 快取的資源群組。
# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="East US"
resourceGroup="redis-cache-rg-$randomIdentifier"
tag="create-manage-cache"
cache="redis-cache-$randomIdentifier"
sku="basic"
size="C0"
# Create a resource group
echo "Creating $resourceGroup in "$location"..."
az group create --resource-group $resourceGroup --location "$location" --tags $tag
# Create a Basic C0 (256 MB) Azure Redis cache
echo "Creating $cache"
az redis create --name $cache --resource-group $resourceGroup --location "$location" --sku $sku --vm-size $size
取得 Azure Cache for Redis 快取的詳細資料
下列腳本會使用 az redis show 和 az redis list-keys 命令來取得並顯示上述快取的名稱、主機名、埠和密鑰詳細數據。
這很重要
只有在啟用快取的存取金鑰時,此 list-keys 作業才能運作。 此命令的輸出可能會藉由顯示秘密來危害安全性,而且可能會觸發敏感性資訊警告。 如需詳細資訊,請參閱 使用 Azure CLI 來管理敏感性資訊。
# Get details of an Azure Cache for Redis cache
echo "Showing details of $cache"
az redis show --name "$cache" --resource-group $resourceGroup
# Retrieve the hostname and ports for an Azure Redis instance
redis=($(az redis show --name "$cache" --resource-group $resourceGroup --query [hostName,enableNonSslPort,port,sslPort] --output tsv))
# Retrieve the keys for an Azure Redis instance
keys=($(az redis list-keys --name "$cache" --resource-group $resourceGroup --query [primaryKey,secondaryKey] --output tsv))
# Display the retrieved hostname, keys, and ports
echo "Hostname:" ${redis[0]}
echo "Non SSL Port:" ${redis[2]}
echo "Non SSL Port Enabled:" ${redis[1]}
echo "SSL Port:" ${redis[3]}
echo "Primary Key:" ${keys[0]}
echo "Secondary Key:" ${keys[1]}
清理資源
下列腳本會使用 az group delete 和 az redis delete 命令來刪除上述快取,然後刪除其資源群組。
# Delete an Azure Redis cache
echo "Deleting $cache"
az redis delete --name "$cache" --resource-group $resourceGroup -y
# echo "Deleting all resources"
az group delete --resource-group $resourceGroup -y