[MSA] prometheus, Grafana 설치

Welcome to Seoyun Dev Log·2024년 12월 3일
0

cloud9 환경 설정
https://www.notion.so/terraform-23390af706ba4605862eedaf420b74d3?pvs=4

ubuntu:~/environment $ cd k8s-demo
ubuntu:~/environment/k8s-demo $ terraform init

사전 작업 k8s 클러스터 설치 / Ingress controller 설치

//위치 k8s-demo
 $ terraform apply --auto-approve
  • Terraform 코드 내용 변경
    • 노드 리소스 증가
      • Auto Scaling Group의 Desired Capacity를 변경 (2→3)
      • t3.medium으로 노드 스케일 업
      • capacity type - SPOT
    • EKS Add-ons 및 Add-on의 실행 권한에 맞는 Role 추가
      • aws-ebs-csi-driver, vpc-cni
#main.tf
provider "aws" {
  region = local.region
}

provider "kubernetes" {
  host                   = module.eks.cluster_endpoint
  cluster_ca_certificate = base64decode(module.eks.cluster_certificate_authority_data)
  token                  = data.aws_eks_cluster_auth.this.token
}

data "aws_eks_cluster_auth" "this" {
  name = module.eks.cluster_name
}

data "aws_availability_zones" "available" {}

locals {
  name   = basename(path.cwd)
  region = "ap-northeast-2"

  vpc_cidr = "10.0.0.0/16"
  azs      = slice(data.aws_availability_zones.available.names, 0, 3)

  tags = {
    Cluster  = local.name
  }
}

################################################################################
# Cluster
################################################################################

module "eks" {
  source  = "terraform-aws-modules/eks/aws"
  version = "~> 19.16"

  cluster_name                   = local.name
  cluster_version                = "1.27"
  cluster_endpoint_public_access = true

  vpc_id     = module.vpc.vpc_id
  subnet_ids = module.vpc.private_subnets

  eks_managed_node_groups = {
    default_node_group = {
      instance_types = ["t3.medium"]
      capacity_type  = "SPOT"
      
      min_size     = 2
      max_size     = 5
      desired_size = 2

    }
  }
  
  node_security_group_additional_rules = {
    ingress_cluster_api_ephemeral_ports_tcp = {
      description   = "Cluster API to kubeseal services"
      protocol      = "tcp"
      from_port     = 8080
      to_port       = 8080
      type          = "ingress"
      source_cluster_security_group = true
    }
  }
  
  cluster_addons = {
    aws-ebs-csi-driver = {
      service_account_role_arn = module.ebs_csi_addon_irsa_role.iam_role_arn
    }
    vpc-cni = {
      resolve_conflicts = "OVERWRITE"
      service_account_role_arn = module.vpc_cni_addon_irsa_role.iam_role_arn
    }
  }

  tags = local.tags
}

module "load_balancer_controller_irsa_role" {
  source = "terraform-aws-modules/iam/aws//modules/iam-role-for-service-accounts-eks"

  role_name                              = "load-balancer-controller"
  attach_load_balancer_controller_policy = true

  oidc_providers = {
    ex = {
      provider_arn               = module.eks.oidc_provider_arn
      namespace_service_accounts = ["kube-system:aws-load-balancer-controller"]
    }
  }

  tags = local.tags
}

module "ebs_csi_addon_irsa_role" {
  source = "terraform-aws-modules/iam/aws//modules/iam-role-for-service-accounts-eks"

  role_name             = "ebs-csi-controller"
  attach_ebs_csi_policy = true

  oidc_providers = {
    ex = {
      provider_arn               = module.eks.oidc_provider_arn
      namespace_service_accounts = ["kube-system:ebs-csi-controller-sa"]
    }
  }

  tags = local.tags
}

module "vpc_cni_addon_irsa_role" {
  source  = "terraform-aws-modules/iam/aws//modules/iam-role-for-service-accounts-eks"

  role_name             = "vpc-cni"
  attach_vpc_cni_policy = true
  vpc_cni_enable_ipv4   = true

  oidc_providers = {
    main = {
      provider_arn               = module.eks.oidc_provider_arn
      namespace_service_accounts = ["kube-system:aws-node"]
    }
  }

  tags = local.tags
}


################################################################################
# Supporting Resoruces
################################################################################

module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "~> 5.0"

  name = local.name
  cidr = local.vpc_cidr

  azs             = local.azs
  public_subnets   = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k)]
  private_subnets  = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k + 3)]
  database_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k + 6)]

  create_database_subnet_group = true

  enable_nat_gateway = true
  single_nat_gateway = true

  public_subnet_tags = {
    "kubernetes.io/role/elb" = 1
  }

  private_subnet_tags = {
    "kubernetes.io/role/internal-elb" = 1
  }

  tags = local.tags
}

