在 Azure 上部署 Kubernetes 的全面指南
1. Azure 虚拟网络
Azure 虚拟网络(VNet)能在 Azure 中创建一个隔离的专用网段,这一概念与 AWS 和 GCP 中的 VPC 类似。用户需指定连续的 IP 范围(即 CIDR)和位置(在 AWS 中称为区域),可在 Azure 全球基础设施位置 查看完整的位置列表。在虚拟网络内,还能创建多个子网,或在创建时启用 Azure 防火墙。Azure 防火墙是一种具备高可用性和可扩展性的网络安全服务,能依据用户指定的规则控制和过滤流量,同时提供入站 DNAT 和出站 SNAT 支持。
要使用 Azure CLI 创建 Azure 虚拟网络,可通过以下两种方式安装 Azure CLI:
- 按照 安装说明 进行安装。
- 直接使用 Azure 云 shell ,它是一个基于云的管理 shell,已预装 Azure CLI。
以下是使用 Azure 云 shell 创建虚拟网络的步骤:
1. 登录账户并附加云存储以持久保存数据。
2. 点击“创建”按钮,等待几秒后,浏览器将启动云 shell 控制台。
Azure CLI 命令以 az 作为组名,可使用 az --help 查看子组列表,或使用 az $subgroup_name --help 了解子组子命令的更多信息。命令格式如下:
# az $subgroup1 [$subgroup2 ...] $commands [$parameters]
下面是创建名为 devops-vnet 的虚拟网络的示例:
1. 创建一个名为 devops 的资源组,位置为美国中部:
# az group create --name devops --location centralus
输出结果:
{
"id": "/subscriptions/f825790b-ac24-47a3-89b8-9b4b3974f0d5/resourceGroups/devops",
"location": "centralus",
"managedBy": null,
"name": "devops",
"properties": {
"provisioningState": "Succeeded"
},
"tags": null
}
- 使用
network.vnet子组创建虚拟网络资源,CIDR 为10.0.0.0/8,其余设置使用默认值:
# az network vnet create --name devops-vnet --resource-group devops --subnet-name default --address-prefixes 10.0.0.0/8
输出结果:
{
"newVNet": {
"addressSpace": {
"addressPrefixes": [
"10.0.0.0/8"
]
},
"ddosProtectionPlan": null,
"dhcpOptions": {
"dnsServers": []
},
"enableDdosProtection": false,
"enableVmProtection": false,
"etag": "W/\"a93c56be-6eab-4391-8fca-25e11625c6e5\"",
"id": "/subscriptions/f825790b-ac24-47a3-89b8-9b4b3974f0d5/resourceGroups/devops/providers/Microsoft.Network/virtualNetworks/devops-vnet",
"location": "centralus",
"name": "devops-vnet",
"provisioningState": "Succeeded",
"resourceGroup": "devops",
"resourceGuid": "f5b9de39-197c-440f-a43f-51964ee9e252",
"subnets": [
{
"addressPrefix": "10.0.0.0/24",
"addressPrefixes": null,
"delegations": [],
"etag": "W/\"a93c56be-6eab-4391-8fca-25e11625c6e5\"",
"id": "/subscriptions/f825790b-ac24-47a3-89b8-9b4b3974f0d5/resourceGroups/devops/providers/Microsoft.Network/virtualNetworks/devops-vnet/subnets/default",
"interfaceEndpoints": null,
"ipConfigurationProfiles": null,
"ipConfigurations": null,
"name": "default",
"networkSecurityGroup": null,
"provisioningState": "Succeeded",
"purpose": null,
"resourceGroup": "devops",
"resourceNavigationLinks": null,
"routeTable": null,
"serviceAssociationLinks": null,
"serviceEndpointPolicies": null,
"serviceEndpoints": null,
"type": "Microsoft.Network/virtualNetworks/subnets"
}
],
"tags": {},
"type": "Microsoft.Network/virtualNetworks",
"virtualNetworkPeerings": []
}
}
可使用 az network vnet list 查看设置列表,或前往 Azure 门户进行查看。
2. 网络安全组
网络安全组与虚拟网络关联,包含一组安全规则。安全规则定义了子网或虚拟机的入站和出站流量策略,用户可借此定义是否允许入站流量访问组资源以及是否允许出站流量。安全规则包含以下要素:
- 优先级(范围为 100 到 4096,数值越小,优先级越高)
- 源或目标(CIDR 块、一组 IP 地址或另一个安全组)
- 协议(TCP/UDP/ICMP)
- 方向(入站/出站)
- 端口范围
- 操作(允许/拒绝)
下面是创建名为 test-nsg 的网络安全组的示例:
# az network nsg create --name test-nsg --resource-group devops --location centralus
输出结果:
{
"NewNSG": {
"defaultSecurityRules": [
{
"access": "Allow",
"description": "Allow inbound traffic from all VMs in VNET",
"destinationAddressPrefix": "VirtualNetwork",
"name": "AllowVnetInBound",
"priority": 65000,
"sourceAddressPrefix": "VirtualNetwork",
// ...
"type": "Microsoft.Network/networkSecurityGroups/defaultSecurityRules"
},
{
"access": "Allow",
"description": "Allow inbound traffic from azure load balancer",
"destinationAddressPrefix": "*",
"name": "AllowAzureLoadBalancerInBound",
"priority": 65001,
"sourceAddressPrefix": "AzureLoadBalancer",
// ...
"type": "Microsoft.Network/networkSecurityGroups/defaultSecurityRules"
},
{
"access": "Deny",
"description": "Deny all inbound traffic",
"destinationAddressPrefix": "*",
"name": "DenyAllInBound",
"priority": 65500,
"sourceAddressPrefix": "*",
// ...
"type": "Microsoft.Network/networkSecurityGroups/defaultSecurityRules"
},
{
"access": "Allow",
"description": "Allow outbound traffic from all VMs to all VMs in VNET",
"destinationAddressPrefix": "VirtualNetwork",
"name": "AllowVnetOutBound",
"priority": 65000,
"sourceAddressPrefix": "VirtualNetwork",
// ...
"type": "Microsoft.Network/networkSecurityGroups/defaultSecurityRules"
},
{
"access": "Allow",
"description": "Allow outbound traffic from all VMs to Internet",
"destinationAddressPrefix": "Internet",
"name": "AllowInternetOutBound",
"priority": 65001,
"sourceAddressPrefix": "*",
// ...
"type": "Microsoft.Network/networkSecurityGroups/defaultSecurityRules"
},
{
"access": "Deny",
"description": "Deny all outbound traffic",
"destinationAddressPrefix": "*",
"name": "DenyAllOutBound",
"priority": 65500,
"sourceAddressPrefix": "*",
// ...
"type": "Microsoft.Network/networkSecurityGroups/defaultSecurityRules"
}
],
"id": "...test-nsg",
"location": "centralus",
"name": "test-nsg",
"networkInterfaces": null,
"provisioningState": "Succeeded",
"resourceGroup": "devops",
"resourceGuid": "9e0e3d0f-e99f-407d-96a6-8e96cf99ecfc",
"securityRules": [],
"subnets": null,
"tags": null,
"type": "Microsoft.Network/networkSecurityGroups"
}
}
可以看到,Azure 为每个网络安全组创建了一组默认规则,默认情况下拒绝所有入站流量并允许出站流量。源和目标不能是 CIDR 块,而必须是服务标签,如 VirtualNetwork 。这些服务标签实际上是一组预定义的 IP 地址前缀,Azure 会每周管理并发布这些服务标签,可在 Microsoft 下载中心 查看发布的服务标签。
默认规则如下表所示:
| 方向 | 优先级 | 源 | 源端口 | 目标 | 目标端口 | 协议 | 操作 |
| ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- |
| 入站 | 65000 | VirtualNetwork | 0 - 65535 | VirtualNetwork | 0 - 65535 | 所有 | 允许 |
| 入站 | 65001 | AzureLoadBalancer | 0 - 65535 | 0.0.0.0/0 | 0 - 65535 | 所有 | 允许 |
| 入站 | 65500 | 0.0.0.0/0 | 0 - 65535 | 0.0.0.0/0 | 0 - 65535 | 所有 | 拒绝 |
| 出站 | 65000 | VirtualNetwork | 0 - 65535 | VirtualNetwork | 0 - 65535 | 所有 | 允许 |
| 出站 | 0.0.0.0/0 | 0 - 65535 | 0.0.0.0/0 | Internet | 0 - 65535 | 所有 | 允许 |
| 出站 | 0.0.0.0/0 | 0 - 65535 | 0.0.0.0/0 | 0.0.0.0/0 | 0 - 65535 | 所有 | 拒绝 |
接下来,创建一个接受所有规则且优先级最高的网络安全组规则,并将其附加到刚创建的 NSG 上:
# az network nsg rule create --name test-nsg --priority 100 --resource-group devops --nsg-name test-nsg
输出结果:
{
"access": "Allow",
"description": null,
"destinationAddressPrefix": "*",
"destinationAddressPrefixes": [],
"destinationApplicationSecurityGroups": null,
"destinationPortRange": "80",
"destinationPortRanges": [],
"direction": "Inbound",
"etag": "W/\"65e33e31-ec3c-4eea-8262-1cf5eed371b1\"",
"id": "/subscriptions/.../resourceGroups/devops/providers/Microsoft.Network/networkSecurityGroups/test-nsg/securityRules/test-nsg",
"name": "test-nsg",
"priority": 100,
"protocol": "*",
"provisioningState": "Succeeded",
"resourceGroup": "devops",
"sourceAddressPrefix": "*",
"sourceAddressPrefixes": [],
"sourceApplicationSecurityGroups": null,
"sourcePortRange": "*",
"sourcePortRanges": [],
"type": "Microsoft.Network/networkSecurityGroups/securityRules"
}
3. 应用程序安全组
应用程序安全组是 VM NIC 的逻辑集合,可作为网络安全组规则中的源或目标,使网络安全组更加灵活。例如,假设有两台 VM 通过 5432 端口访问 PostgreSQL 数据库,可创建两个应用程序安全组 web 和 db ,将 VM 加入 web 组,将数据库加入 db 组,并创建以下网络安全组规则:
| 方向 | 优先级 | 源 | 源端口 | 目标 | 目标端口 | 协议 | 操作 |
| ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- |
| 入站 | 120 | * | * | db | 0 - 65535 | 所有 | 拒绝 |
| 入站 | 110 | web | * | db | 5432 | TCP | 允许 |
根据上述规则,第二条规则的优先级高于第一条,只有 web 组能通过 5432 端口访问 db 组,其他入站流量将被拒绝。
4. 子网
子网可与网络安全组关联,也可与路由表关联以实现特定路由。与 AWS 类似,Azure 也提供路由表资源用于路由管理。默认情况下,Azure 为虚拟网络和子网提供了默认路由,使用 AKS 服务时无需担心路由问题。
创建虚拟网络时,会默认创建一个默认子网,可使用以下命令列出子网:
# az network vnet subnet list --vnet-name devops-vnet --resource-group devops
输出结果:
[
{
"addressPrefix": "10.0.0.0/24",
"addressPrefixes": null,
"delegations": [],
"etag": "W/\"a93c56be-6eab-4391-8fca-25e11625c6e5\"",
"id": "/subscriptions/f825790b-ac24-47a3-89b8-9b4b3974f0d5/resourceGroups/devops/providers/Microsoft.Network/virtualNetworks/devops-vnet/subnets/default",
"interfaceEndpoints": null,
"ipConfigurationProfiles": null,
"ipConfigurations": null,
"name": "default",
"networkSecurityGroup": null,
"provisioningState": "Succeeded",
"purpose": null,
"resourceGroup": "devops",
"resourceNavigationLinks": null,
"routeTable": null,
"serviceAssociationLinks": null,
"serviceEndpointPolicies": null,
"serviceEndpoints": null,
"type": "Microsoft.Network/virtualNetworks/subnets"
}
]
除默认子网外,还可创建一个前缀为 10.0.1.0/24 的子网:
# az network vnet subnet create --address-prefixes 10.0.1.0/24 --name test --vnet-name devops-vnet --resource-group devops
输出结果:
{
"addressPrefix": "10.0.1.0/24",
"addressPrefixes": null,
"delegations": [],
"etag": "W/\"9f7e284f-fd31-4bd3-ad09-f7dc75bb7c68\"",
"id": "/subscriptions/f825790b-ac24-47a3-89b8-9b4b3974f0d5/resourceGroups/devops/providers/Microsoft.Network/virtualNetworks/devops-vnet/subnets/test",
"interfaceEndpoints": null,
"ipConfigurationProfiles": null,
"ipConfigurations": null,
"name": "test",
"networkSecurityGroup": null,
"provisioningState": "Succeeded",
"purpose": null,
"resourceGroup": "devops",
"resourceNavigationLinks": null,
"routeTable": null,
"serviceAssociationLinks": null,
"serviceEndpointPolicies": null,
"serviceEndpoints": null,
"type": "Microsoft.Network/virtualNetworks/subnets"
}
再使用以下命令列出该 VNet 中的子网:
# az network vnet subnet list --vnet-name devops-vnet --resource-group devops | jq .[].name
输出结果:
"default"
"test"
这里的 jq 是一个 JSON 命令行处理器,云 shell 中默认已安装,可用于列出 JSON 输出中所需的字段。若不熟悉 jq ,可查看 手册 。
5. Azure 虚拟机
Azure 中的虚拟机(VM)类似于 Amazon EC2。要启动实例,需知道要使用的 VM 映像,可使用 az vm image list 命令列出可用的映像。以下是列出 VM 映像的示例:
# az vm image list --output table
输出结果:
You are viewing an offline list of images, use --all to retrieve an up-to-date list
Offer Publisher Sku Urn UrnAlias Version
----------------- ------------------ --------- ----------------------------------- ----------------- ---------
CentOS OpenLogic 7.5 OpenLogic:CentOS:7.5:latest CentOS latest latest
CoreOS CoreOS Stable CoreOS:CoreOS:Stable:latest CoreOS latest latest
Debian credativ 8 credativ:Debian:8:latest Debian latest latest
openSUSE-Leap SUSE 42.3 SUSE:openSUSE-Leap:42.3:latest openSUSE-Leap latest
RHEL RedHat 7-RAW RedHat:RHEL:7-RAW:latest RHEL latest latest
SLES SUSE 12-SP2 SUSE:SLES:12-SP2:latest SLES latest latest
UbuntuServer Canonical 16.04-LTS Canonical:UbuntuServer:16.04-LTS:latest UbuntuLTS latest
WindowsServer MicrosoftWindowsServer 2019-Datacenter MicrosoftWindowsServer:WindowsServer:2019-Datacenter:latest Win2019Datacenter latest
WindowsServer MicrosoftWindowsServer 2016-Datacenter MicrosoftWindowsServer:WindowsServer:2016-Datacenter:latest Win2016Datacenter latest
WindowsServer MicrosoftWindowsServer 2012-R2-Datacenter MicrosoftWindowsServer:WindowsServer:2012-R2-Datacenter:latest Win2012R2Datacenter latest
WindowsServer MicrosoftWindowsServer 2012-Datacenter MicrosoftWindowsServer:WindowsServer:2012-Datacenter:latest Win2012Datacenter latest
WindowsServer MicrosoftWindowsServer 2008-R2-SP1 MicrosoftWindowsServer:WindowsServer:2008-R2-SP1:latest Win2008R2SP1 latest
接下来,使用 az vm create 启动 VM,并指定 --generate-ssh-keys 创建 SSH 密钥以进行访问:
# az vm create --resource-group devops --name newVM --image CentOS --admin-username centos-user --generate-ssh-keys
输出结果:
SSH key files '/home/chloe/.ssh/id_rsa' and '/home/chloe/.ssh/id_rsa.pub' have been generated under ~/.ssh to allow SSH access to the VM. If using machines without permanent storage, back up your keys to a safelocation.
- Running ..
{
"fqdns": "",
"id": "/subscriptions/f825790b-ac24-47a3-89b8-9b4b3974f0d5/resourceGroups/devops/providers/Microsoft.Compute/virtualMachines/newVM",
"location": "centralus",
"macAddress": "00-0D-3A-96-6B-A2",
"powerState": "VM running",
"privateIpAddress": "10.0.0.4",
"publicIpAddress": "40.77.97.79",
"resourceGroup": "devops",
"zones": ""
}
可以看到,新创建的 VM 的公共 IP 地址为 40.77.97.79 ,使用之前指定的用户名连接到该 VM:
# ssh centos-user@40.77.97.79
由于一直允许 SSH 访问实例不太安全,下面是禁止 SSH 访问的步骤:
1. 查找附加到该 VM 的网络接口:
# az vm get-instance-view --name newVM --resource-group devops
从输出中找到网络接口的 ID。
2. 根据 NIC ID 查找关联的网络安全组:
# az network nic show --ids /subscriptions/f825790b-ac24-47a3-89b8-9b4b3974f0d5/resourceGroups/devops/providers/Microsoft.Network/networkInterfaces/newVMVMNic | jq .networkSecurityGroup
从输出中找到网络安全组的名称,这里为 newVMNSG 。
3. 列出附加到该 NSG 的规则:
# az network nsg rule list --resource-group devops --nsg-name newVMNSG
输出结果:
[
{
"access": "Allow",
"description": null,
"destinationAddressPrefix": "*",
"destinationAddressPrefixes": [],
"destinationApplicationSecurityGroups": null,
"destinationPortRange": "22",
"destinationPortRanges": [],
"direction": "Inbound",
"etag": "W/\"9ab6b2d7-c915-4abd-9c02-a65049a62f02\"",
"id": "/subscriptions/f825790b-ac24-47a3-89b8-9b4b3974f0d5/resourceGroups/devops/providers/Microsoft.Network/networkSecurityGroups/newVMNSG/securityRules/default-allow-ssh",
"name": "default-allow-ssh",
"priority": 1000,
"protocol": "Tcp",
"provisioningState": "Succeeded",
"resourceGroup": "devops",
"sourceAddressPrefix": "*",
"sourceAddressPrefixes": [],
"sourceApplicationSecurityGroups": null,
"sourcePortRange": "*",
"sourcePortRanges": [],
"type": "Microsoft.Network/networkSecurityGroups/securityRules"
}
]
- 删除默认的 SSH 允许规则:
# az network nsg rule delete --ids "/subscriptions/f825790b-ac24-47a3-89b8-9b4b3974f0d5/resourceGroups/devops/providers/Microsoft.Network/networkSecurityGroups/newVMNSG/securityRules/default-allow-ssh"
删除规则后,将无法再通过 SSH 访问该 VM。
下面是创建 Azure 虚拟网络及相关资源的 mermaid 流程图:
graph LR
A[登录 Azure 账户] --> B[附加云存储]
B --> C[启动云 shell 控制台]
C --> D[创建资源组]
D --> E[创建虚拟网络]
E --> F[创建网络安全组]
F --> G[创建子网]
G --> H[创建虚拟机]
6. 存储账户
存储账户是 Azure 存储解决方案提供的存储对象(如文件、表和磁盘)的容器。用户可根据使用需求创建一个或多个存储账户。
存储账户有三种类型:
- 通用 v2 账户
- 通用 v1 账户
- Blob 存储账户
其中,v1 是旧版存储账户类型,Blob 存储账户仅允许使用 Blob 存储。目前,v2 账户是 Azure 中最推荐的账户类型。
7. 负载均衡器
Azure 负载均衡器的功能与其他公共云的负载均衡器类似,用于将流量路由到后端,并对端点进行健康检查,若发现后端不健康则排空连接。Azure 负载均衡器与其他负载均衡器的主要区别在于,它可以有多个 IP 地址和多个端口。这意味着创建新的 LoadBalancer 服务时,AKS 无需创建新的负载均衡器,而是在现有负载均衡器内创建新的前端 IP。
8. Azure 磁盘
Azure 磁盘有两种类型:托管磁盘和非托管磁盘。在 Azure 托管磁盘出现之前,用户必须创建存储账户才能使用非托管磁盘,而存储账户可能会超出可扩展性目标(https://docs.microsoft.com/en-us/azure/storage/common/storage-scalability-targets),影响 I/O 性能。使用托管磁盘时,无需自行创建存储账户,Azure 会在后台管理这些账户。
Azure 磁盘有不同的性能层级:
- 标准 HDD 磁盘
- 标准 SSD 磁盘
- 高级 SSD 磁盘
HDD 磁盘(https://en.wikipedia.org/wiki/Hard_disk_drive)是一种经济高效的选择,而 SSD(https://en.wikipedia.org/wiki/Solid-state_drive)具有更好的性能。对于 I/O 密集型工作负载,高级层级是最佳选择。使用高级 SSD 磁盘连接到 VM 时,VM 可以达到高达 80,000 IOPS 和 2,000 MB/s 的磁盘吞吐量。
Azure 磁盘还提供四种类型的复制:
- 本地冗余存储(LRS):数据仅持久存储在单个区域
- 区域冗余存储(ZRS):数据跨三个区域持久存储
- 异地冗余存储(GRS):跨区域复制
- 读访问异地冗余存储(RA - GRS):跨区域复制并带有读副本
以下是 Azure 磁盘类型及复制类型的表格总结:
| 磁盘类型 | 性能层级 | 复制类型 |
| ---- | ---- | ---- |
| 托管磁盘 | 标准 HDD、标准 SSD、高级 SSD | LRS、ZRS、GRS、RA - GRS |
| 非托管磁盘 | - | - |
9. Azure Kubernetes 服务
Azure Kubernetes 服务是 Azure 中的托管 Kubernetes 服务。集群包含一组节点(如 Azure VM),与普通的 Kubernetes 节点一样,节点上安装了 kube - proxy 和 kubelet。kube - proxy 与 Azure 虚拟 NIC 通信,管理服务和 Pod 的进出路由;kubelet 接收来自主节点的请求,调度 Pod 并报告指标。
在 Azure 中,可以挂载各种 Azure 存储选项(如 Azure 磁盘和 Azure 文件)作为持久卷(PV),用于持久保存容器的数据。
下面是使用 Azure Kubernetes 服务的简单步骤:
1. 确保已经创建了所需的资源组、虚拟网络、子网等基础设施。
2. 使用 Azure CLI 创建 AKS 集群,示例命令如下:
# az aks create --resource-group devops --name myAKSCluster --node-count 1 --generate-ssh-keys
- 配置 kubectl 以与 AKS 集群通信:
# az aks get-credentials --resource-group devops --name myAKSCluster
- 部署应用程序到 AKS 集群,例如部署一个简单的 Nginx 应用:
# kubectl create deployment nginx --image=nginx
# kubectl expose deployment nginx --port=80 --type=LoadBalancer
以下是使用 Azure Kubernetes 服务的 mermaid 流程图:
graph LR
A[创建基础设施资源] --> B[创建 AKS 集群]
B --> C[配置 kubectl]
C --> D[部署应用程序]
综上所述,在 Azure 上部署 Kubernetes 涉及多个步骤和多种资源的配置,包括虚拟网络、网络安全组、虚拟机、存储账户、负载均衡器、磁盘等。通过合理配置这些资源,可以构建一个高效、安全且可扩展的 Kubernetes 环境。在实际操作中,可根据具体需求和场景灵活调整配置,以满足不同的业务需求。
超级会员免费看
549

被折叠的 条评论
为什么被折叠?



