gpt4 book ai didi

amazon-ec2 - 使用用户数据、云初始化指令和元数据的合适场景是什么

转载 作者:行者123 更新时间:2023-12-03 07:16:38 25 4
gpt4 key购买 nike

请给我一些关于何时适当使用这些设置的建议? userdata、元数据和 cloud-init 指令似乎都实现了相同的目标。

按照AWS文档,当使用用户数据时,我可以通过这种方式设置一个灯服务器。

#!/bin/bash
yum update -y
amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2
yum install -y httpd mariadb-server
systemctl start httpd
systemctl enable httpd
usermod -a -G apache ec2-user
chown -R ec2-user:apache /var/www
chmod 2775 /var/www
find /var/www -type d -exec chmod 2775 {} \;
find /var/www -type f -exec chmod 0664 {} \;
echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php

不错,但是我也可以使用 cloud-init 指令,这对我来说看起来已经干净多了。

#cloud-config
repo_update: true
repo_upgrade: all

packages:
- httpd
- mariadb-server

runcmd:
- [ sh, -c, "amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2" ]
- systemctl start httpd
- sudo systemctl enable httpd
- [ sh, -c, "usermod -a -G apache ec2-user" ]
- [ sh, -c, "chown -R ec2-user:apache /var/www" ]
- chmod 2775 /var/www
- [ find, /var/www, -type, d, -exec, chmod, 2775, {}, \; ]
- [ find, /var/www, -type, f, -exec, chmod, 0664, {}, \; ]
- [ sh, -c, 'echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php' ]

然后我们在 Cloudformation 中有元数据值,说实话,它看起来比 cloud-init 指令更冗长。它是这样的

UserData:
Fn::Base64:
!Sub |
#!/bin/bash -xe
yum update -y aws-cfn-bootstrap
/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource AmazonLinuxInstance --region ${AWS::Region}
systemctl start httpd
Metadata:
AWS::CloudFormation::Init:
config:
packages:
yum:
httpd: []
mariadb-server: []
php: []
php-mysql: []
Tags:
- Key: Project
Value: Autoscale

使用这些技术的合适场景是什么?

最佳答案

在我看来,您考虑使用 cfn-init 和关联的元数据主要原因是您将其与 cfn-hup 结合使用。 .

UserData 的一个问题是它的模板更新不会导致实例中的更新。例如,如果您想要更改 UserDatahttpd 的某些配置设置,这可能会出现问题

当您使用cfn-initcfn-hup时,上述问题可以克服。对于许多人来说,更新元数据以及随后更新实例上的进程的能力是相对于UserData主要优势

关于amazon-ec2 - 使用用户数据、云初始化指令和元数据的合适场景是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63325170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com