aws(学习笔记第十六课) 使用负载均衡器(ELB)解耦webserver以及输出ELB的日志到S3

aws(学习笔记第十六课)

  • 使用负载均衡器(ELB)以及输出ELB的日志到S3

学习内容:

  • 使用负载均衡器(ELB)解耦web server
  • 输出ELB的日志到S3

1. 使用负载均衡器(ELB)

  1. 全体架构
    使用ELB(Elastic Load Balancer)能够解耦外部internet访问和web server之间的耦合,让外部internet访问只能认识ELB,只知道是ELB为它服务,但是具体的web server对于外部来说却是不意识的。

在这里插入图片描述
2. 代码解析

  • 全体代码
    {"AWSTemplateFormatVersion": "2010-09-09","Description": "AWS in Action: chapter 12 (Load Balancer)","Parameters": {"KeyName": {"Description": "Key Pair name","Type": "AWS::EC2::KeyPair::KeyName","Default": "my-cli-key"},"NumberOfServers": {"Description": "Number of servers","Type": "Number","Default": "2","MinValue": "2","MaxValue": "4"}},"Mappings": {"EC2RegionMap": {"ap-northeast-1": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-cbf90ecb"},"ap-southeast-1": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-68d8e93a"},"ap-southeast-2": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-fd9cecc7"},"eu-central-1": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-a8221fb5"},"eu-west-1": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-a10897d6"},"sa-east-1": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-b52890a8"},"us-east-1": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-1ecae776"},"us-west-1": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-d114f295"},"us-west-2": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-e7527ed7"}}},"Resources": {"VPC": {"Type": "AWS::EC2::VPC","Properties": {"CidrBlock": "172.31.0.0/16","EnableDnsHostnames": "true"}},"InternetGateway": {"Type": "AWS::EC2::InternetGateway","Properties": {}},"VPCGatewayAttachment": {"Type": "AWS::EC2::VPCGatewayAttachment","Properties": {"VpcId": {"Ref": "VPC"},"InternetGatewayId": {"Ref": "InternetGateway"}}},"Subnet": {"Type": "AWS::EC2::Subnet","Properties": {"AvailabilityZone": {"Fn::Select": ["0", {"Fn::GetAZs": ""}]},"CidrBlock": "172.31.38.0/24","VpcId": {"Ref": "VPC"}}},"RouteTable": {"Type": "AWS::EC2::RouteTable","Properties": {"VpcId": {"Ref": "VPC"}}},"RouteTableAssociation": {"Type": "AWS::EC2::SubnetRouteTableAssociation","Properties": {"SubnetId": {"Ref": "Subnet"},"RouteTableId": {"Ref": "RouteTable"}}},"RoutePublicNATToInternet": {"Type": "AWS::EC2::Route","Properties": {"RouteTableId": {"Ref": "RouteTable"},"DestinationCidrBlock": "0.0.0.0/0","GatewayId": {"Ref": "InternetGateway"}},"DependsOn": "VPCGatewayAttachment"},"NetworkAcl": {"Type": "AWS::EC2::NetworkAcl","Properties": {"VpcId": {"Ref": "VPC"}}},"SubnetNetworkAclAssociation": {"Type": "AWS::EC2::SubnetNetworkAclAssociation","Properties": {"SubnetId": {"Ref": "Subnet"},"NetworkAclId": {"Ref": "NetworkAcl"}}},"NetworkAclEntryIngress": {"Type": "AWS::EC2::NetworkAclEntry","Properties": {"NetworkAclId": {"Ref": "NetworkAcl"},"RuleNumber": "100","Protocol": "-1","RuleAction": "allow","Egress": "false","CidrBlock": "0.0.0.0/0"}},"NetworkAclEntryEgress": {"Type": "AWS::EC2::NetworkAclEntry","Properties": {"NetworkAclId": {"Ref": "NetworkAcl"},"RuleNumber": "100","Protocol": "-1","RuleAction": "allow","Egress": "true","CidrBlock": "0.0.0.0/0"}},"LoadBalancerSecurityGroup": {"Type": "AWS::EC2::SecurityGroup","Properties": {"GroupDescription": "elb-sg","VpcId": {"Ref": "VPC"},"SecurityGroupIngress": [{"CidrIp": "0.0.0.0/0","FromPort": 80,"IpProtocol": "tcp","ToPort": 80}]}},"LoadBalancer": {"Type": "AWS::ElasticLoadBalancing::LoadBalancer","Properties": {"Subnets": [{"Ref": "Subnet"}],"LoadBalancerName": "elb","Listeners": [{"InstancePort": "80","InstanceProtocol": "HTTP","LoadBalancerPort": "80","Protocol": "HTTP"}],"HealthCheck": {"HealthyThreshold": "3","Interval": "10","Target": "HTTP:80/index.html","Timeout": "5","UnhealthyThreshold": "2"},"SecurityGroups": [{"Ref": "LoadBalancerSecurityGroup"}],"Scheme": "internet-facing"},"DependsOn": "VPCGatewayAttachment"},"WebServerSecurityGroup": {"Type": "AWS::EC2::SecurityGroup","Properties": {"GroupDescription": "awsinaction-sg","VpcId": {"Ref": "VPC"},"SecurityGroupIngress": [{"CidrIp": "0.0.0.0/0","FromPort": 22,"IpProtocol": "tcp","ToPort": 22}, {"FromPort": 80,"IpProtocol": "tcp","SourceSecurityGroupId": {"Ref": "LoadBalancerSecurityGroup"},"ToPort": 80}]}},"LaunchTemplate": {"Type": "AWS::EC2::LaunchTemplate","Metadata": {"AWS::CloudFormation::Init": {"config": {"packages": {"yum": {"httpd": []}},"files": {"/tmp/config": {"content": {"Fn::Join": ["", ["#!/bin/bash -ex\n","PRIVATE_IP=`curl -s http://169.254.169.254/latest/meta-data/local-ipv4`\n","echo \"<html><head><title>$PRIVATE_IP</title></head><body><h1>$PRIVATE_IP</h1></body></html>\" > index.html\n"]]},"mode": "000500","owner": "root","group": "root"}},"commands": {"01_config": {"command": "/tmp/config","cwd": "/var/www/html"}},"services": {"sysvinit": {"httpd": {"enabled": "true","ensureRunning": "true"}}}}}},"Properties": {"LaunchTemplateData":{"EbsOptimized": false,"ImageId": {"Fn::FindInMap": ["EC2RegionMap", {"Ref": "AWS::Region"}, "AmazonLinuxAMIHVMEBSBacked64bit"]},"InstanceType": "t2.micro","NetworkInterfaces":[{"DeviceIndex":0,"AssociatePublicIpAddress":true,"Groups":[{"Ref": "WebServerSecurityGroup"}],"DeleteOnTermination":true}],"KeyName": {"Ref": "KeyName"},"UserData": {"Fn::Base64": {"Fn::Join": ["", ["#!/bin/bash -ex\n","yum update -y aws-cfn-bootstrap\n","/opt/aws/bin/cfn-init -v --stack ", {"Ref": "AWS::StackName"}, " --resource LaunchTemplate --region ", {"Ref": "AWS::Region"}, "\n","/opt/aws/bin/cfn-signal -e $? --stack ", {"Ref": "AWS::StackName"}, " --resource AutoScalingGroup --region ", {"Ref": "AWS::Region"}, "\n"]]}}}}},"AutoScalingGroup": {"Type": "AWS::AutoScaling::AutoScalingGroup","Properties": {"LoadBalancerNames": [{"Ref": "LoadBalancer"}],"LaunchTemplate" : {"LaunchTemplateId" : {"Ref" : "LaunchTemplate"},"Version" : {"Fn::GetAtt" : ["LaunchTemplate","LatestVersionNumber"]}},"MinSize": {"Ref": "NumberOfServers"},"MaxSize": {"Ref": "NumberOfServers"},"DesiredCapacity": {"Ref": "NumberOfServers"},"VPCZoneIdentifier": [{"Ref": "Subnet"}]},"CreationPolicy": {"ResourceSignal": {"Timeout": "PT10M"}},"DependsOn": "VPCGatewayAttachment"}},"Outputs": {"URL": {"Value": {"Fn::Join": ["", ["http://", {"Fn::GetAtt": ["LoadBalancer", "DNSName"]}]]},"Description": "Load Balancer URL"}}
    }
    
  • 主要代码分析
    • WebServerSecurityGroup中的SourceSecurityGroupId
      这里,WebServerSecurityGroupSourceSecurityGroupIdLoadBalancerSecurityGroup,表明只有LoadBalancerSecurityGroup所在的主机,也就是LoadBalancer才能对web server访问80端口。
      "WebServerSecurityGroup": {"Type": "AWS::EC2::SecurityGroup","Properties": {"GroupDescription": "awsinaction-sg","VpcId": {"Ref": "VPC"},"SecurityGroupIngress": [{"CidrIp": "0.0.0.0/0","FromPort": 22,"IpProtocol": "tcp","ToPort": 22}, {"FromPort": 80,"IpProtocol": "tcp","SourceSecurityGroupId": {"Ref": "LoadBalancerSecurityGroup"},"ToPort": 80}]}},
      
    • LoadBalancer加入HealthCheck
      这里,LoadBalancer里面,加入了对web serverHealthCheck,当web serverindex.hmlt80端口能够访问之后,才能够开始转送到web server
      		"LoadBalancer": {"Type": "AWS::ElasticLoadBalancing::LoadBalancer","Properties": {"Subnets": [{"Ref": "Subnet"}],"LoadBalancerName": "elb","Listeners": [{"InstancePort": "80","InstanceProtocol": "HTTP","LoadBalancerPort": "80","Protocol": "HTTP"}],"HealthCheck": {"HealthyThreshold": "3","Interval": "10","Target": "HTTP:80/index.html","Timeout": "5","UnhealthyThreshold": "2"},"SecurityGroups": [{"Ref": "LoadBalancerSecurityGroup"}],"Scheme": "internet-facing"},"DependsOn": "VPCGatewayAttachment"},
      

