Skip to content

Commit

Permalink
Set the infrastructure for mr_persistence up
Browse files Browse the repository at this point in the history
  • Loading branch information
vladbat00 committed Jan 6, 2022
1 parent 080aaba commit f93d270
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 11 deletions.
28 changes: 28 additions & 0 deletions k8s/agones/cluster_role.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
resource "kubernetes_cluster_role" "server_role" {
metadata {
name = "server-role"
}

rule {
api_groups = [""]
resources = ["deployments"]
verbs = ["get", "list"]
}
}

resource "kubernetes_cluster_role_binding" "server_role_binding" {
metadata {
name = "server-role-binding"
}
role_ref {
api_group = "rbac.authorization.k8s.io"
kind = "ClusterRole"
name = "server-role"
}
subject {
kind = "ServiceAccount"
name = "default"
}

depends_on = [kubernetes_cluster_role.server_role]
}
34 changes: 26 additions & 8 deletions k8s/eks_cluster/module.tf
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,13 @@ module "eks" {
worker_groups_launch_template = [
{
name = "default"
override_instance_types = ["t3a.micro", "t3.micro", "t2.micro"]
asg_desired_capacity = 4
asg_min_size = 4
asg_max_size = 4
override_instance_types = ["t3a.small", "t3.small", "t2.small"]
asg_desired_capacity = 3
asg_min_size = 3
asg_max_size = 3
additional_security_group_ids = [aws_security_group.worker_group_mgmt_one.id]
public_ip = true
root_volume_size = 5

kubelet_extra_args = "--node-labels=node.kubernetes.io/lifecycle=`curl -s http://169.254.169.254/latest/meta-data/instance-life-cycle`"
},
Expand All @@ -147,6 +148,7 @@ module "eks" {
asg_max_size = 3
additional_security_group_ids = [aws_security_group.game_server_worker_group.id]
public_ip = true
root_volume_size = 5

tags = [
{
Expand All @@ -171,17 +173,33 @@ module "eks" {
// Node Pools with taints for metrics and system
{
name = "agones-system"
instance_type = "t3a.small"
instance_type = "t3a.micro"
asg_desired_capacity = 1
kubelet_extra_args = "--node-labels=agones.dev/agones-system=true,node.kubernetes.io/lifecycle=`curl -s http://169.254.169.254/latest/meta-data/instance-life-cycle` --register-with-taints=agones.dev/agones-system=true:NoExecute"
public_ip = true
root_volume_size = 5

kubelet_extra_args = "--node-labels=agones.dev/agones-system=true,node.kubernetes.io/lifecycle=`curl -s http://169.254.169.254/latest/meta-data/instance-life-cycle` --register-with-taints=agones.dev/agones-system=true:NoExecute"
},
{
name = "agones-metrics"
instance_type = "t3a.small"
instance_type = "t3a.micro"
asg_desired_capacity = 1
kubelet_extra_args = "--node-labels=agones.dev/agones-metrics=true,node.kubernetes.io/lifecycle=`curl -s http://169.254.169.254/latest/meta-data/instance-life-cycle` --register-with-taints=agones.dev/agones-metrics=true:NoExecute"
public_ip = true
root_volume_size = 5

kubelet_extra_args = "--node-labels=agones.dev/agones-metrics=true,node.kubernetes.io/lifecycle=`curl -s http://169.254.169.254/latest/meta-data/instance-life-cycle` --register-with-taints=agones.dev/agones-metrics=true:NoExecute"
}
]
}

output "vpc_id" {
value = module.vpc.vpc_id
}

output "vpc_public_subnets" {
value = module.vpc.public_subnets
}

output "worker_group_mgmt_one_sg_id" {
value = aws_security_group.worker_group_mgmt_one.id
}
65 changes: 65 additions & 0 deletions k8s/persistence/db.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
variable "persistence_db_password" {
type = string
sensitive = true
}

variable "vpc_id" {
type = string
}

variable "vpc_public_subnets" {
type = set(string)
}

variable "worker_group_mgmt_one_sg_id" {
type = string
}


resource "aws_db_subnet_group" "persistence" {
name = "persistence"
subnet_ids = var.vpc_public_subnets

tags = {
Name = "mr-persistence"
}
}

resource "aws_security_group" "rds" {
name_prefix = "persistence_db"
vpc_id = var.vpc_id

ingress {
from_port = 5432
to_port = 5432
protocol = "tcp"

security_groups = [var.worker_group_mgmt_one_sg_id]
}
}

resource "aws_db_parameter_group" "persistence" {
name = "persistence"
family = "postgres13"

parameter {
name = "log_connections"
value = "1"
}
}

resource "aws_db_instance" "persistence" {
allocated_storage = 5
engine = "postgres"
engine_version = "13.4"
instance_class = "db.t4g.micro"
name = "mr_persistence_production"
username = "postgres"
password = var.persistence_db_password
db_subnet_group_name = aws_db_subnet_group.persistence.name
vpc_security_group_ids = [aws_security_group.rds.id]
parameter_group_name = aws_db_parameter_group.persistence.name
skip_final_snapshot = true
multi_az = true
apply_immediately = true
}
51 changes: 51 additions & 0 deletions k8s/persistence/module.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
resource "kubernetes_deployment" "mr_persistence" {
metadata {
name = "mr-persistence"
}

spec {
selector {
match_labels = {
service = "mr-persistence"
}
}
replicas = 1
template {
metadata {
labels = {
app = "muddle-run"
service = "mr-persistence"
}
}
spec {
termination_grace_period_seconds = 5
container {
name = "mr-persistence"
image = "mvlabat/mr_persistence:latest"
image_pull_policy = "Always"
port {
name = "persistence-pub"
container_port = 8082
}
port {
name = "persistence"
container_port = 8083
}
env {
name = "SENTRY_DSN"
value_from {
secret_key_ref {
name = "sentry-dsn"
key = "persistence"
}
}
}
env {
name = "DATABASE_URL"
value = "postgres://postgres:${var.persistence_db_password}@${aws_db_instance.persistence.endpoint}"
}
}
}
}
}
}
8 changes: 8 additions & 0 deletions k8s/service/module.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ resource "kubernetes_service" "muddle_run_service" {
name = "ws"
port = 8080
}
port {
name = "persistence-pub"
port = 8082
}
}
}

Expand All @@ -46,5 +50,9 @@ resource "kubernetes_service" "muddle_run_autoscaler_webhook" {
name = "webhook"
port = 8081
}
port {
name = "persistence"
port = 8083
}
}
}
28 changes: 25 additions & 3 deletions module.tf
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ variable "sentry_dsn_matchmaker" {
sensitive = true
}

