Feedback

Chat Icon

Cloud-Native Microservices With Kubernetes - 2nd Edition

A Comprehensive Guide to Building, Scaling, Deploying, Observing, and Managing Highly-Available Microservices in Kubernetes

Setting Up the Foundation
10%

Creating the Kubernetes Cluster

We will now create a Kubernetes cluster using the same approach. First, we export the necessary variables.

# The name of the Kubernetes cluster
export DIGITALOCEAN_K8S_CLUSTER_NAME="learning-cluster"

# Choose an available Kubernetes version
# Use `doctl k8s options versions` to get the latest available versions
# or check here: https://slugs.do-api.dev/
export DIGITALOCEAN_K8S_CLUSTER_VERSION="1.32.5-do.5"

# Choose the node size for the cluster
# Use `doctl compute size list` to get the available sizes
# or check here: https://slugs.do-api.dev/
export DIGITALOCEAN_K8S_CLUSTER_NODE_SIZE="s-2vcpu-4gb"

# Choose the number of nodes for the cluster
# 1 is enough for learning purposes
export DIGITALOCEAN_K8S_CLUSTER_NODE_COUNT="1"

Create a Terraform variable file:

cat << EOF > $PROJECT_NAME/kubernetes-terraform/variables.tf
variable "region" {
  default = "${DIGITALOCEAN_REGION}"
}
variable "vpc_uuid" {
  default = "${DIGITALOCEAN_VPC_UUID}"
}
variable "project_name" {
  default = "${DIGITALOCEAN_PROJECT_NAME}"
}
variable "k8s_cluster_name" {
  default = "${DIGITALOCEAN_K8S_CLUSTER_NAME}"
}
variable "k8s_cluster_version" {
  default = "${DIGITALOCEAN_K8S_CLUSTER_VERSION}"
}
variable "k8s_cluster_node_size" {
  default = "${DIGITALOCEAN_K8S_CLUSTER_NODE_SIZE}"
}
variable "k8s_cluster_node_count" {
  default = ${DIGITALOCEAN_K8S_CLUSTER_NODE_COUNT}
}
EOF

Let's move on to creating the Terraform script for the Kubernetes cluster.

cat << EOF > $PROJECT_NAME/kubernetes-terraform/main.tf
terraform {
  required_providers {
    digitalocean = {
      source = "digitalocean/digitalocean"
      version = "~> 2.0"
    }
  }
}

# Get the existing project (created by workspace terraform)
data "digitalocean_project" "project" {
  name = var.project_name
}

# Resource: Create the Kubernetes cluster
resource "digitalocean_kubernetes_cluster" "learning_cluster" {
  name     = var.k8s_cluster_name
  region   = var.region
  version  = var.k8s_cluster_version
  vpc_uuid = var.vpc_uuid

  node_pool {
    name       = "default"
    size       = var.k8s_cluster_node_size
    node_count = var.k8s_cluster_node_count
  }
}

# Resource: Add the cluster to the project
resource "digitalocean_project_resources" "project_resources" {
  project   = data.digitalocean_project.project.id
  resources = [digitalocean_kubernetes_cluster.learning_cluster.urn]
}

# Output: Cluster information
output "cluster_id" {
  value = digitalocean_kubernetes_cluster.learning_cluster.id
}

Cloud-Native Microservices With Kubernetes - 2nd Edition

A Comprehensive Guide to Building, Scaling, Deploying, Observing, and Managing Highly-Available Microservices in Kubernetes

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