2. 输出ELB的日志到S3

  • 首先设置S3 存储桶策略,允许ELB写入logS3
    • policy.json策略文件
      事先作成elb-log-20241208S3 bucket,之后写出policy文件允许logdelivery.elasticloadbalancing.amazonaws.com这个service能够putObjectS3 bucket
      {"Id": "Policy1429136655940","Version": "2012-10-17","Statement": [{"Sid": "Stmt1429136633762","Action": ["s3:PutObject"],"Effect": "Allow","Resource": "arn:aws:s3:::elb-log-20241208/*","Principal": {"AWS": ["127311923021", "027434742980", "797873946194","156460612806", "054676820928", "582318560864","114774131450", "783225319266", "507241528517"]}}]
      }
      
      这里,127311923021027434742980像是一些magic code,这到底是什么呢?可以参照enable-access-logging.html,它们是所在地区的 Elastic Load Balancing 的 ID: AWS 账户。
    • 设置policy.json策略文件给S3bucket
      aws s3api put-bucket-policy --bucket elb-log-20241208 --policy file://policy/elb-log-policy.json
      
      在这里插入图片描述
    • 配置cloudformationELB输入logS3 bucket
      AccessLoggingPolicy这里就是设置描述log输出的设置。
      		"LoadBalancer": {"Type": "AWS::ElasticLoadBalancing::LoadBalancer","Properties": {"Subnets": [{"Ref": "Subnet"}],"LoadBalancerName": "elb","Listeners": [{"InstancePort": "80","InstanceProtocol": "HTTP","LoadBalancerPort": "80","Protocol": "HTTP"}],"HealthCheck": {"HealthyThreshold": "3","Interval": "10","Target": "HTTP:80/index.html","Timeout": "5","UnhealthyThreshold": "2"},"AccessLoggingPolicy":{"ENabled": true,"S3BucketName":"elb-log-20241208","S3BucketPrefix":"my-application/production"}"SecurityGroups": [{"Ref": "LoadBalancerSecurityGroup"}],"Scheme": "internet-facing"},"DependsOn": "VPCGatewayAttachment"},
      
      在这里插入图片描述
      在这里插入图片描述
    • 重新访问ELB
      在这里插入图片描述
    • 检查S3log文件夹
      可以看到log已经开始写入到S3
      在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/888935.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

