Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/existing cluster examples #234

Merged
merged 4 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions examples/aks/aks_cluster_existing/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Existing AKS cluster and CAST AI example with CAST AI Autoscaler policies and additional Node Configurations
Following example shows how to onboard existing AKS cluster to CAST AI, configure [Autoscaler policies](https://docs.cast.ai/reference/policiesapi_upsertclusterpolicies) and additional [Node Configurations](https://docs.cast.ai/docs/node-configuration/).

Example configuration should be analysed in the following order:
1. Create CAST AI related resources to connect AKS cluster to CAST AI, configure Autoscaler and Node Configurations - `castai.tf`

# Usage
1. Rename `tf.vars.example` to `tf.vars`
2. Update `tf.vars` file with your cluster name, cluster_rg, cluster region, subnets and CAST AI API token.
3. Initialize Terraform. Under example root folder run:
```
terraform init
```
4. Run Terraform apply:
```
terraform apply -var-file=tf.vars
```
5. To destroy resources created by this example:
```
terraform destroy -var-file=tf.vars
```
133 changes: 133 additions & 0 deletions examples/aks/aks_cluster_existing/castai.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# Configure Data sources and providers required for CAST AI connection.
data "azurerm_subscription" "current" {}

data "azurerm_kubernetes_cluster" "example" {
name = var.cluster_name
resource_group_name = var.cluster_rg
}

provider "castai" {
api_url = var.castai_api_url
api_token = var.castai_api_token
}

provider "helm" {
kubernetes {
host = data.azurerm_kubernetes_cluster.example.kube_config.0.host
client_certificate = base64decode(data.azurerm_kubernetes_cluster.example.kube_config.0.client_certificate)
client_key = base64decode(data.azurerm_kubernetes_cluster.example.kube_config.0.client_key)
cluster_ca_certificate = base64decode(data.azurerm_kubernetes_cluster.example.kube_config.0.cluster_ca_certificate)
}
}

# Configure AKS cluster connection to CAST AI using CAST AI aks-cluster module.
module "castai-aks-cluster" {
source = "castai/aks/castai"

api_url = var.castai_api_url
castai_api_token = var.castai_api_token
wait_for_cluster_ready = true

aks_cluster_name = var.cluster_name
aks_cluster_region = var.cluster_region
node_resource_group = data.azurerm_kubernetes_cluster.example.node_resource_group
resource_group = data.azurerm_kubernetes_cluster.example.resource_group_name
delete_nodes_on_disconnect = var.delete_nodes_on_disconnect

subscription_id = data.azurerm_subscription.current.subscription_id
tenant_id = data.azurerm_subscription.current.tenant_id


default_node_configuration = module.castai-aks-cluster.castai_node_configurations["default"]

node_configurations = {
default = {
disk_cpu_ratio = 0
subnets = var.subnets
tags = var.tags
max_pods_per_node = 60
}
}

node_templates = {
default_by_castai = {
name = "default-by-castai"
configuration_id = module.castai-aks-cluster.castai_node_configurations["default"]
is_default = true
should_taint = false

constraints = {
on_demand = true
min_cpu = 8
max_cpu = 96
max_memory = 786432
instance_families = {
exclude = ["standard_FSv2", "standard_Dv4"]
}
}
}
}

// Configure Autoscaler policies as per API specification https://api.cast.ai/v1/spec/#/PoliciesAPI/PoliciesAPIUpsertClusterPolicies.
// Here:
// - unschedulablePods - Unscheduled pods policy
// - nodeDownscaler - Node deletion policy
autoscaler_policies_json = <<-EOT
{
"enabled": true,
"unschedulablePods": {
"enabled": true
},
"nodeDownscaler": {
"enabled": true,
"emptyNodes": {
"enabled": true
},
"evictor": {
"aggressiveMode": false,
"cycleInterval": "5m10s",
"dryRun": false,
"enabled": true,
"nodeGracePeriodMinutes": 5,
"scopedMode": false
}
},
"clusterLimits": {
"cpu": {
"maxCores": 100,
"minCores": 1
},
"enabled": false
}
}
EOT

}


resource "castai_rebalancing_schedule" "default" {
name = "rebalance nodes at every 30th minute"
schedule {
cron = "CRON_TZ=America/Argentina/Buenos_Aires */30 * * * *"
}
trigger_conditions {
savings_percentage = 20
}
launch_configuration {
# only consider instances older than 5 minutes
node_ttl_seconds = 300
num_targeted_nodes = 3
rebalancing_min_nodes = 2
keep_drain_timeout_nodes = false
execution_conditions {
enabled = true
achieved_savings_percentage = 10
}
}
}

resource "castai_rebalancing_job" "default" {
cluster_id = module.castai-aks-cluster.cluster_id
rebalancing_schedule_id = castai_rebalancing_schedule.default.id
enabled = true
}
8 changes: 8 additions & 0 deletions examples/aks/aks_cluster_existing/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Following providers required by AKS and Vnet resources.
provider "azurerm" {
features {}
}

provider "azuread" {
tenant_id = data.azurerm_subscription.current.tenant_id
}
5 changes: 5 additions & 0 deletions examples/aks/aks_cluster_existing/tf.vars.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cluster_name = ""
cluster_rg = ""
cluster_region = ""
castai_api_token = ""
subnets = [""]
44 changes: 44 additions & 0 deletions examples/aks/aks_cluster_existing/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# AKS cluster variables.
variable "cluster_name" {
type = string
description = "Name of the AKS cluster, resources will be created for."
}

variable "cluster_rg" {
type = string
description = "Resource Group of the AKS cluster, resources will be created for."
}

variable "cluster_region" {
type = string
description = "Region of the AKS cluster, resources will be created for."
}

variable "castai_api_url" {
type = string
description = "URL of alternative CAST AI API to be used during development or testing"
default = "https://api.cast.ai"
}

# Variables required for connecting EKS cluster to CAST AI
variable "castai_api_token" {
type = string
description = "CAST AI API token created in console.cast.ai API Access keys section"
}

variable "delete_nodes_on_disconnect" {
type = bool
description = "Optional parameter, if set to true - CAST AI provisioned nodes will be deleted from cloud on cluster disconnection. For production use it is recommended to set it to false."
default = true
}

variable "tags" {
type = map(any)
description = "Optional tags for new cluster nodes. This parameter applies only to new nodes - tags for old nodes are not reconciled."
default = {}
}

variable "subnets" {
type = list(string)
description = "Cluster subnets"
}
14 changes: 14 additions & 0 deletions examples/aks/aks_cluster_existing/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
}
azuread = {
source = "hashicorp/azuread"
}
castai = {
source = "castai/castai"
}
}
required_version = ">= 0.13"
}
29 changes: 29 additions & 0 deletions examples/eks/eks_cluster_existing/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
## Existing EKS cluster and CAST AI example with CAST AI Autoscaler policies and additional Node Configurations for

