Terraform: Containers

Terraform and Docker

Managing Docker networks

  • environment: mkdir -p ~/terraform/docker/networks
  • example
    1. Crate new files required
      1
      touch {variables.tf,image.tf,network.tf,main.tf}
    2. Edit variables.tf
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      variable "mysql_root_password" {
      description = "The MySQL root password."
      default = "P4sSw0rd0!"
      }

      variable "ghost_db_username" {
      description = "Ghost blog database username."
      default = "root"
      }

      variable "ghost_db_name" {
      description = "Ghost blog database name."
      default = "ghost"
      }

      variable "mysql_network_alias" {
      description = "The network alias for MySQL."
      default = "db"
      }

      variable "ghost_network_alias" {
      description = "The network alias for Ghost"
      default = "ghost"
      }

      variable "ext_port" {
      description = "Public port for Ghost"
      default = "8080"
      }
    3. Edit image.tf
      1
      2
      3
      4
      5
      6
      7
      resource "docker_image" "ghost_image" {
      name = "ghost:alpine"
      }

      resource "docker_image" "mysql_image" {
      name = "mysql:5.7"
      }
    4. Edit network.tf
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      resource "docker_network" "public_bridge_network" {
      name = "public_ghost_network"
      driver = "bridge"


      resource "docker_network" "private_bridge_network" {
      name = "ghost_mysql_internal"
      driver = "bridge"
      internal = true
      }
    5. Edit main.tf
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      resource "docker_container" "mysql_container" {
      name = "ghost_database"
      image = "${docker_image.mysql_image.name}"
      env = [
      "MYSQL_ROOT_PASSWORD=${var.mysql_root_password}"
      ]
      networks_advanced {
      name = "${docker_network.private_bridge_network.name}"
      aliases = ["${var.mysql_network_alias}"]
      }
      }

      # do not deploy ghost until you have the db ready
      resource "null_resource" "sleep" {
      depends_on = ["docker_container.mysql_container"]
      provisioner "local-exec" {
      command = "sleep 15s"
      }
      }

      resource "docker_container" "blog_container" {
      name = "ghost_blog"
      image = "${docker_image.ghost_image.name}"
      depends_on = ["null_resource.sleep", "docker_container.mysql_container"]
      env = [
      "database__client=mysql",
      "database__connection__host=${var.mysql_network_alias}",
      "database__connection__user=${var.ghost_db_username}",
      "database__connection__password=${var.mysql_root_password}",
      "database__connection__database=${var.ghost_db_name}"
      ]
      ports {
      internal = "2368"
      external = "${var.ext_port}"
      }
      networks_advanced {
      name = "${docker_network.public_bridge_network.name}"
      aliases = ["${var.ghost_network_alias}"]
      }
      networks_advanced {
      name = "${docker_network.private_bridge_network.name}"
      aliases = ["${var.ghost_network_alias}"]
      }
      }
    6. Work with Terraform
      1
      2
      3
      4
      5
      6
      7
      terraform init
      terraform validate
      terraform plan -out=tfplan -var 'ext_port=8082'
      terraform apply tfplan

      # clean up
      terraform destroy -auto-approve -var 'ext_port=8082'

Managing Docker volumes

  • environment
    1
    2
    cp -r ~/terraform/docker/networks ~/terraform/docker/volumes
    cd ../volumes/
  • example
    1. Edit volumes.tf
      1
      2
      3
      resource "docker_volume" "mysql_data_volume" {
      name = "mysql_data"
      }
    2. Edit main.tf
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      resource "docker_container" "mysql_container" {
      name = "ghost_database"
      image = "${docker_image.mysql_image.name}"
      env = [
      "MYSQL_ROOT_PASSWORD=${var.mysql_root_password}"
      ]
      volumes {
      volume_name = "${docker_volume.mysql_data_volume.name}"
      container_path = "/var/lib/mysql"
      }
      networks_advanced {
      name = "${docker_network.private_bridge_network.name}"
      aliases = ["${var.mysql_network_alias}"]
      }
      }

      resource "null_resource" "sleep" {
      depends_on = ["docker_container.mysql_container"]
      provisioner "local-exec" {
      command = "sleep 15s"
      }
      }

      resource "docker_container" "blog_container" {
      name = "ghost_blog"
      image = "${docker_image.ghost_image.name}"
      depends_on = ["null_resource.sleep", "docker_container.mysql_container"]
      env = [
      "database__client=mysql",
      "database__connection__host=${var.mysql_network_alias}",
      "database__connection__user=${var.ghost_db_username}",
      "database__connection__password=${var.mysql_root_password}",
      "database__connection__database=${var.ghost_db_name}"
      ]
      ports {
      internal = "2368"
      external = "${var.ext_port}"
      }
      networks_advanced {
      name = "${docker_network.public_bridge_network.name}"
      aliases = ["${var.ghost_network_alias}"]
      }
      networks_advanced {
      name = "${docker_network.private_bridge_network.name}"
      aliases = ["${var.ghost_network_alias}"]
      }
      }
    3. Work with Terraform
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      terraform init
      terraform validate
      terraform plan -out=tfplan -var 'ext_port=8082'
      terraform apply tfplan
      # list Docker volumes
      docker volume inspect mysql_data
      # list the data in mysql_data
      sudo ls /var/lib/docker/volumes/mysql_data/_data

      # clean up
      terraform destroy -auto-approve -var 'ext_port=8082'

Creating swarm services

  • environment
    1
    2
    cp -r volumes/ services
    cd services
  • example
    1. Edit variables.tf
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      variable "mysql_root_password" {
      description = "The MySQL root password."
      default = "P4sSw0rd0!"
      }

      variable "ghost_db_username" {
      description = "Ghost blog database username."
      default = "root"
      }

      variable "ghost_db_name" {
      description = "Ghost blog database name."
      default = "ghost"
      }

      variable "mysql_network_alias" {
      description = "The network alias for MySQL."
      default = "db"
      }

      variable "ghost_network_alias" {
      description = "The network alias for Ghost"
      default = "ghost"
      }

      variable "ext_port" {
      description = "The public port for Ghost"
      }
    2. Edit images.tf
      1
      2
      3
      4
      5
      6
      7
      resource "docker_image" "ghost_image" {
      name = "ghost:alpine"
      }

      resource "docker_image" "mysql_image" {
      name = "mysql:5.7"
      }
    3. Edit network.tf
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      resource "docker_network" "public_bridge_network" {
      name = "public_network"
      driver = "overlay"
      }

      resource "docker_network" "private_bridge_network" {
      name = "mysql_internal"
      driver = "overlay"
      internal = true
      }
    4. Edit volumes.tf
      1
      2
      3
      resource "docker_volume" "mysql_data_volume" {
      name = "mysql_data"
      }
    5. Edit main.tf
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      # you do not face issues for having the blog defined before db
      # as a swarm, they are on different machines
      resource "docker_service" "ghost-service" {
      name = "ghost"

      task_spec {
      container_spec {
      image = "${docker_image.ghost_image.name}"
      env {
      database__client = "mysql"
      database__connection__host = "${var.mysql_network_alias}"
      database__connection__user = "${var.ghost_db_username}"
      database__connection__password = "${var.mysql_root_password}"
      database__connection__database = "${var.ghost_db_name}"
      }
      }
      networks = [
      "${docker_network.public_bridge_network.name}",
      "${docker_network.private_bridge_network.name}"
      ]
      }

      endpoint_spec {
      ports {
      target_port = "2368"
      published_port = "${var.ext_port}"
      }
      }
      }

      resource "docker_service" "mysql-service" {
      name = "${var.mysql_network_alias}"
      task_spec {
      container_spec {
      image = "${docker_image.mysql_image.name}"
      env {
      MYSQL_ROOT_PASSWORD = "${var.mysql_root_password}"
      }

      # volumes as mounts
      mounts = [
      {
      target = "/var/lib/mysql"
      source = "${docker_volume.mysql_data_volume.name}"
      type = "volume"
      }
      ]
      }
      networks = ["${docker_network.private_bridge_network.name}"]
      }
      }
    6. Work with Terraform
      1
      2
      3
      4
      5
      6
      7
      8
      9
          terraform init
      terraform validate
      terraform plan -out=tfplan -var 'ext_port=8082'
      terraform apply tfplan
      docker service ls
      docker container ls

      s # clean up
      terraform destroy -auto-approve -var 'ext_port=8082'

Using secrets

  • store sensitive data, encrypted with Base64
  • environment: secrets
  • example
    1. Encode the password with Base64

      1
      echo 'p4sSWoRd0!' | base64
    2. Edit variables.tf

      1
      2
      3
      4
      5
      6
      7
      variable "mysql_root_password" {
      default = "cDRzU1dvUmQwIQo="
      }

      variable "mysql_db_password" {
      default = "cDRzU1dvUmQwIQo="
      }
    3. Create ``image.tf

      1
      2
      3
      resource "docker_image" "mysql_image" {
      name = "mysql:5.7"
      }
    4. Edit secrets.tf

      1
      2
      3
      4
      5
      6
      7
      8
      9
      resource "docker_secret" "mysql_root_password" {
      name = "root_password"
      data = "${var.mysql_root_password}"
      }

      resource "docker_secret" "mysql_db_password" {
      name = "db_password"
      data = "${var.mysql_db_password}"
      }
    5. Edit networks.tf

      1
      2
      3
      4
      5
      resource "docker_network" "private_overlay_network" {
      name = "mysql_internal"
      driver = "overlay"
      internal = true
      }
    6. Edit volumes.tf

      1
      2
      3
      resource "docker_volume" "mysql_data_volume" {
      name = "mysql_data"
      }
    7. Edit main.tf

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      resource "docker_service" "mysql-service" {
      name = "mysql_db"

      task_spec {
      container_spec {
      image = "${docker_image.mysql_image.name}"

      secrets = [
      {
      secret_id = "${docker_secret.mysql_root_password.id}"
      secret_name = "${docker_secret.mysql_root_password.name}"
      file_name = "/run/secrets/${docker_secret.mysql_root_password.name}"
      },
      {
      secret_id = "${docker_secret.mysql_db_password.id}"
      secret_name = "${docker_secret.mysql_db_password.name}"
      file_name = "/run/secrets/${docker_secret.mysql_db_password.name}"
      }
      ]

      env {
      MYSQL_ROOT_PASSWORD_FILE = "/run/secrets/${docker_secret.mysql_root_password.name}"
      MYSQL_DATABASE = "mydb"
      MYSQL_PASSWORD_FILE = "/run/secrets/${docker_secret.mysql_db_password.name}"
      }

      mounts = [
      {
      target = "/var/lib/mysql"
      source = "${docker_volume.mysql_data_volume.name}"
      type = "volume"
      }
      ]
      }
      networks = [
      "${docker_network.private_overlay_network.name}"
      ]
      }
      }
    8. Work with Terraform

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      terraform init
      terraform validate
      terraform plan -out=tfplan
      terraform apply tfplan
      # find the MySQL container
      docker container ls
      # log into the MySQL container
      docker container exec -it [CONTAINER_ID] /bin/bash
      # access MySQL
      mysql -u root -p

      # clean up
      terraform destroy -auto-approve

Terraform and Kubernetes

Setting up Kubernetes master and installing Terraform

  1. Edit kube-config.yml
    1
    2
    3
    4
    5
    6
    7
    8
    apiVersion: kubeadm.k8s.io/v1beta1
    kind: ClusterConfiguration
    kubernetesVersion: "v1.13.5"
    networking:
    podSubnet: 10.244.0.0/16
    apiServer:
    extraArgs:
    service-node-port-range: 8000-31274
  2. Initialize Kubernetes
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    sudo kubeadm init --config kube-config.yml
    # copy admin.conf to your home directory
    mkdir -p $HOME/.kube
    sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    sudo chown $(id -u):$(id -g) $HOME/.kube/config
    # install Flannel
    sudo kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

    # untaint the Kubernetes Master
    kubectl taint nodes --all node-role.kubernetes.io/master-
  3. Install Terraform 0.11.13 on the Swarm manager
    1
    2
    3
    4
    sudo curl -O https://releases.hashicorp.com/terraform/0.11.13/terraform_0.11.13_linux_amd64.zip
    sudo unzip terraform_0.11.13_linux_amd64.zip -d /usr/local/bin/
    # check it
    terraform version

Creating a pod

  • environment: ~/terraform/pod
  • example
    1. Edit main.tf

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
        resource "kubernetes_pod" "ghost_alpine" {
      metadata {
      name = "ghost-alpine"
      }

      spec {
      host_network = "true"
      container {
      image = "ghost:alpine"
      name = "ghost-alpine"
      }
      }
      }
    2. Work with Terraform

      1
      2
      3
      4
      5
      6
      7
      8
      9
      terraform init
      terraform validate
      terraform plan
      terraform apply -auto-approve
      # check pods
      kubectl get pods

      # clean up
      terraform destroy -auto-approve

Creating a pod and service

  • environment ~/terraform/service
  • example
    1. Edit main.tf
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      resource "kubernetes_service" "ghost_service" {
      metadata {
      name = "ghost-service"
      }
      spec {
      selector {
      app = "${kubernetes_pod.ghost_alpine.metadata.0.labels.app}"
      }
      port {
      port = "2368"
      target_port = "2368"
      node_port = "8081"
      }
      type = "NodePort"
      }
      }

      resource "kubernetes_pod" "ghost_alpine" {
      metadata {
      name = "ghost-alpine"
      labels {
      app = "ghost-blog"
      }
      }

      spec {
      container {
      image = "ghost:alpine"
      name = "ghost-alpine"
      port {
      container_port = "2368"
      }
      }
      }
      }
    2. Initialize Terraform
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      terraform init
      terraform validate
      terraform plan
      terraform apply -auto-approve

      # check it
      kubectl get pods
      kubectl get services

      # clean up
      terraform destroy -auto-approve

Creating a deployment

  • environment: ~/terraform/deployment
  • example
    1. Edit main.tf
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      resource "kubernetes_service" "ghost_service" {
      metadata {
      name = "ghost-service"
      }
      spec {
      selector {
      app = "${kubernetes_deployment.ghost_deployment.spec.0.template.0.metadata.0.labels.app}"
      }
      port {
      port = "2368"
      target_port = "2368"
      node_port = "8080"
      }

      type = "NodePort"
      }
      }

      resource "kubernetes_deployment" "ghost_deployment" {
      metadata {
      name = "ghost-blog"
      }

      spec {
      replicas = "1"

      selector {
      match_labels {
      app = "ghost-blog"
      }
      }

      template {
      metadata {
      labels {
      app = "ghost-blog"
      }
      }

      spec {
      container {
      name = "ghost"
      image = "ghost:alpine"
      port {
      container_port = "2368"
      }
      }
      }
      }
      }
      }
    2. Working with Terraform
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      terraform init
      terraform validate
      terraform plan
      terraform apply -auto-approve

      # check it
      kubectl get deployments
      kubectl get pods
      kubectl delete pod [POD_ID]

      # clean up
      terraform destroy -auto-approve

Terraform: Introduction

Definition

  • Terraform = tool for building infrastructure
  • Allow simple control versopm
    • opensource
    • enterprise (collaboration, governance)
  • Features
    • Infrastructire as Code (IaC)
      • idempotent
      • high level syntax (Hashicorp)
      • easily resusable
    • Execution plans
      • intents of deploy
      • help ensure eveything in development is intentional
    • Resource graph
      • illustrates all cahnges and dependecies
  • Uses
    • Hybrid clouds
      • cloud agnostic
      • allows deployment to multiple providers simultaneously
    • Mult-tier architecture
      • allows deployment of several layers of architecture
      • usually able to automatically deploy in the correct order
    • Software-defined networking
      • able to deploy network architecture as well
  • Terraform is high-level infrastructire orchestration tool
    • Puppet, Chef, Ansible for configuration management
      • Providers that can tell this tools to perform CM duties

Setting up Docker Installing Terraform

  1. Prerequisires

    • Update the operating system and uninstall older versions
      1
      2
      3
      4
      5
      6
      7
      8
      9
      sudo yum update -y
      sudo yum remove -y docker \
      docker-client \
      docker-client-latest \
      docker-common \
      docker-latest \
      docker-latest-logrotate \
      docker-logrotate \
      docker-engine
  2. Install Docker CE

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    # install utils
    sudo yum install -y yum-utils \
    device-mapper-persistent-data \
    lvm2
    # add docker repo
    sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
    # install docker CE and enable it
    sudo yum -y install docker-ce
    sudo systemctl start docker && sudo systemctl enable docker
    # add user to docker group
    sudo usermod -aG docker cloud_user
    # test installation
    docker --version
  3. Configuring Swarm Manager node

    1
    docker swarm init --advertise-addr [PRIVATE_IP]
  4. Configure the Swarm Worker node

    1
    docker swarm join --token [TOKEN] [PRIVATE_IP]:2377
  5. Verify the Swarm cluster

    1
    docker node ls
  6. Install Terraform 0.11.13 on the Swarm manager

    1
    2
    3
    4
    5
    sudo curl -O https://releases.hashicorp.com/terraform/0.11.13/terraform_0.11.13_linux_amd64.zip
    sudo yum install -y unzip
    sudo unzip terraform_0.11.13_linux_amd64.zip -d /usr/local/bin/
    # test it
    terraform version

Deploying infrastructure with Terraform: basics

Terraform commands

  • Common commands

    Command Action
    apply Builds or changes infrastructure
    console Interactive console for Terraform interpolations
    destroy Destroys Terraform-managed infrastructure
    fmt Rewrites configuration files to canonical format
    get Downloads and installs modules for the configuration
    graph Creates a visual graph of Terraform resources
    import Imports existing infrastructure into Terraform
    init Initializes a new or existing Terraform configuration
    output Reads an output from a state file
    plan Generates and shows an execution plan
    providers Prints a tree of the providers used in the configuration
    push Uploads this Terraform module to Terraform Enterprise to run
    refresh Updates local state file against real resources
    show Inspects Terraform state or plan
    taint Manually marks a resource for recreation
    untaint Manually unmarks a resource as tainted
    validate Validates the Terraform files
    version Prints the Terraform version
    workspace Workspace management
  • Set up the environment

    1
    2
    mkdir -p terraform/basics
    cd terraform/basics
  • Create a Terraform script

    1
    nano main.tf
    • main.tf contents
      1
      2
      3
      4
      # Download the latest Ghost image
      resource "docker_image" "image_id" {
      name = "ghost:latest"
      }
  • Initialize Terraform

    1
    terraform init
  • Validate the Terraform file

    1
    terraform validate
  • List providers in the folder

    1
    ls .terraform/plugins/linux_amd64/
  • List providers used in the configuration

    1
    terraform providers
  • Terraform Plan

    1
    terraform plan
    • useful flags
      • -out=path: Writes a plan file to the given path. This can be used as input to the “apply” command.
      • -var 'foo=bar': Set a variable in the Terraform configuration. This flag can be set multiple times.
  • Terraform Apply

    1
    2
    # you should confirm
    terraform apply
    • useful flags
      • -auto-approve: This skips interactive approval of plan before applying
      • -var 'foo=bar': This sets a variable in the Terraform configuration. It can be set multiple times.
  • List the Docker images

    1
    docker image ls
  • Terraform Show

    1
    terraform show
  • Terraform Destroy

    1
    2
    # you should confirm
    terraform destroy
    • useful flags
      • -auto-approve: Skip interactive approval of plan before applying.
  • Re-list the Docker images

    1
    docker image ls
  • Using a plan

    1
    terraform plan -out=tfplan
  • Applying a plan:

    1
    terraform apply tfplan
  • Show the Docker Image resource:

    1
    terraform show

HashiCorp Configuration Language (HCL)

  • Terraform code files (.tf and .tf.json)

    1
    2
    3
    4
    5
    6
    resource "aws_instance" "example" {
    ami = "abc123"
    network_interface {
    # ...
    }
    }
  • Syntax reference

    Type Syntax
    Single line comments # this is a comment
    Multi-line comments /* This is a comment*/
    Assign values key = value
    Strings "This is a String"
    Strings interpolation ${var.foo}.
    Numbers 10 # assumed as base 10
    Boolean true, false
    Lists of primitive types ["foo", "bar", "baz"]
    Maps { "foo": "bar", "bar": "baz" }
  • Style Conventions

    • Indent two spaces for each nesting level
    • With multiple arguments, align their equals signs
    • Setup the environment
      1
      cd terraform/basics
  • Deploying a container using Terraform

    1. Redeploy the Ghost image
      1
      terraform apply
    2. Open main.tf:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      # Download the latest Ghost image
      resource "docker_image" "image_id" {
      name = "ghost:latest"
      }

      # Start the Container
      resource "docker_container" "container_id" {
      name = "ghost_blog"
      image = "${docker_image.image_id.latest}"
      ports {
      internal = "2368"
      external = "80"
      }
      }
    3. Validate main.tf, plan and apply
      1
      2
      3
      4
      5
      terraform validate
      terraform plan
      terraform apply
      #check
      docker container ls
    4. Check Ghost on browser (http:://[SWAM_MANAGER_IP])
  • Cleaning up the environment

    1
    terraform destroy

Tainting and updating resources

  • Terraform commands:

    • taint: Manually mark a resource for recreation
      1
      terraform taint [NAME]
    • untaint: Manually unmark a resource as tainted
      1
      terraform untaint [NAME]
  • Updating resources

    • edit main.tf
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      # Download the latest Ghost image
      resource "docker_image" "image_id" {
      name = "ghost:alpine"
      }

      # Start the Container
      resource "docker_container" "container_id" {
      name = "ghost_blog"
      image = "${docker_image.image_id.latest}"
      ports {
      internal = "2368"
      external = "80"
      }
      }
    • script
      1
      2
      3
      4
      5
      6
      7
      terraform validate
      terraform plan
      terraform apply
      # list containers
      docker container ls
      # check Ghost image
      docker image ls | grep [IMAGE]
  • Cleaning up the environment

    1
    2
    3
    4
    5
    6
    # reset
    terraform destroy
    # list the Docker images
    docker image ls
    # remove the Ghost blog image
    docker image rm ghost:latest

Terraform console and output

  • Terraform commands:

    • console: Interactive console for Terraform interpolations
  • Working with the Terraform console

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    # Redeploy image and container
    terraform apply
    terraform show
    terraform console
    # Type the following in the console to get the container's name
    docker_container.container_id.name
    # Type the following in the console to get the container's IP
    docker_container.container_id.ip_address
    # Break out of the Terraform console by using Ctrl+C
    terraform destroy
  • Output the name and IP of the container

    • Edit main.tf
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      # Download the latest Ghost Image
      resource "docker_image" "image_id" {
      name = "ghost:latest"
      }

      # Start the Container
      resource "docker_container" "container_id" {
      name = "blog"
      image = "${docker_image.image_id.latest}"
      ports {
      internal = "2368"
      external = "80"
      }
      }

      # Output the IP Address of the Container
      output "ip_address" {
      value = "${docker_container.container_id.ip_address}"
      description = "The IP for the container."
      }

      # Output the Name of the Container
      output "container_name" {
      value = "${docker_container.container_id.name}"
      description = "The name of the container."
      }
    • Script
      1
      2
      3
      4
      5
      terraform validate
      # apply changes to get the output
      terraform apply
      # clean up
      terraform destroy

Input variables

  • Definition

    • Parameters for a Terraform file
    • A variable block configures a single input variable for a Terraform module
    • Each block declares a single variable.
  • Syntax

    1
    2
    3
    variable [NAME] {
    [OPTION] = "[VALUE]"
    }
  • Arguments (between { })

    Optional parameters Value
    type defines the type of the variable (string, list or map)
    default default value. If not provided, Terraform will raise an error if a value is not provided by the caller
    description human-friendly description for the variable
  • Using variables during an apply

    1
    terraform apply -var 'foo=bar'
  • Example with main.tf contents

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    # Define variables
    variable "image_name" {
    description = "Image for container."
    default = "ghost:latest"
    }

    variable "container_name" {
    description = "Name of the container."
    default = "blog"
    }

    variable "int_port" {
    description = "Internal port for container."
    default = "2368"
    }

    variable "ext_port" {
    description = "External port for container."
    default = "80"
    }

    # Download the latest Ghost Image
    resource "docker_image" "image_id" {
    name = "${var.image_name}"
    }

    # Start the Container
    resource "docker_container" "container_id" {
    name = "${var.container_name}"
    image = "${docker_image.image_id.latest}"
    ports {
    internal = "${var.int_port}"
    external = "${var.ext_port}"
    }
    }

    # Output the IP Address of the Container
    output "ip_address" {
    value = "${docker_container.container_id.ip_address}"
    description = "The IP for the container."
    }

    output "container_name" {
    value = "${docker_container.container_id.name}"
    description = "The name of the container."
    }
    1
    2
    3
    4
    5
    6
    7
    8
    terraform validate
    terraform plan
    # apply using the variable
    terraform apply -var 'ext_port=8080'
    # change the container name:
    terraform apply -var 'container_name=ghost_blog' -var 'ext_port=8080'
    # reset
    terraform destroy -var 'ext_port=8080'

Breaking out our variables and outputs

  1. Edit variables.tf
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    #Define variables
    variable "container_name" {
    description = "Name of the container."
    default = "blog"
    }
    variable "image_name" {
    description = "Image for container."
    default = "ghost:latest"
    }
    variable "int_port" {
    description = "Internal port for container."
    default = "2368"
    }
    variable "ext_port" {
    description = "External port for container."
    default = "80"
    }
  2. Edit main.tf:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    # Download the latest Ghost Image
    resource "docker_image" "image_id" {
    name = "${var.image_name}"
    }

    # Start the Container
    resource "docker_container" "container_id" {
    name = "${var.container_name}"
    image = "${docker_image.image_id.latest}"
    ports {
    internal = "${var.int_port}"
    external = "${var.ext_port}"
    }
    }
  3. Edit outputs.tf:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    #Output the IP Address of the Container
    output "ip_address" {
    value = "${docker_container.container_id.ip_address}"
    description = "The IP for the container."
    }

    output "container_name" {
    value = "${docker_container.container_id.name}"
    description = "The name of the container."
    }
  4. Script
    1
    2
    3
    4
    5
    6
    terraform validate
    # plan and apply
    terraform plan -out=tfplan -var container_name=ghost_blog
    terraform apply tfplan
    # clean up
    terraform destroy -auto-approve -var container_name=ghost_blog

Maps and lookups

  • Map: specify different environment variables based on conditions
  • Example
    1. Edit variables.tf
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      #Define variables
      variable "env" {
      description = "env: dev or prod"
      }
      variable "image_name" {
      type = "map"
      description = "Image for container."
      default = {
      dev = "ghost:latest"
      prod = "ghost:alpine"
      }
      }
      variable "container_name" {
      type = "map"
      description = "Name of the container."
      default = {
      dev = "blog_dev"
      prod = "blog_prod"
      }
      }

      variable "int_port" {
      description = "Internal port for container."
      default = "2368"
      }
      variable "ext_port" {
      type = "map"
      description = "External port for container."
      default = {
      dev = "8081"
      prod = "80"
      }
      }
    2. Edit main.tf
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      # Download the latest Ghost Image
      resource "docker_image" "image_id" {
      name = "${lookup(var.image_name, var.env)}"
      }

      # Start the Container
      resource "docker_container" "container_id" {
      name = "${lookup(var.container_name, var.env)}"
      image = "${docker_image.image_id.latest}"
      ports {
      internal = "${var.int_port}"
      external = "${lookup(var.ext_port, var.env)}"
      }
      }
    3. Validate it and deploy
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      terraform validate
      terraform plan -out=tfdev_plan -var env=dev
      terraform apply tfdev_plan
      # plan the prod deploy
      terraform plan -out=tfprod_plan -var env=prod
      # use environment variables
      export TF_VAR_env=prod
      terraform console
      # execute a lookup
      lookup(var.ext_port, var.env)
      # exit the console:
      unset TF_VAR_env

Terraform Workspaces

  • Terraform commands

    Command Type Action
    workspace Command New, list, select and delete Terraform workspaces
    delete Subcommand delete a workspace
    list Subcommand list Workspaces
    new Subcommand create a new workspace
    select Subcommand select a workspace
    show Subcommand show the name of the current workspace
  • Example

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    cd terraform/basics
    # dev workspace
    terraform workspace new dev
    terraform plan -out=tfdev_plan -var env=dev
    terraform apply tfdev_plan

    # prod workspace
    terraform workspace new prod
    terraform plan -out=tfprod_plan -var env=prod
    terraform apply tfprod_plan

    # default workspace
    terraform workspace select default

    # find what workspace we are using now
    terraform workspace show
    # select a workspace
    terraform workspace select dev

    # clean up
    terraform destroy -var env=dev
    terraform workspace select prod
    terraform destroy -var env=prod

Null resources and Local-exec

  • null_resource resource implements the standard resource lifecycle but takes no further action
  • local-exec provisioner invokes a local executable after a resource is created
  1. Edit main.tf
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    # Download the latest Ghost Image
    resource "docker_image" "image_id" {
    name = "${lookup(var.image_name, var.env)}"
    }

    # Start the Container
    resource "docker_container" "container_id" {
    name = "${lookup(var.container_name, var.env)}"
    image = "${docker_image.image_id.latest}"
    ports {
    internal = "${var.int_port}"
    external = "${lookup(var.ext_port, var.env)}"
    }
    }

    resource "null_resource" "null_id" {
    provisioner "local-exec" {
    command = "echo ${docker_container.container_id.name}:${docker_container.container_id.ip_address} >> container.txt"
    }
    }
  2. Null Resource -> perform local commands on our machine without having to deploy extra resources
    1
    2
    3
    4
    5
    6
    7
    8
    9
    # reinitialize
    terraform init
    terraform validate
    terraform plan -out=tfplan -var env=dev
    terraform apply tfplan
    # view container info
    cat container.txt
    # clean up
    terraform destroy -auto-approve -var env=dev

Terraform modules

  • Module: container for multiple resource

    1
    2
    3
    4
    5
    6
    7
    8
    9
    # setup environment for root module
    mkdir -p modules/image
    mkdir -p modules/container
    # files for image
    cd ~/terraform/basics/modules/image
    touch main.tf variables.tf outputs.tf
    # files for container module
    cd ~/terraform/basics/modules/container
    touch main.tf variables.tf outputs.tf

Image module

  • environment: ``~/terraform/basics/modules/image`
  • example
    1. Edit main.tf
      1
      2
      3
      4
      # Download the Image
      resource "docker_image" "image_id" {
      name = "${var.image_name}"
      }
    2. Edit variables.tf
      1
      2
      3
      variable "image_name" {
      description = "Name of the image"
      }
    3. Edit outputs.tf
      1
      2
      3
      output "image_out" {
      value = "${docker_image.image_id.latest}"
      }
    4. Initialize Terraform:
      1
      2
      3
      4
      5
      terraform init
      terraform plan -out=tfplan -var 'image_name=ghost:alpine'
      terraform apply -auto-approve tfplan
      # clean up
      terraform destroy -auto-approve -var 'image_name=ghost:alpine'

Container module

  • environment: ~/terraform/basics/modules/container
  • example: breaking out the container code into it’s own module
    1. Edit main.tf
      1
      2
      3
      4
      5
      6
      7
      8
      9
      # Start the Container
      resource "docker_container" "container_id" {
      name = "${var.container_name}"
      image = "${var.image}"
      ports {
      internal = "${var.int_port}"
      external = "${var.ext_port}"
      }
      }
    2. Edit variables.tf
      1
      2
      3
      4
      5
      #Define variables
      variable "container_name" {}
      variable "image" {}
      variable "int_port" {}
      variable "ext_port" {}
    3. Edit outputs.tf
      1
      2
      3
      4
      5
      6
      7
      8
      #Output the IP Address of the Container
      output "ip" {
      value = "${docker_container.container_id.ip_address}"
      }

      output "container_name" {
      value = "${docker_container.container_id.name}"
      }
    4. Script
      1
      2
      3
      terraform init
      terraform plan -out=tfplan -var 'container_name=blog' -var 'image=ghost:alpine' -var 'int_port=2368' -var 'ext_port=80'
      terraform apply tfplan

Root module

  • environment: ~/terraform/basics/modules/
  • example: refactor the root module to use the image and container modules we created previously
    1. Edit main.tf
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      # Download the image
      module "image" {
      source = "./image"
      image_name = "${var.image_name}"
      }

      # Start the container
      module "container" {
      source = "./container"
      image = "${module.image.image_out}"
      container_name = "${var.container_name}"
      int_port = "${var.int_port}"
      ext_port = "${var.ext_port}"
      }
    2. Edit variables.tf
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      #Define variables
      variable "container_name" {
      description = "Name of the container."
      default = "blog"
      }
      variable "image_name" {
      description = "Image for container."
      default = "ghost:latest"
      }
      variable "int_port" {
      description = "Internal port for container."
      default = "2368"
      }
      variable "ext_port" {
      description = "External port for container."
      default = "80"
      }
    3. Edit outputs.tf
      1
      2
      3
      4
      5
      6
      7
      8
      # Output the IP Address of the Container
      output "ip" {
      value = "${module.container.ip}"
      }

      output "container_name" {
      value = "${module.container.container_name}"
      }
    4. Script
      1
      2
      3
      4
      5
      terraform init
      terraform plan -out=tfplan
      terraform apply tfplan
      # clean up
      terraform destroy -auto-approve

Setting up a Kubernetes cluster with docker

Configure the Kubernetes cluster

1 master, 3 worker nodes

  1. In Node 1, add the Kubernetes repo to /etc/yum.repos.d.
    1
    cat << EOF > /etc/yum.repos.d/kubernetes.repo
  2. Install the repo
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    [kubernetes]
    name=Kubernetes
    baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
    enabled=1
    gpgcheck=1
    repo_gpgcheck=1
    gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg
    https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
    exclude=kube*
    EOF
  3. Disable SELinux
    1
    setenforce 0
  4. Install Kubernetes, enable and start kubelet
    1
    2
    3
    # use a version to be more stable
    yum install -y kubelet-1.11.3 kubeadm-1.11.3 kubectl-1.11.3 --disableexcludes=kubernetes
    systemctl enable kubelet && systemctl start kubelet
  5. Set up the bridge network
    1
    2
    3
    4
    5
    6
    cat <<EOF > /etc/sysctl.d/k8s.conf
    net.bridge.bridge-nf-call-ip6tables = 1
    net.bridge.bridge-nf-call-iptables = 1
    EOF
    # reload so changes take effect
    sysctl --system
  6. Initialize the master node, and set the code network CIDR and start using cluster
    1
    2
    3
    4
    5
    kubeadm init --pod-network-cidr=10.244.0.0/16 --kubernetes-version=v1.11.3
    # start using the cluster
    mkdir -p $HOME/.kube
    sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    sudo chown $(id -u):$(id -g)$HOME/.kube/config
  7. Install Flannel (for networking settings) on the master node
    1
    kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/v0.9.1/Documentation/kube-flannel.yml
  8. Repeat Steps 1-5 in your Node 2 and Node 3 terminals
  9. In your Node 1 terminal window, run the following command
    1
    2
    kubeadmin token create --print-join-command
    # copy the command to join
  10. Paste the previous result command and run it in your Node 2 and Node 3 terminal, and then check everything went ok
    1
    2
    3
    4
    kubectl get nodes
    # 3 nodes
    # 2 with role "none"
    # 1 with role "master"

Create a Pod

  1. Create the pod.yml file and edit it
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    apiVersion: v1
    kind: Pod
    metadata:
    name: nginx-pod-demo
    labels:
    app: nginx-demo
    spec:
    containers:
    - image: nginx:latest
    name: nginx-demo
    ports:
    - containerPort: 80
    imagePullPolicy: Always
  2. Create the pod
    1
    2
    3
    kubectl create -f pod.yml
    # check them
    kubectl get pods

Create the service

  1. Create the service.yml file and edit it
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    kind: Service
    apiVersion: v1
    metadata:
    name: service-demo
    spec:
    selector:
    app: nginx-demo
    ports:
    - protocol: TCP
    port: 80
    targetPort: 80
    type: NodePort
  2. Create the service
    1
    2
    3
    kubectl create -f service.yml
    # check it
    kubectl get services
  3. Browse to PUBLIC_IP_ADDRESS:SERVICE_PORT_NUMBER, check the Nginx welcome screen

Scaling pods in Kubernetes

Deploy

  1. Initialize Kubernetes cluster and install Flannel (see “Configure the Kubernetes cluster, steps 6,7”)
  2. Deploy (deployment.yml)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    kind: Deployment
    apiVersion: v1
    metadata:
    name: httpd-deployment
    labels:
    app: httpd
    spec:
    replicas: 3
    selector:
    matchLabels:
    app: httpd
    template:
    metadata:
    labels:
    app: httpd
    spec:
    containers:
    -name: httpd
    image: httpd:latest
    ports:
    - containerPort: 80
    1
    2
    3
    4
    # deploy
    kubectl create -f deployment.yml
    # check it
    kubectl get pods
  3. Create the service.yml file and edit it
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    kind: Service
    apiVersion: v1
    metadata:
    name: service-deployment
    spec:
    selector:
    app: httpd
    ports:
    - protocol: TCP
    port: 80
    targetPort: 80
    type: NodePort
    1
    2
    3
    4
    # launch
    kubectl create -f service.yml
    # check it
    kubectl get pods

Scale the deployment

  1. Scale up to 5 replicas (deployment.yml)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    kind: Deployment
    apiVersion: v1
    metadata:
    name: httpd-deployment
    labels:
    app: httpd
    spec:
    replicas: 5
    selector:
    matchLabels:
    app: httpd
    template:
    metadata:
    labels:
    app: httpd
    spec:
    containers:
    -name: httpd
    image: httpd:latest
    ports:
    - containerPort: 80
    1
    2
    3
    kubectl apply -f deployment.yml
    # check it
    kubectl get pods
  2. Scale down to 2 replicas (edit deployment.yml)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    kind: Deployment
    apiVersion: v1
    metadata:
    name: httpd-deployment
    labels:
    app: httpd
    spec:
    replicas: 2
    selector:
    matchLabels:
    app: httpd
    template:
    metadata:
    labels:
    app: httpd
    spec:
    containers:
    -name: httpd
    image: httpd:latest
    ports:
    - containerPort: 80
    1
    2
    3
    kubectl apply -f deployment.yml
    # check it
    kubectl get pods

Creating a Helm chart

Creating an application using Helm

  1. Install Helm
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    # get the install script
    curl https://raw.githubusercontent.com/helm/helm/master/scripts/get > /tmp/get_helm.sh
    # verify the download
    cat /tmp/get_helm.sh
    # modify the access permissions
    chmod 700 /tmp/get_helm.sh
    # Set the version to v2.8.2
    DESIRED_VERSION=v2.8.2 /tmp/get_helm.sh
    # init Helm
    helm init --wait
  2. Give Helm permission to work with Kubernetes
    1
    2
    3
    kubectl --namespace=kube-system create clusterrolebinding add-on-cluster-admin --clusterrole=cluster-admin --serviceaccount=kube-system:default
    # check it
    helm ls

Create a Helm chart

  1. Create charts directory and create one chart
    1
    2
    3
    4
    5
    6
    7
    8
    mkdir charts
    cd charts
    # chart for httpd
    helm create httpd
    # check content
    cd httpd/
    ls
    # chart.yaml and values.yaml should exist
  2. Edit the values.yaml file:
    • Under image: change the repository to httpd. Change the tag to latest
    • Under service: change type to NodePort
    • httpd/values.yaml should look like this:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      replicaCount: 1

      image:
      repository: httpd
      tag: latest
      pullPolicy: IfNotPresent

      service:
      type: NodePort
      port: 80

      ingress:
      enabled: false
      annotations: {}
      path: /
      hosts:
      - chart-example.local
      tls: []

      resources: {}
      nodeSelector: {}

      tolerations: []

      affinity: {}

Create the application

  1. From the charts directory, install our application
    1
    2
    3
    helm install --name my-httpd ./httpd/
    # copy the commands listed under the NOTES section of the output and paste them to run them
    # it should return the private IP address and port number of our application
  2. Check the pods
    1
    2
    3
    # check pods
    kubectl get pods
    # you should see the pod that was created by Helm
  3. Check services
    kubectl get services
    # locate the port number of my-httpd displayed in the output
    
  4. Browser to PUBLIC_IP_ADDRESS:SERVICE_PORT to check it works

Setting up dockerswarm (1 manager)

Initialize docker swarm

  1. Initialize the Docker swarm on manager node (SwarmServer1)
    1
    docker swarm init
  2. Copy the docker swarm join command that is displayed for the next step (e.g. docker swarm join --token TOKEN IP_ADDRESS:2377)

Add additional nodes to the swarm

  1. Add worker nodes to the swarm (SwarmServer2 and SwarmServer3) using the command retrieved from the init
    1
    docker swarm join --token TOKEN IP_ADDRESS:2377

Create a swarm service

  1. From the manager node (SwarmServer1), create a service to test your swarm configuration
    1
    2
    3
    docker service create --name weather-app --publish published=80,target=3000 --replicas=3 weather-app
    # verify it
    docker service ls
  2. Paste one of the public IP addresses of our worker nodes in a browser to view the weather app is also showing on the ‘worker nodes’

Backing up and restoring dockerswarm

Join the worker nodes to the swarm

  1. Follow the previous section “initilize and add”, so you get the manager and 2 workers up and running
  2. View the running services on swarm manager node
    1
    2
    3
    4
    5
    docker service ls
    # check backup service is there, we will scale it
    docker service scale backup=3
    # check by showing the service across our nodes
    docker service ps backup

Back up the swarm manager

  1. On the manager node:
    1
    2
    3
    4
    # stop service before
    systemctl stop docker
    # back up
    tar czvf swarm.tgz /var/lib/docker/swarm/

Restore the swarm on the backup manager

  1. Copy the swarm backup from the manager node to the backup manager
    1
    2
    # from manager node terminal
    scp swarm.tgz cloud_user@BACKUP_IP_ADDRESS:/home/cloud_user/
  2. Extract the backup file on backup server (4th server)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    cd /home/cloud_user
    tar xzvf swarm.tgz
    # copy the swarm directory
    /var/lib/docker/swarm
    cp -rf swarm/ /var/lib/docker/
    # restart the service
    systemctl restart docker
    # reinitialize the swarm
    docker swarm init --force-new-cluster
    # copy the swarm join command

Add the worker nodes to the restored cluster

  1. The 2 workers need to leave the first swarm and join the second one
    1
    2
    3
    docker swarm leave
    # do not forget it should have the backup IP address
    docker swarm join --token $NEW_JOIN_TOKEN_HERE

Distribute the replicas across the swarm

  1. Check backup server is up and running
    1
    2
    3
    4
    docker service ls
    # check the service list
    docker service ps backup
    # you should see 3 replicas on backup manager
  2. Make sure our service is distributed across our swarm
    1
    2
    3
    4
    5
    6
    7
    # scale things down to 1
    docker service scale backup=1
    # back up to 3
    docker service scale backup=3
    #check service again
    docker service ps backup
    # service should be up and running on all 3 nodes

Scaling a docker swarm service (2 managers)

Create a swarm

  1. Create a swarm manager (SwarmManager1)
    1
    2
    docker swarm init
    # copy the join command

Add worker nodes

  1. To each worker (e.g. 3 workers)
    1
    docker swarm join --token <TOKEN> <IP_ADDRESS:2377>

The Manager Token

  1. Generate the manager token on SwamManager1 node
    1
    2
    docker swarm join-token manager
    # This command is for when we want a manager node to join
  2. Add a second swam manager server SwamManager2
    1
    docker swarm join --token <TOKEN> <IP_ADDRESS:2377>
  3. Check our work (from SwarmManager1)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    docker node ls
    # 5 nodes running
    # 3 workers
    # 1 MANAGER STATUS: leader (SwarmManager1)
    # 1 MANAGER STATUS: Reachable (SwarmManager2)

    ## ensure the managers are ONLY managers
    docker node update --availability drain <MANAGER1 ID>
    docker node update --availability drain <MANAGER2 ID>

Create a service

  1. Create a service, and replicate it three times (image httpd from DockerHub)
    1
    docker service create --name httpd -p 80:80 --replicas 3 httpd
  2. Check what is running (from SwarmManager1)
    1
    2
    docker service ps httpd
    # shows 3 replicas, 1 on each worker

Scale a service up and down

  1. Scale the httpd service up to 5
    1
    2
    3
    docker service scale httpd=5
    docker service ps httpd
    # check there are 5 now, managers are only managers
  2. Scale the httpd Service back down to 2 Nodes
    1
    2
    3
    docker service scale httpd=2
    docker service ps httpd
    # check there are 2 now, managers are only managers

Prometheus (cAdvisor)

  • Prepare environment

    1. Create prometheus.yml in root’s home directory

      1
      2
      3
      4
      5
      6
      scrape_configs:
      - job_name: cadvisor
      scrape_interval: 5s
      static_configs:
      - targets:
      - cadvisor:8080
    2. Create the Prometheus services via docker-compose.yml

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      version: '3'
      services:
      prometheus:
      image: prom/prometheus:latest
      container_name: prometheus
      ports:
      - 9090:9090
      command:
      - --config.file=/etc/prometheus/prometheus.yml
      volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
      depends_on:
      - cadvisor
      cadvisor:
      image: google/cadvisor:latest
      container_name: cadvisor
      ports:
      - 8080:8080
      volumes:
      - /:/rootfs:ro
      - /var/run:/var/run:rw
      - /sys:/sys:ro
      - /var/lib/docker:/var/lib/docker:ro
    3. Start the environment

      1
      2
      3
      4
      docker-compose up -d
      docker ps
      # The output should show four containers:
      # prometheus, cadvisor, nginx, and redis
    4. Viewing the Prometheus web interface (http://<IP_ADDRESS>:9090/graph/)

      • Top menu: status -> targets
      • Top menu: Graph -> Execute button -> find container_startup_time_second -> Execute
      • Queries: Above that Execute* button and related dropdown is a web form field where we can type different expressions. **rate(container_cpu_usage_seconds_total{name="redis"}[1m])and execute
  • cAdvisor

    • Instance (http://<IP_ADDRESS>:8080/containers/)
    • Individual containers (http://<IP_ADDRESS>:8080/docker/nginx/)
  • Stats in Docker. [[Ctrl]]+[[C]] to exit

    1
    2
    # CPU and memory usage
    docker stats
    • Prettier stats via shell scripts
      1. Create a stats.sh file in /root
        1
        nano ~/stats.sh
      2. Add this command to it
        1
        docker stats --format "table {{.Name}} {{.ID}} {{.MemUsage}} {{.CPUPerc}}"
      3. Make it executable and run it
        1
        2
        3
        chmod a+x stats.sh
        # Run it
        ./stats.sh

Grafana + Prometheus = alerting and monitoring

Setup

  1. Configure Docker via /etc/docker/daemon.json
    1
    2
    3
    4
    {
    "metrics-addr" : "0.0.0.0:9323",
    "experimental" : true
    }
    1
    2
    # restart docker service
    systemctl restart docker
  2. Update Prometheus via /prometheus.yml
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    scrape_configs:
    - job_name: prometheus
    scrape_interval: 5s
    static_configs:
    - targets:
    - prometheus:9090
    - node-exporter:9100
    - pushgateway:9091
    - cadvisor:8080

    - job_name: docker
    scrape_interval: 5s
    static_configs:
    - targets:
    # provide the private IP address of your instance
    - <PRIVATE_IP_ADDRESS>:9323
  3. Update Docker Compose (~/docker-compose.yml)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    version: '3'
    services:
    prometheus:
    image: prom/prometheus:latest
    container_name: prometheus
    ports:
    - 9090:9090
    command:
    - --config.file=/etc/prometheus/prometheus.yml
    volumes:
    - ./prometheus.yml:/etc/prometheus/prometheus.yml:ro
    depends_on:
    - cadvisor
    cadvisor:
    image: google/cadvisor:latest
    container_name: cadvisor
    ports:
    - 8080:8080
    volumes:
    - /:/rootfs:ro
    - /var/run:/var/run:rw
    - /sys:/sys:ro
    - /var/lib/docker/:/var/lib/docker:ro
    pushgateway:
    image: prom/pushgateway
    container_name: pushgateway
    ports:
    - 9091:9091
    node-exporter:
    image: prom/node-exporter:latest
    container_name: node-exporter
    restart: unless-stopped
    expose:
    - 9100
    grafana:
    image: grafana/grafana
    container_name: grafana
    ports:
    - 3000:3000
    environment:
    - GF_SECURITY_ADMIN_PASSWORD=password
    depends_on:
    - prometheus
    - cadvisor
    1
    2
    3
    # apply our changes and rebuild the environment
    docker-compose up -d
    docker ps
  4. Verify that all of our Prometheus targets are healthy
    • Browse http://PUBLIC_IP_ADDRESS:9090 -> Status -> Targets
  5. Check firewall configuration if you are using private IP
    1
    firewall-cmd --add-port 9090/tcp

Configure Grafana

  1. Create a new Grafana data source

    • Browse http://PUBLIC_IP_ADDRESS:3000
    • Type “admin” for username and “password” for password. Click Log In.
    • Home Dashboard -> Add data source -> Name =”Prometheus”
      Type field = “Prometheus”
      URL = http://localhost:9090
    • Replace “localhost” in the URL with the private IP address (e.g. http://PRIVATE_IP_ADDRESS:9090)
    • Click Save & Test.
  2. Add the Docker Dashboard to Grafana

    • Click on ‘+’’ on the left side of the Grafana interface -> Import
    • Paste the JSON for Docker to Grafana into the ‘Or paste JSON field’-> Load. ON Import screen -> dropdown menu for Prometheus -> Prometheus data source we created earlier -> Import
  3. Add an email notification channel

    • Left sidebar -> bell icon -> Notification channels -> Add channel
      • Name = “Email”
      • Email addresses = yourMail@mail.com
  4. Create an Alert for CPU Usage

    • dashboard icon (left sidebar) -> Home -> Docker and system monitoring dashboard -> CPU Usage graph -> Edit
    • Metrics tab -> Add Query: sum(rate(process_cpu_seconds_total[1m])) * 100
    • Alert tab -> Create Alert -> Conditions -> query field: E**: IS ABOVE = “75” -> **Test Rule
    • Notifications (left sidebar) -> ‘+’’ icon next to Sent to -> Select Email Alerts
      • Message text box = “CPU usage is over 75%.”
      • Save, check the box next to Save current variables. description box = “Add alerting”. Save.

JSON for Docker to Grafana

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
{
"__inputs": [
{
"name": "DS_PROMETHEUS",
"label": "Prometheus",
"description": "",
"type": "datasource",
"pluginId": "prometheus",
"pluginName": "Prometheus"
}
],
"__requires": [
{
"type": "grafana",
"id": "grafana",
"name": "Grafana",
"version": "5.2.3"
},
{
"type": "panel",
"id": "graph",
"name": "Graph",
"version": "5.0.0"
},
{
"type": "datasource",
"id": "prometheus",
"name": "Prometheus",
"version": "5.0.0"
},
{
"type": "panel",
"id": "singlestat",
"name": "Singlestat",
"version": "5.0.0"
},
{
"type": "panel",
"id": "table",
"name": "Table",
"version": "5.0.0"
}
],
"annotations": {
"list": [
{
"builtIn": 1,
"datasource": "-- Grafana --",
"enable": true,
"hide": true,
"iconColor": "rgba(0, 211, 255, 1)",
"name": "Annotations & Alerts",
"type": "dashboard"
}
]
},
"description": "A simple overview of the most important Docker host and container metrics. (cAdvisor/Prometheus)",
"editable": true,
"gnetId": 893,
"graphTooltip": 1,
"id": null,
"iteration": 1535656991954,
"links": [],
"panels": [
{
"cacheTimeout": null,
"colorBackground": false,
"colorValue": false,
"colors": [
"rgba(245, 54, 54, 0.9)",
"rgba(237, 129, 40, 0.89)",
"rgba(50, 172, 45, 0.97)"
],
"datasource": "${DS_PROMETHEUS}",
"decimals": 0,
"editable": true,
"error": false,
"format": "s",
"gauge": {
"maxValue": 100,
"minValue": 0,
"show": false,
"thresholdLabels": false,
"thresholdMarkers": true
},
"gridPos": {
"h": 4,
"w": 4,
"x": 0,
"y": 0
},
"height": "",
"id": 24,
"interval": null,
"links": [],
"mappingType": 1,
"mappingTypes": [
{
"name": "value to text",
"value": 1
},
{
"name": "range to text",
"value": 2
}
],
"maxDataPoints": 100,
"nullPointMode": "connected",
"nullText": null,
"postfix": "",
"postfixFontSize": "30%",
"prefix": "",
"prefixFontSize": "20%",
"rangeMaps": [
{
"from": "null",
"text": "N/A",
"to": "null"
}
],
"sparkline": {
"fillColor": "rgba(31, 118, 189, 0.18)",
"full": false,
"lineColor": "rgb(31, 120, 193)",
"show": false
},
"tableColumn": "",
"targets": [
{
"expr": "time() - node_boot_time_seconds{instance=~\".*\"}",
"format": "time_series",
"hide": false,
"intervalFactor": 2,
"legendFormat": "",
"refId": "A",
"step": 1800
}
],
"thresholds": "",
"title": "Uptime",
"type": "singlestat",
"valueFontSize": "80%",
"valueMaps": [
{
"op": "=",
"text": "N/A",
"value": "null"
}
],
"valueName": "current"
},
{
"cacheTimeout": null,
"colorBackground": false,
"colorValue": false,
"colors": [
"rgba(245, 54, 54, 0.9)",
"rgba(237, 129, 40, 0.89)",
"rgba(50, 172, 45, 0.97)"
],
"datasource": "${DS_PROMETHEUS}",
"editable": true,
"error": false,
"format": "none",
"gauge": {
"maxValue": 100,
"minValue": 0,
"show": false,
"thresholdLabels": false,
"thresholdMarkers": true
},
"gridPos": {
"h": 4,
"w": 4,
"x": 4,
"y": 0
},
"id": 31,
"interval": null,
"links": [],
"mappingType": 1,
"mappingTypes": [
{
"name": "value to text",
"value": 1
},
{
"name": "range to text",
"value": 2
}
],
"maxDataPoints": 100,
"nullPointMode": "connected",
"nullText": null,
"postfix": "",
"postfixFontSize": "50%",
"prefix": "",
"prefixFontSize": "50%",
"rangeMaps": [
{
"from": "null",
"text": "N/A",
"to": "null"
}
],
"sparkline": {
"fillColor": "rgba(31, 118, 189, 0.18)",
"full": false,
"lineColor": "rgb(31, 120, 193)",
"show": false
},
"tableColumn": "",
"targets": [
{
"expr": "count(rate(container_last_seen{name=~\".+\"}[$interval]))",
"intervalFactor": 2,
"refId": "A",
"step": 1800
}
],
"thresholds": "",
"title": "Containers",
"type": "singlestat",
"valueFontSize": "120%",
"valueMaps": [
{
"op": "=",
"text": "N/A",
"value": "null"
}
],
"valueName": "current"
},
{
"cacheTimeout": null,
"colorBackground": false,
"colorValue": false,
"colors": [
"rgba(50, 172, 45, 0.97)",
"rgba(237, 129, 40, 0.89)",
"rgba(245, 54, 54, 0.9)"
],
"datasource": "${DS_PROMETHEUS}",
"decimals": 1,
"editable": true,
"error": false,
"format": "percentunit",
"gauge": {
"maxValue": 1,
"minValue": 0,
"show": true,
"thresholdLabels": false,
"thresholdMarkers": true
},
"gridPos": {
"h": 4,
"w": 4,
"x": 8,
"y": 0
},
"id": 26,
"interval": null,
"links": [],
"mappingType": 1,
"mappingTypes": [
{
"name": "value to text",
"value": 1
},
{
"name": "range to text",
"value": 2
}
],
"maxDataPoints": 100,
"nullPointMode": "connected",
"nullText": null,
"postfix": "",
"postfixFontSize": "50%",
"prefix": "",
"prefixFontSize": "50%",
"rangeMaps": [
{
"from": "null",
"text": "N/A",
"to": "null"
}
],
"sparkline": {
"fillColor": "rgba(31, 118, 189, 0.18)",
"full": false,
"lineColor": "rgb(31, 120, 193)",
"show": false
},
"tableColumn": "",
"targets": [
{
"expr": "min((node_filesystem_size_bytes{fstype=~\"xfs|ext4\",instance=~\".+\"} - node_filesystem_free_bytes{fstype=~\"xfs|ext4\",instance=~\".+\"} )/ node_filesystem_size_bytes{fstype=~\"xfs|ext4\",instance=~\".+\"})",
"format": "time_series",
"hide": false,
"intervalFactor": 2,
"refId": "A",
"step": 1800
}
],
"thresholds": "0.75, 0.90",
"title": "Disk space",
"type": "singlestat",
"valueFontSize": "80%",
"valueMaps": [
{
"op": "=",
"text": "N/A",
"value": "null"
}
],
"valueName": "current"
},
{
"cacheTimeout": null,
"colorBackground": false,
"colorValue": false,
"colors": [
"rgba(50, 172, 45, 0.97)",
"rgba(237, 129, 40, 0.89)",
"rgba(245, 54, 54, 0.9)"
],
"datasource": "${DS_PROMETHEUS}",
"decimals": 0,
"editable": true,
"error": false,
"format": "percent",
"gauge": {
"maxValue": 100,
"minValue": 0,
"show": true,
"thresholdLabels": false,
"thresholdMarkers": true
},
"gridPos": {
"h": 4,
"w": 4,
"x": 12,
"y": 0
},
"id": 25,
"interval": null,
"links": [],
"mappingType": 1,
"mappingTypes": [
{
"name": "value to text",
"value": 1
},
{
"name": "range to text",
"value": 2
}
],
"maxDataPoints": 100,
"nullPointMode": "connected",
"nullText": null,
"postfix": "",
"postfixFontSize": "50%",
"prefix": "",
"prefixFontSize": "50%",
"rangeMaps": [
{
"from": "null",
"text": "N/A",
"to": "null"
}
],
"sparkline": {
"fillColor": "rgba(31, 118, 189, 0.18)",
"full": false,
"lineColor": "rgb(31, 120, 193)",
"show": false
},
"tableColumn": "",
"targets": [
{
"expr": "((node_memory_MemTotal_bytes{instance=~\".+\"} - node_memory_MemAvailable_bytes{instance=~\".+\"}) / node_memory_MemTotal_bytes{instance=~\".+\"}) * 100",
"format": "time_series",
"intervalFactor": 2,
"refId": "A",
"step": 1800
}
],
"thresholds": "70, 90",
"title": "Memory",
"type": "singlestat",
"valueFontSize": "80%",
"valueMaps": [
{
"op": "=",
"text": "N/A",
"value": "null"
}
],
"valueName": "current"
},
{
"cacheTimeout": null,
"colorBackground": false,
"colorValue": false,
"colors": [
"rgba(50, 172, 45, 0.97)",
"rgba(237, 129, 40, 0.89)",
"rgba(245, 54, 54, 0.9)"
],
"datasource": "${DS_PROMETHEUS}",
"decimals": 0,
"editable": true,
"error": false,
"format": "decbytes",
"gauge": {
"maxValue": 500000000,
"minValue": 0,
"show": true,
"thresholdLabels": false,
"thresholdMarkers": true
},
"gridPos": {
"h": 4,
"w": 4,
"x": 16,
"y": 0
},
"id": 30,
"interval": null,
"links": [],
"mappingType": 1,
"mappingTypes": [
{
"name": "value to text",
"value": 1
},
{
"name": "range to text",
"value": 2
}
],
"maxDataPoints": 100,
"nullPointMode": "connected",
"nullText": null,
"postfix": "",
"postfixFontSize": "50%",
"prefix": "",
"prefixFontSize": "50%",
"rangeMaps": [
{
"from": "null",
"text": "N/A",
"to": "null"
}
],
"sparkline": {
"fillColor": "rgba(31, 118, 189, 0.18)",
"full": false,
"lineColor": "rgb(31, 120, 193)",
"show": false
},
"tableColumn": "",
"targets": [
{
"expr": "(node_memory_SwapTotal_bytes{instance=~'.*'} - node_memory_SwapFree_bytes{instance=~'.*'})",
"format": "time_series",
"intervalFactor": 2,
"legendFormat": "",
"refId": "A",
"step": 1800
}
],
"thresholds": "400000000",
"title": "Swap",
"type": "singlestat",
"valueFontSize": "80%",
"valueMaps": [
{
"op": "=",
"text": "N/A",
"value": "null"
}
],
"valueName": "current"
},
{
"cacheTimeout": null,
"colorBackground": false,
"colorValue": false,
"colors": [
"rgba(245, 54, 54, 0.9)",
"rgba(237, 129, 40, 0.89)",
"rgba(50, 172, 45, 0.97)"
],
"datasource": "${DS_PROMETHEUS}",
"decimals": 0,
"editable": true,
"error": false,
"format": "percentunit",
"gauge": {
"maxValue": 100,
"minValue": 0,
"show": false,
"thresholdLabels": false,
"thresholdMarkers": true
},
"gridPos": {
"h": 4,
"w": 4,
"x": 20,
"y": 0
},
"id": 27,
"interval": null,
"links": [],
"mappingType": 1,
"mappingTypes": [
{
"name": "value to text",
"value": 1
},
{
"name": "range to text",
"value": 2
}
],
"maxDataPoints": 100,
"nullPointMode": "connected",
"nullText": null,
"postfix": "",
"postfixFontSize": "50%",
"prefix": "",
"prefixFontSize": "50%",
"rangeMaps": [
{
"from": "null",
"text": "N/A",
"to": "null"
}
],
"sparkline": {
"fillColor": "rgba(50, 189, 31, 0.18)",
"full": false,
"lineColor": "rgb(69, 193, 31)",
"show": true
},
"tableColumn": "",
"targets": [
{
"expr": "node_load1{instance=~\".*\"} / count by(job, instance)(count by(job, instance, cpu)(node_cpu_seconds_total{instance=~\".*\"}))",
"format": "time_series",
"intervalFactor": 2,
"refId": "A",
"step": 1800
}
],
"thresholds": "0.8,0.9",
"title": "Load",
"type": "singlestat",
"valueFontSize": "80%",
"valueMaps": [
{
"op": "=",
"text": "N/A",
"value": "null"
}
],
"valueName": "avg"
},
{
"aliasColors": {
"SENT": "#BF1B00"
},
"bars": false,
"dashLength": 10,
"dashes": false,
"datasource": "${DS_PROMETHEUS}",
"editable": true,
"error": false,
"fill": 1,
"grid": {},
"gridPos": {
"h": 6,
"w": 4,
"x": 0,
"y": 4
},
"id": 19,
"legend": {
"avg": false,
"current": false,
"max": false,
"min": false,
"show": false,
"total": false,
"values": false
},
"lines": true,
"linewidth": 1,
"links": [],
"nullPointMode": "null as zero",
"percentage": false,
"pointradius": 1,
"points": false,
"renderer": "flot",
"seriesOverrides": [],
"spaceLength": 10,
"stack": false,
"steppedLine": false,
"targets": [
{
"expr": "sum(rate(container_network_receive_bytes_total{id=\"/\"}[$interval])) by (id)",
"intervalFactor": 2,
"legendFormat": "RECEIVED",
"refId": "A",
"step": 600
},
{
"expr": "- sum(rate(container_network_transmit_bytes_total{id=\"/\"}[$interval])) by (id)",
"hide": false,
"intervalFactor": 2,
"legendFormat": "SENT",
"refId": "B",
"step": 600
}
],
"thresholds": [],
"timeFrom": null,
"timeShift": null,
"title": "Network Traffic",
"tooltip": {
"msResolution": true,
"shared": true,
"sort": 0,
"value_type": "cumulative"
},
"transparent": false,
"type": "graph",
"xaxis": {
"buckets": null,
"mode": "time",
"name": null,
"show": false,
"values": []
},
"yaxes": [
{
"format": "bytes",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
},
{
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": false
}
],
"yaxis": {
"align": false,
"alignLevel": null
}
},
{
"aliasColors": {
"{id=\"/\",instance=\"cadvisor:8080\",job=\"prometheus\"}": "#BA43A9"
},
"bars": false,
"dashLength": 10,
"dashes": false,
"datasource": "${DS_PROMETHEUS}",
"editable": true,
"error": false,
"fill": 1,
"grid": {},
"gridPos": {
"h": 6,
"w": 4,
"x": 4,
"y": 4
},
"id": 5,
"legend": {
"avg": false,
"current": false,
"max": false,
"min": false,
"show": false,
"total": false,
"values": false
},
"lines": true,
"linewidth": 1,
"links": [],
"nullPointMode": "null as zero",
"percentage": false,
"pointradius": 5,
"points": false,
"renderer": "flot",
"seriesOverrides": [],
"spaceLength": 10,
"stack": true,
"steppedLine": false,
"targets": [
{
"expr": "sum(rate(container_cpu_system_seconds_total[1m]))",
"hide": true,
"intervalFactor": 2,
"legendFormat": "a",
"refId": "B",
"step": 120
},
{
"expr": "sum(rate(container_cpu_system_seconds_total{name=~\".+\"}[1m]))",
"hide": true,
"interval": "",
"intervalFactor": 2,
"legendFormat": "nur container",
"refId": "F",
"step": 10
},
{
"expr": "sum(rate(container_cpu_system_seconds_total{id=\"/\"}[1m]))",
"hide": true,
"interval": "",
"intervalFactor": 2,
"legendFormat": "nur docker host",
"metric": "",
"refId": "A",
"step": 20
},
{
"expr": "sum(rate(process_cpu_seconds_total[$interval])) * 100",
"hide": false,
"interval": "",
"intervalFactor": 2,
"legendFormat": "host",
"metric": "",
"refId": "C",
"step": 600
},
{
"expr": "sum(rate(container_cpu_system_seconds_total{name=~\".+\"}[1m])) + sum(rate(container_cpu_system_seconds_total{id=\"/\"}[1m])) + sum(rate(process_cpu_seconds_total[1m]))",
"hide": true,
"intervalFactor": 2,
"legendFormat": "",
"refId": "D",
"step": 120
}
],
"thresholds": [],
"timeFrom": null,
"timeShift": null,
"title": "CPU Usage",
"tooltip": {
"msResolution": true,
"shared": true,
"sort": 0,
"value_type": "cumulative"
},
"type": "graph",
"xaxis": {
"buckets": null,
"mode": "time",
"name": null,
"show": false,
"values": []
},
"yaxes": [
{
"format": "percent",
"label": "",
"logBase": 1,
"max": null,
"min": null,
"show": true
},
{
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": false
}
],
"yaxis": {
"align": false,
"alignLevel": null
}
},
{
"alert": {
"conditions": [
{
"evaluator": {
"params": [
1.25
],
"type": "gt"
},
"query": {
"params": [
"A",
"5m",
"now"
]
},
"reducer": {
"params": [],
"type": "avg"
},
"type": "query"
}
],
"executionErrorState": "alerting",
"frequency": "60s",
"handler": 1,
"name": "Panel Title alert",
"noDataState": "keep_state",
"notifications": [
{
"id": 1
}
]
},
"aliasColors": {},
"bars": false,
"dashLength": 10,
"dashes": false,
"datasource": "${DS_PROMETHEUS}",
"decimals": 0,
"editable": true,
"error": false,
"fill": 1,
"gridPos": {
"h": 6,
"w": 4,
"x": 8,
"y": 4
},
"id": 28,
"legend": {
"avg": false,
"current": false,
"max": false,
"min": false,
"show": false,
"total": false,
"values": false
},
"lines": true,
"linewidth": 1,
"links": [],
"nullPointMode": "connected",
"percentage": false,
"pointradius": 5,
"points": false,
"renderer": "flot",
"seriesOverrides": [],
"spaceLength": 10,
"stack": false,
"steppedLine": false,
"targets": [
{
"expr": "node_load1{instance=~\".*\"} / count by(job, instance)(count by(job, instance, cpu)(node_cpu_seconds_total{instance=~\".*\"}))",
"format": "time_series",
"intervalFactor": 2,
"refId": "A",
"step": 600
}
],
"thresholds": [
{
"colorMode": "critical",
"fill": true,
"line": true,
"op": "gt",
"value": 1.25
}
],
"timeFrom": null,
"timeShift": null,
"title": "Load",
"tooltip": {
"msResolution": false,
"shared": true,
"sort": 0,
"value_type": "individual"
},
"type": "graph",
"xaxis": {
"buckets": null,
"mode": "time",
"name": null,
"show": false,
"values": []
},
"yaxes": [
{
"format": "percentunit",
"label": null,
"logBase": 1,
"max": "1.50",
"min": null,
"show": true
},
{
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": false
}
],
"yaxis": {
"align": false,
"alignLevel": null
}
},
{
"alert": {
"conditions": [
{
"evaluator": {
"params": [
850000000000
],
"type": "gt"
},
"query": {
"params": [
"A",
"5m",
"now"
]
},
"reducer": {
"params": [],
"type": "avg"
},
"type": "query"
}
],
"executionErrorState": "alerting",
"frequency": "60s",
"handler": 1,
"name": "Free/Used Disk Space alert",
"noDataState": "keep_state",
"notifications": [
{
"id": 1
}
]
},
"aliasColors": {
"Belegete Festplatte": "#BF1B00",
"Free Disk Space": "#7EB26D",
"Used Disk Space": "#7EB26D",
"{}": "#BF1B00"
},
"bars": false,
"dashLength": 10,
"dashes": false,
"datasource": "${DS_PROMETHEUS}",
"editable": true,
"error": false,
"fill": 1,
"grid": {},
"gridPos": {
"h": 6,
"w": 4,
"x": 12,
"y": 4
},
"id": 13,
"legend": {
"avg": false,
"current": false,
"max": false,
"min": false,
"show": false,
"total": false,
"values": false
},
"lines": true,
"linewidth": 1,
"links": [],
"nullPointMode": "null as zero",
"percentage": false,
"pointradius": 5,
"points": false,
"renderer": "flot",
"seriesOverrides": [
{
"alias": "Used Disk Space",
"yaxis": 1
}
],
"spaceLength": 10,
"stack": true,
"steppedLine": false,
"targets": [
{
"expr": "node_filesystem_size_bytes{fstype=\"xfs\"} - node_filesystem_free_bytes{fstype=\"xfs\"}",
"format": "time_series",
"hide": false,
"intervalFactor": 2,
"legendFormat": "Used Disk Space",
"refId": "A",
"step": 600
}
],
"thresholds": [
{
"colorMode": "critical",
"fill": true,
"line": true,
"op": "gt",
"value": 850000000000
}
],
"timeFrom": null,
"timeShift": null,
"title": "Used Disk Space",
"tooltip": {
"msResolution": true,
"shared": true,
"sort": 0,
"value_type": "individual"
},
"type": "graph",
"xaxis": {
"buckets": null,
"mode": "time",
"name": null,
"show": false,
"values": []
},
"yaxes": [
{
"format": "bytes",
"label": "",
"logBase": 1,
"max": 1000000000000,
"min": 0,
"show": true
},
{
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": false
}
],
"yaxis": {
"align": false,
"alignLevel": null
}
},
{
"alert": {
"conditions": [
{
"evaluator": {
"params": [
10000000000
],
"type": "gt"
},
"query": {
"params": [
"A",
"5m",
"now"
]
},
"reducer": {
"params": [],
"type": "avg"
},
"type": "query"
}
],
"executionErrorState": "alerting",
"frequency": "60s",
"handler": 1,
"name": "Available Memory alert",
"noDataState": "keep_state",
"notifications": [
{
"id": 1
}
]
},
"aliasColors": {
"Available Memory": "#7EB26D",
"Unavailable Memory": "#7EB26D"
},
"bars": false,
"dashLength": 10,
"dashes": false,
"datasource": "${DS_PROMETHEUS}",
"editable": true,
"error": false,
"fill": 1,
"grid": {},
"gridPos": {
"h": 6,
"w": 4,
"x": 16,
"y": 4
},
"id": 20,
"legend": {
"avg": false,
"current": false,
"max": false,
"min": false,
"show": false,
"total": false,
"values": false
},
"lines": true,
"linewidth": 1,
"links": [],
"nullPointMode": "null as zero",
"percentage": false,
"pointradius": 5,
"points": false,
"renderer": "flot",
"seriesOverrides": [],
"spaceLength": 10,
"stack": true,
"steppedLine": false,
"targets": [
{
"expr": "container_memory_rss{name=~\".+\"}",
"format": "time_series",
"hide": true,
"intervalFactor": 2,
"legendFormat": "{{__name__}}",
"refId": "D",
"step": 20
},
{
"expr": "sum(container_memory_rss{name=~\".+\"})",
"format": "time_series",
"hide": true,
"intervalFactor": 2,
"legendFormat": "{{__name__}}",
"refId": "A",
"step": 20
},
{
"expr": "container_memory_usage_bytes{name=~\".+\"}",
"format": "time_series",
"hide": true,
"intervalFactor": 2,
"legendFormat": "{{name}}",
"refId": "B",
"step": 20
},
{
"expr": "container_memory_rss{id=\"/\"}",
"format": "time_series",
"hide": true,
"intervalFactor": 2,
"legendFormat": "{{__name__}}",
"refId": "C",
"step": 20
},
{
"expr": "sum(container_memory_rss)",
"format": "time_series",
"hide": true,
"intervalFactor": 2,
"legendFormat": "{{__name__}}",
"refId": "E",
"step": 20
},
{
"expr": "node_memory_Buffers",
"format": "time_series",
"hide": true,
"intervalFactor": 2,
"legendFormat": "node_memory_Dirty",
"refId": "N",
"step": 30
},
{
"expr": "node_memory_MemFree",
"format": "time_series",
"hide": true,
"intervalFactor": 2,
"legendFormat": "{{__name__}}",
"refId": "F",
"step": 20
},
{
"expr": "node_memory_MemAvailable",
"format": "time_series",
"hide": true,
"intervalFactor": 2,
"legendFormat": "Available Memory",
"refId": "H",
"step": 20
},
{
"expr": "node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes",
"format": "time_series",
"hide": false,
"intervalFactor": 2,
"legendFormat": "Unavailable Memory",
"refId": "G",
"step": 600
},
{
"expr": "node_memory_Inactive",
"format": "time_series",
"hide": true,
"intervalFactor": 2,
"legendFormat": "{{__name__}}",
"refId": "I",
"step": 30
},
{
"expr": "node_memory_KernelStack",
"format": "time_series",
"hide": true,
"intervalFactor": 2,
"legendFormat": "{{__name__}}",
"refId": "J",
"step": 30
},
{
"expr": "node_memory_Active",
"format": "time_series",
"hide": true,
"intervalFactor": 2,
"legendFormat": "{{__name__}}",
"refId": "K",
"step": 30
},
{
"expr": "node_memory_MemTotal - (node_memory_Active + node_memory_MemFree + node_memory_Inactive)",
"format": "time_series",
"hide": true,
"intervalFactor": 2,
"legendFormat": "Unknown",
"refId": "L",
"step": 40
},
{
"expr": "node_memory_MemFree + node_memory_Inactive ",
"format": "time_series",
"hide": true,
"intervalFactor": 2,
"legendFormat": "{{__name__}}",
"refId": "M",
"step": 30
},
{
"expr": "container_memory_rss{name=~\".+\"}",
"format": "time_series",
"hide": true,
"intervalFactor": 2,
"legendFormat": "{{__name__}}",
"refId": "O",
"step": 30
},
{
"expr": "node_memory_Inactive + node_memory_MemFree + node_memory_MemAvailable",
"format": "time_series",
"hide": true,
"intervalFactor": 2,
"legendFormat": "",
"refId": "P",
"step": 40
}
],
"thresholds": [
{
"colorMode": "critical",
"fill": true,
"line": true,
"op": "gt",
"value": 10000000000
}
],
"timeFrom": null,
"timeShift": null,
"title": "Available Memory",
"tooltip": {
"msResolution": true,
"shared": true,
"sort": 0,
"value_type": "individual"
},
"type": "graph",
"xaxis": {
"buckets": null,
"mode": "time",
"name": null,
"show": false,
"values": []
},
"yaxes": [
{
"format": "bytes",
"label": "",
"logBase": 1,
"max": 16000000000,
"min": 0,
"show": true
},
{
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": false
}
],
"yaxis": {
"align": false,
"alignLevel": null
}
},
{
"aliasColors": {
"IN on /sda": "#7EB26D",
"OUT on /sda": "#890F02"
},
"bars": false,
"dashLength": 10,
"dashes": false,
"datasource": "${DS_PROMETHEUS}",
"editable": true,
"error": false,
"fill": 1,
"grid": {},
"gridPos": {
"h": 6,
"w": 4,
"x": 20,
"y": 4
},
"id": 3,
"legend": {
"avg": false,
"current": false,
"max": false,
"min": false,
"show": false,
"total": false,
"values": false
},
"lines": true,
"linewidth": 1,
"links": [],
"nullPointMode": "null as zero",
"percentage": false,
"pointradius": 5,
"points": false,
"renderer": "flot",
"seriesOverrides": [],
"spaceLength": 10,
"stack": false,
"steppedLine": false,
"targets": [
{
"expr": "-sum(rate(node_disk_read_bytes_total[$interval])) by (device)",
"format": "time_series",
"hide": false,
"intervalFactor": 2,
"legendFormat": "OUT on /{{device}}",
"metric": "node_disk_bytes_read",
"refId": "A",
"step": 600
},
{
"expr": "sum(rate(node_disk_written_bytes_total[$interval])) by (device)",
"format": "time_series",
"intervalFactor": 2,
"legendFormat": "IN on /{{device}}",
"metric": "",
"refId": "B",
"step": 600
}
],
"thresholds": [],
"timeFrom": null,
"timeShift": null,
"title": "Disk I/O",
"tooltip": {
"msResolution": true,
"shared": true,
"sort": 0,
"value_type": "cumulative"
},
"type": "graph",
"xaxis": {
"buckets": null,
"mode": "time",
"name": null,
"show": false,
"values": []
},
"yaxes": [
{
"format": "Bps",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
},
{
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": false
}
],
"yaxis": {
"align": false,
"alignLevel": null
}
},
{
"aliasColors": {},
"bars": false,
"dashLength": 10,
"dashes": false,
"datasource": "${DS_PROMETHEUS}",
"editable": true,
"error": false,
"fill": 1,
"grid": {},
"gridPos": {
"h": 7,
"w": 12,
"x": 0,
"y": 10
},
"id": 8,
"legend": {
"alignAsTable": true,
"avg": false,
"current": false,
"max": false,
"min": false,
"rightSide": true,
"show": true,
"total": false,
"values": false
},
"lines": true,
"linewidth": 2,
"links": [],
"nullPointMode": "null as zero",
"percentage": false,
"pointradius": 5,
"points": false,
"renderer": "flot",
"seriesOverrides": [],
"spaceLength": 10,
"stack": false,
"steppedLine": false,
"targets": [
{
"expr": "sum(rate(container_network_receive_bytes_total{name=~\".+\"}[$interval])) by (name)",
"intervalFactor": 2,
"legendFormat": "{{name}}",
"refId": "A",
"step": 240
},
{
"expr": "- rate(container_network_transmit_bytes_total{name=~\".+\"}[$interval])",
"hide": true,
"intervalFactor": 2,
"legendFormat": "{{name}}",
"refId": "B",
"step": 10
}
],
"thresholds": [],
"timeFrom": null,
"timeShift": null,
"title": "Received Network Traffic per Container",
"tooltip": {
"msResolution": true,
"shared": true,
"sort": 0,
"value_type": "cumulative"
},
"transparent": false,
"type": "graph",
"xaxis": {
"buckets": null,
"mode": "time",
"name": null,
"show": true,
"values": []
},
"yaxes": [
{
"format": "Bps",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
},
{
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
}
],
"yaxis": {
"align": false,
"alignLevel": null
}
},
{
"aliasColors": {},
"bars": false,
"dashLength": 10,
"dashes": false,
"datasource": "${DS_PROMETHEUS}",
"editable": true,
"error": false,
"fill": 1,
"grid": {},
"gridPos": {
"h": 7,
"w": 12,
"x": 12,
"y": 10
},
"id": 9,
"legend": {
"alignAsTable": true,
"avg": false,
"current": false,
"hideEmpty": false,
"hideZero": false,
"max": false,
"min": false,
"rightSide": true,
"show": true,
"total": false,
"values": false
},
"lines": true,
"linewidth": 2,
"links": [],
"nullPointMode": "null as zero",
"percentage": false,
"pointradius": 5,
"points": false,
"renderer": "flot",
"seriesOverrides": [],
"spaceLength": 10,
"stack": false,
"steppedLine": false,
"targets": [
{
"expr": "sum(rate(container_network_transmit_bytes_total{name=~\".+\"}[$interval])) by (name)",
"intervalFactor": 2,
"legendFormat": "{{name}}",
"refId": "A",
"step": 240
},
{
"expr": "rate(container_network_transmit_bytes_total{id=\"/\"}[$interval])",
"hide": true,
"intervalFactor": 2,
"legendFormat": "",
"refId": "B",
"step": 10
}
],
"thresholds": [],
"timeFrom": null,
"timeShift": null,
"title": "Sent Network Traffic per Container",
"tooltip": {
"msResolution": true,
"shared": true,
"sort": 0,
"value_type": "cumulative"
},
"transparent": false,
"type": "graph",
"xaxis": {
"buckets": null,
"mode": "time",
"name": null,
"show": true,
"values": []
},
"yaxes": [
{
"format": "Bps",
"label": "",
"logBase": 1,
"max": null,
"min": null,
"show": true
},
{
"format": "short",
"label": "",
"logBase": 10,
"max": 8,
"min": 0,
"show": false
}
],
"yaxis": {
"align": false,
"alignLevel": null
}
},
{
"aliasColors": {},
"bars": false,
"dashLength": 10,
"dashes": false,
"datasource": "${DS_PROMETHEUS}",
"editable": true,
"error": false,
"fill": 5,
"grid": {},
"gridPos": {
"h": 7,
"w": 12,
"x": 0,
"y": 17
},
"id": 1,
"legend": {
"alignAsTable": true,
"avg": false,
"current": false,
"max": false,
"min": false,
"rightSide": true,
"show": true,
"total": false,
"values": false
},
"lines": true,
"linewidth": 1,
"links": [],
"nullPointMode": "null as zero",
"percentage": false,
"pointradius": 5,
"points": false,
"renderer": "flot",
"seriesOverrides": [],
"spaceLength": 10,
"stack": true,
"steppedLine": false,
"targets": [
{
"expr": "sum(rate(container_cpu_usage_seconds_total{name=~\".+\"}[$interval])) by (name) * 100",
"hide": false,
"interval": "",
"intervalFactor": 2,
"legendFormat": "{{name}}",
"metric": "",
"refId": "F",
"step": 240
}
],
"thresholds": [],
"timeFrom": null,
"timeShift": null,
"title": "CPU Usage per Container",
"tooltip": {
"msResolution": true,
"shared": true,
"sort": 0,
"value_type": "individual"
},
"type": "graph",
"xaxis": {
"buckets": null,
"mode": "time",
"name": null,
"show": true,
"values": []
},
"yaxes": [
{
"format": "percent",
"label": "",
"logBase": 1,
"max": null,
"show": true
},
{
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": false
}
],
"yaxis": {
"align": false,
"alignLevel": null
}
},
{
"aliasColors": {},
"bars": false,
"dashLength": 10,
"dashes": false,
"datasource": "${DS_PROMETHEUS}",
"editable": true,
"error": false,
"fill": 3,
"grid": {},
"gridPos": {
"h": 7,
"w": 12,
"x": 12,
"y": 17
},
"id": 34,
"legend": {
"alignAsTable": true,
"avg": false,
"current": false,
"max": false,
"min": false,
"rightSide": true,
"show": true,
"total": false,
"values": false
},
"lines": true,
"linewidth": 2,
"links": [],
"nullPointMode": "null as zero",
"percentage": false,
"pointradius": 5,
"points": false,
"renderer": "flot",
"seriesOverrides": [],
"spaceLength": 10,
"stack": true,
"steppedLine": false,
"targets": [
{
"expr": "sum(container_memory_swap{name=~\".+\"}) by (name)",
"hide": false,
"intervalFactor": 2,
"legendFormat": "{{name}}",
"refId": "A",
"step": 240
},
{
"expr": "container_memory_usage_bytes{name=~\".+\"}",
"hide": true,
"intervalFactor": 2,
"legendFormat": "{{name}}",
"refId": "B",
"step": 240
}
],
"thresholds": [],
"timeFrom": null,
"timeShift": null,
"title": "Memory Swap per Container",
"tooltip": {
"msResolution": true,
"shared": true,
"sort": 0,
"value_type": "individual"
},
"type": "graph",
"xaxis": {
"buckets": null,
"mode": "time",
"name": null,
"show": true,
"values": []
},
"yaxes": [
{
"format": "bytes",
"label": "",
"logBase": 1,
"max": null,
"min": null,
"show": true
},
{
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
}
],
"yaxis": {
"align": false,
"alignLevel": null
}
},
{
"aliasColors": {},
"bars": false,
"dashLength": 10,
"dashes": false,
"datasource": "${DS_PROMETHEUS}",
"editable": true,
"error": false,
"fill": 3,
"grid": {},
"gridPos": {
"h": 7,
"w": 12,
"x": 0,
"y": 24
},
"id": 10,
"legend": {
"alignAsTable": true,
"avg": false,
"current": false,
"max": false,
"min": false,
"rightSide": true,
"show": true,
"total": false,
"values": false
},
"lines": true,
"linewidth": 2,
"links": [],
"nullPointMode": "null as zero",
"percentage": false,
"pointradius": 5,
"points": false,
"renderer": "flot",
"seriesOverrides": [],
"spaceLength": 10,
"stack": true,
"steppedLine": false,
"targets": [
{
"expr": "sum(container_memory_rss{name=~\".+\"}) by (name)",
"hide": false,
"intervalFactor": 2,
"legendFormat": "{{name}}",
"refId": "A",
"step": 240
},
{
"expr": "container_memory_usage_bytes{name=~\".+\"}",
"hide": true,
"intervalFactor": 2,
"legendFormat": "{{name}}",
"refId": "B",
"step": 240
}
],
"thresholds": [],
"timeFrom": null,
"timeShift": null,
"title": "Memory Usage per Container",
"tooltip": {
"msResolution": true,
"shared": true,
"sort": 0,
"value_type": "individual"
},
"type": "graph",
"xaxis": {
"buckets": null,
"mode": "time",
"name": null,
"show": true,
"values": []
},
"yaxes": [
{
"format": "bytes",
"label": "",
"logBase": 1,
"max": null,
"min": null,
"show": true
},
{
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
}
],
"yaxis": {
"align": false,
"alignLevel": null
}
},
{
"columns": [
{
"text": "Current",
"value": "current"
}
],
"datasource": "${DS_PROMETHEUS}",
"editable": true,
"error": false,
"fontSize": "100%",
"gridPos": {
"h": 10,
"w": 8,
"x": 16,
"y": 24
},
"id": 36,
"links": [],
"pageSize": null,
"scroll": true,
"showHeader": true,
"sort": {
"col": 0,
"desc": true
},
"styles": [
{
"colorMode": null,
"colors": [
"rgba(245, 54, 54, 0.9)",
"rgba(237, 129, 40, 0.89)",
"rgba(50, 172, 45, 0.97)"
],
"decimals": 2,
"pattern": "/.*/",
"thresholds": [
"10000000",
" 25000000"
],
"type": "number",
"unit": "decbytes"
}
],
"targets": [
{
"expr": "sum(container_spec_memory_limit_bytes{name=~\".+\"} - container_memory_usage_bytes{name=~\".+\"}) by (name) ",
"hide": true,
"intervalFactor": 2,
"legendFormat": "{{name}}",
"metric": "",
"refId": "A",
"step": 240
},
{
"expr": "sum(container_spec_memory_limit_bytes{name=~\".+\"}) by (name) ",
"hide": false,
"intervalFactor": 2,
"legendFormat": "{{name}}",
"refId": "B",
"step": 240
},
{
"expr": "container_memory_usage_bytes{name=~\".+\"}",
"hide": true,
"intervalFactor": 2,
"legendFormat": "{{name}}",
"refId": "C",
"step": 240
}
],
"title": "Limit memory",
"transform": "timeseries_aggregations",
"type": "table"
},
{
"columns": [
{
"text": "Current",
"value": "current"
}
],
"datasource": "${DS_PROMETHEUS}",
"editable": true,
"error": false,
"fontSize": "100%",
"gridPos": {
"h": 10,
"w": 8,
"x": 0,
"y": 31
},
"id": 37,
"links": [],
"pageSize": null,
"scroll": true,
"showHeader": true,
"sort": {
"col": 0,
"desc": true
},
"styles": [
{
"colorMode": null,
"colors": [
"rgba(245, 54, 54, 0.9)",
"rgba(237, 129, 40, 0.89)",
"rgba(50, 172, 45, 0.97)"
],
"decimals": 2,
"pattern": "/.*/",
"thresholds": [
"10000000",
" 25000000"
],
"type": "number",
"unit": "decbytes"
}
],
"targets": [
{
"expr": "sum(container_spec_memory_limit_bytes{name=~\".+\"} - container_memory_usage_bytes{name=~\".+\"}) by (name) ",
"hide": true,
"intervalFactor": 2,
"legendFormat": "{{name}}",
"metric": "",
"refId": "A",
"step": 240
},
{
"expr": "sum(container_spec_memory_limit_bytes{name=~\".+\"}) by (name) ",
"hide": true,
"intervalFactor": 2,
"legendFormat": "{{name}}",
"refId": "B",
"step": 240
},
{
"expr": "container_memory_usage_bytes{name=~\".+\"}",
"hide": false,
"intervalFactor": 2,
"legendFormat": "{{name}}",
"refId": "C",
"step": 240
}
],
"title": "Usage memory",
"transform": "timeseries_aggregations",
"type": "table"
},
{
"columns": [
{
"text": "Current",
"value": "current"
}
],
"datasource": "${DS_PROMETHEUS}",
"editable": true,
"error": false,
"fontSize": "100%",
"gridPos": {
"h": 10,
"w": 8,
"x": 8,
"y": 31
},
"id": 35,
"links": [],
"pageSize": null,
"scroll": true,
"showHeader": true,
"sort": {
"col": 1,
"desc": true
},
"styles": [
{
"colorMode": "cell",
"colors": [
"rgba(50, 172, 45, 0.97)",
"rgba(237, 129, 40, 0.89)",
"rgba(245, 54, 54, 0.9)"
],
"decimals": 2,
"pattern": "/.*/",
"thresholds": [
"80",
"90"
],
"type": "number",
"unit": "percent"
}
],
"targets": [
{
"expr": "sum(100 - ((container_spec_memory_limit_bytes{name=~\".+\"} - container_memory_usage_bytes{name=~\".+\"}) * 100 / container_spec_memory_limit_bytes{name=~\".+\"}) ) by (name) ",
"intervalFactor": 2,
"legendFormat": "{{name}}",
"metric": "",
"refId": "A",
"step": 240
},
{
"expr": "sum(container_spec_memory_limit_bytes{name=~\".+\"}) by (name) ",
"hide": true,
"intervalFactor": 2,
"legendFormat": "{{name}}",
"refId": "B",
"step": 240
},
{
"expr": "container_memory_usage_bytes{name=~\".+\"}",
"hide": true,
"intervalFactor": 2,
"legendFormat": "{{name}}",
"refId": "C",
"step": 240
}
],
"title": "Remaining memory",
"transform": "timeseries_aggregations",
"type": "table"
}
],
"refresh": "5m",
"schemaVersion": 16,
"style": "dark",
"tags": [],
"templating": {
"list": [
{
"allValue": ".+",
"current": {},
"datasource": "${DS_PROMETHEUS}",
"hide": 0,
"includeAll": true,
"label": "Container Group",
"multi": true,
"name": "containergroup",
"options": [],
"query": "label_values(container_group)",
"refresh": 1,
"regex": "",
"sort": 0,
"tagValuesQuery": null,
"tags": [],
"tagsQuery": null,
"type": "query",
"useTags": false
},
{
"auto": true,
"auto_count": 50,
"auto_min": "50s",
"current": {
"text": "auto",
"value": "$__auto_interval_interval"
},
"datasource": null,
"hide": 0,
"includeAll": false,
"label": "Interval",
"multi": false,
"name": "interval",
"options": [
{
"selected": true,
"text": "auto",
"value": "$__auto_interval_interval"
},
{
"selected": false,
"text": "30s",
"value": "30s"
},
{
"selected": false,
"text": "1m",
"value": "1m"
},
{
"selected": false,
"text": "2m",
"value": "2m"
},
{
"selected": false,
"text": "3m",
"value": "3m"
},
{
"selected": false,
"text": "5m",
"value": "5m"
},
{
"selected": false,
"text": "7m",
"value": "7m"
},
{
"selected": false,
"text": "10m",
"value": "10m"
},
{
"selected": false,
"text": "30m",
"value": "30m"
},
{
"selected": false,
"text": "1h",
"value": "1h"
},
{
"selected": false,
"text": "6h",
"value": "6h"
},
{
"selected": false,
"text": "12h",
"value": "12h"
},
{
"selected": false,
"text": "1d",
"value": "1d"
},
{
"selected": false,
"text": "7d",
"value": "7d"
},
{
"selected": false,
"text": "14d",
"value": "14d"
},
{
"selected": false,
"text": "30d",
"value": "30d"
}
],
"query": "30s,1m,2m,3m,5m,7m,10m,30m,1h,6h,12h,1d,7d,14d,30d",
"refresh": 2,
"type": "interval"
},
{
"allValue": null,
"current": {},
"datasource": "${DS_PROMETHEUS}",
"hide": 0,
"includeAll": false,
"label": "Node",
"multi": true,
"name": "server",
"options": [],
"query": "label_values(node_boot_time, instance)",
"refresh": 1,
"regex": "/([^:]+):.*/",
"sort": 0,
"tagValuesQuery": null,
"tags": [],
"tagsQuery": null,
"type": "query",
"useTags": false
}
]
},
"time": {
"from": "now-24h",
"to": "now"
},
"timepicker": {
"refresh_intervals": [
"5s",
"10s",
"30s",
"1m",
"5m",
"15m",
"30m",
"1h",
"2h",
"1d"
],
"time_options": [
"5m",
"15m",
"1h",
"6h",
"12h",
"24h",
"2d",
"7d",
"30d"
]
},
"timezone": "browser",
"title": "Docker and system monitoring",
"uid": "fTo9rH2ik",
"version": 12
}
0%