深入理解C#的TCPIP通信机制

本文还有配套的精品资源&#xff0c;点击获取 简介&#xff1a;在分布式系统和实时数据交换应用中&#xff0c;C#作为一种现代面向对象编程语言&#xff0c;利用其***命名空间下的Socket类&#xff0c;提供强大的TCP/IP通信功能。本文将探讨C#中TCP/IP通信的基本概念、使用方…

高项 - 项目管理原则与项目绩效域

个人总结&#xff0c;仅供参考&#xff0c;欢迎加好友一起讨论 博文更新参考时间点&#xff1a;2024-12 高项 - 章节与知识点汇总&#xff1a;点击跳转 文章目录 高项 - 项目管理原则与项目绩效域项目管理12条原则原则1&#xff1a;成为勤勉、尊重和关心他人的管家 (p202)原则…

仿真技术助力高尔夫球打破传统设计局限,实现球杆强大的功能

Altair近日宣布与业内领先的高尔夫装备制造商 Cleveland Golf 开展合作&#xff0c;以设计新款 HiBore XL 球杆。借助 Altair 先进的仿真与设计技术&#xff0c;Cleveland Golf 不断刷新高尔夫装备的行业标准&#xff0c;并在球杆产品设计方面实现突破。 Cleveland Golf 借助 A…

