EC2用户数据脚本在Centos7 AMI上运行非常缓慢

时间:2019-01-08 18:37:37

标签: performance amazon-web-services amazon-ec2 centos user-data

每次用户数据脚本接触AWS市场上centos 7 AMI上的磁盘时,都会出现25秒的延迟。

这是我的剧本:

#!/bin/bash -ex
echo "[TIMER] START $(date +%s.%N)"
current_user=$(whoami)
echo "Running as: $current_user"
sudo id -u myuser &>/dev/null || sudo useradd myuser
echo "[TIMER] CreatedUser $(date +%s.%N)"
time sudo yum update -y
echo "[TIMER] Yum Update $(date +%s.%N)"
sudo mkdir -p /opt/myuser/resources
echo "[TIMER] Create /opt/myuser/resources $(date +%s.%N)"

sudo bash -c "cat > /etc/systemd/system/my-service.service" <<EOF
[Unit]
Description=My Service
After=network-online.target

[Service]
User=myuser
Group=myuser
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/bash -ex -c 'echo "Hello World"'

[Install]
Alias=my-service
WantedBy=default.target
EOF

echo "[TIMER] Make my-service.service $(date +%s.%N)"
sudo chmod 644 /etc/systemd/system/my-service.service
echo "[TIMER] Chmod $(date +%s.%N)"
sudo systemctl daemon-reload
echo "[TIMER] daemon-reload $(date +%s.%N)"
sudo systemctl enable my-service
echo "[TIMER] enable $(date +%s.%N)"
sudo systemctl start my-service
echo "[TIMER] END: my-service $(date +%s.%N)"

我启动了此AMI的c5.large并将其用作我的用户数据脚本:https://aws.amazon.com/marketplace/pp/B00O7WM7QW

计时器结果:

[TIMER] START 1546978269.809559549
[TIMER] CreatedUser 1546978320.472706964
[TIMER] Yum Update 1546978356.991642552
[TIMER] Create /opt/myuser/resources 1546978382.033044767
[TIMER] Make my-service.service 1546978407.074353857
[TIMER] Chmod 1546978432.111791937
[TIMER] daemon-reload 1546978457.195078083
[TIMER] enable 1546978482.265036318
[TIMER] END: my-service 1546978507.313735938
| CENTOS 7                                                  |                      |             |
|-----------------------------------------------------------|----------------------|-------------|
|                                                           |                      |             |
| log                                                       | timestamp            | seconds     |
| [TIMER] START 1546978269.809559549                        | 1546978269.809559549 |             |
| [TIMER] CreatedUser 1546978320.472706964                  | 1546978320.472706964 | 50.66315007 |
| [TIMER] Yum Update 1546978356.991642552                   | 1546978356.991642552 | 36.51893997 |
| [TIMER] Create /opt/myuser/resources 1546978382.033044767 | 1546978382.033044767 | 25.04139996 |
| [TIMER] Make my-service.service 1546978407.074353857      | 1546978407.074353857 | 25.04131007 |
| [TIMER] Chmod 1546978432.111791937                        | 1546978432.111791937 | 25.03743982 |
| [TIMER] daemon-reload 1546978457.195078083                | 1546978457.195078083 | 25.08328009 |
| [TIMER] enable 1546978482.265036318                       | 1546978482.265036318 | 25.06995988 |
| [TIMER] END: my-service 1546978507.313735938              | 1546978507.313735938 | 25.04870009 |
|                                                           |                      |             |
|                                                           | total (s)            | 237.50418   |
|                                                           |                      |             |
|                                                           | total (m)            | 3.958402999 |

如果在我的ASCII表中向右滚动,您会看到mkdirchmoduseradd之类的简单命令耗时25秒。为什么会这样?

编辑:

/etc/hosts

的内容
$ hostname
ip-172-31-40-213.us-west-2.compute.internal
$ cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

