Como vimos en la primera parte, tenemos varias formas de crear nuestro clúster Hadoop en HDInsight.
En este post veremos cómo crearlo desde código .NET y desde PowerShell.
Crear el clúster desde código C#, con el SDK de HDInsight
Paso 1: Instalar los paquetes NuGet necesarios
Para crear nuestro clúster desde C# primero deberemos instalar el paquete NuGet de administración de HDInsight. El paquete que usaremos en este post es: Microsoft.WindowsAzure.Management.HDInsight
.
Paso 2: Función de creación del clúster
Luego podremos usar la siguiente función para crear nuestro clúster:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Security.Cryptography.X509Certificates; | |
using Microsoft.WindowsAzure.Management.HDInsight; | |
namespace Hdinsight | |
{ | |
public static class Helpers | |
{ | |
private static readonly Guid SubscriptionId = new Guid("<subscription id>"); | |
private const string CertThumbprint = "<certificate thumbprint>"; | |
private const string ClusterName = "<cluster name>"; | |
private const string ClusterLocation = "<cluster location>"; // should be the same as the storage account below | |
private const string StorageAccountName = "<storage account name>.blob.core.windows.net"; | |
private const string StorageAccountKey = "<storage account key>"; | |
private const string StorageContainer = "<container name>"; | |
private const string AdminUserName = "<cluster admin name>"; | |
private const string AdminPassword = "<cluster admin password>"; | |
private const int NodeCount = <node count>; | |
public static void CreateCluster() | |
{ | |
var cert = GetCertificateFromStore(CertThumbprint, StoreLocation.CurrentUser); | |
var creds = new HDInsightCertificateCredential(SubscriptionId, cert); | |
var client = HDInsightClient.Connect(creds); | |
var clusterInfo = new ClusterCreateParametersV2 | |
{ | |
Name = ClusterName, | |
Location = ClusterLocation, | |
DefaultStorageAccountName = StorageAccountName, | |
DefaultStorageAccountKey = StorageAccountKey, | |
DefaultStorageContainer = StorageContainer, | |
UserName = AdminUserName, | |
Password = AdminPassword, | |
ClusterSizeInNodes = NodeCount, | |
Version = "3.1" // default | |
}; | |
client.CreateCluster(clusterInfo); | |
} | |
private static X509Certificate2 GetCertificateFromStore(string thumbprint, StoreLocation storeLocation) | |
{ | |
var store = new X509Store(StoreName.My, storeLocation); | |
try | |
{ | |
store.Open(OpenFlags.ReadOnly); | |
var certCollection = store.Certificates; | |
var currentCerts = certCollection.Find(X509FindType.FindByTimeValid, DateTime.Now, false); | |
var signingCert = currentCerts.Find(X509FindType.FindByThumbprint, thumbprint, false); | |
return signingCert.Count != 0 ? signingCert[0] : null; | |
} | |
finally | |
{ | |
store.Close(); | |
} | |
} | |
} | |
} |
Crear el clúster mediante un script de Azure PowerShell, desde Windows
Paso 1: Instalar Azure PowerShell
Para poder crear nuestro clúster desde Azure PowerShell, primero deberemos haber instalado la herramienta, la cual puede instalarse vía Microsoft Web Platform installer o directamente desde la página de GitHub del proyecto.
Esto instalará tanto los cmdlets de Azure como una consola customizada de Azure PowerShell, pero podremos correr los comandos desde la consola normal de PowerShell.
Paso 2: Configurar Azure PowerShell
Lo primero que debemos hacer es asociar nuestra cuenta de Microsoft Azure a la sesión de PowerShell, lo cual haremos ejecutando el siguiente script:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add-AzureAccount |
Esto nos solicitará nuestras credenciales y dejará todo listo para continuar.
Paso 3: Crear el clúster
Para crear nuestro clúster necesitaremos crear:
- Una cuenta de Azure Storage.
- Un contenedor de Azure Blobs.
- Nuestro clúster.
Crear la cuenta de Azure Storage
Si no tenemos una cuenta de Storage, necesitaremos crear una para usar con nuestro clúster. Para ello usaremos el siguiente script:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$storageAccountName = "<StorageAcccountName>" # Nombre de la cuenta de Storage | |
$location = "<MicrosoftDataCenter>" # Por ejemplo, "East US" | |
# Crear una cuenta de Azure Storage | |
New-AzureStorageAccount –StorageAccountName $storageAccountName –Location $location |
Crear el Contenedor en Azure Blob storage
Para crear nuestro contenedor simplemente ejecutamos el siguiente script:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$storageAccountName = "<StorageAccountName>" # Nombre de la cuenta de Storage | |
$containerName="<ContainerName>" # Nombre del Contenedor que crearemos | |
# Creamos el objeto de contexto que representa a la cuenta de Storage | |
$storageAccountKey = Get-AzureStorageKey $storageAccountName | %{ $_.Primary } | |
$destContext = New-AzureStorageContext –StorageAccountName $storageAccountName | |
–StorageAccountKey $storageAccountKey | |
# Creamos el Contenedor en Blob storage | |
New-AzureStorageContainer –Name $containerName –Context $destContext |
Crear el clúster HDInsight
Finalmente creamos nuestro clúster:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$subscriptionName = "<AzureSubscriptionName>" # La suscripción de Azure usada para la creación del clúster HDInsight | |
$storageAccountName = "<AzureStorageAccountName>" # El clúster HDInsight necesita de una cuenta de Azure Storage para el sistema de archivos por defecto | |
$clusterName = "<HDInsightClusterName>" # El nombre del clúster HDInsight a crear | |
$clusterNodes = <ClusterSizeInNodes> # El número de nodos del clúster HDInsight | |
$hadoopUserName = "<HadoopUserName>" # Nombre de usuario para Hadoop. Es el usuario que se utilizará para conectarse al clúster y correr trabajos en él. | |
$hadoopUserPassword = "<HadoopUserPassword>" | |
$secPassword = ConvertTo-SecureString $hadoopUserPassword –AsPlainText –Force | |
$credential = New-Object System.Management.Automation.PSCredential($hadoopUserName,$secPassword) | |
# Obtener la cuenta de Storage primaria a partir del nombre | |
Select-AzureSubscription $subscriptionName | |
$storageAccountKey = Get-AzureStorageKey $storageAccountName | %{ $_.Primary } | |
$containerName = $clusterName # Contenedor de Azure Blob usado como sistema de archivos por defecto del clúster HDInsight | |
# La ubicación del clúster HDInsight. Debe ser el mismo data center que la cuenta de Storage. | |
$location = Get-AzureStorageAccount –StorageAccountName $storageAccountName | %{$_.Location} | |
# Crear un nuevo clúster HDInsight | |
New-AzureHDInsightCluster –Name $clusterName –Credential $credential –Location $location –DefaultStorageAccountName "$storageAccountName.blob.core.windows.net" –DefaultStorageAccountKey $storageAccountKey –DefaultStorageContainerName $containerName –ClusterSizeInNodes $clusterNodes –ClusterType Hadoop |
Próximos pasos
En la tercera parte veremos cómo crear nuestro cúster desde MacOS con la Azure CLI (y cómo instalarla y configurarla).