-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #387 from systemaccounting/386-add-dev-k8s
386 add dev k8s
- Loading branch information
Showing
30 changed files
with
428 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
infrastructure/terraform/aws/environments/prod/.terraform.lock.hcl
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module "microk8s" { | ||
count = var.enable_microk8s ? 1 : 0 | ||
source = "../../microk8s/v001" | ||
env = var.env | ||
env_id = var.env_id | ||
ssm_prefix = var.ssm_prefix | ||
instance_type = var.microk8s_instance_type | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
resource "aws_instance" "default" { | ||
ami = data.aws_ami.ubuntu.id | ||
instance_type = var.instance_type | ||
key_name = aws_key_pair.default.key_name | ||
vpc_security_group_ids = [aws_security_group.default.id] | ||
# provision in the same subnet as rds postgres | ||
subnet_id = tolist(data.aws_db_subnet_group.default.subnet_ids)[0] | ||
user_data = file("${path.module}/user-data.sh") | ||
tags = { | ||
Name = "${local.NAME}-${local.ID_ENV}" | ||
} | ||
} | ||
|
||
# passed around in the state file, demo only | ||
resource "tls_private_key" "default" { | ||
algorithm = "RSA" | ||
rsa_bits = 4096 | ||
} | ||
|
||
resource "aws_key_pair" "default" { | ||
key_name = "${local.MICROK8S_SSH_KEY_NAME_PREFIX}-${local.ID_ENV}" | ||
public_key = sensitive(tls_private_key.default.public_key_openssh) | ||
} | ||
|
||
data "aws_ami" "ubuntu" { | ||
most_recent = true | ||
owners = ["099720109477"] # Canonical | ||
filter { | ||
name = "name" | ||
values = ["ubuntu/images/hvm-ssd-gp3/ubuntu-noble-24.04-amd64-server-*"] | ||
} | ||
filter { | ||
name = "virtualization-type" | ||
values = ["hvm"] | ||
} | ||
} | ||
|
||
# permissive for demo | ||
resource "aws_security_group" "default" { | ||
name = "${local.NAME}-sec-grp-${local.ID_ENV}" | ||
description = "${local.NAME} access in ${local.SPACED_ID_ENV}" | ||
vpc_id = data.aws_vpc.default.id | ||
|
||
ingress { | ||
description = "ssh" | ||
from_port = 22 | ||
to_port = 22 | ||
protocol = "tcp" | ||
cidr_blocks = ["0.0.0.0/0"] | ||
} | ||
|
||
ingress { | ||
description = "microk8s" | ||
from_port = 16443 | ||
to_port = 16443 | ||
protocol = "tcp" | ||
cidr_blocks = ["0.0.0.0/0"] | ||
} | ||
|
||
ingress { | ||
description = "measure node port" | ||
from_port = 30010 | ||
to_port = 30010 | ||
protocol = "tcp" | ||
cidr_blocks = ["0.0.0.0/0"] | ||
} | ||
|
||
egress { | ||
from_port = 0 | ||
to_port = 0 | ||
protocol = "-1" | ||
cidr_blocks = ["0.0.0.0/0"] | ||
} | ||
} | ||
|
||
data "aws_db_subnet_group" "default" { | ||
# todo: replace hardcoded "db-subnet-group" prefix with variable here and | ||
# in infrastructure/terraform/aws/modules/environment/v001/rds.tf | ||
name = "db-subnet-group-${local.ID_ENV}" | ||
} | ||
|
||
data "aws_vpc" "default" { | ||
default = true | ||
} |
10 changes: 10 additions & 0 deletions
10
infrastructure/terraform/aws/modules/microk8s/v001/locals.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
locals { | ||
NAME = "microk8s" | ||
ID_ENV = "${var.env_id}-${var.env}" | ||
TITLED_ID_ENV = replace(title(local.ID_ENV), "-", "") | ||
SPACED_ID_ENV = replace(local.ID_ENV, "-", " ") | ||
PROJECT_CONF = yamldecode(file("../../../../../project.yaml")) | ||
MICROK8S_CONF = local.PROJECT_CONF.infrastructure.terraform.aws.modules.microk8s.env_var.set | ||
MICROK8S_SSH_KEY_NAME_PREFIX = local.MICROK8S_CONF.MICROK8S_SSH_KEY_NAME_PREFIX.default | ||
MICROK8S_SSH_PORT = local.MICROK8S_CONF.MICROK8S_SSH_PORT.default | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
resource "aws_ssm_parameter" "ssh_pub_key" { | ||
name = "/${var.ssm_prefix}/${local.MICROK8S_CONF.MICROK8S_SSH_PUB_KEY.ssm}" | ||
description = "microk8s ssh public key in ${local.SPACED_ID_ENV}" | ||
type = "SecureString" | ||
value = aws_key_pair.default.public_key | ||
} | ||
|
||
resource "aws_ssm_parameter" "ssh_priv_key" { | ||
name = "/${var.ssm_prefix}/${local.MICROK8S_CONF.MICROK8S_SSH_PRIV_KEY.ssm}" | ||
description = "microk8s ssh private key in ${local.SPACED_ID_ENV}" | ||
type = "SecureString" | ||
value = tls_private_key.default.private_key_pem | ||
} | ||
|
||
resource "aws_ssm_parameter" "ssh_host" { | ||
name = "/${var.ssm_prefix}/${local.MICROK8S_CONF.MICROK8S_SSH_HOST.ssm}" | ||
description = "microk8s ssh host in ${local.SPACED_ID_ENV}" | ||
type = "SecureString" | ||
value = aws_instance.default.public_ip | ||
} |
11 changes: 11 additions & 0 deletions
11
infrastructure/terraform/aws/modules/microk8s/v001/user-data.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/bash | ||
sudo apt update | ||
sudo snap install microk8s --classic --channel=1.31 | ||
sudo microk8s enable dns ingress | ||
sudo usermod -a -G microk8s ubuntu | ||
mkdir -p /home/ubuntu/.kube | ||
sudo chown -f -R ubuntu /home/ubuntu/.kube | ||
chmod 0700 /home/ubuntu/.kube | ||
wget -O- https://carvel.dev/install.sh >install.sh | ||
sudo bash install.sh | ||
rm install.sh |
4 changes: 4 additions & 0 deletions
4
infrastructure/terraform/aws/modules/microk8s/v001/variables.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
variable "env" {} | ||
variable "env_id" {} | ||
variable "ssm_prefix" {} | ||
variable "instance_type" {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
RELATIVE_PROJECT_ROOT_PATH=$(shell REL_PATH="."; while [ $$(ls "$$REL_PATH" | grep project.yaml | wc -l | xargs) -eq 0 ]; do REL_PATH="$$REL_PATH./.."; done; printf '%s' "$$REL_PATH") | ||
include $(RELATIVE_PROJECT_ROOT_PATH)/make/shared.mk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#@ load("@ytt:data", "data") | ||
#@ def convert_to_node_port(port): | ||
#@ port_str = str(port) | ||
#@ new_port_str = "3" + port_str[1:] | ||
#@ return int(new_port_str) | ||
#@ end | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: measure-node-port | ||
spec: | ||
selector: | ||
app: measure | ||
ports: | ||
- protocol: TCP | ||
port: #@ data.values.MEASURE_PORT | ||
targetPort: #@ data.values.MEASURE_PORT | ||
nodePort: #@ convert_to_node_port(data.values.MEASURE_PORT) | ||
type: NodePort |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,65 @@ | ||
RELATIVE_PROJECT_ROOT_PATH=.. | ||
include $(RELATIVE_PROJECT_ROOT_PATH)/make/shared.mk | ||
SSH_KEY_SSM_SUFFIX=$(shell yq '.infrastructure.terraform.aws.modules.microk8s.env_var.set.MICROK8S_SSH_PRIV_KEY.ssm' $(PROJECT_CONF)) | ||
MICROK8S_SSH_KEY_NAME_PREFIX=$(shell yq '.infrastructure.terraform.aws.modules.microk8s.env_var.set.MICROK8S_SSH_KEY_NAME_PREFIX.default' $(PROJECT_CONF)) | ||
MICROK8S_SSH_USER=$(shell yq '.infrastructure.terraform.aws.modules.microk8s.env_var.set.MICROK8S_SSH_USER.default' $(PROJECT_CONF)) | ||
MICROK8S_MANIFESTS_DIR=$(shell yq '.infrastructure.terraform.aws.modules.microk8s.env_var.set.MICROK8S_MANIFESTS_DIR.default' $(PROJECT_CONF)) | ||
|
||
# avoid assigning ENV_ID on local commands or when ENV is not assigned | ||
ifneq ($(origin ENV), undefined) | ||
ifneq ($(ENV), local) | ||
ENV_ID=$(shell cd $(RELATIVE_PROJECT_ROOT_PATH); ENV=$(ENV) bash scripts/print-env-id.sh) | ||
ID_ENV=$(ENV_ID)-$(ENV) | ||
SSH_KEY=$(ID_ENV).pem | ||
MICROK8S_NAME=$(MICROK8S_SSH_KEY_NAME_PREFIX)-$(ID_ENV) | ||
MANIFESTS_DIR=/home/$(MICROK8S_SSH_USER)/$(MICROK8S_MANIFESTS_DIR) | ||
endif | ||
endif | ||
|
||
deploy-local: | ||
@$(MAKE) --no-print-directory -C local env ENV=local | ||
@$(MAKE) --no-print-directory env ENV=local | ||
ytt --data-values-file ./local/$(ENV_FILE_NAME) --data-values-file $(ENV_FILE_NAME) -f . | kubectl apply -f - | ||
ytt --data-values-file ./local/$(ENV_FILE_NAME) -f . | kubectl apply -f - | ||
|
||
delete-local: | ||
@$(MAKE) --no-print-directory -C local env ENV=local | ||
@$(MAKE) --no-print-directory env ENV=local | ||
ytt --data-values-file ./local/$(ENV_FILE_NAME) --data-values-file $(ENV_FILE_NAME) -f . | kubectl delete -f - | ||
ytt --data-values-file ./local/$(ENV_FILE_NAME) -f . | kubectl delete -f - | ||
|
||
delete-all-local: | ||
kubectl delete all --all -n default | ||
|
||
list-all: | ||
kubectl get all -n default | ||
kubectl get all -n default | ||
|
||
### cloud | ||
|
||
get-ssh-key: | ||
@$(MAKE) -s test-env-arg | ||
(cd $(RELATIVE_PROJECT_ROOT_PATH); bash scripts/get-ssh-key.sh --env $(ENV) --ssm-suffix $(SSH_KEY_SSM_SUFFIX) --dir $(CURDIR)) | ||
|
||
connect: | ||
@$(MAKE) -s test-env-arg | ||
@$(MAKE) -s get-ssh-key | ||
@K8S_IP=$$(aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" "Name=tag:Name,Values=$(MICROK8S_NAME)" --query "Reservations[].Instances[0].PublicIpAddress" --output text); \ | ||
ssh -q -o StrictHostKeyChecking=accept-new -o IdentitiesOnly=yes -i $(SSH_KEY) $(MICROK8S_SSH_USER)@$$K8S_IP | ||
|
||
deploy: | ||
@$(MAKE) -s test-env-arg | ||
@$(MAKE) --no-print-directory -C dev env ENV=dev | ||
@$(MAKE) -s get-ssh-key | ||
@echo "*** wating for ok instance status before deploying..." | ||
@INSTANCE_ID=$$(aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" "Name=tag:Name,Values=$(MICROK8S_NAME)" --query "Reservations[].Instances[0].InstanceId" --output text); \ | ||
aws ec2 wait instance-status-ok --instance-ids $$INSTANCE_ID | ||
@$(MAKE) -s deploy-now | ||
|
||
deploy-now: | ||
@$(MAKE) -s test-env-arg | ||
@echo "*** deploying..." | ||
@K8S_IP=$$(aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" "Name=tag:Name,Values=$(MICROK8S_NAME)" --query "Reservations[].Instances[0].PublicIpAddress" --output text); \ | ||
scp -q -o StrictHostKeyChecking=accept-new -o IdentitiesOnly=yes -i $(SSH_KEY) event.yml measure.yml redis.yml $(MICROK8S_SSH_USER)@$$K8S_IP:$(MANIFESTS_DIR); \ | ||
scp -q -o StrictHostKeyChecking=accept-new -o IdentitiesOnly=yes -i $(SSH_KEY) -r ./$(ENV) $(MICROK8S_SSH_USER)@$$K8S_IP:$(MANIFESTS_DIR)/; \ | ||
ssh -q -o StrictHostKeyChecking=accept-new -o IdentitiesOnly=yes -i $(SSH_KEY) $(MICROK8S_SSH_USER)@$$K8S_IP 'ytt --data-values-file $(MANIFESTS_DIR)/$(ENV)/$(ENV_FILE_NAME) -f $(MANIFESTS_DIR) | microk8s kubectl apply -f -' | ||
|
||
delete-all: | ||
@$(MAKE) -s test-env-arg | ||
@K8S_IP=$$(aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" "Name=tag:Name,Values=$(MICROK8S_NAME)" --query "Reservations[].Instances[0].PublicIpAddress" --output text); \ | ||
ssh -q -o StrictHostKeyChecking=accept-new -o IdentitiesOnly=yes -i $(SSH_KEY) $(MICROK8S_SSH_USER)@$$K8S_IP 'microk8s kubectl delete all --all -n default' |
Oops, something went wrong.