AWS Load Balancer Ingress Controller 설치
Terraform 기반으로 K8S Cluster를 실행 후에 설치 진행

# Kubeconfig 설정
aws eks --region ap-northeast-2 update-kubeconfig --name k8s-demo

# K8S Cluster 연결 확인 
kubectl get no

# Helm charts repo 추가 
helm repo add eks https://aws.github.io/eks-charts

# AWS Load Balancer Ingress Controller 설치 
# !! 590974975982 부분은 본인 AWS 계정으로 변경
helm install aws-load-balancer-controller eks/aws-load-balancer-controller \
  -n kube-system --set clusterName=k8s-demo --set serviceAccount.create=true \
  --set serviceAccount.name=aws-load-balancer-controller \
  --set serviceAccount.annotations."eks\.amazonaws\.com/role-arn"="arn:aws:iam::123123123123:role/load-balancer-controller"

#123123123123이 숫자는 본인의 AWS 계정과 연동하면 됨

helm 설치
https://whchoi98.gitbook.io/k8s/eks-2/helm


Prometheus + Grafana 설치

git clone git@github.com:attachnplay/kube-prometheus.git

cd kube-prometheus

# Create the namespace and CRDs, and then wait for them to be availble before creating the remaining resources
kubectl create -f manifests/setup

# Wait until the "servicemonitors" CRD is created. The message "No resources found" means success in this context.
until kubectl get servicemonitors --all-namespaces ; do date; sleep 1; echo ""; done

kubectl create -f manifests/

  • grafana url:

📌 XTERNAL-IP pending

Cloud9에서 k9s를 실행하면서 grafana-loadbalancer-svc의 External-IP가 Pending 상태로 나오는 이유는 Kubernetes 클러스터의 LoadBalancer 타입 서비스가 External IP를 할당하지 못한 것과 관련이 있습니다. 이는 일반적으로 Cloud9 환경과 관련된 네트워크 설정 또는 Kubernetes 클러스터의 LoadBalancer 구현 문제 때문입니다.

주요 원인 및 해결 방법
1. 클라우드 제공자 LoadBalancer 지원 여부
Cloud9은 AWS에서 실행되며 Kubernetes 클러스터가 AWS에서 운영 중일 경우, LoadBalancer 서비스는 AWS의 Elastic Load Balancer(ELB)를 통해 External IP를 할당합니다.

문제: 클러스터가 적절한 IAM 권한이나 설정을 가지지 않으면 ELB 생성이 실패합니다.
해결 방법:
Kubernetes 클러스터에 적절한 IAM 역할이 부여되었는지 확인합니다.
bash
코드 복사

-> localhost:3000으로 접속


Log 기록을 위한 Loki 설치

: 운영 수준의 구성은 다소 무겁기 때문에 Loki 사용 방법 확인을 목적으로 함

https://grafana.com/docs/loki/latest/setup/install/

  • monolithic 구성으로 설치
helm repo add grafana https://grafana.github.io/helm-charts

helm repo update

helm pull grafana/loki

tar zxf (내려받은 파일 예, loki-5.39.0).tgz

$ vim values-dev.yaml
loki:
  commonConfig:
    replication_factor: 1
  schemaConfig:
    configs:
      - from: "2024-04-01"
        store: tsdb
        object_store: s3
        schema: v13
        index:
          prefix: loki_index_
          period: 24h
  pattern_ingester:
      enabled: true
  limits_config:
    allow_structured_metadata: true
    volume_enabled: true
    retention_period: 672h # 28 days retention
  compactor:
    retention_enabled: true 
    delete_request_store: s3
  ruler:
    enable_api: true

minio:
  enabled: true
      
deploymentMode: SingleBinary

singleBinary:
  replicas: 1

# Zero out replica counts of other deployment modes
backend:
  replicas: 0
read:
  replicas: 0
write:
  replicas: 0

ingester:
  replicas: 0
querier:
  replicas: 0
queryFrontend:
  replicas: 0
queryScheduler:
  replicas: 0
distributor:
  replicas: 0
compactor:
  replicas: 0
indexGateway:
  replicas: 0
bloomCompactor:
  replicas: 0
bloomGateway:
  replicas: 0

** vim 에서 붙여넣기 할 때 형식이 제대로 안될 경우
:set paste 를 하고 i(insert)모드를 눌러서 다시 붙여넣기

X-Scope-OrgID : 1


profile
하루 일지 보단 행동 고찰 과정에 대한 개발 블로그

0개의 댓글

관련 채용 정보