shell一键创建openstack云主机

云主机

    • 脚本实现
    • 脚本测试

部署完openstack平台后,使用shell脚本一键创建云主机,并实现访问
使用此脚本前平台不要创建网络、路由、安全组规则等!!

脚本实现

root@controller:~/openstack-y-ovs# cat cloud-host-test.sh
#!/bin/bash
source /etc/openstack/openrc.sh
source /etc/keystone/admin-openrc.sh

intsub=10.0.0.0/24
intsubgateway=10.0.0.2
extsub=192.168.200.0/24
extsubgateway=192.168.200.2

echo "创建内部网络和子网"
openstack network create --internal --provider-network-type vxlan int-net
openstack subnet create int-net-sub --network int-net --subnet-range $intsub --gateway $intsubgateway --dns-nameserver 114.114.114.114

echo "创建外部网络和子网"
openstack network create --provider-physical-network $Physical_NAME --provider-network-type flat  --external ext-net
openstack subnet create ext-net-sub --network ext-net --subnet-range $extsub --gateway $extsubgateway --dns-nameserver 114.114.114.114 --no-dhcp

echo "创建路由"
openstack router create Route
openstack router set Route --external-gateway ext-net
openstack router add subnet Route int-net-sub


echo "创建镜像"
openstack image create cirros-test --disk-format qcow2  --file  cirros-0.3.4-x86_64-disk.img

echo "创建实例类型"
openstack flavor create --vcpus 1 --ram 1024 --disk 10 test

echo "开放安全组规则"
# 获取默认安全组ID
default_group_ids=($(openstack security group list --column ID --format value))
default_group_id=${default_group_ids[0]}
# 创建ICMP规则
openstack security group rule create --proto icmp $default_group_id
# 创建TCP规则
openstack security group rule create --proto tcp --dst-port 22:22 $default_group_id
bash nova-service-restart.sh
bash neutron-service-restart.sh
sleep 10

echo "创建云主机"
openstack server create --flavor test --image cirros-test --nic net-id=int-net vm-test

echo "绑定浮动IP"
openstack floating ip create ext-net
floating_ip_address=$(openstack floating ip list --format value --column "Floating IP Address")
openstack server add floating ip vm-test $floating_ip_address

脚本测试

