Skip to content

Latest commit

 

History

History
247 lines (177 loc) · 4.7 KB

File metadata and controls

247 lines (177 loc) · 4.7 KB

Setting Up the Foundation: The Infrastructure

Context and Prerequisites

# Install kubectl
curl -LO "https://dl.k8s.io/release/v1.32.2/bin/linux/amd64/kubectl"
install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl

# Enable bash completion
echo 'source <(kubectl completion bash)' >>~/.bashrc

# Create an alias for kubectl (k is easier to type than kubectl)
echo 'alias k=kubectl' >>~/.bashrc
echo 'complete -o default -F __start_kubectl k' >>~/.bashrc

# Load the new configuration
source ~/.bashrc
curl -fsSL https://get.docker.com | sh
apt install jq unzip -y
# Download the Helm installation script and make it executable
curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
chmod 700 get_helm.sh

# Run the installation script and remove it after installation
./get_helm.sh
rm get_helm.sh
# Download Terraform archive
curl https://releases.hashicorp.com/terraform/1.11.1/terraform_1.11.1_linux_amd64.zip -o terraform.zip

# Unzip the archive
unzip terraform.zip

# Move the binary to /usr/local/bin
mv terraform /usr/local/bin

# Remove the archive
rm terraform.zip

How Immutable Infrastructure Improves Security

Eliminating Configuration Drift

resource "aws_instance" "web" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
  user_data     = file("init.sh")
}

Reducing the Attack Surface for Persistent Threats

Providing Faster Response to Vulnerabilities

resource "cluster" "example" {
  name    = "example"
  version = "1.21.3"
}

Enforcing Security Compliance Automatically

Preventing Unauthorized Modifications

Secure Software Updates and Rollbacks

The Kubernetes Cluster (Infrastructure as Code)

mkdir -p $HOME/RestQR/deploy/terraform
cat <<EOF > $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
# Go to your DigitalOcean account 
# and create a new Personal Access Token 
# if it's not already created.
export DIGITALOCEAN_TOKEN="<YOUR_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.
# You may want to increase the size later depending on the workload.
export NODE_COUNT=1
# This file contains sensitive information
# and **should be ignored by git**
cat <<EOF > $HOME/RestQR/deploy/terraform/variables.tf
variable "do_token" {
  description = "DigitalOcean API Token"
  type        = string
  sensitive   = true
  default     = "$DIGITALOCEAN_TOKEN"
}

variable "names" {
  description = "List of Kubernetes cluster names"
  type        = list(string)
  default = [
    "$CLUSTER_NAME"
  ]
}

variable "region" {
  description = "Region for the Kubernetes clusters"
  type        = string
  default     = "$REGION"
}

variable "k8s_version" {
  description = "Kubernetes version for the cluster"
  type        = string
  default     = "$K8S_VERSION"
}

variable "node_size" {
  description = "Size of the Kubernetes nodes"
  type        = string
  default     = "$NODE_SIZE"
}

variable "node_count" {
  description = "Number of nodes per cluster"
  type        = number
  default     = $NODE_COUNT
}
EOF
cd $HOME/RestQR/deploy/terraform
terraform init
terraform plan -out=tfplan.binary
terraform apply tfplan.binary
# Create the kubeconfig directory
mkdir -p $HOME/.kube

# Get the kubeconfig file
# CLUSTER_NAME is a variable that we exported earlier
# Verify it by running `echo $CLUSTER_NAME`
terraform output -json kubeconfigs | \
  jq -r ".\"$CLUSTER_NAME\"" > \
  $HOME/.kube/config

# Verify the connection
kubectl get nodes