Search code examples
azureazure-virtual-machineazure-cloud-services

Multiple virtual machines with different url's in one azure cloud service?


I am trying to add multiple virtual machines in one cloud service.

For now, I am able to create one cloud service and one virtual machine in this cloud service. After this, I am trying to add another virtual machine to the same sloud service in the same deployment, and this proces is also successful, but what I can't understand is how to set different url for this new virtual machine!?

Because now, url for first virtual machine is myservicename.cloudapp.net, and for the second virtual machine is the same!! And they have same ip. How can this be? How can I access second virtual machine when it has same url?

Port for first vm is: 80, and for second vm is 81.

Questions:

  1. How to acces individual vm?

  2. Is there a way to have url's like: myservicename.vmname.cloudapp.net? So that first vm url is: myservicename.vmname1.cloudapp.net, and url for second machine: myservicename.vmname2.cloudapp.net

code:

private async Task CreateVirtualMachine() { DeploymentGetResponse deploymentResponse = await _computeManagementClient.Deployments.GetBySlotAsync("myservicename", DeploymentSlot.Production);

    if (deploymentResponse == null)
    {
        var parameters = new VirtualMachineCreateDeploymentParameters
        {
            DeploymentSlot = DeploymentSlot.Production,
            Name = "mservicename",
            Label = "myservicename"
        };

        parameters.Roles.Add(new Role
        {
            OSVirtualHardDisk = new OSVirtualHardDisk
            {
                HostCaching = VirtualHardDiskHostCaching.ReadWrite,
                SourceImageName = "imagename"
            },

            RoleName = "vmname",
            RoleType = VirtualMachineRoleType.PersistentVMRole.ToString(),
            RoleSize = VirtualMachineRoleSize.Small,
            ProvisionGuestAgent = true
        });

        parameters.Roles[0].ConfigurationSets.Add(new ConfigurationSet
        {
            ComputerName = "vmname",
            ConfigurationSetType = ConfigurationSetTypes.LinuxProvisioningConfiguration,
            HostName = "vmname",
            AdminUserName = "adminusername",
            AdminPassword = "adminpass",
            UserName = "username",
            UserPassword = "userpass",
            DisableSshPasswordAuthentication = false,

        });

        parameters.Roles[0].ConfigurationSets.Add(new ConfigurationSet
        {
            ConfigurationSetType = ConfigurationSetTypes.NetworkConfiguration,
            InputEndpoints = new List<InputEndpoint>()
                    {
                        new InputEndpoint()
                        {
                            Name = "HTTP",
                            Protocol = InputEndpointTransportProtocol.Tcp,
                            LocalPort =  80,
                            Port = 80
                        }
                    }
        });

        var response = await _computeManagementClient.VirtualMachines.CreateDeploymentAsync("mservicename", parameters);

    }
    else
    {
        var createParameters = new VirtualMachineCreateParameters
        {
            OSVirtualHardDisk = new OSVirtualHardDisk
            {
                HostCaching = VirtualHardDiskHostCaching.ReadWrite,
                SourceImageName = "imagename"
            },

            RoleName = "vmname",
            RoleSize = VirtualMachineRoleSize.Small,
            ProvisionGuestAgent = true,

            ConfigurationSets = new List<ConfigurationSet>
                {
                    new ConfigurationSet
                    {

                        ComputerName = "vmname",
                        ConfigurationSetType = ConfigurationSetTypes.LinuxProvisioningConfiguration,
                        HostName = "vmname",
                        AdminUserName = "adminusername",
                        AdminPassword = "adminpass",
                        UserName = "username",
                        UserPassword = "userpass",
                        DisableSshPasswordAuthentication = false
                    },
                    new ConfigurationSet
                    {
                        ConfigurationSetType = ConfigurationSetTypes.NetworkConfiguration,
                        InputEndpoints = new List<InputEndpoint>()
                        {
                            new InputEndpoint()
                            {
                                Name = "HTTP",
                                Protocol = InputEndpointTransportProtocol.Tcp,
                                LocalPort =  81,
                                Port = 81
                            }
                        }
                    }
                }
        };

        var responseCreate = await _computeManagementClient.VirtualMachines.CreateAsync("mservicename", deploymentResponse.Name, createParameters);

    }
}

Solution

  • An azure cloud service (yourdns.cloudapp.net) has one DNS name and one IP address. All VMs within that cloud service sit behind that single ip address. If you have an endpoint set up (e.g. port 80), then you may have that endpoint load-balanced across all of your VM's. Or... you may have a port direct-map to a specific VM (e.g. port 8080 going to an admin app on one of your vm's). You'll notice that, for things like ssh, each vm gets its own ssh port, which is an endpoint which maps to port 22 on a specific vm.

    If you want your vm's to have different IP addresses/dns names, you need to either:

    • split them up into separate cloud services (.cloudapp.net)
    • Deploy them in the new Resource Manager model, where you no longer have .cloudapp.net constructs.