EC2で突然インスタンスが落ちた or 不調な場合に確認しておきたいこと

Overview

  • aws ec2 describe-instance-status --instance-ids で該当インスタンスInstanceStatusSystemStatus を確認する

内容

aws ec2 describe-instance-status --instance-ids を実行してaws側で問題がないのか確認する。

{
    "InstanceStatuses": [
        {
            "InstanceStatus": {
                "Status": "impaired",
                "Details": [
                    {
                        "Status": "failed",
                        "ImpairedSince": "2020-12-07T07:12:00.000Z",
                        "Name": "reachability"
                    }
                ]
            },
            "AvailabilityZone": "ap-northeast-1c",
            "InstanceId": "i-0b7c76cd65cdd67da",
            "InstanceState": {
                "Code": 16,
                "Name": "running"
            },
            "Events": [
                {
                    "Code": "instance-stop",
                    "InstanceEventId": "instance-event-057aea7218a39c050",
                    "Description": "The instance is running on degraded hardware",
                    "NotBefore": "2020-12-21T02:00:00.000Z"
                }
            ],
            "SystemStatus": {
                "Status": "impaired",
                "Details": [
                    {
                        "Status": "failed",
                        "ImpairedSince": "2020-12-07T07:12:00.000Z",
                        "Name": "reachability"
                    }
                ]
            }
        }
    ]
}

impaired なので障害発生中とわかる。

InstanceStatus

OS以上のインスタンスのステータスのこと。

instance-status.status - The status of the instance (ok | impaired | initializing | insufficient-data | not-applicable).

ref DescribeInstanceStatus - Amazon Elastic Compute Cloud

impaired が表示されていれば障害とわかる。

SystemStatus

物理層インスタンスのステータスのこと。

unicorn を Server::Starter 経由でhot deployを実現する

blog.livedoor.jp

上記の記事を参考にServer::Starter 経由でhot deployに対応したのでその記録です。

内容

Server::Starter をinstall

$ brew install cpanminus
$ sudo cpanm Server::Starter

$ ↪ cpanm Server::Starter
Server::Starter is up to date. (0.35)

$ perldoc -l Server::Starter
/usr/local/Cellar/perl/5.30.0/lib/perl5/site_perl/5.30.0/Server/Starter.pm

$ ↪ /usr/local/Cellar/perl/5.30.0/bin/start_server
server program not specified

Hello World! を返却するシンプルなアプリケーションファイルを記述。

  • config.ru
require 'rubygems'
require 'sinatra/base'

#worker_processes 2

class HelloApp < Sinatra::Base
  get '/' do
    'Hello World!'
  end
end
run HelloApp
# frozen_string_literal: true

worker_processes 8
timeout 30
preload_app true

current_dir = "./simple-unicorn"
working_directory current_dir

listen "#{current_dir}/tmp/sockets/unicorn.sock", backlog: 2048

if ENV.key?('SERVER_STARTER_PORT')
  fds = ENV['SERVER_STARTER_PORT'].split(';').map do |x|
    _path_or_port, fd = x.split('=', 2)
    fd
  end
  ENV['UNICORN_FD'] = fds.join(',')
  ENV.delete('SERVER_STARTER_PORT')
end

before_fork do |server, worker|
  $stdout.puts "before fork"
  # Throttle the master from forking too quickly by sleeping.  Due
  # to the implementation of standard Unix signal handlers, this
  # helps (but does not completely) prevent identical, repeated signals
  # from being lost when the receiving process is busy.
  sleep 1
end

status_file = "#{current_dir}/server.status"
after_fork do |server, worker|
  $stdout.puts "after fork"
  pids = File.readlines(status_file).map { |line| line.chomp.split(':') }.to_h
  old_gen = ENV['SERVER_STARTER_GENERATION'].to_i - 1

  if old_pid = pids[old_gen.to_s]
    begin
      sig = ((worker.nr + 1) >= server.worker_processes) ? :QUIT : :TTOU
      $stdout.puts "sig: #{sig}, old_pid.to_i: #{old_pid.to_i}"
      Process.kill(sig, old_pid.to_i)
    rescue Errno::ENOENT, Errno::ESRCH # rubocop:disable Lint/HandleExceptions
    end
  else
    $stdout.puts "not condition(old_pid = pids[old_gen.to_s]): #{old_pid = pids[old_gen.to_s]}"
  end
