Skip to content

Container

Define containers that run in a pod using the Container class.

Environment variables

Environment variables can be added to containers using various sources, via semantically explicit API’s:

import * as kplus from 'cdk8s-plus-17'

const pod = new kplus.Pod(this, 'Pod');
const container = pod.addContainer({
  image: 'my-app'
});

// explicitly use a value.
container.addEnv('endpoint', kplus.EnvValue.fromValue('value'));

// use a specific key from a config map.
const backendsConfig = kplus.ConfigMap.fromConfigMapName('backends');
container.addEnv('endpoint', kplus.EnvValue.fromConfigMap(backendsConfig, 'endpoint'));

// use a specific key from a secret.
const credentials = kplus.Secret.fromSecretName('credentials');
container.addEnv('password', kplus.EnvValue.fromSecret(credentials, 'password'));

Volume Mounts

A very common capability is to mount a volume with some data onto a container. Using pure kubernetes API, this would require writing something like:

kind: Pod
apiVersion: v1
spec:
  containers:
    - name: main
      volumeMounts:
        - mountPath: /path/to/mount
          name: 'config-volume'
  volumes:
    - name: 'config-volume'
      configMap:
        name: 'config'

Notice the apparent redundancy of having to specify the volume name twice. Also, if you happen to need the same mount in other pods, you would need to duplicate this configuration. This can get complex and cluttered very fast.

In contrast, here is how to do this with cdk8s+:

import * as kplus from 'cdk8s-plus-17';

const config = kplus.ConfigMap.fromConfigMapName('config');
const volume = kplus.Volume.fromConfigMap(config);

const pod = new kplus.Pod(this, 'Pod');
const container = pod.addContainer({
  image: 'my-app'
})

// Cool alert: every pod that will later be configured with this container,
// will automatically have access to this volume, so you don't need to explicitly add it to the pod spec!.
container.mount('/path/to/mount', volume);

Probes

A Probe is a diagnostic performed periodically by the kubelet on a Container. To perform a diagnostic, the kubelet calls a Handler implemented by the container.

A Probe instance can be created through one of the fromXxx static methods:

  • Probe.fromHttpGet()
  • Probe.fromCommand()

Readiness, liveness, and startup probes can be configured at the container-level through the readiness, liveness, and startup options:

new kplus.Pod(this, 'Pod', {
  containers: [
    {
      // ...
      readiness: kplus.Probe.fromHttpGet('/ping'),
    }
  ]
});

See the API reference for details.