APPPA
云服务器组管理
在云服务器的/root/huawei 目录下编写 create_server_group.py 文件,并导入提供的 huaweicloudapi.py 文件获取连接。编写 Python 代码,调用 compute 类,创建华为云的云服务器组,具体要求为
(1) 云服务器组名称:chinaskills_server_group;
(2) 云服务器组策略:反亲和性;
(3)如果安全组已经存在,代码中需要先删除;
(4)使用其源码的 get 方法输出此云服务器组的详细信息。
[root@chinaskill huawei]# cat huaweicloudapi.py
from openstack import connection
"""
两种获取华为云Python API认证连接方式:
1.通过账号密码方式
(1) 输入账号密码和domain
(2) 其他参数禁止修改
2.通过AKSK方式
(1) 输入AKSK和项目ID
(2) 其他参数禁止修改
以上两种方式任选一种
"""
def get_conn_aksk():
conn = connection.Connection(
cloud="myhuaweicloud.com", # 认证地址
ak="", # Access Key Id
sk="", # Secret Access Key
project_id="", # 项目id, cn-north-4 华北-北京四
region="cn-east-3" # 项目名, cn-north-4 华北-北京四
)
return conn
[root@chinaskill huawei]# cat create_server_group.py
import huaweicloudapi as api
import collections
# delete exist server group
def if_exist(name):
server_groups = conn.compute.server_groups()
for server_group in server_groups:
if name == server_group.name:
conn.compute.delete_server_group(server_group.id)
# create server group
def create_server_group(name):
if_exist(name)
data = {
"name": name,
"policies": ['anti-affinity']
}
server_group = conn.compute.create_server_group(**data)
print(server_group.name, server_group.id, server_group.policies)
if __name__ == "__main__":
conn = api.get_conn_aksk()
name = "chinaskills_server_group"
create_server_group(name)
[root@chinaskill huawei]# python3 create_server_group.py
chinaskills_server_group 364f3852-fe3c-4660-9de1-769179e017c6 ['anti-affinity']
安全组管理
在云服务器的/root/huawei 目录下编写 create_security_group.py 文件,并导入提供的huaweicloudapi.py 文件获取连接。编写 Python 代码,调用 vpcv1 类,创建华为云的安全组,具体要求为
(1) 安全组名称:chinaskills_security_group;
(2) 如果安全组已经存在,代码中需要先删除;
(3) 使用其源码的 get 方法输出此安全组的详细信息。
[root@chinaskill huawei]# cat create_security_group.py
import huaweicloudapi as api
# delete exist security group
def if_exist(name):
security_groups = conn.vpcv1.security_groups()
for security_group in security_groups:
if name == security_group.name:
conn.vpcv1.delete_security_group(security_group)
# create security group
def create_security_group(name):
if_exist(name)
data = {
"name": name,
}
security_group = conn.vpcv1.create_security_group(**data)
print(security_group)
if __name__ == '__main__':
conn = api.get_conn_aksk()
name = "chinaskills_security_group"
create_security_group(name)
[root@chinaskill huawei]# python3 create_security_group.py
openstack.vpc.v1.security_group.SecurityGroup(name=chinaskills_security_group, id=c656ec1d-4691-4826-8807-f83cc0723e2a, description=, enterprise_project_id=0, security_group_rules=[{'description': None, 'direction': 'egress', 'ethertype': 'IPv6', 'id': '30af9ef0-7abb-4ca7-8e86-cdae13cbf302', 'port_range_max': None, 'port_range_min': None, 'protocol': None, 'remote_group_id': None, 'remote_ip_prefix': None, 'security_group_id': 'c656ec1d-4691-4826-8807-f83cc0723e2a', 'tenant_id': '0bcb715a338025922f52c001624dfcc9', 'remote_address_group_id': None}, {'description': None, 'direction': 'egress', 'ethertype': 'IPv4', 'id': '1b599891-83de-46a0-b782-c797c65cc7c4', 'port_range_max': None, 'port_range_min': None, 'protocol': None, 'remote_group_id': None, 'remote_ip_prefix': None, 'security_group_id': 'c656ec1d-4691-4826-8807-f83cc0723e2a', 'tenant_id': '0bcb715a338025922f52c001624dfcc9', 'remote_address_group_id': None}, {'description': '', 'direction': 'ingress', 'ethertype': 'IPv4', 'id': '7509085e-eaab-4ca9-9791-546ea767bbbc', 'port_range_max': None, 'port_range_min': None, 'protocol': None, 'remote_group_id': 'c656ec1d-4691-4826-8807-f83cc0723e2a', 'remote_ip_prefix': None, 'security_group_id': 'c656ec1d-4691-4826-8807-f83cc0723e2a', 'tenant_id': '0bcb715a338025922f52c001624dfcc9', 'remote_address_group_id': None}, {'description': 'Permit default Linux SSH port.', 'direction': 'ingress', 'ethertype': 'IPv4', 'id': 'e35dba40-1d2c-4906-8b44-0be86856fa33', 'port_range_max': 22, 'port_range_min': 22, 'protocol': 'tcp', 'remote_group_id': None, 'remote_ip_prefix': '0.0.0.0/0', 'security_group_id': 'c656ec1d-4691-4826-8807-f83cc0723e2a', 'tenant_id': '0bcb715a338025922f52c001624dfcc9', 'remote_address_group_id': None}, {'description': 'Permit default Windows remote desktop port.', 'direction': 'ingress', 'ethertype': 'IPv4', 'id': 'b3a3db02-298a-4579-b5e6-f3329bd6927c', 'port_range_max': 3389, 'port_range_min': 3389, 'protocol': 'tcp', 'remote_group_id': None, 'remote_ip_prefix': '0.0.0.0/0', 'security_group_id': 'c656ec1d-4691-4826-8807-f83cc0723e2a', 'tenant_id': '0bcb715a338025922f52c001624dfcc9', 'remote_address_group_id': None}, {'description': '', 'direction': 'ingress', 'ethertype': 'IPv6', 'id': 'e67c5d26-7462-4c90-ba5d-0d7c37afe5a0', 'port_range_max': None, 'port_range_min': None, 'protocol': None, 'remote_group_id': 'c656ec1d-4691-4826-8807-f83cc0723e2a', 'remote_ip_prefix': None, 'security_group_id': 'c656ec1d-4691-4826-8807-f83cc0723e2a', 'tenant_id': '0bcb715a338025922f52c001624dfcc9', 'remote_address_group_id': None}])
安全组规则管理
在云服务器的/root/huawei 目录下编写 create_security_group_rule.py 文件,并导入提供的 huaweicloudapi.py 文件获取连接。基于上一题的安全组,编写 Python 代码,调用 vpcv1 类,创建华为云的安全组规则,具体要求为
(1) 使用安全组名称获取其 ID(不允许直接填写安全组 ID);
(2) 删除此安全组里所有规则(保证代码可以重复执行);
(3) 放通出方向规则:所有协议端口;
(4) 放通入方向规则:TCP 协议 22 端口;
(5) 放通入方向规则:ICMP 协议所有端口;
(6) 使用其源码的 get 方法输出此安全组的详细信息。
[root@chinaskill huawei]# cat create_security_group_rule.py
import huaweicloudapi as api
# get security group id
def get_security_group_id(name):
security_groups = conn.vpcv1.security_groups()
for security_group in security_groups:
if name == security_group.name:
return security_group.id
# delete exist security group rule
def if_exist(security_group_id):
data = {
"security_group_id": security_group_id,
}
sgrs = conn.vpcv1.security_group_rules(**data)
for sgr in sgrs:
conn.vpcv1.delete_security_group_rule(sgr.id)
# create security group rule
def create_security_group_rule(security_group_id):
if_exist(security_group_id)
egress = {
"security_group_id": security_group_id,
"direction": "egress"
}
ssh = {
"security_group_id": security_group_id,
"direction": "ingress",
"protocol": "tcp",
"port_range_min": 22,
"port_range_max": 22
}
icmp = {
"security_group_id": security_group_id,
"direction": "ingress",
"protocol": "icmp"
}
print(conn.vpcv1.create_security_group_rule(**egress))
print(conn.vpcv1.create_security_group_rule(**ssh))
print(conn.vpcv1.create_security_group_rule(**icmp))
if __name__ == '__main__':
conn = api.get_conn_aksk()
security_group_id = get_security_group_id("chinaskills_security_group")
create_security_group_rule(security_group_id)
[root@chinaskill huawei]# python3 create_security_group_rule.py
openstack.vpc.v1.security_group_rule.SecurityGroupRule(security_group_id=c656ec1d-4691-4826-8807-f83cc0723e2a, direction=egress, description=, ethertype=IPv4, id=e861cf18-be4b-4278-b565-b2a5081e6855, port_range_max=None, port_range_min=None, protocol=None, remote_group_id=None, remote_ip_prefix=None, tenant_id=0bcb715a338025922f52c001624dfcc9)
openstack.vpc.v1.security_group_rule.SecurityGroupRule(security_group_id=c656ec1d-4691-4826-8807-f83cc0723e2a, direction=ingress, protocol=tcp, port_range_min=22, port_range_max=22, description=, ethertype=IPv4, id=94eec5b9-5a05-44ed-bcd3-0f3c02e65d53, remote_group_id=None, remote_ip_prefix=None, tenant_id=0bcb715a338025922f52c001624dfcc9)
openstack.vpc.v1.security_group_rule.SecurityGroupRule(security_group_id=c656ec1d-4691-4826-8807-f83cc0723e2a, direction=ingress, protocol=icmp, description=, ethertype=IPv4, id=babe1193-ce8b-42d8-a18c-c873d3e877cf, port_range_max=None, port_range_min=None, remote_group_id=None, remote_ip_prefix=None, tenant_id=0bcb715a338025922f52c001624dfcc9)
虚拟私有云管理
在云服务器的 /root/huawei 目录下编写 create_vpc.py 文件,并导入提供的 huaweicloudapi.py 文件获取连接。编写 Python 代码,调用 vpcv1 类,创建华为云的虚拟私有云,具体要求为
(1) 虚拟私有云名称:chinaskills_vpc;
(2) 如果虚拟私有云已经存在,代码中需要先删除;
(3) 使用其源码的 get 方法输出此虚拟私有云的详细信息(状态要求为 OK)。
[root@chinaskill huawei]# cat create_vpc.py
import huaweicloudapi as api
import time
# delete exist vpc
def if_exist(name):
vpcs = conn.vpcv1.vpcs()
for vpc in vpcs:
if name == vpc.name:
conn.vpcv1.delete_vpc(vpc.id)
# create vpc
def create_vpc(name):
if_exist(name)
data = {
"name": name
}
vpc1 = conn.vpcv1.create_vpc(**data)
# 等待状态为OK
time.sleep(3)
vpc2 = conn.vpcv1.get_vpc(vpc1.id)
print(vpc2.name, vpc2.id, vpc2.status)
if __name__ == '__main__':
conn = api.get_conn_aksk()
name = "chinaskills_vpc"
create_vpc(name)
[root@chinaskill huawei]# python3 create_vpc.py
chinaskills_vpc e67aa383-2ebf-48ba-9532-b6b04a99af07 OK
虚拟私有云子网管理
在云服务器的/root/huawei 目录下编写 create_subnet.py 文件,并导入提供的 huaweicloudapi.py 文件获取连接。编写 Python 代码,调用 vpcv1 类,创建华为云的虚拟私有云子网,具体要求为
(1) 使用虚拟私有云名称获取其 ID(不允许直接填写虚拟私有云 ID);
(2) 虚拟私有云子网名称:chinaskills_subnet;
(3) 虚拟私有云子网网段:192.168.100.0/24;
(4) 虚拟私有云子网网关:192.168.100.1;
(5) 虚拟私有云子网可用区域:cn-north-4a;
(6) 如果虚拟私有云子网已经存在,代码中需要先删除;
(7) 使用其源码的 get 方法输出此虚拟私有云子网的详细信息(状态要求为 ACTIVE)。
完成后提交云服务器节点的用户名、密码和 IP 地址到答题框。
[root@chinaskill huawei]# cat create_subnet.py
import huaweicloudapi as api
import time
# get vpc id
def get_vpc_id(name):
vpcs = conn.vpcv1.vpcs()
for vpc in vpcs:
if name == vpc.name:
return vpc.id
# delete exist subnet
def if_exist(name, vpc_id):
subnets = conn.vpcv1.subnets()
for subnet in subnets:
if name == subnet.name:
conn.vpcv1.delete_subnet(subnet.id, vpc_id)
# create subnet
def create_subnet(name, cidr, gateway_ip, vpc_name, availability_zone):
vpc_id = get_vpc_id(vpc_name)
if_exist(name, vpc_id)
# 如果存在则删除,等待删除完成
time.sleep(3)
data = {
"name": name,
"cidr": cidr,
"gateway_ip": gateway_ip,
"vpc_id": vpc_id,
"availability_zone": availability_zone,
}
subnet1 = conn.vpcv1.create_subnet(**data)
# 等待状态为ACTIVE
time.sleep(3)
subnet2 = conn.vpcv1.get_subnet(subnet1.id)
print(subnet2)
if __name__ == '__main__':
conn = api.get_conn_aksk()
name = "chinaskills_subnet"
cidr = "192.168.100.0/24"
gateway_ip = "192.168.100.1"
vpc_name = "chinaskills_vpc"
availability_zone = "cn-east-3a"
create_subnet(name, cidr, gateway_ip, vpc_name, availability_zone)
[root@chinaskill huawei]# python3 create_subnet.py
openstack.vpc.v1.subnet.Subnet(id=19667c09-eaac-47dc-ba41-f6a8569b1269, name=chinaskills_subnet, cidr=192.168.100.0/24, dnsList=[], status=ACTIVE, vpc_id=e67aa383-2ebf-48ba-9532-b6b04a99af07, gateway_ip=192.168.100.1, dhcp_enable=True, availability_zone=cn-east-3a, neutron_network_id=19667c09-eaac-47dc-ba41-f6a8569b1269, neutron_subnet_id=4477fd7a-345e-4fa2-a49b-07730ace4e22)
密钥对管理
在云服务器的/root/huawei 目录下编写 create_keypair.py 文件,并导入提供的 huaweicloudapi.py 文件获取连接。编写 Python 代码,调用 compute 类,创建华为云的秘钥对,具体要求为
(1) 秘钥对名称:chinaskills_keypair;
(2) 如果秘钥对已经存在,代码中需要先删除;
(3) 使用其源码的 get 方法输出此秘钥对详细信息。
[root@chinaskill huawei]# cat create_keypair.py
import huaweicloudapi as api
# delete exist keypair
def if_exist(name):
keypairs = conn.compute.keypairs()
for keypair in keypairs:
if name == keypair.name:
conn.compute.delete_keypair(name)
# create keypair
def create_keypair(name):
if_exist(name)
data = {
"name": name
}
keypair = conn.compute.create_keypair(**data)
print(keypair)
if __name__ == "__main__":
conn = api.get_conn_aksk()
name = "chinaskills_keypair"
create_keypair(name)
[root@chinaskill huawei]# python3 create_keypair.py
openstack.compute.v2.keypair.Keypair(name=chinaskills_keypair, fingerprint=SHA256:mpGbwkvbOWpPQ5pN170SBYTpv4kjaMhjUk8a4VCw75Q, public_key=ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCqlNhP/zGLPx4i+99yZXg4DCAlBlmF85vljxf1NFvk32pwiKTh5ISyEF1n7icYUbI3fuwsanQrPZLDjInUsbj0Hb69/y9NR3KaDMF3lw0Q3NYNjex9L++avGSZlwuprbIUflfVpOdHDGWCeesPPnF056z/nvLpp7PEiLSCEFTUKCeSfQRxloj4VbcBC9F2yVP57aiI+Jox1kTR25gOSiwnSTeEdzqInpt6EN592uGWkh1yodJHRliUaRMcCaQVxUjMvP1oTgWQET9fVm62At/hsHj3DfkT0JLwLawUV7qE/7MvQpRN8Pvt1tTCz7aQaPR+IH9C2jUYPJFs7SwheRM/ Generated-by-Nova
, private_key=-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAqpTYT/8xiz8eIvvfcmV4OAwgJQZZhfOb5Y8X9TRb5N9qcIik
4eSEshBdZ+4nGFGyN37sLGp0Kz2Sw4yJ1LG49B2+vf8vTUdymgzBd5cNENzWDY3s
fS/vmrxkmZcLqa2yFH5X1aTnRwxlgnnrDz5xdOes/57y6aezxIi0ghBU1Cgnkn0E
cZaI+FW3AQvRdslT+e2oiPiaMdZE0duYDkosJ0k3hHc6iJ6behDefdrhlpIdcqHS
R0ZYlGkTHAmkFcVIzLz9aE4FkBE/X1ZutgLf4bB49w35E9CS8C2sFFe6hP+zL0KU
TfD77dbUws+2kGj0fiB/Qto1GDyRbO0sIXkTPwIDAQABAoIBAAz+9A/8MEkckO43
ZEmx41vZyKUpe66R5cGyii+7QnkIcHKMZotzPozWZFdjtITMzJgfylyD67/TnBfb
4DBdLesjTQHqrC7mD5we9qpmzq8HNCOqOWc8PLP8QGnXMSNHQXcAPS7gtmIaVq1/
HmJ17mzqrap38KfOiAqkI6R5HA1htdkEEqFXw2wWTi+MZzsX+DX492KATewCUqK2
oYPkSwqfBoHwYK6rhtgMB04u/N3QAPEAywC9iLJyrDb+vGB6OfpTlsHI9AzRS1FS
4a5w7H0t2fHmTmB0mCsZ2oLW4ukqaopUaK9XPDJvwBGdowPBUfXvc0vAqUABri3G
zLT2M0kCgYEA4jKZtjELhQub3J8+FJTNC14BLPLDEW6WD2dmDUoOo0GDRO6DiuCw
27XEZPxRPBFojtQOG+D063jr4L6tJVYITO1Kn3m+qDLAFd/NnBKDU1EcVfleNi86
mvCEHN+KhhPxFPsjBCjZnjCVC8kJcoV/kekkd5DY72IY95cVsmHwAkUCgYEAwQ5e
rwgs11Nv9NiE1zYCeM65mlNq/S9RteatUGR3eZJZwe4/bxoz6e8eARvm6ElIltKU
iFSM7ijM/i/iwcAQ8ZcSvaMaUdcR4sNBF+PqdE7UrnaFcqowUOtoq1Glr+bCpfVr
pk6gIbeEaZdmJnBPYRT9MoxSibcDpisbl0r/2bMCgYB/P4ABYm3clr9DycGeymbf
Mi7vHlJmiBOa7swWRcceSEiUiqktg2F6+DJXrSn15MUxNFILlO2zWf84G4xEKLXa
sNL+C/sZnBM7Ov/sAO4n/MU4CGTbjo4lg5COvaKr8qP47Z6xRFnICASQ8LOD3V36
s3F0YePoJwNspW3rP/VJEQKBgQCnt3ssZlbGbjc6KAwoB0TYfQejecrd0AkVlSSG
TfG5jRorL3auZ74oAlM3tWzB6kCEvsbJNWMF2C5WuXUuy1Qs/gkK9LJ93PEt5mEb
hyZ9CqscMd03Q4EcG5dgFltx2HDLys1NusrE3/DJ5vyYckOT3K8nBd8CwxsJdjPP
GRnynwKBgH4D5fcxgOmgLf3xAQAB2UAh4WSZfPKKJSGyGOgurTsVKWz5Djdk+SAF
1kVul4xaXf6vZ9mz5PPVvFq9KAwW+u0B9yBgTvNG+qziBkQqFk2eQPe0g7Ydpw2Q
+rd2aZNgDtjExD9flti0q1QQ0ZPGkv4OT3FsPtYxfc3XS8txxAYm
-----END RSA PRIVATE KEY-----
, user_id=0bcb715ae480f30e1f15c00159d9840b)
云服务器管理
在云服务器的/root/huawei 目录下编写 create_server.py 文件,并导入提供的huaweicloudapi.py 文件获取连接。编写 Python 代码,调用 ecs 类,创建华为云的云服务器,具体要求为
(1) 云服务器名称:chinaskills_server;
(2) 云服务器可用区域:cn-north-4a;
(3) 云服务器镜像:CentOS 7.5 64bit;
(4) 云服务系统盘:磁盘规格 SSD、大小 100G;
(5) 云服务数据盘:SSD、大小 50G;
(6) 云服务器规格:c6.large.2;
(7) 云服务器网络: chinaskills_subnet;
(8) 云服务器安全组:chinaskills_security_group;
(9) 云服务器组:chinaskills_server_group;
(10) 云服务器秘钥对:chinaskills_keypair;
(11) 云服务器弹性公网 IP:类型为全动态 BGP、带宽为 5 Mbit/s;
(12) 云服务器标签:chinaskills=true;
(13) 如果云服务器已经存在,代码中需要先删除;
(14) 以上需求可用直接使用其 ID,例如:镜像 ID、网络 ID 等等;
(15) 使用其源码的 get 方法输出此云服务器的详细信息。(云服务器状态要求为 ACTIVE 且数据盘挂载成功)
[root@centos7-9-2009 huawei]# cat create_server.py
import huaweicloudapi as api
# delete exist server
def if_exist(name):
query = {
"name": name
}
servers = conn.ecs.servers(**query)
for server in servers:
for each_server in server.servers:
if name == each_server["name"]:
data = {
"servers": [
{
"id": each_server["id"]
}
],
"delete_publicip": True,
"delete_volume": True
}
conn.ecs.delete_server(**data)
# create linux server
def create_server(
availability_zone,
name, image_id,
flavor_id,
security_groups_id,
vpc_id, subnet_id,
server_group_id,
key_name
):
if_exist(name)
data = {
"server": {
"availability_zone": availability_zone,
"name": name,
"root_volume": {
"volumetype": "SSD",
"size": 100,
},
"data_volumes": [
{
"volumetype": "SSD",
"size": 50,
"multiattach": "false",
"hw:passthrough": "false"
}
],
"imageRef": image_id,
"flavorRef": flavor_id,
"security_groups": [
{
"id": security_groups_id
}
],
"vpcid": vpc_id,
"nics": [
{
"subnet_id": subnet_id
}
],
"os:scheduler_hints": {
"group": server_group_id
},
"key_name": key_name,
"publicip": {
"eip": {
"iptype": "5_bgp",
"bandwidth": {
"size": 10,
"sharetype": "PER"
}
}
},
"server_tags": [
{
"key": "chinaskills",
"value": "true"
}
],
}
}
server = conn.ecs.create_server(**data)
print(server)
if __name__ == "__main__":
conn = api.get_conn_aksk()
availability_zone = "cn-east-3b"
name = "chinaskills_server"
image_id = "c04b00b1-5d31-4852-9917-ba4a9e2ced9b"
flavor_id = "c3.2xlarge.4"
security_groups_id = "c656ec1d-4691-4826-8807-f83cc0723e2a"
vpc_id = "e67aa383-2ebf-48ba-9532-b6b04a99af07"
subnet_id = "19667c09-eaac-47dc-ba41-f6a8569b1269"
server_group_id = "364f3852-fe3c-4660-9de1-769179e017c6"
key_name = "chinaskills_keypair"
server = create_server(
availability_zone,
name, image_id,
flavor_id,
security_groups_id,
vpc_id,
subnet_id,
server_group_id,
key_name
)
[root@centos7-9-2009 huawei]# python3 create_server.py
openstack.ecs.v1.server.Servers(server={'availability_zone': 'cn-east-3b', 'name': 'chinaskills_server', 'root_volume': {'volumetype': 'SSD', 'size': 100}, 'data_volumes': [{'volumetype': 'SSD', 'size': 50, 'multiattach': 'false', 'hw:passthrough': 'false'}], 'imageRef': 'c04b00b1-5d31-4852-9917-ba4a9e2ced9b', 'flavorRef': 'c3.2xlarge.4', 'security_groups': [{'id': 'c656ec1d-4691-4826-8807-f83cc0723e2a'}], 'vpcid': 'e67aa383-2ebf-48ba-9532-b6b04a99af07', 'nics': [{'subnet_id': '19667c09-eaac-47dc-ba41-f6a8569b1269'}], 'os:scheduler_hints': {'group': '364f3852-fe3c-4660-9de1-769179e017c6'}, 'key_name': 'chinaskills_keypair', 'publicip': {'eip': {'iptype': '5_bgp', 'bandwidth': {'size': 10, 'sharetype': 'PER'}}}, 'server_tags': [{'key': 'chinaskills', 'value': 'true'}]}, job_id=ff8080828365d906018430ee70897337, serverIds=['7136e8a4-80d2-4370-aca9-808f739c3b19'])
弹性伸缩配置
在云服务器的/root/huawei 目录下编写 create_as_config.py 文件,并导入提供的 huaweicloudapi.py 文件获取连接。编写 Python 代码,调用 auto_scaling 类,创建华为云的弹性伸缩配置,具体要求为
(1) 伸缩配置名称:chinaskills_as_config;
(2) 镜像配置:CentOS 7.5 64bit;
(3) 磁盘配置:系统盘规格 SSD、大小 100G;数据盘规格 SSD、大小 50G;
(4) 规格配置:c6.large.2;
(5) 安全组配置:chinaskills_security_group;
(6) 秘钥对配置:chinaskills_keypair;
(7) 弹性公网配置:类型为全动态 BGP、带宽为 5 Mbit/s;
(8) 如果弹性伸缩配置已经存在,代码中需要先删除;
(9)使用其源码的 get 方法输出此弹性伸缩配置的详细信息。
import huaweicloudapi as api
# delete exist config
def if_exist(config_name):
query = {
"name": config_name
}
configs = conn.auto_scaling.configs(**query)
for config in configs:
conn.auto_scaling.delete_config(config.id)
# creat config
def create_config(config_name):
if_exist(config_name)
instance_config = {
"flavor_id": "c6.large.2",
"image_id": "c95a7ae9-4240-4584-b5a0-ed0418803a99",
"disk": [{
"size": 100,
"volume_type": "SSD",
"disk_type": "SYS"
},
{
"size": 50,
"volume_type": "SSD",
"disk_type": "DATA"
}],
"key_name": "chinaskills_keypair",
"security_groups": ["0171fe8b-1953-4bdf-b4a6-fd4fde6cadf7"],
"public_ip": {
"eip": {
"ip_type": "5_bgp",
"bandwidth": {
"size": 10,
"share_type": "PER",
"charging_mode": "bandwidth"
}
}
}
}
config = conn.auto_scaling.create_config(config_name, **instance_config)
print(config)
if __name__ == "__main__":
conn = api.get_conn_user()
config_name = "chinaskills_as_config"
create_config(config_name)
弹性伸缩组管理
在云服务器的/root/huawei 目录下编写 create_as_group.py 文件,并导入提供的 huaweicloudapi.py 文件获取连接。编写 Python 代码,调用 auto_scaling 类,创建华为云的弹性伸缩组,具体要求为
(1) 伸缩组名称:chinaskills_as_group;
(2) 区域配置:cn-north-4a;
(3) 弹性伸缩配置:chinaskills_as_config;
(4) vpc 配置:chinaskills_vpc;
(5) 子网配置:chinaskills_subnet;
(6) 安全组配置:chinaskills_security_group;
(7) 云服务器数配置:最大实例 3 台、最小实例 1 台、期望实例 2 台;
(8) 配置弹性公网 IP 随实例释放;
(9) 以上需求可用直接使用其 ID,例如:网络 ID、安全组 ID 等等;
(10) 如果弹性伸缩组已经存在,代码中需要先删除;
(11) 使用其源码的 get 方法输出此弹性伸缩组的详细信息。
完成后提交云服务器节点的用户名、密码和 IP 地址到答题框。
from openstack import connection
import time
con = connection.Connection(
cloud="myhuaweicloud.com",
ak="********************",
sk="********************",
project_id="aaa5152ee9bd453084d139da9f927268",
region="cn-southwest-2"
)
group = con.auto_scaling.find_group("chinaskills_as_group")
if group is not None:
con.auto_scaling.delete_group(group)
group_data = {
"name": "chinaskills_as_group",
"availability_zones": [
"cn-southwest-2a"
],
"scaling_configuration_id": "dacd4047-c0c7-4440-9d93-e6e4c35cc494",
"vpc_id": "29adb01c-a0e8-422d-9ea5-eadd86034dc6",
"networks": [
{
"id": "91df789e-0d99-430d-9e9b-762203bbf446"
}
],
"security_groups": [
{
"id": "fcbff996-4a55-48f3-9e10-3390445b16c3"
}
],
"max_instance_number": 3,
"min_instance_number": 1,
"desire_instance_number": 2,
"delete_publicip": "true"
}
group = con.auto_scaling.create_group(**group_data)
print(con.auto_scaling.get_group(group))
弹性伸缩组策略管理
在/root/huawei 目录下编写 crerate_as_policy_daily.py 文件,并导入提供的huaweicloudapi.py 文件获取连接。编写 Python 代码,调用 auto_scaling 类,创建华为云的弹性伸缩组策略,具体要求如下:
(1) 基于伸缩组:chinaskills_as_group(不允许直接使用其 ID)
(2) 伸缩策略名称:chinaskills_as_policy_daily
(3) 策略类型:周期策略
(4) 重复周期:按天
(5) 触发时间:00:00
(6) 生效时间: 2021-06-10T00:00Z 到 2021-12-31T00:00Z
(7) 执行动作:增加 1 实例
(8) 冷却时间:900 (秒)
(9) 如果伸缩策略已经存在,代码中需要先删除
(10) 使用其源码的 get 方法输出此伸策略的详细信息
import huaweicloudapi as api
# delete policy daily
def if_exist(as_policy_name, scaling_group_name):
scaling_group_id = get_scaling_group_id(scaling_group_name)
policies = conn.auto_scaling.policies(scaling_group_id)
for policie in policies:
if as_policy_name == policie.name:
conn.auto_scaling.delete_policy(policie.id)
# creat policy Alarm
# creat policy daily
def creat_policy_Daily(as_policy_name, scaling_group_name):
if_exist(as_policy_name, scaling_group_name)
scaling_group_id = get_scaling_group_id(scaling_group_name)
policy = {
"name": as_policy_name,
"scaling_policy_action": {
"operation": "ADD",
"instance_number": 1
},
"cool_down_time": 900,
"scheduled_policy": {
"launch_time": "00:00",
"recurrence_type": "Daily",
"recurrence_value": None,
"start_time": "2021-06-10T00:00Z",
"end_time": "2021-12-31T00:00Z"
},
"type": "RECURRENCE",
"scaling_group_id": scaling_group_id
}
policy = conn.auto_scaling.create_policy(**policy)
policy = conn.auto_scaling.get_policy(policy)
print(policy)
# get group id
def get_scaling_group_id(scaling_group_name):
group = conn.auto_scaling.find_group(scaling_group_name)
return group.id
if __name__ == "__main__":
conn = api.get_conn_user()
scaling_group_name = "chinaskills_as_group"
as_policy_name = "chinaskills_as_policy_daily"
creat_policy_Daily(as_policy_name, scaling_group_name)
云硬盘管理
在/root/huawei 目录下编写 create_block_store.py 文件,并导入提供的huaweicloudapi.py 文件获取连接。编写 Python 代码,调用 block_store 类,创建华为云的云硬盘,具体要求如下:
(1) 云硬盘可用区域:cn-north-4a
(2) 云硬盘名称:chinaskills_volume
(3) 云硬盘规格和大小:超高 IO,100G
(4) 设置云硬盘共享
(5) 设置云硬盘加密,加密秘钥为默认的 KMS 密钥
(6) 如果云硬盘已经存在,代码中需要先删除
(7) 使用其源码的 get 方法输出此云硬盘的详细信息(状态要求为 available)
import huaweicloudapi as api
import time
# delete exist volume
def if_exist(name):
volumes = conn.block_store.volumes()
for volume in volumes:
if name == volume.name:
conn.block_store.delete_volume(volume.id)
# create volume
def create_volume(name, size):
if_exist(name)
data = {
"name": name,
"availability_zone": "cn-north-4a",
"volume_type": "SSD",
"size": size,
"metadata": {
"__system__encrypted": "1",
"__system__cmkid": "584c37c2-ce9c-4b9c-adc2-a2d4118f6beb"
},
"multiattach": True
}
volume = conn.block_store.create_volume(**data)
# 等待云硬盘状态为available
time.sleep(5)
volume = conn.block_store.get_volume(volume.id)
print(volume)
if __name__ == "__main__":
conn = api.get_conn_user()
name = "chinaskills_volume"
size = 100
create_volume(name, size)
- 点赞
- 收藏
- 关注作者
评论(0)