python字符串处理基础操作总结

1.去掉空格或者特殊符号 input_str.strip() #去掉所有空格 input_str.lstrip() #去掉左边空格 input_str.rstrip() #去掉右边空格 def print_hi():input_str 今天天气不错&#xff0c;风和日丽 out input_str.strip()print(input_str)print(out)if __name__ __main__:print…

Trimble X9三维激光扫描仪高效应对化工厂复杂管道扫描测绘挑战【沪敖3D】

化工安全关系到国计民生&#xff0c;近年来随着化工厂数字化改革不断推进&#xff0c;数字工厂逐步成为工厂安全管理的重要手段。而化工管道作为工厂设施的重要组成部分&#xff0c;由于其数量多、种类繁杂&#xff0c;一直是企业管理的重点和难点。 传统的化工管廊往往缺乏详…

日志基础示例python和c++

文章目录 0. 引言1. python2. c 0. 引言 本文主要记录python版本和c版本常用的日志基础示例。 1. python python版本常用的是logging库&#xff0c;结合colorlog库&#xff0c;可根据不同日志级别打印不同颜色的日志&#xff0c;为了便于分析问题&#xff0c;还添加了日志保…

【Linux】基础IO-----文件详解

目录 一、文件理解&#xff1a; 二、C语言的文件操作&#xff1a; 1、fopen&#xff1a; 什么是当前路径&#xff1a; 2、fclose&#xff1a; 3、fwrite&#xff1a; 4、默认打开的三个流&#xff1a; 三、系统文件&#xff1a; 1、open&#xff1a; 2、close&#xf…

第7章:响应式设计 --[CSS零基础入门]

什么是响应式设计 响应式设计&#xff08;Responsive Web Design, RWD&#xff09;是一种网页设计和开发的方法&#xff0c;它使网站能够根据用户的设备特性&#xff08;如屏幕尺寸、分辨率、方向等&#xff09;自动调整其布局和内容。响应式设计的目标是确保网站在不同类型的…

探索 ONLYOFFICE 8.2 版本:更高效、更安全的云端办公新体验

引言 在当今这个快节奏的时代&#xff0c;信息技术的发展已经深刻改变了我们的工作方式。从传统的纸质文件到电子文档&#xff0c;再到如今的云端协作&#xff0c;每一步技术进步都代表着效率的飞跃。尤其在后疫情时代&#xff0c;远程办公成为常态&#xff0c;如何保持团队之间…

Vue-打印自定义HTML表格

自定义打印方法 1. 准备HTML结构 首先&#xff0c;构造了一个基本的HTML页面框架&#xff0c;并设置了页面的字符编码为UTF-8&#xff0c;以确保中文和其他特殊字符能正确显示。页面的标题设置为传入的 title 参数值。 let printStr "<html><head><met…