end

before_exec do |_|
  ENV['BUNDLE_GEMFILE'] = "#{current_dir}/Gemfile"
end

Server::Starter 経由でunicornの起動

↪ /usr/local/Cellar/perl/5.30.0/bin/start_server --path=/simple-unicorn/tmp/sockets/unicorn.sock --signal-on-term=QUIT --signal-on-hup=QUIT --status-file=/simple-unicorn/server.status --pid-file=/simple-unicorn/tmp/pids/unicorn.pid --kill-old-delay=5 -- bundle exec --keep-file-descriptors unicorn -c config/unicorn.conf config.ru
start_server (pid:61975) starting now...
starting new worker 61977
I, [2020-12-24T09:10:31.291689 #61977]  INFO -- : inherited addr=/simple-unicorn/tmp/sockets/unicorn.sock fd=4
I, [2020-12-24T09:10:31.291816 #61977]  INFO -- : Refreshing Gem list

unicornのpidを確認

↪ ps aux | grep unicorn
<user> 62057   0.0  0.0  4278320    872 s012  S+    9:10AM   0:00.00 grep --color=auto unicorn
<user> 62055   0.0  0.0  4364876   2080 s011  S+    9:10AM   0:00.00 unicorn worker[7] -c config/unicorn.conf config.ru
<user> 62054   0.0  0.0  4364876   2004 s011  S+    9:10AM   0:00.00 unicorn worker[6] -c config/unicorn.conf config.ru
<user> 62051   0.0  0.0  4364876   2080 s011  S+    9:10AM   0:00.00 unicorn worker[5] -c config/unicorn.conf config.ru
<user> 62043   0.0  0.0  4364876   2020 s011  S+    9:10AM   0:00.00 unicorn worker[4] -c config/unicorn.conf config.ru
<user> 62036   0.0  0.0  4364876   2132 s011  S+    9:10AM   0:00.00 unicorn worker[3] -c config/unicorn.conf config.ru
<user> 62026   0.0  0.0  4373068   2144 s011  S+    9:10AM   0:00.00 unicorn worker[2] -c config/unicorn.conf config.ru
<user> 62009   0.0  0.0  4364876   2144 s011  S+    9:10AM   0:00.00 unicorn worker[1] -c config/unicorn.conf config.ru
<user> 61991   0.0  0.0  4364876   2176 s011  S+    9:10AM   0:00.00 unicorn worker[0] -c config/unicorn.conf config.ru
<user> 61977   0.0  0.1  4365132  24988 s011  S+    9:10AM   0:00.45 unicorn master -c config/unicorn.conf config.ru
<user> 61975   0.0  0.1  4327700  11608 s011  S+    9:10AM   0:00.08 /usr/local/Cellar/perl/5.30.0/bin/start_server --path=/simple-unicorn/tmp/sockets/unicorn.sock --signal-on-term=QUIT --signal-on-hup=QUIT --status-file=/simple-unicorn/server.status --pid-file=/simple-unicorn/tmp/pids/unicorn.pid --kill-old-delay=5 -- bundle exec --keep-file-descriptors unicorn -c config/unicorn.conf config.ru

worker_processes が8なのを確認できるので、Server::Starter にHUPシグナルを送信しhot deployを行う。

$ kill -HUP 61975

プロセスを確認。

↪ ps aux | grep unicorn
<user> 62346   0.0  0.0  4293680    864 s012  S+    9:15AM   0:00.00 grep --color=auto unicorn
<user> 62344   0.0  0.0  4380236   2384 s011  S+    9:15AM   0:00.00 unicorn worker[1] -c config/unicorn.conf config.ru
<user> 62340   0.0  0.0  4380236   2268 s011  S+    9:15AM   0:00.00 unicorn worker[0] -c config/unicorn.conf config.ru
<user> 62331   0.0  0.2  4380492  25348 s011  S+    9:15AM   0:00.36 /usr/local/bin/unicorn -c config/unicorn.conf config.ru
<user> 62051   0.0  0.0  4364876   2124 s011  S+    9:10AM   0:00.01 unicorn worker[5] -c config/unicorn.conf config.ru
<user> 62043   0.0  0.0  4364876   2080 s011  S+    9:10AM   0:00.01 unicorn worker[4] -c config/unicorn.conf config.ru
<user> 62036   0.0  0.0  4364876   2172 s011  S+    9:10AM   0:00.01 unicorn worker[3] -c config/unicorn.conf config.ru
<user> 62026   0.0  0.0  4373068   2220 s011  S+    9:10AM   0:00.00 unicorn worker[2] -c config/unicorn.conf config.ru
<user> 62009   0.0  0.0  4364876   2184 s011  S+    9:10AM   0:00.00 unicorn worker[1] -c config/unicorn.conf config.ru
<user> 61991   0.0  0.0  4364876   2188 s011  S+    9:10AM   0:00.00 unicorn worker[0] -c config/unicorn.conf config.ru
<user> 61977   0.0  0.1  4365132  24996 s011  S+    9:10AM   0:00.45 unicorn master -c config/unicorn.conf config.ru
<user> 61975   0.0  0.1  4327700  11616 s011  S+    9:10AM   0:00.08 /usr/local/Cellar/perl/5.30.0/bin/start_server --path=/simple-unicorn/tmp/sockets/unicorn.sock --signal-on-term=QUIT --signal-on-hup=QUIT --status-file=/simple-unicorn/server.status --pid-file=/simple-unicorn/tmp/pids/unicorn.pid --kill-old-delay=5 -- bundle exec --keep-file-descriptors unicorn -c config/unicorn.conf config.ru

プロセスを確認し新規worker(6234462340)が立ち上がっていることが確認できる。 この状態で、status fileも確認すると古い世代とmaster、新しい世代とmasterの両方を確認。

↪ cat /simple-unicorn/server.status
1:61977
2:62331
↪ ps aux | grep unicorn
<user> 62363   0.0  0.0  4287536    884 s012  S+    9:15AM   0:00.00 grep --color=auto unicorn
<user> 62360   0.0  0.0  4380236   2208 s011  S+    9:15AM   0:00.00 unicorn worker[7] -c config/unicorn.conf config.ru
<user> 62359   0.0  0.0  4380236   2412 s011  S+    9:15AM   0:00.00 unicorn worker[6] -c config/unicorn.conf config.ru
<user> 62358   0.0  0.0  4380236   2260 s011  S+    9:15AM   0:00.00 unicorn worker[5] -c config/unicorn.conf config.ru
<user> 62357   0.0  0.0  4380236   2400 s011  S+    9:15AM   0:00.00 unicorn worker[4] -c config/unicorn.conf config.ru
<user> 62351   0.0  0.0  4380236   2364 s011  S+    9:15AM   0:00.00 unicorn worker[3] -c config/unicorn.conf config.ru
<user> 62350   0.0  0.0  4380236   2440 s011  S+    9:15AM   0:00.00 unicorn worker[2] -c config/unicorn.conf config.ru
<user> 62344   0.0  0.0  4380236   2412 s011  S+    9:15AM   0:00.00 unicorn worker[1] -c config/unicorn.conf config.ru
<user> 62340   0.0  0.0  4380236   2268 s011  S+    9:15AM   0:00.00 unicorn worker[0] -c config/unicorn.conf config.ru
<user> 62331   0.0  0.2  4380492  25348 s011  S+    9:15AM   0:00.37 unicorn master -c config/unicorn.conf config.ru
<user> 61975   0.0  0.1  4327700  11616 s011  S+    9:10AM   0:00.08 /usr/local/Cellar/perl/5.30.0/bin/start_server --path=/simple-unicorn/tmp/sockets/unicorn.sock --signal-on-term=QUIT --signal-on-hup=QUIT --status-file=/simple-unicorn/server.status --pid-file=/simple-unicorn/tmp/pids/unicorn.pid --kill-old-delay=5 -- bundle exec --keep-file-descriptors unicorn -c config/unicorn.conf config.ru

時間が経経過し、masterのpidが 61975 から 62331に代わり入れ替わっていることが確認できる。

statusファイルも次世代と新規masterに更新されていることも確認できた。

↪ cat /simple-unicorn/server.status
2:62331

おわり。

「トランプの最後通謀 墓穴を掘った習近平」を読んだ

rebuild.fm

宮川さんの話をしていた「敵の敵は味方」理論で中国の敵であるトランプさんを指示している人が日本にも多いのかな。

右寄りの本を読んでみた。

当初の目的は、

  • トランプさんのやってきたことは何か。
  • 習近平さんのいる中国に対してどのようなことが行われていたのか。

この2点を知るために本書を手に取った。

ドッグイヤーをつけた箇所を抜粋して記録。

中国が全人代で決定した国家安全法により香港の「一国二制度」は崩壊。

1997年、イギリスが香港を返還した時、中国は2047年までの50年間は「一国二制度」を堅持することを約束したが、それを反故した。

www.jiji.com

台湾からの通謀を無視したWHO

2019年12月31日に台湾からWHOに新型コロナウイルスが「人から人への感染がある」ことを警告していたが、WHOは中国の顔色を窺ってかこの警告を無視した。

www.afpbb.com

台湾はほぼ完全に「武漢ウイルス」を押さえ込んだ。1月24日から中国にいくのを禁止し、31日までに台湾に入ってきた中国人旅行客、6000人強を同日までに全員中国へ帰すなど政府の対応がしっかりされている。

medical.nikkeibp.co.jp

中国は超限戦を仕掛けている可能性がある

他国との戦争に勝つために新テロ戦や生物・化学兵器ハッカー戦、麻薬販売などを含む、ありとあらゆる手段を使うように提案し、中国政府と当局はそれを参考に戦争の準備を進めている。 これが超限戦で中国はこれを実施している可能性が高い。

www.kadokawa.co.jp

武漢ウイルスによる中国の死者数が少なすぎる。携帯電話の解約件数が死者数の実態を表すのでは?

この本に書かれているのは、5月末時点で人口比(十万人あたりの死者数)でみると、

  • ベルギー 82人
  • アメリカ 31人
  • 日本 0.7人
  • 中国 0.03 人

中国の桁数が少なすぎる。2020年1月の携帯電話の解約数は1447万台だったが、2020年2月に入ると2100万台まで増加している。この1ヶ月間で約650万台も契約解除が増えたことが死者数と関係していたのではないかと筆者の一人の坂東さんが疑っていて共感できた。

www.nna.jp

中国の尖閣諸島の領土権主張

1884年古賀辰四郎さんが探検隊を派遣して尖閣諸島を開拓したのがはじまり。その後、工場などが建設され村ができ、1895年の1月14日に日本の領土に編入する閣議決定がされ1896年に沖縄県八重山郡編入された。 1919年に中国福建省の31人の漁民が台風で遭難して尖閣諸島に漂着したのを島の人が助け、中華民国政府から尖閣諸島の人々に感謝状が渡された。1953年に人民日報には「沖縄の尖閣諸島」という記事が掲載された。

それにも関わらず、中国は突然尖閣諸島は台湾の一部で中国領土と主張し始めてくるのはちょっとな。。。。

https://www.mofa.go.jp/mofaj/area/senkaku/pdfs/senkaku.pdf

1953年1月8日付け人民日報記事

www.excite.co.jp

中国に対してトランプさんはどのようなことを行なったのか。

  • WHOからの脱退

中国が武漢ウイルス問題を隠蔽や、実質的な中国支配下にあることから米国が望むような改革を実行しないことで脱退を宣言。

president.jp

中国は軍部と民間の間に明確な区別がなくありとあらゆる手段で知的財産を搾取しているため、人民解放軍と関係があるような研究者や留学生を完全に排除する方針を打ち出し実施。

中国の法律により、中国企業は会計報告を外国に公開しないことになっている。にも関わらず、米国で上場するためには透明性の高いバランスシートの公開が求められるため、バランスシートを誤魔化して市場に上場しそれを今までの米国の政権は野放しにしてきた。

jp.reuters.com

jp.reuters.com

国民の資産である年金基金の投資で、一部が中国企業へ投資され中国の経済力を成長させていることも気になるところ。

  • 「国家安全法」制定への制裁

香港の「最恵国待遇」の取り消しを行政府に指示。アメリカは中国に対して高い関税を課していたが、香港は「最恵国待遇」があるため通常関税のままだった。そのため、香港を抜け穴として米国輸出することは可能だった。 また、米国が中国に禁止している特許も香港企業であれば利用することができた。それも今回「最恵国待遇」を取り消しにすれば利用できなくなる。 香港の自由を弾圧した政府関係者の米国への入国禁止や、保有している在米資産の凍結・没収なども行われる可能性も今後でてくる。

CentOS6サポート終了でyumミラーサイト参照先を書き換え

Overview

  • CentOS6のメンテナンスアップデートが2020/11/30で終了したので、それに伴いyumミラーサイトからも消えた。
  • ミラーサイトを参照している箇所を vault.centos.org に入れ替えた。

内容

CentOS6のサポートが終了し、yumが失敗するようになるので延命のためにミラーサイトを書き換える。

  • Before
[base]
name=CentOS-$releasever - Base
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

#released updates
[updates]
name=CentOS-$releasever - Updates
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
baseurl=http://mirror.centos.org/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

この状態で実行すると失敗する。

]$ yum search ffmpeg
Loaded plugins: fastestmirror, presto, versionlock
Loading mirror speeds from cached hostfile
YumRepo Error: All mirror URLs are not using ftp, http[s] or file.
 Eg. Invalid release/repo/arch combination/
removing mirrorlist with no valid mirrors: /var/tmp/yum-infra-hUIxzP/x86_64/6/base/mirrorlist.txt
Error: Cannot find a valid baseurl for repo: base
  • After
$ cat /etc/yum.repos.d/CentOS-SCLo-scl.repo
[base]
name=CentOS-6.5 - Base
baseurl=https://vault.centos.org/6.5/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
exclude=kernel kernel-devel
enabled=1

[updates]
name=CentOS-6.5 - Updates
baseurl=https://vault.centos.org/6.5/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
exclude=kernel kernel-devel kernel-firmware kernel-headers
enabled=1

[extras]
name=CentOS-6.5 - Extras
baseurl=https://vault.centos.org/6.5/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
exclude=kernel kernel-devel kernel-firmware kernel-headers
enabled=1

上記に書き換え実行するとパッケージの検索が出来るようになる。

$ yum search ffmpeg
Loaded plugins: fastestmirror, presto, versionlock
Loading mirror speeds from cached hostfile
epel/metalink                                                                                                                                                                                                                     | 4.8 kB     00:00
 * epel: d2lzkl7pfhq30w.cloudfront.net
base                                                                                                                                                                                                                              | 3.8 kB     00:00
Not using downloaded repomd.xml because it is older than what we have:
  Current   : Sat Jun 30 00:37:23 2018
  Downloaded: Wed Jan  8 18:48:10 2014
mobaconda                                                                                                                                                                                                                         | 1.6 kB     00:00
mobaconda                                                                                                                                                                                                                                        148/148
================================================================================================================== N/S Matched: ffmpeg ==================================================================================================================
ffmpeg-devel.x86_64 : Development package for ffmpeg
ffmpeg-libs.x86_64 : Libraries for ffmpeg
ffmpeg-rtmp-debuginfo.x86_64 : Debug information for package ffmpeg-rtmp
ffmpeg-rtmp-devel.x86_64 : Development package for ffmpeg-rtmp
ffmpeg-rtmp-libs.x86_64 : Libraries for ffmpeg-rtmp
ffmpeg28-debuginfo.x86_64 : Debug information for package ffmpeg28
ffmpeg28-devel.x86_64 : Development package for ffmpeg28
ffmpeg28-libs.x86_64 : Libraries for ffmpeg28
ffmpeg.x86_64 : Digital VCR and streaming server
ffmpeg-rtmp.x86_64 : Digital VCR and streaming server
ffmpeg28.x86_64 : Digital VCR and streaming server

  Name and summary matches only, use "search all" for everything