【问题标题】:Ubuntu: Could not enable service cfn-hupUbuntu:无法启用服务 cfn-hup
【发布时间】:2017-02-07 04:20:14
【问题描述】:

我在 CloudFormation 模板上使用自定义 Ubuntu AMI,我收到一条错误消息,上面写着 CREATE_FAILED AWS::AutoScaling::AutoScalingGroup WebServerGroup Received 0 SUCCESS signal(s) out of 1. Unable to satisfy 100% MinSuccessfulInstancesPercent requirement。我检查了/var/log/cfn-init.log 上创建的EC2 实例上的日志文件,我在日志文件上看到了Could not enable service cfn-hup。以下是我的模板部分:

"WebServerGroup": {
  "Type": "AWS::AutoScaling::AutoScalingGroup",
  "Properties": {
    "AvailabilityZones": {
      "Ref": "AZs"
    },
    "VPCZoneIdentifier": {
      "Ref": "Subnets"
    },
    "LaunchConfigurationName": {
      "Ref": "LaunchConfig"
    },
    "MinSize": "1",
    "MaxSize": "10",
    "DesiredCapacity": {
      "Ref": "InstanceCount"
    },
    "TargetGroupARNs" : [{"Ref": "TargetGroup"}],
    "Tags": [{
      "Key": "opsworks_stack_id",
      "Value": {
        "Ref": "OpsWorksStackId"
      },
      "PropagateAtLaunch": "true"
    }]
  },
  "CreationPolicy": {
    "ResourceSignal": {
      "Timeout": "PT15M"
    }
  },
  "UpdatePolicy": {
    "AutoScalingRollingUpdate": {
      "MinInstancesInService": "1",
      "MaxBatchSize": "1",
      "WaitOnResourceSignals": "true",
      "PauseTime": "PT15M"
    }
  }
},


"LaunchConfig" : {
  "Type" : "AWS::AutoScaling::LaunchConfiguration",
  "Metadata" : {
    "Comment" : "Install a simple application",
    "AWS::CloudFormation::Init" : {
      "config" : {

        "files" : {

          "/etc/cfn/cfn-hup.conf" : {
            "content" : { "Fn::Join" : ["", [
              "[main]\n",
                "stack=", { "Ref" : "AWS::StackId" }, "\n",
                "region=", { "Ref" : "AWS::Region" }, "\n"
              ]]},
            "mode"    : "000400",
            "owner"   : "root",
            "group"   : "root"
          },

          "/etc/cfn/hooks.d/cfn-auto-reloader.conf" : {
            "content": { "Fn::Join" : ["", [
              "[cfn-auto-reloader-hook]\n",
                "triggers=post.update\n",
                "path=Resources.LaunchConfig.Metadata.AWS::CloudFormation::Init\n",
                "action=/usr/local/bin/cfn-init -v ",
                "         --stack ", { "Ref" : "AWS::StackName" },
                "         --resource LaunchConfig ",
                "         --region ", { "Ref" : "AWS::Region" }, "\n",
                "runas=root\n"
              ]]}
          }
        },

        "services" : {
          "sysvinit" : {
            "cfn-hup" : { "enabled" : "true", "ensureRunning" : "true",
            "files" : ["/etc/cfn/cfn-hup.conf", "/etc/cfn/hooks.d/cfn-auto-reloader.conf"]}
            }
          }
        }
      }
    },

    "Properties" : {
      "AssociatePublicIpAddress" : "true",
      "ImageId" : { "Ref" : "AmiId" },
      "SecurityGroups" : [ { "Ref" : "LoadBalancerSecurityGroup" } ],
      "KeyName"        : { "Ref" : "KeyName" },
      "InstanceType" : { "Ref" : "InstanceType" },
      "IamInstanceProfile": {"Ref" :"RoleName"},
      "UserData"       : { "Fn::Base64" : { "Fn::Join" : ["", [
       "#!/bin/bash -xe\n",
       "easy_install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz\n",

       "/usr/local/bin/cfn-init -v ",
       "         --stack ", { "Ref" : "AWS::StackName" },
       "         --resource LaunchConfig ",
       "         --region ", { "Ref" : "AWS::Region" }, "\n",

       "/usr/local/bin/cfn-signal -e $? ",
       "         --stack ", { "Ref" : "AWS::StackName" },
       "         --resource WebServerGroup ",
       "         --region ", { "Ref" : "AWS::Region" }, "\n",

       "sed -i'' -e 's/.*requiretty.*//' /etc/sudoers", "\n",
       "pip install --upgrade awscli", "\n",

       "INSTANCE_ID=$(aws opsworks register ",
       "         --use-instance-profile ",
       "         --infrastructure-class ec2 ",
       "         --region ", { "Ref" : "AWS::Region" },
       "         --stack-id ", { "Ref" : "OpsWorksStackId" },
       "         --override-hostname $(tr -cd 'a-z' < /dev/urandom |head -c8) --local 2>&1 |grep -o 'Instance ID: .*' |cut -d' ' -f3)", "\n",

       "aws opsworks wait instance-registered ", 
       "         --region ", { "Ref" : "AWS::Region" },
       "         --instance-id $INSTANCE_ID", "\n",

       "aws opsworks assign-instance ",
       "         --region ", { "Ref" : "AWS::Region" },
       "         --instance-id $INSTANCE_ID ",
       "         --layer-ids ", { "Ref": "OpsWorksLayerId" }, "\n"
     ]]}}
    }
  }

