【问题标题】:How to Install and configure Redis on ElasticBeanstalk如何在 ElasticBeanstalk 上安装和配置 Redis
【发布时间】:2014-12-19 03:20:22
【问题描述】:

如何在 AWS ElasticBeanstalk 上安装和配置 Redis?有谁知道如何编写一个 .ebextension 脚本来实现这一点?

【问题讨论】:

    标签: redis amazon-elastic-beanstalk


    【解决方案1】:

    如果您使用 ElastiCache(如 RDS,但适用于 Memcached 或 Redis),则接受的答案非常好。但是,如果您要告诉 EB 将 Redis 配置到 EC2 实例中,它会在其中启动您的应用程序,那么您需要一个不同的配置文件,例如 this gist:

    packages: 
      yum:
        gcc-c++: [] 
        make: []
    sources:
      /home/ec2-user: http://download.redis.io/releases/redis-2.8.4.tar.gz
    commands:
      redis_build:
        command: make
        cwd: /home/ec2-user/redis-2.8.4
      redis_config_001:
        command: sed -i -e "s/daemonize no/daemonize yes/" redis.conf
        cwd: /home/ec2-user/redis-2.8.4
      redis_config_002:
        command: sed -i -e "s/# maxmemory <bytes>/maxmemory 500MB/" redis.conf
        cwd: /home/ec2-user/redis-2.8.4
      redis_config_003:
        command: sed -i -e "s/# maxmemory-policy volatile-lru/maxmemory-policy allkeys-lru/" redis.conf
        cwd: /home/ec2-user/redis-2.8.4
      redis_server:
        command: src/redis-server redis.conf
        cwd: /home/ec2-user/redis-2.8.4
    

    重要提示: 命令按名称按字母顺序执行,因此如果您选择的名称与 redis_buildredis_config_xxxredis_server 不同,请确保它们在你所期望的方式。

    您的另一个选择是使用 Docker 使用 Redis 将您的应用程序容器化,然后将您的应用程序部署为一定数量的 Docker 容器,而不是使用您编写的任何语言。为 Flask 应用程序执行此操作描述为 here

    您可以将它们全部塞入一个容器中并以这种方式进行部署,这更容易,但扩展性不好,或者您可以使用 AWS 的 Elastic Beanstalk 多容器部署。如果您使用过docker-compose,则可以使用this 工具将docker-compose.yml 转换为AWS 想要的形式Dockerrun.aws.json

    【讨论】:

    • 我们是否需要为 AWS 上的 Redis 创建一个新的 ElastiCache 集群?或者我们可以简单地在文件中添加上面的脚本并完成?
    • kpp,如果您使用的是弹性 beanstalk,这将在它提供的 VM 上安装 redis。这可能是你想要的。 ElastiCache 适用于您超出此范围时使用
    • 此解决方案假定您只有一个服务器,对吧?要按预期运行(单个逻辑 Redis),必须将安装配置为运行集群(Sentinel?)。不过,我对此还不够熟悉,无法说明如何实现。
    • 这是一个很好的观点,马丁,鉴于这个答案得到的流量,我应该更新它。但是自从写完这篇文章后不久我就没有和 Beanstalk 合作过,所以我不确定该怎么做。如果有人有建议,我会更新我的答案。
    【解决方案2】:

    AWS Elastic Beanstalk 确实通过 .ebextensions 文件夹提供 resource configuration。本质上,您需要告诉 Elastic Beanstalk 除了您的应用程序之外您还希望配置什么。用于配置到默认 vpc。你需要

    创建一个 .ebextensions 文件夹

    添加一个 elasticache.config 文件

    并包含以下内容。

    Resources:
      MyCacheSecurityGroup:
        Type: "AWS::EC2::SecurityGroup"
        Properties:
          GroupDescription: "Lock cache down to webserver access only"
          SecurityGroupIngress :
            - IpProtocol : "tcp"
              FromPort :
                Fn::GetOptionSetting:
                  OptionName : "CachePort"
                  DefaultValue: "6379"
              ToPort :
                Fn::GetOptionSetting:
                  OptionName : "CachePort"
                  DefaultValue: "6379"
              SourceSecurityGroupName:
                Ref: "AWSEBSecurityGroup"
      MyElastiCache:
        Type: "AWS::ElastiCache::CacheCluster"
        Properties:
          CacheNodeType:
            Fn::GetOptionSetting:
              OptionName : "CacheNodeType"
              DefaultValue : "cache.t1.micro"
          NumCacheNodes:
            Fn::GetOptionSetting:
              OptionName : "NumCacheNodes"
              DefaultValue : "1"
          Engine:
            Fn::GetOptionSetting:
              OptionName : "Engine"
              DefaultValue : "redis"
          VpcSecurityGroupIds:
            -
              Fn::GetAtt:
                - MyCacheSecurityGroup
                - GroupId
    
    Outputs:
      ElastiCache:
        Description : "ID of ElastiCache Cache Cluster with Redis Engine"
        Value :
          Ref : "MyElastiCache"
    

    引用自:“如何将 ElasticCache 资源添加到 Elastic Beanstalk VPC” http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-environment-resources-elasticache.html

    【讨论】:

    • 我这样做并得到了[Error: Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED]。可能命令 redis-server 尚未启动。有什么方法可以检查 Redis 状态?
    • @theChinmay redis 将独立部署,您的应用应该从环境变量中获取 IP 并连接到该 IP 而不是本地主机
    • @LucaG。您知道如何在 Amazon Elasticbeanstalk NodeJS 应用程序中获取 IP 或主机吗?谢谢
    • @TezroSolutions 以@TerryB 提供的示例为例,应该是这样的:option_settings:aws:elasticbeanstalk:application:environment:REDIS_CLUSTER: '`{ "Fn::GetAtt": ["MyElastiCache", "RedisEndpoint.Address"]}`' 供参考:docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/…
    • 只能在单个 docker 环境中工作。在多 docker 环境中不起作用
    猜你喜欢
    • 2015-10-18
    • 1970-01-01
    • 2015-03-28
    • 1970-01-01
    • 2014-11-21
    • 2013-07-19
    • 1970-01-01
    • 2016-02-27
    • 2021-11-03
    相关资源
    最近更新 更多