forked from pulumi/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
222 lines (206 loc) · 8.33 KB
/
index.ts
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright 2016-2020, Pulumi Corporation. All rights reserved.
import * as aws from "@pulumi/aws";
import * as awsx from "@pulumi/awsx";
import * as eks from "@pulumi/eks";
import * as k8s from "@pulumi/kubernetes";
import * as pulumi from "@pulumi/pulumi";
// Getting necessary settings from the Pulumi config
const config = new pulumi.Config();
const sqlUserName = config.require("sql-user-name");
const sqlUserPassword = config.requireSecret("sql-user-password");
const region = aws.config.region;
// Creating an EKS cluster in which our project will run
const eksCluster = new eks.Cluster("eksCluster", {
name: "eksCluster",
instanceType: "t2.medium",
desiredCapacity: 3,
minSize: 2,
maxSize: 4,
deployDashboard: false,
enabledClusterLogTypes: [
"api",
"audit",
"authenticator",
],
});
// To prevent information in the MongoDB database from being lost in the case of a shutdown or reset,
// everything will be stored inside an Elastic Block Store volume
const ebsVolume = new aws.ebs.Volume("storage-volume", {
size: 1,
type: "gp2",
availabilityZone: region + "a",
encrypted: true,
}, {
protect: false,
});
const databaseName = "votes";
// Creating a Kubernetes deployment for the MongoDB database. A single pod will receive queries, and will
// modify the data accordingly
const databaseAppName = "database-side-service";
const databaseAppLabels = { appClass: databaseAppName };
const databaseDeployment = new k8s.apps.v1.Deployment(databaseAppName, {
metadata: { name: databaseAppName, labels: databaseAppLabels },
spec: {
replicas: 1,
selector: { matchLabels: databaseAppLabels },
template: {
metadata: { labels: databaseAppLabels },
spec: {
containers: [{
name: databaseAppName,
image: awsx.ecr.buildAndPushImage("database-side-service", "./databaseside").image(),
ports: [{ name: "http", containerPort: 27017 }],
env: [
{ name: "DATABASE_NAME", value: databaseName },
{ name: "USER_NAME", value: sqlUserName },
{ name: "USER_PASSWORD", value: sqlUserPassword },
],
volumeMounts: [{ // The EBS volume is mounted to the pod, allowing the database to permanently store data
name: "persistent-volume",
mountPath: "/persistentVolume",
}],
resources: {
limits: {
memory: "1Gi",
cpu: "1000m",
},
},
}],
volumes: [{
name: "persistent-volume",
awsElasticBlockStore: {
volumeID: ebsVolume.id,
},
}],
affinity: { // The pod is configured to always launch in the same availability zone as the EBS volume
nodeAffinity: {
requiredDuringSchedulingIgnoredDuringExecution: {
nodeSelectorTerms: [{
matchExpressions: [{
key: "failure-domain.beta.kubernetes.io/zone",
operator: "In",
values: [ebsVolume.availabilityZone],
}],
}],
},
},
},
},
},
strategy: {
type: "Recreate",
},
}}, {
deleteBeforeReplace: true,
provider: eksCluster.provider,
});
// A Kubernetes service is created for the database using an internal IP address. Other components within
// the cluster can now communicate with it
const databasesideListener = new k8s.core.v1.Service("database-side-listener", {
metadata: { labels: databaseDeployment.metadata.labels },
spec: {
type: "ClusterIP",
ports: [{ port: 27017, targetPort: "http" }],
selector: databaseAppLabels,
publishNotReadyAddresses: false,
}}, {
provider: eksCluster.provider,
},
);
const mongodbAddress = databasesideListener.spec.clusterIP;
// Creating a deployment for the server which receives requests from the users, and translates them into
// queries for the database. Any number of pods can be created for this deployment
const serverAppName = "server";
const serverAppLabels = { appClass: serverAppName };
const serverDeployment = new k8s.apps.v1.Deployment("server-side-service", {
metadata: { labels: serverAppLabels },
spec: {
replicas: 2,
selector: { matchLabels: serverAppLabels },
template: {
metadata: { labels: serverAppLabels },
spec: {
containers: [{
name: serverAppName,
image: awsx.ecr.buildAndPushImage("server-side-service", "./serverside").image(),
ports: [{ name: "http", containerPort: 5000 }],
env: [
{ name: "USER_NAME", value: sqlUserName },
{ name: "USER_PASSWORD", value: sqlUserPassword },
{ name: "MONGODB_ADDRESS", value: mongodbAddress },
{ name: "MONGODB_PORT", value: String(27017) },
{ name: "DATABASE_NAME", value: databaseName },
],
resources: {
limits: {
memory: "500Mi",
cpu: "500m",
},
},
}],
},
},
}}, {
provider: eksCluster.provider,
});
// A service is created for the server to open it to the internet. Anyone can send commands to the
// service's Load Balancer, which will autimatically distribute them across the availible pods.
const serversideListener = new k8s.core.v1.Service("server-side-listener", {
metadata: { labels: serverDeployment.metadata.labels },
spec: {
type: "LoadBalancer",
ports: [{ port: 5000, targetPort: "http" }],
selector: serverAppLabels,
publishNotReadyAddresses: false,
}}, {
provider: eksCluster.provider,
},
);
// The final deployment is created for the client compoment. It acts as the main web page of the
// voting application
const clientAppName = "client";
const clientAppLabels = { appClass: clientAppName };
const clientDeployment = new k8s.apps.v1.Deployment("client-side-service", {
metadata: { labels: clientAppLabels },
spec: {
replicas: 2,
selector: { matchLabels: clientAppLabels },
template: {
metadata: { labels: clientAppLabels },
spec: {
containers: [{
name: clientAppName,
image: awsx.ecr.buildAndPushImage("client-side-service", "./clientside").image(),
ports: [{ name: "http", containerPort: 3000 }],
env: [
{ name: "SERVER_HOSTNAME", value: serversideListener.status.loadBalancer.ingress[0].hostname },
],
resources: {
limits: {
memory: "500Mi",
cpu: "500m",
},
},
}],
},
},
}}, {
provider: eksCluster.provider,
});
// Like with the server, a Load Balancer service is created for the clientside component. Users
// will connect to it using port 3000, and will be balanced across the availible pods
const clientsideListener = new k8s.core.v1.Service("client-side-listener", {
metadata: { labels: clientDeployment.metadata.labels },
spec: {
type: "LoadBalancer",
ports: [{ port: 3000, targetPort: "http" }],
selector: clientAppLabels,
publishNotReadyAddresses: false,
}}, {
provider: eksCluster.provider,
},
);
// Exporting the KubeConfig of the cluster, and the URL of the clientside listener. We can now
// use the URL to connect to our application
export const kubeConfig = eksCluster.kubeconfig;
export const URL = clientsideListener.status.loadBalancer.ingress[0].hostname;