Setting Up the Foundation
10%
Creating the Workspace Server
Create a directory where we will store some initial files.
# First of all, choose a directory where you want to store the files we will use.
PROJECT_NAME="CloudNativeMicroservices"
# Create the folder structure
mkdir -p $PROJECT_NAME
# Create subdirectories for Terraform files
mkdir -p $PROJECT_NAME/workspace-terraform
mkdir -p $PROJECT_NAME/kubernetes-terraform
Generate an SSH key pair that we will use to access the server:
# Create a unique name for the SSH key to avoid conflicts
# with other keys in your ~/.ssh directory
# Make sure you are not overwriting an existing key
SSH_UNIQUE_NAME="$HOME/.ssh/$PROJECT_NAME"
# generate the keys (public and private)
# This will overwrite the keys if they already exist
ssh-keygen -t rsa \
-b 4096 \
-C "$PROJECT_NAME" \
-f $SSH_UNIQUE_NAME -N "" \
<<< y
# add the key to the ssh-agent
ssh-add $SSH_UNIQUE_NAME
Export the DigitalOcean token as well as other variables that we will also use later when calling Terraform. Make sure to change the values marked with [CHANGE_ME].
# Export the DigitalOcean token.
# Get one here: https://cloud.digitalocean.com/account/api/tokens
export DIGITALOCEAN_TOKEN="[CHANGE_ME]"
# Choose the best region for you.
# More options here: https://www.digitalocean.com/docs/platform/availability-matrix/
export DIGITALOCEAN_REGION="fra1"
# I recommend using Ubuntu 24.04 for this project.
export DIGITALOCEAN_IMAGE="ubuntu-24-04-x64"
# SSH key variables
export DIGITALOCEAN_SSH_KEY_NAME="$SSH_UNIQUE_NAME"
export DIGITALOCEAN_SSH_PUBLIC_KEY_PATH="$SSH_UNIQUE_NAME.pub"
export DIGITALOCEAN_SSH_PRIVATE_KEY_PATH="$SSH_UNIQUE_NAME"
# VPC variables.
# You can use the default VPC or create a new one.
# Use doctl to get the VPC UUID (`doctl vpcs list | grep $DIGITALOCEAN_REGION`)
# Or by using your web console: https://cloud.digitalocean.com/networking/vpc
export DIGITALOCEAN_VPC_UUID="[CHANGE_ME]"
export DIGITALOCEAN_PROJECT_NAME="$PROJECT_NAME"
# Workload cluster variables
export DIGITALOCEAN_WORKSPACE_VM_NAME="workspace"
export DIGITALOCEAN_WORKSPACE_VM_SIZE="s-2vcpu-4gb"
Create a Terraform file that will be used to store the variables used to create the workspace server.
# Create a Terraform variable file.
cat << EOF > $PROJECT_NAME/workspace-terraform/variables.tf
variable "region" {
default = "${DIGITALOCEAN_REGION}"
}
variable "image" {
default = "${DIGITALOCEAN_IMAGE}"
}
variable "vpc_uuid" {
default = "${DIGITALOCEAN_VPC_UUID}"
}
variable "workspace_vm_size" {
default = "${DIGITALOCEAN_WORKSPACE_VM_SIZE}"
}
variable "workspace_vm_name" {
default = "${DIGITALOCEAN_WORKSPACE_VM_NAME}"
}
variable "project_name" {
default = "${DIGITALOCEAN_PROJECT_NAME}"
}
variable "ssh_key_name" {
default = "${DIGITALOCEAN_SSH_KEY_NAME}"
}
variable "ssh_public_key_path" {
default = "${DIGITALOCEAN_SSH_PUBLIC_KEY_PATH}"
}
variable "ssh_private_key_path" {
default = "${DIGITALOCEAN_SSH_PRIVATE_KEY_PATH}"
}
EOF
Let's move on to creating the Terraform script that will launch our infrastructure.
Cloud-Native Microservices With Kubernetes - 2nd Edition
A Comprehensive Guide to Building, Scaling, Deploying, Observing, and Managing Highly-Available Microservices in KubernetesEnroll now to unlock all content and receive all future updates for free.
