Skip to content

Container

Gregory Nickonov edited this page Mar 13, 2019 · 19 revisions

Quick Start

R.deployment :backend do
  container do
    image 'tutum/hello-world'
  end
end

Sets up main container of deployment with tutum/hello-world image.

Container

Kubernetes Documentation

Property Kubernetes property Type
args args Array of String
command command Array of String
env env Array of EnvVar
env_from envFrom Array of EnvFromSource
image image String
image_pull_policy imagePullPolicy String
lifecycle lifecycle Lifecycle
liveness_probe livenessProbe Probe
name name String
ports ports Array of ContainerPort
readiness_probe readinessProbe Probe
resources resources ResourceRequirements
security_context securityContext SecurityContext
stdin stdin Boolean
stdin_once stdinOnce Boolean
termination_message_path terminationMessagePath String
termination_message_policy terminationMessagePolicy String
tty tty Boolean
volume_devices volumeDevices Array of VolumeDevice
volume_mounts volumeMounts Array of VolumeMount
working_dir workingDir String

args

Passing list of values adds them to the args array:

R.deployment :backend do
  container do
    args '-v', '1', '-l', 42
  end
end

command

Passing list of values adds them to the command array:

R.deployment :backend do
  container do
    command '/bin/sh', '-c', 'ls -al'
  end
end

environment

A complex helper to manipulate container's environment-oriented properties env and env_from. Available methods are described in ContainerEnvironmentHelper.

add

Kubernetes Documentation

Key-value pairs passed to add are converted into name and value properties of EnvVar objects and added to env array:

R.deployment :backend do
  container do
    environment do
      add DB_NAME: 'backend', DB_USER: 'postgres'
    end
  end
end

add_config_map_key

Kubernetes Documentation

Adds a specified key from the specified ConfigMap as an environment variable with the specified name to the env array:

R.scope :backend do
  R.config_map do
    set DB_NAME: 'database'
  end

  R.deployment do
    container do
      environment do
        # Adds a value of the key 'DATABASE' from the ConfigMap 'db' as an environment
        # variable 'DB_NAME' without failing if it is doesn't exists
        add_config_map_key :DB_NAME, :db, key: :DATABASE, optional: true

        # Adds a value of the key 'DB_NAME' from the ConfigMap 'backend' as an environment
        # variable 'DB_NAME' failing if it is doesn't exists
        add_config_map_key :DB_NAME, :backend, key: :DB_NAME

        # The same as above, but the ConfigMap's key name is defaulted to the environment
        # variable name (which is 'DB_NAME')
        add_config_map_key :DB_NAME, :backend

        # The same as above, but the ConfigMap's key name is defaulted to the environment
        # variable name (which is 'DB_NAME') and the name of the ConfigMap itself is defaulted
        # to the name of the current scope (which is 'backend')
        add_config_map_key :DB_NAME
      end
    end
  end
end

add_secret_key

Kubernetes Documentation

Adds a specified key from the specified Secret as an environment variable with the specified name to the env array:

R.scope :backend do
  R.secret do
    set DB_PASSWORD: 'supersecret'
  end

  R.deployment do
    container do
      environment do
        # Adds a value of the key 'PASSWORD' from the Secret 'db' as an environment
        # variable 'DB_PASSWORD' without failing if it is doesn't exists
        add_secret_key :DB_PASSWORD, :db, key: :PASSWORD, optional: true

        # Adds a value of the key 'DB_PASSWORD' from the Secret 'backend' as an environment
        # variable 'DB_PASSWORD' failing if it is doesn't exists
        add_secret_key :DB_PASSWORD, :backend, key: :DB_PASSWORD

        # The same as above, but the ConfigMap's key name is defaulted to the environment
        # variable name (which is 'DB_PASSWORD')
        add_secret_key :DB_PASSWORD, :backend

        # The same as above, but the ConfigMap's key name is defaulted to the environment
        # variable name (which is 'DB_PASSWORD') and the name of the ConfigMap itself is defaulted
        # to the name of the current scope (which is 'backend')
        add_secret_key :DB_PASSWORD
      end
    end
  end
end

add_field

Kubernetes Documentation

Adds an environment variable with the reference to the property of the current object to the env array:

R.deployment :backend do
  container do
    environment do
      add_field :KUBERNETES_POD_NAME, 'metadata.name'
    end
  end
end

There is an optional last argument, api_version which can be used to alter the version of the schema the field path is written in terms of.

add_resource_field

Kubernetes Documentation

Adds an environment variable with the reference to the current (or specified) container's resource field to the env array:

R.deployment :backend do
  container do
    environment do
      # Reference resource field from the same container
      add_resource_field :KUBERNETES_MEMORY_LIMITS, 'limits.memory'
    end
  end

  container :sidecar do
    environment do
      # Reference resource field from the main container with the divisor 1m
      add_resource_field :KUBERNETES_CPU_REQUESTS, 'requests.cpu',
                         container_name: :main, divisor: '1m'
    end
  end
end

use_config_map

Kubernetes Documentation

Adds a reference to the specified ConfigMap to the env_from array:

R.scope :backend do
  R.config_map do
    set DEBUG: 'true'
  end

  R.deployment do
    container do
      environment do
        # Reference 'backend' ConfigMap
        use_config_map :backend

        # Reference ConfigMap with the same name as the current scope
        use_config_map

        # Reference 'diagnostics' ConfigMap, prefix all keys with 'DIAGNOSTICS_' and
        # do not fail, if that ConfigMap is doesn't exists
        use_config_map :diagnostics, optional: true, prefix: 'DIAGNOSTICS_'
      end
    end
  end
end

use_secret

Kubernetes Documentation

Adds a reference to the specified Secret to the env_from array:

R.scope :backend do
  R.secret do
    set ROOT_PASSWORD: 'supersecret'
  end

  R.deployment do
    container do
      environment do
        # Reference 'backend' Secret
        use_secret :backend

        # Reference Secret with the same name as the current scope
        use_secret

        # Reference 's3' Secret, prefix all keys with 'S3_' and
        # do not fail, if that Secret is doesn't exists
        use_secret :s3, optional: true, prefix: 'S3_'
      end
    end
  end
end

expose_port

Adds a port information to the ports array:

R.deployment :backend do
  container do
    expose_port 80, host_ip: '0.0.0.0', host_port: 8080, name: :http, protocol: :TCP
  end
end

expose_default_http_port

A shortcut for exposing container's port 80 with the name 'http:

R.deployment :backend do
  container do
    expose_default_http_port
  end
end

image

With arguments passed sets image and optionally sets image pull policy for the container:

R.deployment :backend do
  container do
    # Specify image 
    image 'myregistry.com/backend:latest'

    # Specify image and image pull policy
    image 'myregistry.com/backend:latest', :IfNotExists
  end
end

mount_device

Adds a VolumeDevice with the specified parameters to the volume_devices array:

R.deployment :backend do
  container do
    mount_device :data, '/data'
  end
end

mount_volume

Adds a VolumeMount with the specified parameters to the volume_mounts array:

R.deployment :backend do
  container do
    # Mount volume 'appsettings' at path '/etc/config'
    mount_device :data, '/data'

    # Mount volume 'appsettings' at path '/etc/config' marking it as readonly
    mount_volume :appsettings, '/etc/config', readonly: true

    # Mount volume 'appsettings' at path '/etc/config' marking it as readonly
    mount_volume :appsettings, '/etc/config', readonly: true

    # Mount volume 'appsettings' at path '/etc/config' marking it as readonly,
    # specifying mount propagation mode and root inside the volume
    mount_volume :appsettings, '/etc/config', readonly: true,
                 mount_propagation: :MountPropagationNone, sub_path: '/data'
  end
end

ContainerPort

Kubernetes Documentation

Property Kubernetes property Type
container_port containerPort Integer
host_ip hostIP String
host_port hostPort Integer
name name String
protocol protocol String

Lifecycle

Kubernetes Documentation

Property Kubernetes property Type
post_start postStart Handler
pre_stop preStop Handler

Handler

Kubernetes Documentation

Property Kubernetes property Type
exec exec ExecAction
http_get httpGet HTTPGetAction
tcp_socket tcpSocket TCPSocketAction

Handler also provides all helpers defined by an ActionsOwner.

VolumeDevice

Kubernetes Documentation

Property Kubernetes property Type
device_path devicePath String
name name String

VolumeMount

Kubernetes Documentation

Property Kubernetes property Type
mount_path mountPath String
mount_propagation mountPropagation String
name name String
readonly readOnly Boolean
sub_path subPath String

SecurityContext

Kubernetes Documentation

Property Kubernetes property Type
allow_privilege_escalation allowPrivilegeEscalation Boolean
capabilities capabilities Capabilities
privileged privileged Boolean
proc_mount procMount String
readonly_root_filesystem readOnlyRootFilesystem Boolean
run_as_group runAsGroup Integer
run_as_non_root runAsNonRoot Boolean
run_as_user runAsUser Integer
se_linux_options seLinuxOptions SELinuxOptions

Capabilities

Kubernetes Documentation

Property Kubernetes property Type
add add Array of String
drop drop Array of String

add

When list of values is passed add adds them to the add array:

container do
  security_context.capabilities.add 'A', 'B', 'C'
end

drop

When list of values is passed drop adds them to the drop array:

container do
  security_context.capabilities.drop 'A', 'B', 'C'
end

Clone this wiki locally