为什么我的SystemD服务无法在重新启动后重新启动?

时间:2020-07-22 13:59:42

标签: amazon-ec2 systemd amazon-ami hashicorp-packer

我正在使用Packer构建AMI,该Packer配置了自定义SystemD单元。然后将AMI部署到EC2。问题是,如果EC2重新启动,则设备不会重新启动。

这是我的单位:

[Unit]
Description=My service
After=network.target
StartLimitIntervalSec=0
StartLimitAction=reboot

[Service]
Type=simple
Restart=always
RestartSec=30
User=ubuntu
ExecStart=/opt/app/app

[Install]
WantedBy=multi-user.target

这是我的Packer配置:

{
  "variables": {
    "aws_access_key": "{{env `AWS_ACCESS_KEY_ID`}}",
    "aws_secret_key": "{{env `AWS_SECRET_ACCESS_KEY`}}",
    "region":         "{{env `AWS_REGION`}}"
  },
  "builders": [
    {
      "access_key": "{{user `aws_access_key`}}",
      "ami_name": "my-app-{{timestamp}}",
      "instance_type": "t2.micro",
      "region": "{{user `region`}}",
      "secret_key": "{{user `aws_secret_key`}}",
      "source_ami_filter": {
        "filters": {
        "virtualization-type": "hvm",
        "name": "ubuntu/images/*ubuntu-bionic-18.04-amd64-server-*",
        "root-device-type": "ebs"
        },
        "owners": [ "099720109477" ],
        "most_recent": true
      },
      "ssh_username": "ubuntu",
      "type": "amazon-ebs"
    }
  ],
  "provisioners": [
    {
      "type": "shell",
      "script": "{{template_dir}}/provision.sh"
    },
    {
      "type": "file",
      "source": "{{template_dir}}/files/app.service",
      "destination": "/tmp/upload/etc/systemd/system/app.service"
    },
    {
      "type": "file",
      "source": "{{template_dir}}/../bin/Release/netcoreapp3.1/",
      "destination": "/tmp/upload/opt/app"
    },
    {
      "type": "shell",
      "inline": [
        "sudo rsync -a /tmp/upload/ /",
        "cd /opt/app",
        "sudo systemctl daemon-reload",
        "sudo systemctl enable app.service"
      ]
    }
  ]
}

奇怪的是,如果我通过SSH进入正在运行的EC2并启用该服务,则它在重启后确实会重启。

sudo systemctl enable app.service
sudo reboot

这使我认为我没有正确创建AMI,但是在Packer配置中,我启用该服务!

为什么我的AMI没有启用SystemD单元?

0 个答案:

没有答案