Following up from the first blog post, let’s continue with Endpoint configuration and some .NET code to test the connection to the Couchbase node.

Endpoint Configuration, what is that exactly?

By default, Azure blocks most external connections to the Virtual Machine (VM). The default exceptions are PowerShell (port: 5986) and Remote Desktop (port: 59548).
Connection attempts to other ports are blocked by Azure Endpoint Protection, unless they are added to Endpoint Protection as a “pass through”. You can think of Endpoint protection as a “Cloud Firewall” or perhaps port forwarding in a DMZ Zone.
Regardless, all ports used by the Couchbase Client need to be added to the Endpoint protection to allow the the client and node to communicate.
The complete port list for Couchbase Server/Client can be found here:
Please note the Client/server (“yes/no”) header.
http://docs.couchbase.com/admin/admin/Install/install-networkPorts.html

No need to open more ports then the client is using/needs.
In this sample/demo we will not use SSL and therefore there is no need to open port 11207 as this is only used for SSL etc.

The ports that we need to add to Endpoint Protection for the .NET Client to connect to the node are:

8091 – Web Admin
8092 – API
11210 – Bucket (no SSL)

Note: If you are planning to use N1QL (SQL for Documents) you also need to add port 8093! 

Endpoint Configuration, adding the ports

Go to https://manage.windowsazure.com 
and navigate to the Virtual Machine created in “part 1”.

Hit the “Add” button and follow the on screen guide to add the 3 ports to the Endpoint Configuration.
In the current Azure Portal can only add a single port at a time, this is a bit time consuming and will hopefully change in the future.

Admin Console

As part of the Endpoint Configuration we added port 8091, the Couchbase Server Console.
Navigate to:
http://{your-cb-server-node-dns-name}.cloudapp.net:8091

You should now see the Couchbase Console login page. If you get a “timeout” or “unknown page” error, then please check that the Couchbase Server is running and port 8091 has been successfully added to the Endpoint Configuration.

Assuming that everything went as planned and the browser showed the Couchbase Console Login page, let’s continue to build a .NET Client to connect to the Node.

The .NET SDK – Building a test Client with .NET

The Official .NET SDK for Couchbase is open source and can be downloaded, edited, changed etc. from Github, https://github.com/couchbase/couchbase-net-client

For convenience the SDK is also avaliable on Nuget, https://www.nuget.org/packages/CouchbaseNetClient/

With the SDK easy at hand, the only step missing is coding a small test project.

Open Visual Studio and create a new Console app.

Right click the Solution in Solution Explorer and click Manage Nuget Packages for this Solution.

Search for “Couchbase SDK” and install the package as shown on the picture below.

Depending on network speed, the package download + dependencies can take some time.

Couchbase SDK uses “System.Configuration” as part of the configuration and setting up the connection uris in code. Therefore you will need to add a ref. to this DLL as well.

Now we are ready to do some actual C# code and connect to our one node Couchbase Cluster.

Connecting to the Cluster with the Couchbase .NET SDK!

Open program.cs and replace the existing code with the code below (all code lines):

Please note that you need to update the Server Configuration to point to your own Couchbase Cluster, but with that change done we are ready to run the code.

The Main method is the entry point and the first piece of user code run in the cmd app.
It starts by initializing ClusterHelper, as Singleton with “multi-tons” Bucket references. This is the recommended way to access Bucket’s and Cluster Configuration.
ClusterHelper ensures that there is only one instance of a bucket (thread safe) that can be shared for the lifetime of the application.

You can read more about the details of the code and the Couchbase .NET SDK here.

Next time…

In part 3 we will scale the Cluster to have multiple nodes within a private virtual network.

Thanks for reading
Martin

Posted by Martin Esmann, Developer Advocate, Couchbase

6 Comments

  1. Martin, thanks for posting these excellent blog entries re Azure & Couchbase. A great help as we assess Couchbase.

    Unf. the simple test app above fails for us. Crashes at the upsert line 50, with this error:

    \”An unhandled exception of type \’System.NullReferenceException\’ occurred in Couchbase.NetClient.dll\”

    Looking at the \’bucket\’ object we noticed this error on the \”IsSecure\” property:
    \”<exception of=\”\” type=\”\” \’couchbase.serverunavailableexception\’=\”\” was=\”\” thrown.=\”\”>\”

    Our endpoint (http://xxxxxxxxxxx.cloudapp.ne…:8091/pools) is fine and available / returns data.

    Any ideas on why we would be seeing this exception ?

    1. Hi Aj,
      Thanks for following the series and sorry to hear that you have challenges with the sample code.
      I just tested the sample again without any errors, have you changed anything in the code? perhaps UseSsl?

      1. No worries at all Martin. So I did more digging today and found the problem via netstat on my local mac. The virtual IP address that is assigned to my VM (Azure) was being mapped on my mac to a totally different, IP address and port.

        eg. Azure
        DNS name: mycouchbasevm.cloudapp.net
        Virtual IP address: 192.xxx.yy.zzz

        via netstat on my mac, there was an entry that looked like this :

        tcp4 0 0 111.21.xxx.yyy.67911 192.xxx.yy.zzz.8081 ESTABLISHED

        if I changed the azure endpoint in your example to use 111.21.xxx.yyy, port 67911 then it began working fine and I was immediately getting results back from the Couchbase server instance running on Azure. For completeness, by that I mean :-

        new Uri(\”http://111.21.xxx.yyy:67911/pools\”)

      2. hi Martin, no worries at all and of course, its going to be something screwy on my setup client side.

        In terms of the test C# code block, no changes made at all, other of course than the endpoint for my Azure VM. I\’ll do some more digging and come back here if any specific questions.

        1. What about Endpoint Protection, have you opened all the required ports? If you missed a port, you could get a timeout or node not found exception.
          Feel free to send me an email (martin (at) couchbase.com) If you would like me to help debug your code/setup.

      3. alrighty, issue resolved. tracked down a minor typo in my hosts file on azure vm. apologies. onwards and upwards ! 🙂

Leave a reply