Following example shows how to onboard existing EKS cluster to CAST AI, configure [Autoscaler policies](https://docs.cast.ai/reference/policiesapi_upsertclusterpolicies) and additional [Node Configurations](https://docs.cast.ai/docs/node-configuration/).

IAM policies required to connect the cluster to CAST AI in the example are created by [castai/eks-role-iam/castai module](https://github.com/castai/terraform-castai-eks-role-iam).

Example configuration should be analysed in the following order:
1. Creates IAM and other CAST AI related resources to connect EKS cluster to CAST AI, configure Autoscaler and Node Configurations - `castai.tf`

# Usage
1. Rename `tf.vars.example` to `tf.vars`
2. Update `tf.vars` file with your cluster name, cluster region, vpc_id, cluster_security_group_id, node_security_group_id, subnets and CAST AI API token.
3. Initialize Terraform. Under example root folder run:
```
terraform init
```
4. Run Terraform apply:
```
terraform apply -var-file=tf.vars
```
5. To destroy resources created by this example:
```
terraform destroy -var-file=tf.vars
```

> **Note**
>
> If you are onboarding existing cluster to CAST AI you need to also update [aws-auth](https://docs.aws.amazon.com/eks/latest/userguide/add-user-role.html) configmap. In the configmap instance profile
> used by CAST AI has to be present. Example of entry can be found [here](https://github.com/castai/terraform-provider-castai/blob/157babd57b0977f499eb162e9bee27bee51d292a/examples/eks/eks_cluster_autoscaler_polices/eks.tf#L28-L38).
Loading