root@controller:~/openstack-y-ovs# bash cloud-host-test.sh
创建内部网络和子网
+---------------------------+--------------------------------------+
| Field                     | Value                                |
+---------------------------+--------------------------------------+
| admin_state_up            | UP                                   |
| availability_zone_hints   |                                      |
| availability_zones        |                                      |
| created_at                | 2023-06-11T03:59:43Z                 |
| description               |                                      |
| dns_domain                | None                                 |
| id                        | cd02f3f7-1dc6-4498-a0cd-ca8d940e7fa3 |
| ipv4_address_scope        | None                                 |
| ipv6_address_scope        | None                                 |
| is_default                | False                                |
| is_vlan_transparent       | None                                 |
| mtu                       | 1450                                 |
| name                      | int-net                              |
| port_security_enabled     | True                                 |
| project_id                | 7d43e5ca81e14f28b12e2035c1ff354e     |
| provider:network_type     | vxlan                                |
| provider:physical_network | None                                 |
| provider:segmentation_id  | 729                                  |
| qos_policy_id             | None                                 |
| revision_number           | 1                                    |
| router:external           | Internal                             |
| segments                  | None                                 |
| shared                    | False                                |
| status                    | ACTIVE                               |
| subnets                   |                                      |
| tags                      |                                      |
| updated_at                | 2023-06-11T03:59:43Z                 |
+---------------------------+--------------------------------------+
+----------------------+---------------------------------------+
| Field                | Value                                 |
+----------------------+---------------------------------------+
| allocation_pools     | 10.0.0.3-10.0.0.254,10.0.0.1-10.0.0.1 |
| cidr                 | 10.0.0.0/24                           |
| created_at           | 2023-06-11T03:59:46Z                  |
| description          |                                       |
| dns_nameservers      | 114.114.114.114                       |
| dns_publish_fixed_ip | None                                  |
| enable_dhcp          | True                                  |
| gateway_ip           | 10.0.0.2                              |
| host_routes          |                                       |
| id                   | cf27a789-9a02-47f6-bee4-cf317bdd2c0b  |
| ip_version           | 4                                     |
| ipv6_address_mode    | None                                  |
| ipv6_ra_mode         | None                                  |
| name                 | int-net-sub                           |
| network_id           | cd02f3f7-1dc6-4498-a0cd-ca8d940e7fa3  |
| project_id           | 7d43e5ca81e14f28b12e2035c1ff354e      |
| revision_number      | 0                                     |
| segment_id           | None                                  |
| service_types        |                                       |
| subnetpool_id        | None                                  |
| tags                 |                                       |
| updated_at           | 2023-06-11T03:59:46Z                  |
+----------------------+---------------------------------------+
创建外部网络和子网
+---------------------------+--------------------------------------+
| Field                     | Value                                |
+---------------------------+--------------------------------------+
| admin_state_up            | UP                                   |
| availability_zone_hints   |                                      |
| availability_zones        |                                      |
| created_at                | 2023-06-11T03:59:50Z                 |
| description               |                                      |
| dns_domain                | None                                 |
| id                        | 8e667215-a7c6-412d-919e-e20a68eef9a7 |
| ipv4_address_scope        | None                                 |
| ipv6_address_scope        | None                                 |
| is_default                | False                                |
| is_vlan_transparent       | None                                 |
| mtu                       | 1500                                 |
| name                      | ext-net                              |
| port_security_enabled     | True                                 |
| project_id                | 7d43e5ca81e14f28b12e2035c1ff354e     |
| provider:network_type     | flat                                 |
| provider:physical_network | provider                             |
| provider:segmentation_id  | None                                 |
| qos_policy_id             | None                                 |
| revision_number           | 1                                    |
| router:external           | External                             |
| segments                  | None                                 |
| shared                    | False                                |
| status                    | ACTIVE                               |
| subnets                   |                                      |
| tags                      |                                      |
| updated_at                | 2023-06-11T03:59:50Z                 |
+---------------------------+--------------------------------------+
+----------------------+-----------------------------------------------------------+
| Field                | Value                                                     |
+----------------------+-----------------------------------------------------------+
| allocation_pools     | 192.168.200.1-192.168.200.1,192.168.200.3-192.168.200.254 |
| cidr                 | 192.168.200.0/24                                          |
| created_at           | 2023-06-11T03:59:54Z                                      |
| description          |                                                           |
| dns_nameservers      | 114.114.114.114                                           |
| dns_publish_fixed_ip | None                                                      |
| enable_dhcp          | False                                                     |
| gateway_ip           | 192.168.200.2                                             |
| host_routes          |                                                           |
| id                   | 59a18c77-acc4-4ff5-a27b-882b870a4389                      |
| ip_version           | 4                                                         |
| ipv6_address_mode    | None                                                      |
| ipv6_ra_mode         | None                                                      |
| name                 | ext-net-sub                                               |
| network_id           | 8e667215-a7c6-412d-919e-e20a68eef9a7                      |
| project_id           | 7d43e5ca81e14f28b12e2035c1ff354e                          |
| revision_number      | 0                                                         |
| segment_id           | None                                                      |
| service_types        |                                                           |
| subnetpool_id        | None                                                      |
| tags                 |                                                           |
| updated_at           | 2023-06-11T03:59:54Z                                      |
+----------------------+-----------------------------------------------------------+
创建路由
+-------------------------+--------------------------------------+
| Field                   | Value                                |
+-------------------------+--------------------------------------+
| admin_state_up          | UP                                   |
| availability_zone_hints |                                      |
| availability_zones      |                                      |
| created_at              | 2023-06-11T03:59:57Z                 |
| description             |                                      |
| distributed             | False                                |
| external_gateway_info   | null                                 |
| flavor_id               | None                                 |
| ha                      | False                                |
| id                      | 713dfa83-592a-4bdb-a0da-68a74ac56da8 |
| name                    | Route                                |
| project_id              | 7d43e5ca81e14f28b12e2035c1ff354e     |
| revision_number         | 1                                    |
| routes                  |                                      |
| status                  | ACTIVE                               |
| tags                    |                                      |
| updated_at              | 2023-06-11T03:59:57Z                 |
+-------------------------+--------------------------------------+
创建镜像
+------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
| Field            | Value
                                                 |