http1.0、1.1、2.0、 3.0

http1.0、1.1、2.0、 3.0 http1.1 引入长连接&#xff0c;在1.0&#xff0c;每次请求都需要建立新的TCP连接&#xff0c;处理请求完毕后立即断开。就导致处理大量图片&#xff0c;链接等资源&#xff0c;需要大量的连接与断开&#xff0c;造成资源浪费和时间延迟。而长连接允许…

跟着问题学15——GRU网络结构详解及代码实战

1 RNN的缺陷——长期依赖的问题 &#xff08;The Problem of Long-Term Dependencies&#xff09; 前面一节我们学习了RNN神经网络&#xff0c;它可以用来处理序列型的数据&#xff0c;比如一段文字&#xff0c;视频等等。RNN网络的基本单元如下图所示&#xff0c;可以将前面的…

pytest中使用conftest做测试前置和参数化

pytest中比较高阶的应用是&#xff0c;使用conftest去做测试前置工作、测试收尾工作和参数化。conftest是pytest的一个组件&#xff0c;用于配置测试环境和参数。通过conftest, 可以创建一个可复用的测试配置文件&#xff0c;以便在多个测试模块之间共享配置信息。即&#xff0…

04 创建一个属于爬虫的主虚拟环境

文章目录 回顾conda常用指令创建一个爬虫虚拟主环境Win R 调出终端查看当前conda的虚拟环境创建 spider_base 的虚拟环境安装完成查看环境是否存在 为 pycharm 配置创建的爬虫主虚拟环境选一个盘符来存储之后学习所写的爬虫文件用 pycharm 打开创建的文件夹pycharm 配置解释器…

mvn test 失败,单独运行单元测试成功

标题mvn test 失败&#xff0c;单独运行单元测试成功 使用junit4进行单元测试时是通过的&#xff0c;但是在执行maven的test与package时测试不通过 报错信息&#xff1a; parse data from Nacos error,dataId:guoyu-new-asset-dev.yml,data: ....... 配置文件内容 ....... o…

android 富文本及展示更多组件

模拟微博 #热贴 和 用户 的这种 富文本形式组件&#xff0c;不说了&#xff0c; 直接上代码 package com.tongtong.feat_watch.viewimport android.content.Context import android.graphics.Color import android.util.AttributeSet import android.view.LayoutInflater impo…

gitlab 生成并设置 ssh key

一、介绍 &#x1f3af; 本文主要介绍 SSH Key 的生成方法&#xff0c;以及如何在GitLab上添加SSH Key。GitLab 使用SSH协议与Git 进行安全通信。当您使用 SSH密钥 对 GitLab远程服务器进行身份验证时&#xff0c;您不需要每次都提供您的用户名和密码。SSH使用两个密钥&#x…

保姆级教程Docker部署Nacos镜像

目录 1、创建挂载目录 2、拉取 Nacos 镜像 3、临时启动并复制文件 4、创建Nacos表结构 5、修改Nacos配置 6、正式启动 Nacos 7、登录Nacos 1、创建挂载目录 在宿主机上创建一个目录用于配置文件映射&#xff0c;这个目录将作为数据卷挂载到容器内部&#xff0c;使得我…

【北京迅为】iTOP-4412全能版使用手册-第六十七章 USB鼠标驱动详解

iTOP-4412全能版采用四核Cortex-A9&#xff0c;主频为1.4GHz-1.6GHz&#xff0c;配备S5M8767 电源管理&#xff0c;集成USB HUB,选用高品质板对板连接器稳定可靠&#xff0c;大厂生产&#xff0c;做工精良。接口一应俱全&#xff0c;开发更简单,搭载全网通4G、支持WIFI、蓝牙、…

【银河麒麟操作系统真实案例分享】内存黑洞导致服务器卡死分析全过程

了解更多银河麒麟操作系统全新产品&#xff0c;请点击访问 麒麟软件产品专区&#xff1a;https://product.kylinos.cn 开发者专区&#xff1a;https://developer.kylinos.cn 文档中心&#xff1a;https://documentkylinos.cn 现象描述 机房显示器连接服务器后黑屏&#xff…