diff --git a/.gitignore b/.gitignore index f413516..9fc74c6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,40 @@ +# Local .terraform directories **/.terraform* -**/terraform* + +# .tfstate files +*.tfstate +*.tfstate.* + +# Crash log files +crash.log + +# Exclude all .tfvars files, which are likely to contain sentitive data, such as +# password, private keys, and other secrets. These should not be part of version +# control as they are data points which are potentially sensitive and subject +# to change depending on the environment. +# +*.tfvars + +# Ignore override files as they are usually used to override resources locally and so +# are not checked in +override.tf +override.tf.json +*_override.tf +*_override.tf.json + +# Include override files you do wish to add to version control using negated pattern +# +# !example_override.tf + +# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan +# example: *tfplan* + +# Ignore CLI configuration files +.terraformrc +terraform.rc + +# IntelliJ project files +.idea +*.iml +out +gen \ No newline at end of file diff --git a/modules/rabbitmq-cluster/README.md b/modules/rabbitmq-cluster/README.md new file mode 100644 index 0000000..9d93e2f --- /dev/null +++ b/modules/rabbitmq-cluster/README.md @@ -0,0 +1,119 @@ +# RabbitMQ @ Kubernetes + +Deploy a RabbitMQ cluster on kubernetes using the RabbitmqOperator. + +## Notes + +* Use the label `spotinst.io/restrict-scale-down` to prevent right sizing. + +## Implementation + +```hcl +# +# Use the s3 bucket for state management. +# +terraform { + + backend "s3" {} + +} + +# +# Get kubernetes cluster info. +# +data "aws_eks_cluster" "cluster" { + + # + # mlfabric k8 cluster specifically for github action runners. + # + name = var.cluster_name + +} + +# +# Retrieve authentication for kubernetes from aws. +# +data "aws_eks_cluster_auth" "cluster" { + + # + # mlfabric k8 cluster specifically for github action runners. + # + name = var.cluster_name + +} + +# +# Install the rabbitmq cluster object. +# +variable "aws_profile" {} +variable "aws_region" {} + +# +# Retrieve authentication for kubernetes from aws. +# +provider "aws" { + + profile = var.aws_profile + region = var.aws_region + +} + +# +# Get kubernetes cluster info. +# +data "aws_eks_cluster" "cluster" { + + name = var.cluster_name + +} + +# +# Retrieve authentication for kubernetes from aws. +# +data "aws_eks_cluster_auth" "cluster" { + + name = var.cluster_name + +} + +provider "kubernetes" { + host = data.aws_eks_cluster.cluster.endpoint + token = data.aws_eks_cluster_auth.cluster.token + cluster_ca_certificate = base64decode(data.aws_eks_cluster.cluster.certificate_authority[ 0 ].data) +} + +# +# Install the rabbitmq cluster object. +# +module "rabbitmq-nontls" { + + source = "app.terraform.io/MAA-ML-DEVOPS/rabbitmq-cluster/kubernetes" + version = "2.0.7" + + namespace = "default" + name = "rabbitmq" + internal_cidrs = "8.0.0.224/32" + limit_cpu = "7" + limit_memory = "15Gi" + replicas = 3 + default_username = "rabbitmq" + default_password = "supersecret" + + # + # Restrict rabbitmq to running on nodes with this selector. + # + role = "infra" + + labels = { + + # + # Prevent right sizing of the workload which causes rabbitmq + # to be rescheduled if downsizing occurs. + # + "spotinst.io/restrict-scale-down" = "true" + + } + +} + +``` diff --git a/modules/rabbitmq-cluster/main.tf b/modules/rabbitmq-cluster/main.tf new file mode 100644 index 0000000..81cdc06 --- /dev/null +++ b/modules/rabbitmq-cluster/main.tf @@ -0,0 +1,149 @@ +resource "kubernetes_manifest" "cluster" { + + manifest = { + + "apiVersion" = "rabbitmq.com/v1beta1" + "kind" = "RabbitmqCluster" + + "metadata" = { + + "namespace" = var.namespace + "name" = var.name + "labels" = var.labels + + } + + "spec" = { + + replicas = var.replicas + image = var.image + + service = { + + type = "LoadBalancer" + + annotations = { + + "service.beta.kubernetes.io/aws-load-balancer-type" = "nlb" + "service.beta.kubernetes.io/aws-load-balancer-internal" = var.internal_cidrs + + } + + } + + affinity = { + + nodeAffinity = { + + requiredDuringSchedulingIgnoredDuringExecution = { + + nodeSelectorTerms = [ + + { + + matchExpressions = [ + + { + + key = "role" + operator = "In" + values = [ var.role ] + + } + + ] + + } + + ] + + } + + } + + } + + override = { + + statefulSet = { + + spec = { + + template = { + + metadata = { + + labels = var.labels + + } + + } + + } + + } + + } + + persistence = { + + storageClassName = "gp2" + storage = "${ var.storage_gb }Gi" + + } + + resources = { + + requests = { + + cpu = var.limit_cpu + memory = var.limit_memory + + } + + limits = { + + cpu = var.limit_cpu + memory = var.limit_memory + + } + + } + + rabbitmq = { + + additionalPlugins = var.additional_plugins + additionalConfig = <