Skip to main content

Overview

The Deployment construct creates a Kubernetes Deployment with rolling update strategy, volume mounts, and service account support.

Class Definition

export class Deployment extends Construct {
  constructor(scope: Construct, appname: string, props: DeploymentProps)
}

Constructor Parameters

scope
Construct
required
Parent construct
appname
string
required
Application name for labels and metadata
props
DeploymentProps
required
Deployment configuration properties

DeploymentProps

Extends ContainerProps with deployment-specific options.

Container Properties

image
string
required
Docker image name (without tag)
tag
string
default:"GIT_SHA env var"
Docker image tag. Defaults to GIT_SHA environment variable.
port
number
default:"80"
External port for the service
containerPort
number
default:"port"
Internal container port. Defaults to the value of port.
secret
string
Name of Kubernetes secret to mount as environment variables
cmd
string[]
Container command (entrypoint)
cmd: ['gunicorn', 'app.wsgi:application', '--bind', '0.0.0.0:8000']
env
Array<{name: string, value: string}>
Additional environment variables
env: [
  { name: 'LOG_LEVEL', value: 'INFO' },
  { name: 'ENVIRONMENT', value: 'production' }
]
Note: GIT_SHA is automatically added to all containers.
pullPolicy
'IfNotPresent' | 'Always' | 'Never'
default:"IfNotPresent"
Container image pull policy

Deployment-Specific Properties

replicas
number
default:"1"
Number of pod replicas to run
secretMounts
VolumeMount[]
Secret volume mounts for the container
secretMounts: [
  {
    name: 'app-secrets',
    mountPath: '/etc/secrets',
    subPath: 'config.json'
  }
]
volumeMounts
VolumeMount[]
Additional volume mounts (appends to secretMounts)
volumeMounts: [
  {
    name: 'config-volume',
    mountPath: '/etc/config'
  }
]
volumes
Volume[]
Volume definitions for the pod
volumes: [
  {
    name: 'config-volume',
    configMap: {
      name: 'app-config'
    }
  }
]
serviceAccount
KubeServiceAccount
Service account to attach to deployment pods. If not provided, uses default service account.

Deployment Strategy

All deployments use a RollingUpdate strategy with:
  • maxSurge: 3 (can create up to 3 extra pods during update)
  • maxUnavailable: 0 (always maintain full availability)
This ensures zero-downtime deployments.

Labels

Each deployment automatically applies the label:
app.kubernetes.io/name: <appname>
This label is used for pod selection and service matching.

Usage Examples

Basic Deployment

import { Deployment } from './deployment';

new Deployment(chart, 'api', {
  image: 'pennlabs/api',
  replicas: 3,
  port: 8000
});

With Secrets

new Deployment(chart, 'backend', {
  image: 'pennlabs/backend',
  replicas: 2,
  secret: 'backend-secrets',  // Mounted as environment variables
  secretMounts: [             // Mounted as files
    {
      name: 'db-credentials',
      mountPath: '/etc/secrets',
      subPath: 'credentials.json'
    }
  ]
});

With Custom Command

new Deployment(chart, 'worker', {
  image: 'pennlabs/app',
  cmd: ['celery', '-A', 'app', 'worker', '--loglevel=info'],
  replicas: 4,
  secret: 'app-secrets'
});

With Service Account

import { KubeServiceAccount } from './imports/k8s';

const serviceAccount = new KubeServiceAccount(chart, 'my-sa', {
  metadata: { name: 'my-app-sa' }
});

new Deployment(chart, 'api', {
  image: 'pennlabs/api',
  replicas: 2,
  serviceAccount: serviceAccount
});

With ConfigMap Volumes

new Deployment(chart, 'app', {
  image: 'pennlabs/app',
  replicas: 2,
  volumeMounts: [
    {
      name: 'config',
      mountPath: '/etc/app/config'
    }
  ],
  volumes: [
    {
      name: 'config',
      configMap: {
        name: 'app-config'
      }
    }
  ]
});

High Availability Setup

new Deployment(chart, 'api', {
  image: 'pennlabs/api',
  tag: 'v2.1.0',
  replicas: 5,
  port: 8000,
  env: [
    { name: 'LOG_LEVEL', value: 'INFO' },
    { name: 'WORKERS', value: '4' }
  ],
  pullPolicy: 'Always'
});

Container Naming

All containers are named worker by default (see src/container.ts:162).

Environment Variables

Required environment variable:
GIT_SHA
string
required
Git commit SHA. Automatically injected into all containers and used as default image tag.

Source Reference

Defined in src/deployment.ts:48

Build docs developers (and LLMs) love