Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explore topologySpreadConstraints for K8s deployments #2824

Merged
merged 4 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions deployment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ const googleConfig = {
const supertokens = deploySuperTokens(
{ apiKey: supertokensApiKey.result },
{ dependencies: [dbMigrations] },
deploymentEnv,
);

const zendeskConfig = new pulumi.Config('zendesk');
Expand Down
1 change: 1 addition & 0 deletions deployment/services/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export function deployApp({
imagePullSecret,
readinessProbe: '/api/health',
livenessProbe: '/api/health',
availabilityOnEveryNode: true,
env: [
{ name: 'DEPLOYED_DNS', value: deploymentEnv.DEPLOYED_DNS },
{ name: 'NODE_ENV', value: 'production' },
Expand Down
3 changes: 2 additions & 1 deletion deployment/services/billing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { parse } from 'pg-connection-string';
import * as k8s from '@pulumi/kubernetes';
import * as pulumi from '@pulumi/pulumi';
import { DeploymentEnvironment } from '../types';
import { isProduction } from '../utils/helpers';
import { serviceLocalEndpoint } from '../utils/local-endpoint';
import { ServiceDeployment } from '../utils/service-deployment';
import { DbMigrations } from './db-migrations';
Expand Down Expand Up @@ -39,7 +40,7 @@ export function deployStripeBilling({
{
image,
imagePullSecret,
replicas: 1,
replicas: isProduction(deploymentEnv) ? 2 : 1,
readinessProbe: '/_readiness',
livenessProbe: '/_health',
env: {
Expand Down
3 changes: 2 additions & 1 deletion deployment/services/emails.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as k8s from '@pulumi/kubernetes';
import * as pulumi from '@pulumi/pulumi';
import { DeploymentEnvironment } from '../types';
import { isProduction } from '../utils/helpers';
import { serviceLocalEndpoint } from '../utils/local-endpoint';
import { ServiceDeployment } from '../utils/service-deployment';
import { Redis } from './redis';
Expand Down Expand Up @@ -53,7 +54,7 @@ export function deployEmails({
livenessProbe: '/_health',
exposesMetrics: true,
image,
replicas: 1,
replicas: isProduction(deploymentEnv) ? 2 : 1,
},
[redis.deployment, redis.service],
).deploy();
Expand Down
3 changes: 2 additions & 1 deletion deployment/services/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,11 @@ export function deployGraphQL({
{
imagePullSecret,
image,
replicas: isProduction(deploymentEnv) ? 2 : 1,
replicas: isProduction(deploymentEnv) ? 3 : 1,
pdb: true,
readinessProbe: '/_readiness',
livenessProbe: '/_health',
availabilityOnEveryNode: true,
env: {
...apiEnv,
...deploymentEnv,
Expand Down
3 changes: 2 additions & 1 deletion deployment/services/policy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as k8s from '@pulumi/kubernetes';
import * as pulumi from '@pulumi/pulumi';
import { DeploymentEnvironment } from '../types';
import { isProduction } from '../utils/helpers';
import { ServiceDeployment } from '../utils/service-deployment';

const commonConfig = new pulumi.Config('common');
Expand Down Expand Up @@ -31,7 +32,7 @@ export function deploySchemaPolicy({
readinessProbe: '/_readiness',
livenessProbe: '/_health',
exposesMetrics: true,
replicas: 1,
replicas: isProduction(deploymentEnv) ? 2 : 1,
pdb: true,
}).deploy();
}
2 changes: 1 addition & 1 deletion deployment/services/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function deployProxy({
address: commonConfig.get('staticIp'),
aksReservedIpResourceGroup: commonConfig.get('aksReservedIpResourceGroup'),
})
.deployProxy({ replicas: isProduction(deploymentEnv) ? 2 : 1 })
.deployProxy({ replicas: isProduction(deploymentEnv) ? 3 : 1 })
.registerService({ record: appHostname }, [
{
name: 'app',
Expand Down
3 changes: 2 additions & 1 deletion deployment/services/rate-limit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { parse } from 'pg-connection-string';
import * as k8s from '@pulumi/kubernetes';
import * as pulumi from '@pulumi/pulumi';
import { DeploymentEnvironment } from '../types';
import { isProduction } from '../utils/helpers';
import { serviceLocalEndpoint } from '../utils/local-endpoint';
import { ServiceDeployment } from '../utils/service-deployment';
import { DbMigrations } from './db-migrations';
Expand Down Expand Up @@ -41,7 +42,7 @@ export function deployRateLimit({
'rate-limiter',
{
imagePullSecret,
replicas: 1,
replicas: isProduction(deploymentEnv) ? 2 : 1,
readinessProbe: '/_readiness',
livenessProbe: '/_health',
env: {
Expand Down
1 change: 1 addition & 0 deletions deployment/services/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export function deploySchema({
{
image,
imagePullSecret,
availabilityOnEveryNode: true,
env: {
...deploymentEnv,
...commonEnv,
Expand Down
5 changes: 4 additions & 1 deletion deployment/services/supertokens.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import * as kx from '@pulumi/kubernetesx';
import * as pulumi from '@pulumi/pulumi';
import { Output } from '@pulumi/pulumi';
import { DeploymentEnvironment } from '../types';
import { isProduction } from '../utils/helpers';
import { serviceLocalEndpoint } from '../utils/local-endpoint';

export function deploySuperTokens(
{ apiKey }: { apiKey: Output<string> },
resourceOptions: {
dependencies: pulumi.Resource[];
},
deploymentEnv: DeploymentEnvironment,
) {
const apiConfig = new pulumi.Config('api');

Expand Down Expand Up @@ -55,7 +58,7 @@ export function deploySuperTokens(
const deployment = new kx.Deployment(
'supertokens',
{
spec: pb.asDeploymentSpec({ replicas: 1 }), // <-- here,
spec: pb.asDeploymentSpec({ replicas: isProduction(deploymentEnv) ? 3 : 1 }),
},
{
dependsOn: resourceOptions.dependencies,
Expand Down
3 changes: 2 additions & 1 deletion deployment/services/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ export function deployTokens({
readinessProbe: '/_readiness',
livenessProbe: '/_health',
exposesMetrics: true,
replicas: isProduction(deploymentEnv) ? 2 : 1,
availabilityOnEveryNode: true,
replicas: isProduction(deploymentEnv) ? 3 : 1,
image,
env: {
...deploymentEnv,
Expand Down
3 changes: 2 additions & 1 deletion deployment/services/usage-estimation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as k8s from '@pulumi/kubernetes';
import * as pulumi from '@pulumi/pulumi';
import { DeploymentEnvironment } from '../types';
import { isProduction } from '../utils/helpers';
import { ServiceDeployment } from '../utils/service-deployment';
import { Clickhouse } from './clickhouse';
import { DbMigrations } from './db-migrations';
Expand Down Expand Up @@ -30,7 +31,7 @@ export function deployUsageEstimation({
{
image,
imagePullSecret,
replicas: 1,
replicas: isProduction(deploymentEnv) ? 2 : 1,
readinessProbe: '/_readiness',
livenessProbe: '/_health',
env: {
Expand Down
1 change: 1 addition & 0 deletions deployment/services/usage-ingestor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export function deployUsageIngestor({
replicas,
readinessProbe: '/_readiness',
livenessProbe: '/_health',
availabilityOnEveryNode: true,
env: {
...deploymentEnv,
...commonEnv,
Expand Down
1 change: 1 addition & 0 deletions deployment/services/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export function deployUsage({
replicas,
readinessProbe: '/_readiness',
livenessProbe: '/_health',
availabilityOnEveryNode: true,
env: {
...deploymentEnv,
...commonEnv,
Expand Down
3 changes: 2 additions & 1 deletion deployment/services/webhooks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as k8s from '@pulumi/kubernetes';
import * as pulumi from '@pulumi/pulumi';
import { DeploymentEnvironment } from '../types';
import { isProduction } from '../utils/helpers';
import { ServiceDeployment } from '../utils/service-deployment';
import type { Broker } from './cf-broker';
import { Redis } from './redis';
Expand Down Expand Up @@ -47,7 +48,7 @@ export function deployWebhooks({
readinessProbe: '/_readiness',
livenessProbe: '/_health',
exposesMetrics: true,
replicas: 1,
replicas: isProduction(deploymentEnv) ? 2 : 1,
image,
},
[redis.deployment, redis.service],
Expand Down
24 changes: 24 additions & 0 deletions deployment/utils/service-deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class ServiceDeployment {
cpuAverageToScale: number;
};
};
availabilityOnEveryNode?: boolean;
command?: string[];
},
protected dependencies?: Array<pulumi.Resource | undefined | null>,
Expand Down Expand Up @@ -93,13 +94,31 @@ export class ServiceDeployment {
);
}

const topologySpreadConstraints: k8s.types.input.core.v1.TopologySpreadConstraint[] = [];

if (this.options.availabilityOnEveryNode) {
// This will ensure that services that has >1 replicas will be scheduled on every available node
// and ensure that we are not exposed to downtime issues caused by node failures/restarts:
topologySpreadConstraints.push({
maxSkew: 1,
topologyKey: 'topology.kubernetes.io/zone',
whenUnsatisfiable: 'DoNotSchedule',
labelSelector: {
matchLabels: {
app: this.name,
},
},
});
}

const pb = new PodBuilder({
restartPolicy: asJob ? 'Never' : 'Always',
imagePullSecrets: this.options.imagePullSecret
? [{ name: this.options.imagePullSecret.metadata.name }]
: undefined,
terminationGracePeriodSeconds: 60,
volumes: this.options.volumes,
topologySpreadConstraints,
containers: [
{
livenessProbe,
Expand Down Expand Up @@ -150,6 +169,10 @@ export class ServiceDeployment {
annotations: {},
};

metadata.labels = {
app: this.name,
};

if (this.options.exposesMetrics) {
metadata.annotations = {
'prometheus.io/port': '10254',
Expand All @@ -174,6 +197,7 @@ export class ServiceDeployment {
},
{
annotations: metadata.annotations,
labels: metadata.labels,
},
),
},
Expand Down
Loading