来自/var/log/messages的示例日志,systemd日志显示创建sudo会话需要25秒:

Jan  9 23:50:32 ip-172-31-35-166 cloud-init: + echo '[TIMER] Make my-service.service 1547077832.899069408'
Jan  9 23:50:32 ip-172-31-35-166 cloud-init: [TIMER] Make my-service.service 1547077832.899069408
Jan  9 23:50:32 ip-172-31-35-166 cloud-init: + sudo chmod 644 /etc/systemd/system/my-service.service
Jan  9 23:50:32 ip-172-31-35-166 systemd: Removed slice User Slice of root.
Jan  9 23:50:32 ip-172-31-35-166 systemd: Created slice User Slice of root.
Jan  9 23:50:32 ip-172-31-35-166 systemd: Started Session c3 of user root.
Jan  9 23:50:57 ip-172-31-35-166 cloud-init: ++ date +%s.%N
Jan  9 23:50:57 ip-172-31-35-166 cloud-init: + echo '[TIMER] Chmod 1547077857.946078493'
Jan  9 23:50:57 ip-172-31-35-166 cloud-init: [TIMER] Chmod 1547077857.946078493

journalctl日志显示了可能的罪魁祸首:

Jan 09 23:50:32 ip-172-31-35-166.us-west-2.compute.internal cloud-init[1197]: + echo '[TIMER] Make my-service.service 1547077832.899069408'
Jan 09 23:50:32 ip-172-31-35-166.us-west-2.compute.internal cloud-init[1197]: [TIMER] Make my-service.service 1547077832.899069408
Jan 09 23:50:32 ip-172-31-35-166.us-west-2.compute.internal cloud-init[1197]: + sudo chmod 644 /etc/systemd/system/my-service.service
Jan 09 23:50:32 ip-172-31-35-166.us-west-2.compute.internal systemd[1]: Removed slice User Slice of root.
Jan 09 23:50:32 ip-172-31-35-166.us-west-2.compute.internal sudo[13392]:     root : TTY=unknown ; PWD=/ ; USER=root ; COMMAND=/bin/chmod 644 /etc/systemd/system/my-service.service
Jan 09 23:50:32 ip-172-31-35-166.us-west-2.compute.internal systemd[1]: Created slice User Slice of root.
Jan 09 23:50:32 ip-172-31-35-166.us-west-2.compute.internal systemd[1]: Started Session c3 of user root.
Jan 09 23:50:57 ip-172-31-35-166.us-west-2.compute.internal sudo[13392]: pam_systemd(sudo:session): Failed to create session: Connection timed out
Jan 09 23:50:57 ip-172-31-35-166.us-west-2.compute.internal sudo[13392]: pam_unix(sudo:session): session opened for user root by (uid=0)
Jan 09 23:50:57 ip-172-31-35-166.us-west-2.compute.internal sudo[13392]: pam_unix(sudo:session): session closed for user root
Jan 09 23:50:57 ip-172-31-35-166.us-west-2.compute.internal cloud-init[1197]: ++ date +%s.%N
Jan 09 23:50:57 ip-172-31-35-166.us-west-2.compute.internal cloud-init[1197]: + echo '[TIMER] Chmod 1547077857.946078493'
Jan 09 23:50:57 ip-172-31-35-166.us-west-2.compute.internal cloud-init[1197]: [TIMER] Chmod 1547077857.946078493

更多,我发现:https://github.com/systemd/systemd/issues/2863

此问题已在更高版本的systemd中修复,但AWS EC2上的centos随附系统版本219,我自己无法真正对其进行更新。有什么建议么?我可以放置一些配置来避免此问题吗?我可以在用户数据脚本中删除sudo的 most 个实例,但是我确实需要它,例如:

sudo -H -u myuser bash -ex <<EOF
  ... commands
EOF

FWIW Amazon Linux 2带有相同版本的systemd,但没有表现出这种行为。

0 个答案:

没有答案