View Source FLAMEK8sBackend (flame_k8s_backend v0.2.1)

Kubernetes Backend implementation.

Usage

Configure the flame backend in our configuration.

# config.exs
if config_env() == :prod do
  config :flame, :backend, FLAMEK8sBackend
  config :flame, FLAMEK8sBackend, log: :debug
end

Options

The following backend options are supported:

  • container_name - If your application pod runs multiple containers (initContainers excluded), use this option to pass the name of the container running this application. If not given, the first container in the list of containers is used to lookup env vars and resources to be used for the runner pods.

  • :log - The log level to use for verbose logging. Defaults to false.

  • :token_path - Path to the service account token. Defaults to "/var/run/secrets/kubernetes.io/serviceaccount"

Prerequisites

In order for this to work, your application needs to meet some requirements.

Env Variables

In order for the backend to be able to get informations from your pod and use them for the runner pods (e.g. env variables), you have to define POD_NAME and POD_NAMESPACE environment variables on your pod.

apiVersion: apps/v1
kind: Deployment
spec:
selector:
  matchLabels:
    app: myapp
template:
  spec:
    containers:
      - env:
          - name: POD_NAME
            valueFrom:
              fieldRef:
                apiVersion: v1
                fieldPath: metadata.name
          - name: POD_NAMESPACE
            valueFrom:
              fieldRef:
                apiVersion: v1
                fieldPath: metadata.namespace

RBAC

Your application needs run as a service account with permissions to manage pods. This is a simple

---
apiVersion: v1
kind: ServiceAccount
metadata:
name: myapp
namespace: app-namespace
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: app-namespace
name: pod-mgr
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["create", "get", "list", "delete", "patch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: myapp-pod-mgr
namespace: app-namespace
subjects:
- kind: ServiceAccount
  name: myapp
  namespace: app-namespace
roleRef:
kind: Role
name: pod-mgr
apiGroup: rbac.authorization.k8s.io
---
apiVersion: apps/v1
kind: Deployment
spec:
template:
  spec:
    serviceAccountName: my-app

Clustering

Your application needs to be able to form a cluster with your runners. Define POD_IP, RELEASE_DISTRIBUTION and RELEASE_NODE environment variables on your pods as follows:

apiVersion: apps/v1
kind: Deployment
spec:
template:
  spec:
    containers:
      - env:
          - name: POD_IP
            valueFrom:
              fieldRef:
                apiVersion: v1
                fieldPath: status.podIP
          - name: RELEASE_DISTRIBUTION
            value: name
          - name: RELEASE_NODE
            value: my_app@$(POD_IP)