Over the past few years working at EMC we regularly use NVP/NSX in some of our lab environments and throughout the years this means that we have needed to upgrade, manipulate and overhaul our network architecture with NVP/NSX every so often. We have been using a Python library developed internally by myself and Patrick Mullaney with some help from Erik Smith in the early days, and I wanted to share some of its tooling.
(need to drop this in here)
*Disclaimer : By no means does EMC make any representation or take any obligation with regard to the use of this library, NVP or NSX in any way shape or form. This post is the thoughts of the author and the author alone. The examples and API calls have mainly been testing against NVP up to 3.2 before it became NSX. However, most calls should work against the NSX API except for any net-new api endpoints that have not been added. Another note is that this API is not fully featured, it is merely a tool that we use in the lab for what we need, it can be extended, expanded however you like.(update 4/14/15′ See Open-source section toward the bottom for more information )
I’ll be working on opensource this library and try and fill in some of the missing features as I go along. There are two main uses for this python library:
1. Manage NVP/NSX Infrastructure
Managing, automating, and orchestrating the setup of NVP/NSX components was a must for us. We wanted to be able to spin up and down environments on the flow, and or manage upgrading new components when we wanted to upgrade.
The Library allows you to remotely setup Hypervisor Nodes, Gateway Nodes, Service Nodes etc. (Examples Below)
2. Python bindings to NVP/NSX REST API
Having python bindings for some of the investigative projects we have been working on was out first motivation. A) because we developed and were familiar with Python, B) We have been working with OpenStack and it just made sense.
With the library you can list networking, attach ports, query nodes etc. (Examples are given in the test.py example below.)
Manage NVP/NSX Infrastructure Let me preface this by saying this isn’t a complete M&O / DevOps / Baremetal Provisioning service for NVP or NSX components, it does however manage setting up much of the logical state components with as little hands-on cli commands as possible, like setting up OVS certificates, creating integrations bridges and contacting the Control Cluster to register the node as a Hypervisor Node, Service Node etc. Openvswitch is the only thing that does need to be installed, and any other nicira debs that come with their software switch/ hypervisor node offering. You just need to install the debs and then let the configuration tool take over. (I am running this on Ubuntu 14.04 in this demo)
sudo dpkg --purge openvswitch-pki sudo dpkg -i openvswitch-datapath-dkms_1.11.0*.deb sudo dpkg -i openvswitch-common_1.11.0*.deb openvswitch-switch_1.11.0*.deb sudo dpkg -i nicira-ovs-hypervisor-node_1.11.0*.deb
Once OVS is installed on the nodes that you will be adding to your NVP/NSX architecture you can use the library and cli tools to setup, connect, and register the virtual network components. The way this is done is by describing the infrastructure components in JSON form from a single control host, or even a NVP/NSX linux host. We thought about using YAML here, but after everything we chose JSON. Sorry if you a YAML fan. The first thing needed by the tooling is some basic control cluster information. The IP of the controller, the username and password along with the port to use for authentication. (sorry for the ugly blacked out IPs) Next, you can describe different types of nodes for setup and configuration. These nodes can be:
SERVICE, GATEWAY, or COMPUTE
The difference with the SERVICE or GATEWAY node is that it will have a:
"mgmt_rendezvous_server" : true|false "mgmt_rendezvous_client" : true|false
respectively instead of a “data network interface. Service or Gateway nodes need this metadata in it’s configuration JSON to be created correctly. I dont have examples of using this (Sorry) and am focusing on using this for Hypervisor Nodes, since this is what we find we are creating/reconfiguring most. Here is an example of such a config. Once this configuration is complete, you can now reference the “name” of the compute node, and it will provision the COMPUTE node to the NVP/NSX system/cluster. If the hypervisor node is remote the toolkit will use remote sudo SSH, so you will need to enter a user/pass when you get prompted. As you can see, the command runs through all the process needed to setup the node, and at the end of it, you should have a working hypervisor node ready to go. (Scroll down, you can verify it’s setup by looking at the UI) Here is what it looks like when you do it remotely. You’ll then see similar output to the below, running through the sequence of setting up the hypervisor node remotely, keys, OVS calls, and contacting the NVP/NSX cluster to register the node using the PKI.
After setup you can verify that the node is setup by logging into it if your remote, and running the ovs-vsctl show command. This will show you all configuration that has been done to setup the hypervisor node. Managers and Controllers are setup and connected, and bridge interfaces created and ready to connect tunnels. We can also verify that the Hypervisor node is setup correctly by looking at the NSX/NVP Manager Dashboard. This shows that the Ubuntu Node is now connected, using the correct Transport Zone, and is up and ready to go. Thats the end of what I wanted to show as far as remote configuration of NVP/NSX components goes. We use this a lot when setting up our OpenStack environments and when we add or remove new Compute nodes that need to talk on the Neutron/Virtual Network. Again there are some tweaks and cleanups I need to address, but hopefully I can have this available on a public repo soon.
Python bindings to NVP/NSX REST API
Now I want to get into using the toolkit’s NVP/NSX Python API. This is a API written in python that addresses the REST API’s exposed from NVP/NSX. The main class of the API takes Service Libraries as arguments, and them calls the init method on them to instantiate their provided functions. For instance “ControlSevices” focuses on control cluster API calls rather than the logical virtual network components.
from nvp2 import NVPClient from transport import Transport from network_services import NetworkServices from control_services import ControlServices import logging class NVPApi(Transport, NetworkServices, ControlServices) client = NVPClient() log = logging def __init__(self, debug=False, verbose=False): self.client.login() if verbose: self.log.basicConfig(level=self.log.INFO) if debug: self.log.basicConfig(level=self.log.DEBUG) Transport.__init__(self, self.client, self.log) NetworkServices.__init__(self, self.client, self.log) ControlServices.__init__(self, self.client, self.log)
Some example of how to instantiate the library class and use the provided service functions for NVP/NSX are below. This is not a full featured list, and more than less they are for NVP 3.2 and below. We are in the process of making sure it works across the board, so far NVP/NSX APIs have been pretty good at being backward compatible.
from api.nvp_api import NVPApi
#Instantiate an API Object #api = NVPApi() api = NVPApi(debug=True) #api = NVPApi(debug=True, verbose=True)
#print "Available Functions of the API" # print dir(api)
# See an existing "Hypervisor Node" print api.tnode_exists("My_Hypervisor_Node")
# Check for existing Transport Zones print api.get_transport_zones()
# Check for existing Transport Zone print api.check_transport_exists("My_Transport_Zone")
# Check Control Cluster Nodes nodes = api.get_control_cluster() for node in nodes: print "\n" print node['display_name'] for role in node['roles']: print "%s" % role['role'] print "%s\n" % role['listen_addr'] print "\n"
# Check stats for interfaces on transport node stats = api.get_interface_statistics("80d9cb27-432c-43dc-9a6b-15d4c45005ee", "breth2+") print "Interface %s" % ("breth2") for stat, val in stats.iteritems(): print "%s -- %s" % (stat, val) print "\n"
I hope you enjoyed reading through some of this, it’s really gone from a bunch of scripts no one can understand to something halfway useful. There are definitely better ways to do this, and by no means is this a great solution, it just one we had lying around that I still use from time to time. Managing the JSON state/descriptions about the nodes was the hardest part when multiple people start using this. We wound up managing the files with Puppet and also using Puppet for installing base openvswitch software for new NVP/NSX components on Ubuntu servers. Happy Friday, Happy Coding, Happy Halloween!
I wanted to update the post with information about the library and how to access it. The great folks at EMC Code ( http://emccode.github.io/ ) have added this project to the #DevHigh5 Program and it is available on their site. Look at the right side and lick the DevHigh5 tag, and look for Nicira NVP/NSX Python under the projects.
Information can be seen by hovering over the project.
The code can be viewed on Github and on pypi and can be installed via pip install nvpnsxapi