你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

通过群集创建高级 Azure Redis 缓存

在此方案中,了解如何通过启用的群集与两个分片创建 6 GB 高级层 Azure Redis 缓存。 然后,了解如何获取 Azure Cache for Redis 实例的详细信息,包括预配状态、主机名、端口和 Azure Cache for Redis 实例密钥。 最后,了解如何删除缓存。

如果没有 Azure 订阅,请在开始之前创建一个 Azure 免费帐户

先决条件

示例脚本

启动 Azure Cloud Shell

Azure Cloud Shell 是免费的交互式 shell,可以使用它运行本文中的步骤。 它预安装有常用 Azure 工具并将其配置与帐户一起使用。

若要打开 Cloud Shell,只需要从代码块的右上角选择“试一试”。 也可以通过转到 https://shell.azure.com 在单独的浏览器标签页中启动 Cloud Shell。

当 Cloud Shell 打开时,请验证是否为环境选择了“Bash”。 后续会话将在 Bash 环境中使用 Azure CLI,选择“复制”以复制代码块,将其粘贴到 Cloud Shell 中,然后按 Enter 来运行它。

登录 Azure

Cloud Shell 会在登录时使用的初始帐户下自动进行身份验证。 使用以下脚本通过其他订阅登录,将 <Subscription ID> 替换为 Azure 订阅 ID。 如果没有 Azure 订阅,请在开始之前创建一个 Azure 免费帐户

subscription="<subscriptionId>" # add subscription here

az account set -s $subscription # ...or use 'az login'

有关详细信息,请参阅设置有效的订阅以交互方式登录

运行脚本

# Create and manage a premium P1 Redis Cache with clustering

# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="East US"
resourceGroup="msdocs-redis-cache-rg-$randomIdentifier"
tag="create-manage-premium-cache-cluster"
cache="msdocs-redis-cache-$randomIdentifier"
sku="premium"
size="P1"
shardCount="2"

# Create a resource group
echo "Creating $resourceGroup in "$location"..."
az group create --name $resourceGroup --location "$location" --tags $tag

# Create a Premium P1 (6 GB) Redis Cache with clustering enabled and 2 shards (for a total of 12 GB)
echo "Creating $cache"
az redis create --name $cache --resource-group $resourceGroup --location "$location" --sku $sku --vm-size $size --shard-count $shardCount

# Get details of an Azure Cache for Redis
echo "Showing details of $cache"
az redis show --name $cache --resource-group $resourceGroup 

# Retrieve the hostname and ports for an Azure Redis Cache instance
redis=($(az redis show --name $resourceGroup --resource-group $resourceGroup --query [hostName,enableNonSslPort,port,sslPort] --output tsv))

# Retrieve the keys for an Azure Redis Cache instance
keys=($(az redis list-keys --name contosoCache --resource-group contosoGroup --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]}

# Delete a redis cache
echo "Deleting $cache"
az redis delete --name $resourceGroup --resource-group $resourceGroup -y

清理资源

使用 az group delete 命令删除资源组以及与其关联的所有资源 - 除非你持续需要这些资源。 其中一些资源在创建和删除时可能要稍等片刻。

az group delete --name $resourceGroup

示例参考

此脚本使用以下命令创建资源组并通过启用群集创建高级层 Azure Redis 缓存。 表中的每条命令均链接到特定于命令的文档。

命令 说明
az group create 创建用于存储所有资源的资源组。
az redis create 创建 Azure Redis 缓存实例。
az redis show 检索 Azure Redis 缓存实例的详细信息。
az redis list-keys 检索 Azure Redis 缓存实例的访问密钥。
az redis 删除 删除 Azure Redis 缓存实例。

后续步骤

有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档

有关用于创建基本 Azure Cache for Redis 的 Azure Cache for Redis CLI 脚本示例,请参阅 Azure Cache For Redis