variable "sentry_dsn_persistence" {
type = string
default = ""
sensitive = true
}

variable "persistence_db_password" {
type = string
sensitive = true
}

provider "aws" {
profile = "default"
region = var.region
Expand Down Expand Up @@ -117,8 +128,9 @@ resource "kubernetes_secret" "sentry_dsn" {
name = "sentry-dsn"
}
data = {
server = var.sentry_dsn_server
matchmaker = var.sentry_dsn_matchmaker
server = var.sentry_dsn_server
matchmaker = var.sentry_dsn_matchmaker
persistence = var.sentry_dsn_persistence
}
}

Expand All @@ -145,6 +157,16 @@ module "aws_autoscaler" {
depends_on = [module.eks_cluster]
}

module "persistence" {
source = "./k8s/persistence"
depends_on = [module.aws_load_balancer_controller, module.helm_agones, kubernetes_secret.sentry_dsn]

persistence_db_password = var.persistence_db_password
vpc_id = module.eks_cluster.vpc_id
vpc_public_subnets = module.eks_cluster.vpc_public_subnets
worker_group_mgmt_one_sg_id = module.eks_cluster.worker_group_mgmt_one_sg_id
}

module "matchmaker" {
source = "./k8s/matchmaker"
depends_on = [module.aws_load_balancer_controller, module.helm_agones, kubernetes_secret.sentry_dsn]
Expand All @@ -157,7 +179,7 @@ module "web_client" {

module "service" {
source = "./k8s/service"
depends_on = [module.matchmaker, module.web_client]
depends_on = [module.matchmaker, module.persistence, module.web_client]
}

module "route53" {
Expand Down

0 comments on commit f93d270

Please sign in to comment.