Hot questions for Using Azure in virtual machine

Top Java Programmings / Azure / virtual machine

Question:

Case 1. I installed mongo on my machine (Mac OS), and mongo is configured with bind-ip : 127.0.0.1. A remote machine in LAN can connect to my mongodb using my IP. (Wrong observation : See UPDATE section)

Case 2. I installed mongo on Azure Ubuntu VM, mongo configured with bind-ip:127.0.0.1. No remote machine from the same VNet can connect to that mongo using private vnet IP.

MongoDB Network Setting (both case 1 and case 2):

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1  

Exception (Case 2):

2018-04-03 06:35:29.948 INFO 44518 --- [72.17.1.4:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server 172.17.1.4:27017

com.mongodb.MongoSocketOpenException: Exception opening socket at com.mongodb.connection.SocketStream.open(SocketStream.java:63) ~[mongodb-driver-core-3.4.1.jar!/:na] at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115) ~[mongodb-driver-core-3.4.1.jar!/:na] at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113) ~[mongodb-driver-core-3.4.1.jar!/:na] at java.lang.Thread.run(Thread.java:748) [na:1.8.0_151] Caused by: java.net.ConnectException: Connection refused (Connection refused) at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_151] at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_151] at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_151] at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_151] at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_151] at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_151] at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:57) ~[mongodb-driver-core-3.4.1.jar!/:na] at com.mongodb.connection.SocketStream.open(SocketStream.java:58) ~[mongodb-driver-core-3.4.1.jar!/:na] ... 3 common frames omitted

Whats is different in Azure VM that it behaves differently from a Mac OS installation?

Note: Changing the bindIp to the IP (172.17.1.4) or 0.0.0.0 works.

UPDATE:

Case 1: The remote access on my local machine's mongo was because of the distribution I picked which was configured to allow remote connections.


Answer:

Based on my knowledge, 127.0.0.1 only works inside machine. You could check this blog.

By default, MongoDB bind to local interface only, it will restrict the remote connections. If you don’t care about security, just comment out to accept any remote connections (NOT Recommend).

I suggest you could configure Mongodb like bind_ip = 127.0.0.1,172.17.1.4.

Question:

I'm trying to create a VM using Azure's Java ARM SDK (0.9.3) and I want to tag all of the related resources with a tag, so later on I can delete them by this tag (and I don't want to create a separate resource group for this VM and then delete it).

I have seen in the feedback forums here - https://feedback.azure.com/forums/216843-virtual-machines/suggestions/8945692-delete-vm-with-all-associated-resources that others are trying to do the same, but is there a way to tag all the related resources during creation time, rather that iterating all of them, because this will be the same as deleting them one by one.

In general - is there a better way for deleting all the resources without tags?

Thanks!


Answer:

@s-rusev, It seems that the only way for deleting all the resources without tags is using Azure Resource Management REST API for template deployments to creating and deleting a VM. Please see the REST API reference Template Deployments to know how to manage deployments of multiple resources.

Question:

I am looking for a way to get the power state of a VM through the Azure ARM API (more specifically with the Java SDK), so far without any luck.

The code which retrieves the VM is the following:

import com.microsoft.azure.management.compute.ComputeManagementService;
import com.microsoft.azure.management.compute.ComputeManagementClient;
import com.microsoft.azure.management.compute.models.VirtualMachineGetResponse;
import com.microsoft.azure.management.compute.models.VirtualMachine;

@Test
public void getVirtualMachine() throws Exception {
    ComputeManagementClient client = ComputeManagementService.create(createConfiguration());
    VirtualMachineGetResponse response = client.getVirtualMachinesOperations().get("vdimtest5rg", "vdim-test-5");
    VirtualMachine virtualMachine = response.getVirtualMachine();
}

The REST call to which this results is:

GET /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachines/{virtualMachineName}?api-version=2015-06-15

What is the recommended way to get the power state of a virtual machine in Azure through ARM?

I don't mind using a plain HTTP request, as long as I don't have to fall back to the ASM API and authenticate with a client certificate.


Answer:

You need to be calling the 'InstanceView' of the virtual machine, which will give you the status of the associated disks and machine.

/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachines/{VMName}/InstanceView?api-version=2015-05-01-preview

Which should give you something that contains a snippet like this -

  "statuses": [
    {
      "code": "ProvisioningState/succeeded",
      "level": "Info",
      "displayStatus": "Provisioning succeeded",
      "time": "2016-01-26T20:00:46.4647319+00:00"
    },
    {
      "code": "PowerState/running",
      "level": "Info",
      "displayStatus": "VM running"
    }
  ]

Question:

I am using the Azure Rest API. How can I check if a virtual machine is running or stopped? for first I thought to use the provisioning State, but it gives no useful info


Answer:

Try the code below. PowerState is what you need to check.

       using (ComputeManagementClient computeClient = new ComputeManagementClient(credentials))
        {
            HostedServiceListResponse services  = await computeClient.HostedServices.ListAsync();
            foreach(HostedServiceListResponse.HostedService service in services.HostedServices)
            {
                DeploymentGetResponse deployment = await computeClient.Deployments.GetBySlotAsync(service.ServiceName, DeploymentSlot.Production);

                var powerState = deployment.RoleInstances[0].PowerState;

            }
        }

Question:

I have created a Java Web Application using Jetty (in Eclipse, using OSGI etc.). The application itself runs quite well (when being tested locally), so I wanted to run it on an Azure virtual machine in order to be accessible for external users (for testing reasons).

What I did so far:

  • created an Azure account
  • create a virtual machine with Windows Server running in it
  • downloaded all my eclipse files etc. to the virtual machine
  • started the application (in fact in eclipse, not the compiled jar) in the virtual machine; the application is published to port 8080
  • so, when i run a webbrowser in the VM and connect to localhost:8080, everything works well
  • but when I try to access the website from external (using my assigned domain of the VM, something.cloudapp.net:8080), it does not work
  • I also created endpoints in the azure management console for this VM (80, 8080, etc.)

Does anyone ever tried to run a java webapp on Azure or has a hint what could go wrong here?


Answer:

By default, windows servers in Azure have the windows firewall enabled. This would block external connections to port 8080 by default. Try adding an appropriate exception to the windows firewall rules.

Question:

Tried to run the CreateVMExample.java from Azure-Java-SDK

   VirtualMachine vm = ComputeHelper.createVM(
                        resourceManagementClient, computeManagementClient, networkResourceProviderClient, storageManagementClient,
                        context, "javaSampleVM", "Foo12", "BaR@123rgababaab")
                        .getVirtualMachine();

                System.out.println(vm.getName() + " is created");

and got the following error.

Exception in thread "main" java.util.concurrent.ExecutionException: com.microsoft.windowsazure.exception.ServiceException: MissingSubscriptionRegistration: The subscription is not registered to use namespace 'Microsoft.Storage'.
    at java.util.concurrent.FutureTask.report(FutureTask.java:122)
    at java.util.concurrent.FutureTask.get(FutureTask.java:188)
    at com.microsoft.azure.management.storage.StorageAccountOperationsImpl.create(StorageAccountOperationsImpl.java:737)
    at com.microsoft.azure.utility.StorageHelper.createStorageAccount(StorageHelper.java:71)
    at com.microsoft.azure.utility.StorageHelper.createStorageAccount(StorageHelper.java:53)
    at com.microsoft.azure.utility.ComputeHelper.createVM(ComputeHelper.java:246)
    at com.microsoft.azure.utility.ComputeHelper.createVM(ComputeHelper.java:213)
    at com.microsoft.azure.auth.CreateVMExample.main(CreateVMExample.java:74)

As I'm new to Azure from AWS, couldn't understand where to begin. saw same kind of error releavnt to Azure PS. But not much helpful. Please suggest something. I'm using Azure Active Directory Auth. Thanks.


Answer:

The issue is caused by authenticating with Azure AD. The doc "Authenticating Azure Resource Management request"( https://msdn.microsoft.com/en-us/library/azure/dn790557.aspx) says "All of the tasks that you do on resources using the Azure Resource Manager must be authenticated with Azure Active Directory. ". So using the Azure Resource Management API of Class ComputeHelper must be authenticated with AAD.

You need to setup authentication on Azure AD by using the Management Portal firstly. For the steps of details, please refer to https://msdn.microsoft.com/en-us/library/azure/dn790557.aspx#bk_portal and https://azure.microsoft.com/en-us/documentation/articles/resource-group-create-service-principal-portal/.

There is a sample project (https://github.com/Azure-Samples/active-directory-java-graphapi-web) for calling Azure AD in Java that it can help you.

Question:

Tell me please, how can I get all options for instance on Azure using Java ?

For example, I mean all VM parameters: name, instance type, launch time and etc. So that I can use them in my code and get a detailed message in case of all kinds of errors associated with the VM on Azure. Thanks


Answer:

You can use Azure virtual machine libraries to manage your virtual machines.

To use Azure management library, firstly, you need to get authenticated. For more details, you may refer to: Authentication in Azure Management Libraries for Java


Another way is to use Azure REST API. Here is the tutorial for your getting started: https://docs.microsoft.com/en-us/rest/api/azure/

You can use HttpClient or You can also directly try that API on the page:

Question:

I have a springboot application running on a Microsoft azure VM machine, which I want to be able to communicate with from the outside world.

When I type in the address to the application in a browser on the VM machine, which in this case is http://localhost:8080/spring, I get the expected reply back.

However, when I try to reach this application from any other device, even when using the public ip address for the virtualmachine, for some reason I cannot access it.

Do anyone of you know what I should do to solve this problem?


Answer:

You will need to export the port that you want to access externally.

You can find a documentation on how to open ports here, through the Azure portal.

To summarize from their documentation:

  1. Create a Network Security Group (NSG)
  2. Select from the newly created NSG, create a new Inbound security rule
  3. Select HTTP, your port that you desire (in your case 8080), priority 100, give it a name.
  4. Your final step is to associate your NSG with a subnet or a specific network interface.

Question:

I am trying to Create Virtual Machine Deployment operation through azure rest API as of the documentation here.

But I fail to figure out parameters to create a VM with a OSImage(like Ubuntu 14.04). What should be the values for SourceImageName and MediaLink of OSVirtualHadrDisk?


Answer:

You can easily get a list of available image names using Get-AzureVMImage from the Azure PowerShell Cmdlets.

Get-AzureVMImage | where { $_.ImageFamily -like "*ubuntu*14.04*" } | Sort-Object PublishedDate | Format-List ImageFamily, ImageName

...
ImageFamily : Ubuntu Server 14.04 LTS
ImageName   : b39f27a8b8c64d52b05eac6a62ebad85__Ubuntu-14_04_1-LTS-amd64-server-20140927-en-us-30GB

ImageFamily : Ubuntu Server 14.04 LTS
ImageName   : b39f27a8b8c64d52b05eac6a62ebad85__Ubuntu-14_04_1-LTS-amd64-server-20141125-en-us-30GB

ImageFamily : Ubuntu Server 14.04 LTS
ImageName   : b39f27a8b8c64d52b05eac6a62ebad85__Ubuntu-14_04_1-LTS-amd64-server-20150123-en-us-30GB
...

So, for example, your SourceImageName would be as follows for the image published 01-23-2015:

b39f27a8b8c64d52b05eac6a62ebad85__Ubuntu-14_04_1-LTS-amd64-server-20150123-en-us-30GB

The MediaLink is the name and location of the VHD that is created for your virtual machine. It can be any name you want provided it resides in your Azure Storage Account. For example:

https://[YOUR STORAGE ACCT NAME].blob.core.windows.net/vhds/[SOME UNIQUE NAME].vhd  

If you want to see an example, create a Virtual Machine in your subscription using the Azure Management Portal and then look at the Disks section of the DASHBOARD page for your Virtual Machine.