【问题讨论】:

    标签: amazon-web-services ubuntu amazon-ec2 cloud amazon-cloudformation


    【解决方案1】:

    Ubuntu 18.04 和 YAML 中的新示例。它只是在 Ubuntu 实例上安装 apache2 并在其上设置 cfn-hup。这样您就可以在您的 cfn 模板中修改示例 index.html 的内容并进行更新。 cfn-hup 应该检测到更改(1 分钟间隔)并更新 index.html。

    要运行它,不要忘记更新UBUNTUAMI 与您所在的地区。还需要设置SecurityGroupId在80端口允许ssh和www

    ---
    
    Parameters:    
    
      InstanceProfile:
        Type: String
        Default: ''
    
      SecurityGroupId:
        Type: String
    
      KeyPairName: 
        Type: String
        Default: ''
        Description: A key pair for an instance for ssh    
    
    Mappings:
    
      UBUNTUAMI:
        us-east-1:
          HVM64: ami-07ebfd5b3428b6f4d
        ap-southeast-2:
          HVM64: ami-02a599eb01e3b3c5b      
    
    Conditions:
    
      HaveInstanceProfile:
        !Not [!Equals [!Ref InstanceProfile, '']]
    
      HaveKeyPair:
        !Not [!Equals [!Ref KeyPairName, '']]    
    
    
    Resources:
    
      MyInstance1:
        Type: AWS::EC2::Instance
    
        CreationPolicy:
          ResourceSignal:
            Timeout: PT10M
    
    
        Metadata:
          AWS::CloudFormation::Init: 
            configSets: 
              default_install: ["apache2", "cfn-hup"]
              update_apache: ["apache2"]
            apache2: 
              packages: 
                apt:
                  apache2: []
                  tmux: []
                  mc: []
              files: 
                /var/www/html/index.html: 
                  content: |
                    Hello World 1 from HOST_NAME                
              commands: 
                02_add_hostname:
                  command: sed -i s/HOST_NAME/$(hostname -f)/g /var/www/html/index.html
                03_start_and_enable_apache2:
                  command: "systemctl start apache2 && systemctl enable apache2"                
    
            cfn-hup:
              files:
                /etc/cfn/cfn-hup.conf:
                  content: !Sub |
                     [main]
                     stack=${AWS::StackId}
                     region=${AWS::Region}
                     interval=1
                     verbose=true
                  mode: '000400'
                  owner: root
                  group: root               
                /etc/cfn/hooks.d/cfn-auto-reloader.conf:
                  content: !Sub |
                     [cfn-auto-reloader-hook]
                     triggers=post.update
                     path=Resources.MyInstance1.Metadata.AWS::CloudFormation::Init.apache2
                     action=/usr/local/bin/cfn-init -v --stack ${AWS::StackId} --resource MyInstance1 --region ${AWS::Region} --configsets update_apache
                     runas=root
                  mode: '000400'
                  owner: root
                  group: root               
                /lib/systemd/system/cfn-hup.service:
                  content: |
                      [Unit]
                      Description=cfn-hup daemon
                      [Service]
                      Type=simple
                      ExecStart=/usr/local/bin/cfn-hup
                      Restart=always
                      [Install]
                      WantedBy=multi-user.target                         
              commands: 
                05_start_enable_cfn-hup:
                  command: "systemctl start cfn-hup && systemctl enable cfn-hup"
    
    
        Properties:                
          IamInstanceProfile: !If [HaveInstanceProfile, !Ref InstanceProfile, !Ref "AWS::NoValue"]
          ImageId: !FindInMap [UBUNTUAMI, !Ref "AWS::Region", HVM64]    
          InstanceType: t2.micro
          KeyName: !If [HaveKeyPair, !Ref KeyPairName, !Ref "AWS::NoValue"]      
          Monitoring: false            
          SecurityGroupIds: [!Ref SecurityGroupId]
          #SourceDestCheck: Boolean
          UserData: 
            Fn::Base64: !Sub |
                    #!/bin/bash -xe
    
                    apt update -y 
    
                    apt install -y python-pip
    
                    pip install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz   
    
                    /usr/local/bin/cfn-init -v \
                      --configsets default_install  \
                      --stack ${AWS::StackName} \
                      --resource MyInstance1 \
                      --region ${AWS::Region}      
    
                    is_hup_active=$(systemctl is-active cfn-hup)  
    
                    [[ ${!is_hup_active} == "active" ]]
    
                    /usr/local/bin/cfn-signal -e $? \
                      --stack ${AWS::StackName} \
                      --resource MyInstance1 \
                      --region ${AWS::Region}                        
          Tags: 
            - Key: Owner
              Value: ExampleUser
    
    Outputs:
    
        PublicIp:
            Value: !GetAtt MyInstance1.PublicIp
    
        InstanceId:
            Value: !Ref MyInstance1
    

    【讨论】:

    • 我的可执行文件最终位于 /opt/aws/bin/cfn-hup
    • @MarkHayward 你能解释一下你的意思吗?你有任何错误吗?
    【解决方案2】:

    我弄清楚我的代码有什么问题。它是cfn-hup 服务。安装后不启动。查看用户数据的变化。以下是更新后的代码。

    "LaunchConfig" : {
      "Type" : "AWS::AutoScaling::LaunchConfiguration",
      "Metadata" : {
        "Comment" : "Install a simple application",
        "AWS::CloudFormation::Init" : {
          "config" : {
    
            "files" : {
    
              "/etc/cfn/cfn-hup.conf" : {
                "content" : { "Fn::Join" : ["", [
                  "[main]\n",
                    "stack=", { "Ref" : "AWS::StackId" }, "\n",
                    "region=", { "Ref" : "AWS::Region" }, "\n"
                  ]]},
                "mode"    : "000400",
                "owner"   : "root",
                "group"   : "root"
              },
    
              "/etc/cfn/hooks.d/cfn-auto-reloader.conf" : {
                "content": { "Fn::Join" : ["", [
                  "[cfn-auto-reloader-hook]\n",
                    "triggers=post.update\n",
                    "path=Resources.LaunchConfig.Metadata.AWS::CloudFormation::Init\n",
                    "action=/usr/local/bin/cfn-init -v ",
                    "         --stack ", { "Ref" : "AWS::StackName" },
                    "         --resource LaunchConfig ",
                    "         --region ", { "Ref" : "AWS::Region" }, "\n",
                    "runas=root\n"
                  ]]}
              }
            },
    
            "services" : {
              "sysvinit" : {
                "cfn-hup" : { "enabled" : "true", "ensureRunning" : "true",
                "files" : ["/etc/cfn/cfn-hup.conf", "/etc/cfn/hooks.d/cfn-auto-reloader.conf"]}
                }
              }
            }
          }
        },
    
        "Properties" : {
          "AssociatePublicIpAddress" : "true",
          "ImageId" : { "Ref" : "AmiId" },
          "SecurityGroups" : [ { "Ref" : "LoadBalancerSecurityGroup" } ],
          "KeyName"        : { "Ref" : "KeyName" },
          "InstanceType" : { "Ref" : "InstanceType" },
          "IamInstanceProfile": {"Ref" :"RoleName"},
          "UserData"       : { "Fn::Base64" : { "Fn::Join" : ["", [
           "#!/bin/bash \n",
           "easy_install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz\n",
           "sudo ln /usr/local/bin/cfn-hup /etc/init.d/ \n",
           "sudo initctl reload-configuration  \n",
           "sudo chmod 700 /etc/init.d/cfn-hup  \n",
           "sudo chown root:root /etc/init.d/cfn-hup \n",
           "sudo update-rc.d cfn-hup defaults  \n",
           "sudo update-rc.d cfn-hup enable  \n",
           "/usr/local/bin/cfn-init -v ",
           "         --stack ", { "Ref" : "AWS::StackName" },
           "         --resource LaunchConfig ",
           "         --region ", { "Ref" : "AWS::Region" }, "\n",
    
           "/usr/local/bin/cfn-signal -e $? ",
           "         --stack ", { "Ref" : "AWS::StackName" },
           "         --resource WebServerGroup ",
           "         --region ", { "Ref" : "AWS::Region" }, "\n",
    
           "sed -i'' -e 's/.*requiretty.*//' /etc/sudoers", "\n",
           "pip install --upgrade awscli", "\n",
    
           "INSTANCE_ID=$(aws opsworks register ",
           "         --use-instance-profile ",
           "         --infrastructure-class ec2 ",
           "         --region ", { "Ref" : "AWS::Region" },
           "         --stack-id ", { "Ref" : "OpsWorksStackId" },
           "         --override-hostname $(tr -cd 'a-z' < /dev/urandom |head -c8) --local 2>&1 |grep -o 'Instance ID: .*' |cut -d' ' -f3)", "\n",
    
           "aws opsworks wait instance-registered ", 
           "         --region ", { "Ref" : "AWS::Region" },
           "         --instance-id $INSTANCE_ID", "\n",
    
           "aws opsworks assign-instance ",
           "         --region ", { "Ref" : "AWS::Region" },
           "         --instance-id $INSTANCE_ID ",
           "         --layer-ids ", { "Ref": "OpsWorksLayerId" }, "\n"
         ]]}}
        }
      }
    

    【讨论】:

    • 这对我有用。亚太地区的 RHEL 7.5 HVM。
    猜你喜欢
    • 1970-01-01
    • 2018-06-26
    • 2014-11-27
    • 2020-06-17
    • 2017-08-31
    • 1970-01-01
    • 2022-06-14
    • 1970-01-01
    • 2017-04-23
    相关资源
    最近更新 更多