Feedback

Chat Icon

DevSecOps in Practice

A Hands-On Guide to Operationalizing DevSecOps at Scale

Setting Up the Foundation: The Infrastructure
14%

The Kubernetes Cluster (Infrastructure as Code)

To create the cluster using Terraform, start by creating a new directory for the HCL files (HashiCorp Configuration Language) that we are going to use:

mkdir -p $HOME/RestQR/deploy/terraform

Create a new file called main.tf and add the following content:

cat < $HOME/RestQR/deploy/terraform/main.tf
terraform {
  required_providers {
    digitalocean = {
      source  = "digitalocean/digitalocean"
      version = "~> 2.0"
    }
  }
}

provider "digitalocean" {
  token = var.do_token
}

resource "digitalocean_kubernetes_cluster" "myclusters" {
  for_each = { for name in var.names : name => name }

  name    = each.value
  region  = var.region
  version = var.k8s_version

  node_pool {
    name       = "default"
    size       = var.node_size
    node_count = var.node_count
    auto_scale = true
    min_nodes  = 1
    max_nodes  = 3
  }
}

output "kubeconfigs" {
  description = "Kubeconfig files for the Kubernetes clusters"
  value = {
    for name, cluster in digitalocean_kubernetes_cluster.myclusters :
    name => cluster.kube_config[0].raw_config
  }
  sensitive = true
}
EOF

Export some variables that we are going to use later. You can use do-api.dev to find the available regions, sizes, Kubernetes versions, and more:

# Go to your DigitalOcean account
# and create a new Personal Access Token
# if it's not already created.
export DIGITALOCEAN_TOKEN=""

# The name of the cluster
# Keep it simple and lowercase
export CLUSTER_NAME="restqr"

# The version of Kubernetes
# Versions slugs may change, check the available versions on do-api.dev
export K8S_VERSION="1.30.10-do.0"

# Choose the region where you want to create the cluster
export REGION="fra1"

# The number of nodes in the cluster
export NODE_SIZE="s-2vcpu-4gb"

# The size of the nodes.

DevSecOps in Practice

A Hands-On Guide to Operationalizing DevSecOps at Scale

Enroll now to unlock all content and receive all future updates for free.