OpenStack: Neutron 为云主机创建网络脚本分析

来自CloudWiki
跳转至: 导航搜索

crudini用法

crudini --set [--existing] config_file section [param] [value]

crudini --get [--format=sh|ini] config_file [section] [param]

crudini --del [--existing] config_file section [param]

crudini --merge [--existing] config_file [section]

crudini 用于操作ini文件,可以设置、获取、删除、合并其中的变量

其中:config_file 代表要操作的文件名,section 表示变量所在的部分,如以下配置文件1.ini:

[DEFAULT]
user = admin
passwd = admin
port = 8088

[URL]
client = 127.0.0.1:8088
admin = 127.0.0.1:8080

section 则表示了以上配置文件中的DEFAULT 和 URL ,在命令中不需要加中括号[],param 则如user ,passwd,client 等。

添加

格式:$ crudini --set config_file section parameter value

例子:

$ crudini --set 1.ini DEFAULT user demo

$ crudini --set 1.ini DEFAULT passwd 000000

更新

$ crudini --set [--existing] config_file section parameter value

删除

删除变量:

$ crudini --del config_file section parameter

删除section:

$ crudini --del config_file section

获取

$ crudini --del config_file section parameter

如果该标量不在某一个section里面,则section用一个空字符表示:

$ crudini --del config_file parameter

合并

将another.ini配置文件合并到config_file中:

$ crudini --merge config_file < another.ini

配置FLAT网络

控制节点

iaas-install-neutron-controller-flat.sh

#!/bin/bash
source /etc/xiandian/openrc.sh #注册环境变量
source /etc/keystone/admin-openrc.sh

ovs-vsctl add-br br-ex  #添加网桥
ovs-vsctl add-port br-ex $INTERFACE_NAME  #添加端口,把外网网卡添加到ovs
cat > /etc/sysconfig/network-scripts/ifcfg-$INTERFACE_NAME <<EOF #配置文件
DEVICE=$INTERFACE_NAME
TYPE=Ethernet
BOOTPROTO=none
ONBOOT=yes
EOF
systemctl restart network  #重启网络
#flat_networks:
crudini --set  /etc/neutron/plugins/ml2/ml2_conf.ini ml2_type_flat flat_networks  physnet1

crudini --set /etc/neutron/plugins/ml2/ml2_conf.ini ml2 tenant_network_types  flat
crudini --set  /etc/neutron/plugins/ml2/openvswitch_agent.ini ovs bridge_mappings  physnet1:br-ex
#重启代理插件
systemctl restart neutron-openvswitch-agent

#获取tenantId 
tenantID=`openstack project list | grep service | awk '{print $2}'`

#打印
echo -e "\033[31m\nCreate a sample flat network\n\033[0m "

#创建网络
neutron net-create --tenant-id $tenantID sharednet1 --shared --provider:network_type flat --provider:physical_network physnet1


计算节点

iaas-install-neutron-compute-flat.sh

#!/bin/bash
source /etc/xiandian/openrc.sh  #引用环境变量

ovs-vsctl add-br br-ex #添加网桥
ovs-vsctl add-port br-ex $INTERFACE_NAME  #为网桥添加第几块网卡?
cat > /etc/sysconfig/network-scripts/ifcfg-$INTERFACE_NAME <<EOF
DEVICE=$INTERFACE_NAME
TYPE=Ethernet
BOOTPROTO=none
ONBOOT=yes
EOF

systemctl restart network

crudini --set  /etc/neutron/plugins/ml2/ml2_conf.ini ml2_type_flat flat_networks physnet1
crudini --set /etc/neutron/plugins/ml2/ml2_conf.ini ml2 tenant_network_types  flat
crudini --set  /etc/neutron/plugins/ml2/openvswitch_agent.ini ovs bridge_mappings  physnet1:br-ex
systemctl restart neutron-openvswitch-agent

创建GRE网络

控制节点

iaas-install-neutron-controller-gre.sh:

#!/bin/bash
source /etc/xiandian/openrc.sh  #启用环境变量
source /etc/keystone/admin-openrc.sh #启用环境变量

crudini --set /etc/neutron/plugins/ml2/ml2_conf.ini ml2 tenant_network_types  gre  #修改网络类型
crudini --set /etc/neutron/plugins/ml2/ml2_conf.ini ml2_type_gre tunnel_id_ranges  1:1000  

ovs-vsctl add-br br-ex   #创建网桥
ovs-vsctl add-port br-ex $INTERFACE_NAME  #添加网卡到网桥上
#修改网卡配置文件
cat > /etc/sysconfig/network-scripts/ifcfg-$INTERFACE_NAME <<EOF
DEVICE=$INTERFACE_NAME
TYPE=Ethernet
BOOTPROTO=none
ONBOOT=yes
EOF
#重启网络
systemctl restart network

#修改配置文件
crudini --set  /etc/neutron/l3_agent.ini DEFAULT  external_network_bridge  br-ex
crudini --set  /etc/neutron/plugins/ml2/openvswitch_agent.ini ovs bridge_mappings  physnet1:br-ex
crudini --set  /etc/neutron/plugins/ml2/openvswitch_agent.ini agent tunnel_types  gre
crudini --set  /etc/neutron/plugins/ml2/openvswitch_agent.ini ovs local_ip $HOST_IP
crudini --set  /etc/neutron/plugins/ml2/openvswitch_agent.ini ovs  enable_tunneling True
crudini --set  /etc/neutron/plugins/ml2/openvswitch_agent.ini ovs bridge_mappings external:br-ex

#重启服务
systemctl restart neutron-server
systemctl restart neutron-l3-agent neutron-openvswitch-agent

计算节点

Compute:

#!/bin/bash
source /etc/xiandian/openrc.sh#启动环境变量
crudini --set /etc/neutron/plugins/ml2/ml2_conf.ini ml2 tenant_network_types  gre#修改网络类型
crudini --set /etc/neutron/plugins/ml2/ml2_conf.ini ml2_type_gre tunnel_id_ranges  1:1000  
ovs-vsctl add-br br-ex  #创建网桥
ovs-vsctl add-port br-ex $INTERFACE_NAME  #将网卡添加到网桥
#修改网卡配置文件
cat > /etc/sysconfig/network-scripts/ifcfg-$INTERFACE_NAME <<EOF
DEVICE=$INTERFACE_NAME
TYPE=Ethernet
BOOTPROTO=none
ONBOOT=yes
EOF
#重启网络
systemctl restart network
#修改配置文件
crudini --set  /etc/neutron/plugins/ml2/openvswitch_agent.ini ovs bridge_mappings  physnet1:br-ex
crudini --set  /etc/neutron/plugins/ml2/openvswitch_agent.ini agent tunnel_types  gre
crudini --set  /etc/neutron/plugins/ml2/openvswitch_agent.ini ovs local_ip $HOST_IP_NODE
crudini --set  /etc/neutron/plugins/ml2/openvswitch_agent.ini ovs  enable_tunneling True
crudini --set  /etc/neutron/plugins/ml2/openvswitch_agent.ini ovs bridge_mappings external:br-ex
#重启服务
systemctl restart neutron-openvswitch-agent