+------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
| container_format | bare
                                                 |
| created_at       | 2023-06-11T04:00:13Z
                                                 |
| disk_format      | qcow2
                                                 |
| file             | /v2/images/1c5f3bdb-5a2f-4742-aa11-5b8ee6e5d47f/file
                                                 |
| id               | 1c5f3bdb-5a2f-4742-aa11-5b8ee6e5d47f
                                                 |
| min_disk         | 0
                                                 |
| min_ram          | 0
                                                 |
| name             | cirros-test
                                                 |
| owner            | 7d43e5ca81e14f28b12e2035c1ff354e
                                                 |
| properties       | os_hidden='False', owner_specified.openstack.md5='', owner_specified.openstack.object='images/cirros-test', owner_specified.openstack.sha256='' |
| protected        | False
                                                 |
| schema           | /v2/schemas/image
                                                 |
| status           | queued
                                                 |
| tags             |
                                                 |
| updated_at       | 2023-06-11T04:00:13Z
                                                 |
| visibility       | shared
                                                 |
+------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
创建实例类型
+----------------------------+--------------------------------------+
| Field                      | Value                                |
+----------------------------+--------------------------------------+
| OS-FLV-DISABLED:disabled   | False                                |
| OS-FLV-EXT-DATA:ephemeral  | 0                                    |
| description                | None                                 |
| disk                       | 10                                   |
| id                         | 5c9ca796-fed3-4bd0-a280-6c86d5667b2d |
| name                       | test                                 |
| os-flavor-access:is_public | True                                 |
| properties                 |                                      |
| ram                        | 1024                                 |
| rxtx_factor                | 1.0                                  |
| swap                       |                                      |
| vcpus                      | 1                                    |
+----------------------------+--------------------------------------+
开放安全组规则
+-------------------------+--------------------------------------+
| Field                   | Value                                |
+-------------------------+--------------------------------------+
| created_at              | 2023-06-11T04:00:23Z                 |
| description             |                                      |
| direction               | ingress                              |
| ether_type              | IPv4                                 |
| id                      | 0b6b15ab-fea8-4486-b093-6f81c623e2fc |
| name                    | None                                 |
| port_range_max          | None                                 |
| port_range_min          | None                                 |
| project_id              | 7d43e5ca81e14f28b12e2035c1ff354e     |
| protocol                | icmp                                 |
| remote_address_group_id | None                                 |
| remote_group_id         | None                                 |
| remote_ip_prefix        | 0.0.0.0/0                            |
| revision_number         | 0                                    |
| security_group_id       | a43726b8-18e0-4a78-b440-95e9a87cb10c |
| tags                    | []                                   |
| tenant_id               | 7d43e5ca81e14f28b12e2035c1ff354e     |
| updated_at              | 2023-06-11T04:00:23Z                 |
+-------------------------+--------------------------------------+
+-------------------------+--------------------------------------+
| Field                   | Value                                |
+-------------------------+--------------------------------------+
| created_at              | 2023-06-11T04:00:27Z                 |
| description             |                                      |
| direction               | ingress                              |
| ether_type              | IPv4                                 |
| id                      | ec568e0a-cf36-4b88-8375-eb27479866db |
| name                    | None                                 |
| port_range_max          | 22                                   |
| port_range_min          | 22                                   |
| project_id              | 7d43e5ca81e14f28b12e2035c1ff354e     |
| protocol                | tcp                                  |
| remote_address_group_id | None                                 |
| remote_group_id         | None                                 |
| remote_ip_prefix        | 0.0.0.0/0                            |
| revision_number         | 0                                    |
| security_group_id       | a43726b8-18e0-4a78-b440-95e9a87cb10c |
| tags                    | []                                   |
| tenant_id               | 7d43e5ca81e14f28b12e2035c1ff354e     |
| updated_at              | 2023-06-11T04:00:27Z                 |
+-------------------------+--------------------------------------+
bash: nova-service-restart.sh: No such file or directory
bash: neutron-service-restart.sh: No such file or directory
创建云主机
+-------------------------------------+----------------------------------------------------+
| Field                               | Value                                              |
+-------------------------------------+----------------------------------------------------+
| OS-DCF:diskConfig                   | MANUAL                                             |
| OS-EXT-AZ:availability_zone         |                                                    |
| OS-EXT-SRV-ATTR:host                | None                                               |
| OS-EXT-SRV-ATTR:hypervisor_hostname | None                                               |
| OS-EXT-SRV-ATTR:instance_name       |                                                    |
| OS-EXT-STS:power_state              | NOSTATE                                            |
| OS-EXT-STS:task_state               | scheduling                                         |
| OS-EXT-STS:vm_state                 | building                                           |
| OS-SRV-USG:launched_at              | None                                               |
| OS-SRV-USG:terminated_at            | None                                               |
| accessIPv4                          |                                                    |
| accessIPv6                          |                                                    |
| addresses                           |                                                    |
| adminPass                           | WrCqg2ooqFEP                                       |
| config_drive                        |                                                    |
| created                             | 2023-06-11T04:00:43Z                               |
| flavor                              | test (5c9ca796-fed3-4bd0-a280-6c86d5667b2d)        |
| hostId                              |                                                    |
| id                                  | b647682b-5981-4f73-b773-0ebdaf70efcb               |
| image                               | cirros-test (1c5f3bdb-5a2f-4742-aa11-5b8ee6e5d47f) |
| key_name                            | None                                               |
| name                                | vm-test                                            |
| progress                            | 0                                                  |
| project_id                          | 7d43e5ca81e14f28b12e2035c1ff354e                   |
| properties                          |                                                    |
| security_groups                     | name='default'                                     |
| status                              | BUILD                                              |
| updated                             | 2023-06-11T04:00:43Z                               |
| user_id                             | 15a69f9403a049ccacb708412babac77                   |
| volumes_attached                    |                                                    |
+-------------------------------------+----------------------------------------------------+
绑定浮动IP
+---------------------+--------------------------------------+
| Field               | Value                                |
+---------------------+--------------------------------------+
| created_at          | 2023-06-11T04:03:36Z                 |
| description         |                                      |
| dns_domain          | None                                 |
| dns_name            | None                                 |
| fixed_ip_address    | None                                 |
| floating_ip_address | 192.168.200.168                      |
| floating_network_id | 8e667215-a7c6-412d-919e-e20a68eef9a7 |
| id                  | 4fedb502-25d0-436f-94f4-4b758ae625d3 |
| name                | 192.168.200.198                      |
| port_details        | None                                 |
| port_id             | None                                 |
| project_id          | 7d43e5ca81e14f28b12e2035c1ff354e     |
| qos_policy_id       | None                                 |
| revision_number     | 0                                    |
| router_id           | None                                 |
| status              | DOWN                                 |
| subnet_id           | None                                 |
| tags                | []                                   |
| updated_at          | 2023-06-11T04:03:36Z                 |
+---------------------+--------------------------------------+
root@controller:~/openstack-y-ovs# openstack server list
+--------------------------------------+---------+--------+-------------------------------------+-------------+--------+
| ID                                   | Name    | Status | Networks                            | Image       | Flavor |
+--------------------------------------+---------+--------+-------------------------------------+-------------+--------+
| b647682b-5981-4f73-b773-0ebdaf70efcb | vm-test | ACTIVE | int-net=10.0.0.184, 192.168.200.168 | cirros-test | test   |
+--------------------------------------+---------+--------+-------------------------------------+-------------+--------+
root@controller:~/openstack-y-ovs# ping 192.168.200.168
PING 192.168.200.168 (192.168.200.168) 56(84) bytes of data.
64 bytes from 192.168.200.168: icmp_seq=1 ttl=63 time=6.20 ms
64 bytes from 192.168.200.168: icmp_seq=2 ttl=63 time=3.92 ms
^C
--- 192.168.200.168 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 3.920/5.061/6.203/1.141 ms

你可能感兴趣的:(Linux,私有云(openstack),openstack,网络,tcp/ip)