Skip to content

Instantly share code, notes, and snippets.

@bgelens
Last active October 29, 2020 20:32
Show Gist options
  • Save bgelens/14647e8a64bd88c5e731975ca29380c4 to your computer and use it in GitHub Desktop.
Save bgelens/14647e8a64bd88c5e731975ca29380c4 to your computer and use it in GitHub Desktop.
Sample using Pulumi Azure NextGen provider to create AKS and deploy app into it.
using Pulumi;
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AzureNextGen = Pulumi.AzureNextGen;
using K8s = Pulumi.Kubernetes;
using Helm3 = Pulumi.Kubernetes.Helm.V3;
class MyStack : Stack
{
public MyStack()
{
var config = new Config();
var rgname = config.Require("rgname");
var vnetAddressPrefix = config.Get("shortLocataddressSpaceion") ?? "10.14.0.0/16";
var aksSubnetAddressPrefix = config.Get("aksSubnetAddressPrefix") ?? "10.14.4.0/22";
var aksName = "aks01";
var vnetName = "vnet-01";
var aksSubnetName = "subnet-aks-01";
var aksVersion = config.Get("aksVersion") ?? "1.19.0";
var rsg = new AzureNextGen.Resources.Latest.ResourceGroup(name: "rg", args: new AzureNextGen.Resources.Latest.ResourceGroupArgs
{
ResourceGroupName = rgname,
Location = "westeurope"
});
var vnet = new AzureNextGen.Network.Latest.VirtualNetwork(name: "aks-vnet", args: new AzureNextGen.Network.Latest.VirtualNetworkArgs
{
VirtualNetworkName = vnetName,
AddressSpace = new AzureNextGen.Network.Latest.Inputs.AddressSpaceArgs
{
AddressPrefixes =
{
vnetAddressPrefix
}
},
Subnets = new AzureNextGen.Network.Latest.Inputs.SubnetArgs
{
Name = aksSubnetName,
AddressPrefix = aksSubnetAddressPrefix
},
Location = rsg.Location,
ResourceGroupName = rsg.Name
});
var aks = new AzureNextGen.ContainerService.Latest.ManagedCluster(name: "aks", new AzureNextGen.ContainerService.Latest.ManagedClusterArgs
{
ResourceName = aksName,
KubernetesVersion = aksVersion,
ResourceGroupName = rsg.Name,
Location = rsg.Location,
EnableRBAC = true,
EnablePodSecurityPolicy = false,
AadProfile = new AzureNextGen.ContainerService.Latest.Inputs.ManagedClusterAADProfileArgs
{
Managed = true,
AdminGroupObjectIDs = "2e62c9e5-46c7-4c78-81f6-029538c95d9d" // group does not exist
},
NetworkProfile = new AzureNextGen.ContainerService.Latest.Inputs.ContainerServiceNetworkProfileArgs
{
NetworkPlugin = "azure",
DnsServiceIP = "10.244.0.10",
ServiceCidr = "10.244.0.0/22",
DockerBridgeCidr = "172.17.0.1/16"
},
AgentPoolProfiles = {
new AzureNextGen.ContainerService.Latest.Inputs.ManagedClusterAgentPoolProfileArgs
{
Count = 1,
OsType = "Linux",
OsDiskType = "Managed",
Type = "VirtualMachineScaleSets",
Name = "general",
Mode = "System",
MinCount = 1,
MaxCount = 3,
EnableAutoScaling = true,
VmSize = "Standard_DS2_v2",
VnetSubnetID = vnet.Subnets.Apply(s => s.ElementAt(0).Id ?? "")
}
},
Identity = new AzureNextGen.ContainerService.Latest.Inputs.ManagedClusterIdentityArgs { Type = "SystemAssigned" },
DnsPrefix = aksName
});
var kubeConfig = Output.Tuple(rsg.Name, aks.Name).Apply(names =>
GetKubeConfig(names.Item1, names.Item2));
this.KubeConfig = kubeConfig.Apply(Output.CreateSecret);
var k8sprovider = new K8s.Provider(name: "k8sprovider", args: new K8s.ProviderArgs
{
KubeConfig = kubeConfig,
Cluster = aksName
});
var appdeploy = new K8s.Apps.V1.Deployment(name: "nginx", options: new CustomResourceOptions { Provider = k8sprovider }, args: new K8s.Types.Inputs.Apps.V1.DeploymentArgs
{
Metadata = new K8s.Types.Inputs.Meta.V1.ObjectMetaArgs
{
Name = "nginx-deployment"
},
Kind = "Deployment",
Spec = new K8s.Types.Inputs.Apps.V1.DeploymentSpecArgs
{
Selector = new K8s.Types.Inputs.Meta.V1.LabelSelectorArgs
{
MatchLabels = {
{ "app", "nginx" }
}
},
Template = new K8s.Types.Inputs.Core.V1.PodTemplateSpecArgs
{
Metadata = new K8s.Types.Inputs.Meta.V1.ObjectMetaArgs
{
Labels = {
{ "app", "nginx" }
}
},
Spec = new K8s.Types.Inputs.Core.V1.PodSpecArgs
{
Containers = new K8s.Types.Inputs.Core.V1.ContainerArgs
{
Name = "nginx",
Image = "nginx:1.14.2",
Ports = new K8s.Types.Inputs.Core.V1.ContainerPortArgs
{
ContainerPortValue = 80
}
}
}
}
}
});
var service = new K8s.Core.V1.Service(name: "service", options: new CustomResourceOptions { Provider = k8sprovider }, args: new K8s.Types.Inputs.Core.V1.ServiceArgs
{
ApiVersion = "v1",
Kind = "Service",
Metadata = new K8s.Types.Inputs.Meta.V1.ObjectMetaArgs
{
Name = "nginx-svc"
},
Spec = new K8s.Types.Inputs.Core.V1.ServiceSpecArgs
{
Type = "LoadBalancer",
Ports = new K8s.Types.Inputs.Core.V1.ServicePortArgs
{
Port = 80
},
Selector = {
{ "app", "nginx" }
}
}
});
var votingapp = new Helm3.Chart(releaseName: "votingapp", options: new ComponentResourceOptions { Provider = k8sprovider }, args: new K8s.Helm.ChartArgs
{
Repo = "azure-samples",
Chart = "azure-vote",
Version = "0.1.1",
Values = {
{ "value1", "tabs" },
{ "value2", "spaces" },
{ "serviceType", "LoadBalancer"}
}
});
this.Url = Output.Format($"http://{service.Status.Apply(lb => lb.LoadBalancer.Ingress[0].Ip)}");
this.VotingUrl = Output.Format($"http://{votingapp.GetResource<K8s.Core.V1.Service>("azure-vote-front").Apply(s => s.Status.Apply(lb => lb.LoadBalancer.Ingress[0].Ip))}");
}
[Output("kubeconfig")] public Output<string> KubeConfig { get; set; }
[Output("url")] public Output<string> Url { get; set; }
[Output("votingappurl")] public Output<string> VotingUrl { get; set; }
private static async Task<string> GetKubeConfig(string resourceGroupName, string clusterName)
{
var credentials = await AzureNextGen.ContainerService.Latest.ListManagedClusterAdminCredentials.InvokeAsync(new AzureNextGen.ContainerService.Latest.ListManagedClusterAdminCredentialsArgs
{
ResourceGroupName = resourceGroupName,
ResourceName = clusterName
});
var encoded = credentials.Kubeconfigs[0].Value;
var data = Convert.FromBase64String(encoded);
return Encoding.UTF8.GetString(data);
}
}
@bgelens
Copy link
Author

bgelens commented Oct 29, 2020

prereq:

helm repo add azure-samples https://azure-samples.github.io/helm-charts/
helm repo update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment