Managing Docker With Rancher APIs By Using Python Or Curl – Part 2

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Installing and managing Docker by Using the Rancher API’s – Part 2

In the first part of this article, I discussed how to install/configure Rancher, how to use it from within the Web-UI. below I am going to focus on the Rancher APIs, options to connect and how to use them.

Working with the Rancher API

Before we start using the Rancher API, I suggest you to visit your Rancher API reference page.
The API page can be accessed by going to.
http://rancher.domain.com:8080/v1
Rancher API Web-UI Reference

This makes it extremely easy to explore the Rancher API capability’s.

Now, lets just dive in with a simple example.

Using CURL to access API data

Rancher APIs are written in a way to work with any RESTful API request.
The simplest way to manipulate and test Rancher data is by using CURL.
Tip: All the Rancher documentation is written using CURL examples.

After you generated Rancher API keys, we are now ready to use them, just follow the the below examples. for how to generated your Rancher API keys, please read – Rancher APIs Part 1.
Note:Since all access to Rancher requires the use of API keys, I decided to use that in my examples below.

Lets break out the request above.

      First part, the Rancher Access Key and Rancher Secret Key
      Second part, your rancher domain/port followed by the version, in this case v1
      Last part, the request detail, in this case the key is apiKey

After the version(in my case v1), you specify the resource you are going to use, lets look on a more specific request below.

The previous request returned all apiKey’s, below I am querying for just one specific key.


What nice about Rancher is, you can explore the api and context right from within the Ranger Web-UI, in our example the apiKey.

All of this is available as a click thru in the Web-UI.

Rancher handy commend line tool – Gdapi

Next I am going to show you the Rancher API commend line tool Gdapi.
Gdapi is a Python client for Rancher APIs.

First you need to make sure its installed on your system.
To install Gdapi on your system, just run the below.

Next, to use Gdapi we need to set the below variables.

Make sure Gdapi works by running gdapi –help, it should return all gdapi available options.

Now that we are ready to use gdapi, lets look / use the same example from the previous section.
This can be done by running the below.

Tip: To get back all api Keys, just omit the more specific options. an example is below.

In summery, Gdapi is a very handy tool to explore many aspects of the Rancher API.
For more information on Gdapi click here.

Using Python to manipulate the Rancher API data

In this section, I am using Python to access the Rancher APIs.

Below, I am again using the previous example, accessing the same Rancher API data, but instead of CURL or Gdapi I am using Python to manipulate the Ranger API data.

Note: There is also a Python binding for Gdapi, but I was not able getting it to work (it seemed deprecated in the recent versions, as any REST calls should work).

Ruby API bindings for Rancher

In case you are looking for a Ruby option, there is also a Ruby wrapper for the Rancher APIs.
To install the Ruby bindings, just run the below.

Full details is available here.

To summarize, Rancher is an extremely powerful platform for managing Docker, both on the UI front or APIs, there are a number of alternatives but Rancher is defiantly a very good option.

Now its your turn. What tools worked best for you to manage a Docker environment? please let me know in the comments below.

Leave a Reply

avatar
  Subscribe  
Notify of