public static void Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, ILogger log) { var tenantId = "1dc1085b-9aa9-42dc-bedd-4c7dcfe4570e"; var clientSecret = "Code@Gen2018"; var clientId = "c56cd20f-df34-4ab6-8c3d-bc90547d2838"; var clusterId = "/subscriptions/f6fec764-fbe4-42a9-8f27-93cfe2f839cd/resourcegroups/CodeGen2018/providers/Microsoft.ContainerService/managedClusters/CodeGen"; var url = "https://codegenaks-31b27eec.hcp.northeurope.azmk8s.io:443/api/v1/namespaces/kube-system/services/http:heapster:/proxy/apis/metrics/v1alpha1/nodes?labelSelector="; var token = "701c47b9b820885ffa01f2727829d57a"; double averageUsage; var handler = new HttpClientHandler { ServerCertificateCustomValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true }; using (var client = new HttpClient(handler)) { log.LogInformation($"Querying Cluster Metrics..."); client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", $"Bearer {token}"); var response = client.GetStringAsync(url).GetAwaiter().GetResult(); var typedMetric = JsonConvert.DeserializeObject <K8sMetric>(response); averageUsage = Math.Round(typedMetric.Items.Average(x => x.Usage.CPUUsage), 0); } log.LogInformation($"Average CPU usage is {averageUsage}"); var credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(clientId, clientSecret, tenantId, AzureEnvironment.AzureGlobalCloud); var azure = Azure.Configure().Authenticate(credentials).WithDefaultSubscription(); IKubernetesCluster kubernetesCluster = azure.KubernetesClusters.GetById(clusterId); var agentPool = kubernetesCluster.AgentPools.FirstOrDefault().Value as IKubernetesClusterAgentPool; var nodeCount = agentPool.Count; log.LogInformation($"Current node count is {nodeCount}"); if (!kubernetesCluster.ProvisioningState.Equals("Succeeded")) { log.LogInformation($"Cluster level operation in progress. State: {kubernetesCluster.ProvisioningState}"); return; } if (averageUsage >= 50) { nodeCount += 1; log.LogInformation($"Scaling up by 1 VM"); kubernetesCluster.Update().WithAgentPoolVirtualMachineCount(nodeCount).Apply(); } else if (averageUsage < 50 && nodeCount > 3) { nodeCount -= 1; log.LogInformation($"Scaling down by 1 VM"); kubernetesCluster.Update().WithAgentPoolVirtualMachineCount(nodeCount).Apply(); } log.LogInformation($"Autoscaling ended..."); }
public void KubernetesClusterCRUDTest() { using (var context = FluentMockContext.Start(GetType().FullName)) { var aksName = TestUtilities.GenerateName("aks"); var dnsPrefix = "dns" + aksName; var rgName = "rg" + aksName; var agentPoolName = "ap0" + aksName; var networkName = "net" + aksName; var agentPoolSubnetName = "agentsub" + networkName; var virtualNodeSubnetName = "virtualNodesub" + networkName; IKubernetesCluster kubernetesCluster = null; var containerServiceManager = TestHelper.CreateContainerServiceManager(); var resourceManager = TestHelper.CreateResourceManager(); var networkManager = TestHelper.CreateNetworkManager(); try { string envSecondaryServicePrincipal = Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION_2"); if (String.IsNullOrWhiteSpace(envSecondaryServicePrincipal) || !File.Exists(envSecondaryServicePrincipal)) { envSecondaryServicePrincipal = Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION"); } string servicePrincipalClientId = GetSecondaryServicePrincipalClientID(envSecondaryServicePrincipal); string servicePrincipalSecret = GetSecondaryServicePrincipalSecret(envSecondaryServicePrincipal); var network = networkManager.Networks.Define(networkName) .WithRegion(Region.USCentral) .WithNewResourceGroup(rgName) .WithAddressSpace("10.100.0.0/16") .DefineSubnet(agentPoolSubnetName) .WithAddressPrefix("10.100.0.0/24") .Attach() .DefineSubnet(virtualNodeSubnetName) .WithAddressPrefix("10.100.1.0/24") .Attach() .Create(); kubernetesCluster = containerServiceManager.KubernetesClusters.Define(aksName) .WithRegion(Region.USCentral) .WithNewResourceGroup(rgName) .WithLatestVersion() .WithRootUsername("testaks") .WithSshKey(SshKey) .WithServicePrincipalClientId(servicePrincipalClientId) .WithServicePrincipalSecret(servicePrincipalSecret) .DefineAgentPool(agentPoolName) .WithVirtualMachineSize(ContainerServiceVMSizeTypes.StandardD2V2) .WithAgentPoolVirtualMachineCount(1) .WithAgentPoolType(AgentPoolType.VirtualMachineScaleSets) .WithAgentPoolMode(AgentPoolMode.System) .WithVirtualNetwork(network.Id, agentPoolSubnetName) .Attach() .WithDnsPrefix("mp1" + dnsPrefix) .WithVirtualNode(virtualNodeSubnetName) .DefineNetworkProfile .WithNetworkPlugin(NetworkPlugin.Azure) .Attach() .WithTag("tag1", "value1") .Create(); Assert.NotNull(kubernetesCluster.Id); Assert.Equal(Region.USCentral, kubernetesCluster.Region); Assert.Equal("testaks", kubernetesCluster.LinuxRootUsername); Assert.Equal(1, kubernetesCluster.AgentPools.Count); Assert.NotNull(kubernetesCluster.AgentPools[agentPoolName]); Assert.Equal(1, kubernetesCluster.AgentPools[agentPoolName].Count); Assert.Equal(ContainerServiceVMSizeTypes.StandardD2V2, kubernetesCluster.AgentPools[agentPoolName].VMSize); Assert.Equal(AgentPoolType.VirtualMachineScaleSets, kubernetesCluster.AgentPools[agentPoolName].Type); Assert.NotNull(kubernetesCluster.Tags["tag1"]); kubernetesCluster = containerServiceManager.KubernetesClusters.GetByResourceGroup(rgName, aksName); // Updates resource kubernetesCluster = kubernetesCluster.Update() .WithAgentPoolVirtualMachineCount(agentPoolName, 5) .WithTag("tag2", "value2") .WithTag("tag3", "value3") .WithoutTag("tag1") .Apply(); Assert.Equal(1, kubernetesCluster.AgentPools.Count); Assert.Equal(5, kubernetesCluster.AgentPools[agentPoolName].Count); Assert.NotNull(kubernetesCluster.Tags["tag2"]); Assert.True(!kubernetesCluster.Tags.ContainsKey("tag1")); } finally { try { resourceManager.ResourceGroups.BeginDeleteByName(rgName); } catch { } } } }
public void KubernetesClusterCRUDTest() { using (var context = FluentMockContext.Start(GetType().FullName)) { var aksName = TestUtilities.GenerateName("aks"); var dnsPrefix = "dns" + aksName; var rgName = "rg" + aksName; var agentPoolName = "ap0" + aksName; IKubernetesCluster kubernetesCluster = null; var containerServiceManager = TestHelper.CreateContainerServiceManager(); var resourceManager = TestHelper.CreateResourceManager(); try { kubernetesCluster = containerServiceManager.KubernetesClusters.Define(aksName) .WithRegion(Region.USCentral) .WithNewResourceGroup(rgName) .WithLatestVersion() .WithRootUsername("testaks") .WithSshKey(SshKey) .WithServicePrincipalClientId("spId") .WithServicePrincipalSecret("spSecret") .DefineAgentPool(agentPoolName) .WithVirtualMachineCount(1) .WithVirtualMachineSize(ContainerServiceVirtualMachineSizeTypes.StandardD1V2) .Attach() .WithDnsPrefix("mp1" + dnsPrefix) .WithTag("tag1", "value1") .Create(); Assert.NotNull(kubernetesCluster.Id); Assert.Equal(Region.USCentral, kubernetesCluster.Region); Assert.Equal("testaks", kubernetesCluster.LinuxRootUsername); Assert.Equal(1, kubernetesCluster.AgentPools.Count); Assert.NotNull(kubernetesCluster.AgentPools[agentPoolName]); Assert.Equal(1, kubernetesCluster.AgentPools[agentPoolName].Count); Assert.Equal(ContainerServiceVirtualMachineSizeTypes.StandardD1V2, kubernetesCluster.AgentPools[agentPoolName].VMSize); Assert.NotNull(kubernetesCluster.Tags["tag1"]); kubernetesCluster = containerServiceManager.KubernetesClusters.GetByResourceGroup(rgName, aksName); // Updates resource kubernetesCluster = kubernetesCluster.Update() .WithAgentVirtualMachineCount(agentPoolName, 5) .WithTag("tag2", "value2") .WithTag("tag3", "value3") .WithoutTag("tag1") .Apply(); Assert.Equal(1, kubernetesCluster.AgentPools.Count); Assert.Equal(5, kubernetesCluster.AgentPools[agentPoolName].Count); Assert.NotNull(kubernetesCluster.Tags["tag2"]); Assert.True(!kubernetesCluster.Tags.ContainsKey("tag1")); } finally { try { resourceManager.ResourceGroups.BeginDeleteByName(rgName); } catch { } } } }
/** * An Azure Container Services sample for managing a Kubernetes cluster. * - Create a Kubernetes cluster * - Update the number of agent virtual machines in an Azure Container Service */ public static void RunSample(IAzure azure, string clientId, string secret) { string rgName = SdkContext.RandomResourceName("rgaks", 15); string aksName = SdkContext.RandomResourceName("akssample", 30); Region region = Region.USCentral; string rootUserName = "******"; string sshPublicKey = // replace with a real SSH public key "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCyhPdNuJUmTeLsaZL83vARuSVlN5qbKs7j" + "Cm723fqH85rIRQHgwEUXJbENEgZT0cXEgz4h36bQLMYT3/30fRnxYl8U6gRn27zFiMwaDstOjc9EofStODbiHx9A" + "Y1XYStjegdf+LNa5tmRv8dZEdj47XDxosSG3JKHpSuf0fXr4u7NjgAxdYOxyMSPAEcfXQctA+ybHkGDLdjLHT7q5C" + "4RXlQT7S9v5z532C3KuUSQW7n3QBP3xw/bC8aKcJafwZUYjYnw7owkBnv4TsZVva2le7maYkrtLH6w+XbhfHY4WwK" + "Y2Xxl1TxSGkb8tDsa6XgTmGfAKcDpnIe0DASJD8wFF [email protected]"; string servicePrincipalClientId = clientId; // replace with a real service principal client id string servicePrincipalSecret = secret; // and corresponding secret try { //============================================================= // ... //============================================================= // If service principal client id and secret are not set via the local variables, attempt to read the service // principal client id and secret from a secondary ".azureauth" file set through an environment variable. // // If the environment variable was not set then reuse the main service principal set for running this sample. if (String.IsNullOrWhiteSpace(servicePrincipalClientId) || String.IsNullOrWhiteSpace(servicePrincipalSecret)) { string envSecondaryServicePrincipal = Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION_2"); if (String.IsNullOrWhiteSpace(envSecondaryServicePrincipal) || !File.Exists(envSecondaryServicePrincipal)) { envSecondaryServicePrincipal = Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION"); } servicePrincipalClientId = Utilities.GetSecondaryServicePrincipalClientID(envSecondaryServicePrincipal); servicePrincipalSecret = Utilities.GetSecondaryServicePrincipalSecret(envSecondaryServicePrincipal); } //============================================================= // Create a Kubernetes cluster Utilities.Log("Creating a Kubernetes cluster with one agent and one virtual machine"); IKubernetesCluster kubernetesCluster = azure.KubernetesClusters.Define(aksName) .WithRegion(region) .WithNewResourceGroup(rgName) .WithLatestVersion() .WithRootUsername(rootUserName) .WithSshKey(sshPublicKey) .WithServicePrincipalClientId(servicePrincipalClientId) .WithServicePrincipalSecret(servicePrincipalSecret) .DefineAgentPool("ap") .WithVirtualMachineSize(ContainerServiceVirtualMachineSizeTypes.StandardD1V2) .WithAgentPoolVirtualMachineCount(1) .Attach() .WithDnsPrefix("dns-" + aksName) .Create(); Utilities.Log("Created Kubernetes cluster: " + kubernetesCluster.Id); Utilities.Print(kubernetesCluster); //============================================================= // Updates a Kubernetes cluster agent with two virtual machines Utilities.Log("Updating the Kubernetes cluster agent with two virtual machines"); kubernetesCluster.Update() .WithAgentPoolVirtualMachineCount(2) .Apply(); Utilities.Log("Updated Kubernetes cluster: " + kubernetesCluster.Id); Utilities.Print(kubernetesCluster); } finally { try { Utilities.Log("Deleting Resource Group: " + rgName); azure.ResourceGroups.BeginDeleteByName(rgName); Utilities.Log("Deleted Resource Group: " + rgName); } catch (NullReferenceException) { Utilities.Log("Did not create any resources in Azure. No clean up is necessary"); } catch (Exception g) { Utilities.Log(g); } } }
static async Task AsyncMain(string[] args) { var credentials = SdkContext.AzureCredentialsFactory.FromFile("../../Credentials/authfile.json"); var azure = Azure .Configure() .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic) .Authenticate(credentials) .WithDefaultSubscription(); Console.WriteLine("Your subscription ID: \r\n" + azure.SubscriptionId); Console.WriteLine("Creating an AKS Cluster!"); String rootUser = azureConfig.GetValue("RootUser").ToString(); String rgName = azureConfig.GetValue("ResourceGroup").ToString(); String aksName = azureConfig.GetValue("ClusterName").ToString(); String location = azureConfig.GetValue("Location").ToString(); String sshPublicKey = azureConfig.GetValue("SshPublicKey").ToString(); String clientSecret = JObject.Parse(File.ReadAllText("../../Credentials/authfile.json")).GetValue("clientSecret").ToString(); try { Console.WriteLine("Trying to create the cluster..."); IKubernetesCluster cluster = await azure.KubernetesClusters.Define(aksName) .WithRegion(location) .WithNewResourceGroup(rgName) .WithLatestVersion() .WithRootUsername(rootUser) .WithSshKey(sshPublicKey) .WithServicePrincipalClientId(credentials.ClientId) .WithServicePrincipalSecret(clientSecret) .DefineAgentPool("agentpool") .WithVirtualMachineSize(ContainerServiceVMSizeTypes.StandardA2) .WithAgentPoolVirtualMachineCount(2) .Attach() .WithDnsPrefix("dns-" + aksName) .CreateAsync(); Console.WriteLine("Created Kubernetes Cluster: " + cluster.Id); Console.WriteLine(cluster); Console.WriteLine("Updating Kubernetes Cluster. More VMs!!: " + cluster.Id); await cluster.Update() .WithAgentPoolVirtualMachineCount(4) .ApplyAsync(); Console.WriteLine("Updated Kubernetes Cluster: " + cluster.Id); Console.WriteLine(cluster); } catch (Exception g) { Console.WriteLine(g); } finally { try { Console.WriteLine("Deleting Resource Group: " + rgName); await azure.ResourceGroups.BeginDeleteByNameAsync(rgName); Console.WriteLine("Deleted Resource Group: " + rgName); } catch (NullReferenceException) { Console.WriteLine("Did not create any resources in Azure. No clean up is necessary"); } catch (Exception g) { Console.WriteLine(g); } } }