terraform deploy eks and nginx and put the log data to cloud logging services on aws
To deploy EKS and Nginx using Terraform and put the log data to Cloud Logging services on AWS, you need to follow these steps:
- Create an IAM user with the necessary permissions to deploy EKS and Cloud Logging services on AWS.
- Install the AWS CLI and Terraform on your local machine.
- Create a Terraform configuration file for deploying EKS and Nginx, including the necessary configurations for Cloud Logging services.
- Initialize the Terraform configuration by running the
terraform initcommand. - Plan the Terraform deployment by running the
terraform plancommand. - Deploy the Terraform configuration by running the
terraform applycommand. - Verify that EKS and Nginx are deployed successfully by accessing the Nginx endpoint.
- Verify that the log data is being sent to Cloud Logging services by checking the CloudWatch logs.
Here's an example Terraform configuration file for deploying EKS and Nginx and sending the log data to Cloud Logging services on AWS:
provider "aws" {
region = "us-west-2"
}
locals {
cluster_name = "my-eks-cluster"
}
module "eks" {
source = "terraform-aws-modules/eks/aws"
cluster_name = local.cluster_name
subnets = ["subnet-xxxxxx", "subnet-yyyyyy", "subnet-zzzzzz"]
vpc_id = "vpc-xxxxxx"
tags = {
Terraform = "true"
Environment = "dev"
}
}
resource "aws_cloudwatch_log_group" "nginx_logs" {
name = "/var/log/nginx/access.log"
}
resource "aws_cloudwatch_log_stream" "nginx_logs_stream" {
name = "nginx-logs"
log_group_name = aws_cloudwatch_log_group.nginx_logs.name
}
resource "aws_eks_node_group" "workers" {
cluster_name = local.cluster_name
node_group_name = "workers"
node_role_arn = module.eks.node_group_default_node_role_arn
subnet_ids = ["subnet-xxxxxx", "subnet-yyyyyy", "subnet-zzzzzz"]
scaling_config {
desired_size = 2
max_size = 2
min_size = 2
}
remote_access {
ec2_ssh_key = "my-ssh-key"
source_security_group_id = module.eks.cluster_security_group_id
}
depends_on = [
module.eks
]
tags = {
Terraform = "true"
Environment = "dev"
}
}
resource "aws_launch_configuration" "nginx" {
name_prefix = "nginx-"
image_id = "ami-xxxxxx"
instance_type = "t2.micro"
security_groups = [module.eks.cluster_security_group_id]
user_data = <<-EOF
#!/bin/bash
yum install nginx -y
systemctl start nginx
EOF
lifecycle {
create_before_destroy = true
}
depends_on = [
aws_cloudwatch_log_group.nginx_logs,
aws_eks_node_group.workers,
]
associate_public_ip_address = true
root_block_device {
volume_size = 20
}
tags = {
Terraform = "true"
Environment = "dev"
}
}
resource "aws_autoscaling_group" "nginx" {
name = "nginx-asg"
launch_configuration = aws_launch_configuration.nginx.name
vpc_zone_identifier = ["subnet-xxxxxx", "subnet-yyyyyy", "subnet-zzzzzz"]
min_size = 1
max_size = 1
desired_capacity = 1
depends_on = [
aws_launch_configuration.nginx,
aws_eks_node_group.workers,
]
lifecycle {
create_before_destroy = true
}
tags = {
Terraform = "true"
Environment = "dev"
}
}
resource "aws_cloudwatch_log_subscription_filter" "nginx_logs_filter" {
name = "nginx-logs-filter"
log_group_name = aws_cloudwatch_log_group.nginx_logs.name
filter_pattern = ""
destination_arn = module.eks.cloudwatch_log_destination_arn
depends_on = [
module.eks,
aws_cloudwatch_log_group.nginx_logs,
]
}
In this example, we're deploying EKS, Nginx, and Cloud Logging services on AWS. We're creating an EKS cluster, a worker node group, and an autoscaling group for Nginx. We're also creating a CloudWatch Log Group and a Log Stream for the Nginx logs and subscribing them to the Cloud Logging services using a filter. Finally, we're tagging all the resources with Terraform and environment tags for better organization and management
原文地址: https://www.cveoy.top/t/topic/cJZ1 著作权归作者所有。请勿转载和采集!