forked from pulumi/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KubernetesStack.cs
107 lines (97 loc) · 3.35 KB
/
KubernetesStack.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2016-2020, Pulumi Corporation. All rights reserved.
using Pulumi;
using Pulumi.DigitalOcean;
using Pulumi.DigitalOcean.Inputs;
using Pulumi.Kubernetes.Core.V1;
using Pulumi.Kubernetes.Types.Inputs.Apps.V1;
using Pulumi.Kubernetes.Types.Inputs.Core.V1;
using Pulumi.Kubernetes.Types.Inputs.Meta.V1;
using Config = Pulumi.Config;
using Provider = Pulumi.Kubernetes.Provider;
using ProviderArgs = Pulumi.Kubernetes.ProviderArgs;
class KubernetesStack : Stack
{
public KubernetesStack()
{
var config = new Config();
var nodeCount = config.GetInt32("nodeCount") ?? 2;
var appReplicaCount = config.GetInt32("appReplicaCount") ?? 5;
var domainName = config.Get("domainName");
var cluster = new KubernetesCluster("do-cluser", new KubernetesClusterArgs
{
Region = "sfo2",
Version = "latest",
NodePool = new KubernetesClusterNodePoolArgs
{
Name = "default",
Size = "s-2vcpu-2gb",
NodeCount = nodeCount
}
});
var k8sProvider = new Provider("do-k8s", new ProviderArgs
{
KubeConfig = cluster.KubeConfigs.GetAt(0).Apply(v => v.RawConfig)
});
var app = new Pulumi.Kubernetes.Apps.V1.Deployment("do-app-dep", new DeploymentArgs
{
Spec = new DeploymentSpecArgs
{
Selector = new LabelSelectorArgs
{
MatchLabels =
{
{"app", "app-nginx"}
}
},
Replicas = appReplicaCount,
Template = new PodTemplateSpecArgs
{
Metadata = new ObjectMetaArgs
{
Labels =
{
{"app", "app-nginx"}
}
},
Spec = new PodSpecArgs
{
Containers = new ContainerArgs
{
Name = "nginx",
Image = "nginx"
}
}
}
}
}, new CustomResourceOptions {Provider = k8sProvider});
var appService = new Service("do-app-svc", new ServiceArgs
{
Spec = new ServiceSpecArgs
{
Type = "LoadBalancer",
Selector = app.Spec.Apply(spec => spec.Template.Metadata.Labels),
Ports = new ServicePortArgs
{
Port = 80
}
}
}, new CustomResourceOptions {Provider = k8sProvider});
this.IngressIp = appService.Status.Apply(status => status.LoadBalancer.Ingress[0].Ip);
if (!string.IsNullOrWhiteSpace(domainName))
{
var domain = new Domain("do-domain", new DomainArgs
{
Name = domainName,
IpAddress = this.IngressIp
});
var cnameRecord = new DnsRecord("do-domain-cname", new DnsRecordArgs
{
Domain = domain.Name,
Type = "CNAME",
Name = "www",
Value = "@"
});
}
}
[Output] public Output<string> IngressIp { get; set; }
}