2018年11月22日木曜日

ラズパイでLet's Encryptの証明書更新に失敗

ラズパイを自宅用の各種サーバー(DNS、メール、ウェブ他)として使っています。httpsのサーバー証明書の発行・更新にはLet's Encriptを使っています。今日たまたま証明書を更新しようとすると下記のエラーに陥りました。何度やっても失敗します。


matsu@rpi001 ~/letsencript/certbot $ sudo ./certbot-auto renew 
Bootstrapping dependencies for Debian-based OSes... (you can skip this with --no-
bootstrap)
Hit http://archive.raspberrypi.org jessie InRelease                            
Hit http://mirrordirector.raspbian.org jessie InRelease                        
Hit http://archive.raspberrypi.org jessie/main Sources
Hit http://mirrordirector.raspbian.org jessie/main armhf Packages
Hit http://archive.raspberrypi.org jessie/ui Sources                           
Hit http://archive.raspberrypi.org jessie/main armhf Packages                  
Hit http://mirrordirector.raspbian.org jessie/contrib armhf Packages           
Hit http://archive.raspberrypi.org jessie/ui armhf Packages                    
Hit http://mirrordirector.raspbian.org jessie/non-free armhf Packages          
Hit http://mirrordirector.raspbian.org jessie/rpi armhf Packages               
Ign http://archive.raspberrypi.org jessie/main Translation-en_US               
Ign http://archive.raspberrypi.org jessie/main Translation-en                  
Ign http://archive.raspberrypi.org jessie/ui Translation-en_US                 
Ign http://archive.raspberrypi.org jessie/ui Translation-en                    
Ign http://mirrordirector.raspbian.org jessie/contrib Translation-en_US        
Ign http://mirrordirector.raspbian.org jessie/contrib Translation-en           
Ign http://mirrordirector.raspbian.org jessie/main Translation-en_US
Ign http://mirrordirector.raspbian.org jessie/main Translation-en
Ign http://mirrordirector.raspbian.org jessie/non-free Translation-en_US
Ign http://mirrordirector.raspbian.org jessie/non-free Translation-en
Ign http://mirrordirector.raspbian.org jessie/rpi Translation-en_US
Ign http://mirrordirector.raspbian.org jessie/rpi Translation-en
Reading package lists... Done
Reading package lists... Done
Building dependency tree       
Reading state information... Done
augeas-lenses is already the newest version.
ca-certificates is already the newest version.
gcc is already the newest version.
libaugeas0 is already the newest version.
libffi-dev is already the newest version.
libssl-dev is already the newest version.
openssl is already the newest version.
python is already the newest version.
python-dev is already the newest version.
python-virtualenv is already the newest version.
virtualenv is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 433 not upgraded.
Creating virtual environment...
Installing Python packages...
Had a problem while installing Python packages.

pip prints the following errors: 
=====================================================
Collecting argparse==1.4.0 (from -r /tmp/tmp.e1u7wLIvLq/letsencrypt-auto-requirements.txt (line 11))
  Downloading https://files.pythonhosted.org/packages/f2/94/3af39d34be01a24a6e65433d19e107099374224905f1e0cc6bbe1fd22a2f/argparse-1.4.0-py2.py3-none-any.whl
Collecting pycparser==2.14 (from -r /tmp/tmp.e1u7wLIvLq/letsencrypt-auto-require
(中略)

Certbot has problem setting up the virtual environment.

Based on your pip output, the problem can likely be fixed by 
increasing the available memory.

Consult https://certbot.eff.org/docs/install.html#problems-with-python-virtual-environment
for possible solutions.
You may also find some support resources at https://certbot.eff.org/support/ .

エラーメッセージにもあるように、純粋にメモリが足りない模様。確かにsyslogを見ると、OOMキラー(メモリ不足のときに動作中のプロセスが終了させられる仕組み)も発動していました。Let's Encriptは毎回自分自身をアップデートするので、アップデートのせいでメモリを多く使うようになったのか、とこの時は思いました。

OOMキラーで必要なdaemonが停止させられた可能性もあるためまずはリブート。物理メモリは簡単には増やせないので、swapを100Mから2GBに増やしました。その上で実行したところ、正常動作して期待通りサーバー証明書が更新されました。まずはめでたし。

しかし、実行中でtopコマンドでメモリの状況を観測していたのですが、swapを使った形跡がありません。あれ?おかしいなと思い、リブート前のtopの結果(偶然ターミナルログに残っていた)とリブート後のtopの結果を比較してみました。

リブート前

top - 11:11:55 up 233 days, 36 min,  4 users,  load average: 5.59, 10.66, 6.33
Tasks: 383 total,   1 running, 382 sleeping,   0 stopped,   0 zombie
%Cpu(s):  1.3 us,  0.6 sy,  0.0 ni, 98.1 id,  0.0 wa,  0.0 hi,  0.1 si,  0.0 st
KiB Mem:    948120 total,   732324 used,   215796 free,     9424 buffers
KiB Swap:   102396 total,   102396 used,        0 free.    76364 cached Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND     
11277 dovecot   20   0   81700  79644   1556 S   0.0  8.4   6:18.62 auth        
 8018 bind      20   0  119980  32256    316 S   0.0  3.4 380:41.05 named       
32603 matsu     20   0   15716  11432   6224 S   0.7  1.2   0:00.99 getmail     
21817 news      20   0   14740   7236      4 S   0.0  0.8   0:01.26 controlchan 
11746 matsu     20   0   16748   6188      4 S   0.0  0.7   0:01.74 getmail     
31888 matsu     20   0   16748   6188      4 S   0.0  0.7   0:01.84 getmail     
 2736 matsu     20   0   16748   6184      4 S   0.0  0.7   0:01.81 getmail     
 4670 matsu     20   0   15716   5252      4 S   0.0  0.6   0:01.43 getmail     
29979 matsu     20   0   15716   5228      4 S   0.0  0.6   0:01.10 getmail     
22534 matsu     20   0   15716   5212      4 S   0.0  0.5   0:00.98 getmail     
 1193 matsu     20   0   15716   5208      4 S   0.0  0.5   0:00.56 getmail     
 1559 matsu     20   0   15716   5208      4 S   0.0  0.5   0:00.57 getmail     
 2525 matsu     20   0   15716   5208      4 S   0.0  0.5   0:01.00 getmail     
 2652 matsu     20   0   15716   5208      4 S   0.0  0.5   0:00.68 getmail     
 3074 matsu     20   0   15716   5208      4 S   0.0  0.5   0:00.57 getmail   

リブート後

top - 12:30:47 up  1:17,  5 users,  load average: 0.06, 0.47, 1.37
Tasks: 123 total,   1 running, 122 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.4 us,  0.2 sy,  0.0 ni, 99.3 id,  0.0 wa,  0.0 hi,  0.1 si,  0.0 st
KiB Mem:    948120 total,   626872 used,   321248 free,    33892 buffers
KiB Swap:  2097148 total,        0 used,  2097148 free.   468300 cached Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND     
  517 bind      20   0   90112  32568   6024 S   0.0  3.4   0:17.99 named       
 4600 matsu     20   0   15716  11636   6428 S   0.7  1.2   0:00.76 getmail     
  671 news      20   0   14740  10820   3588 S   0.0  1.1   0:01.42 controlchan 
 4502 www-data  20   0  237040   8148   4560 S   0.0  0.9   0:00.57 apache2     
  802 root      20   0   11888   7956   5444 S   0.0  0.8   0:03.49 apache2     
  664 news      20   0   14056   7884   4040 S   0.0  0.8   0:00.06 innd        
 4501 www-data  20   0  235672   5836   2408 S   0.0  0.6   0:00.14 apache2     
 4590 postfix   20   0    7516   5668   5108 S   0.0  0.6   0:00.07 smtpd       
 4243 postfix   20   0    7516   5652   5072 S   0.0  0.6   0:00.09 smtpd       
 1251 root      20   0   11636   5300   4624 S   0.0  0.6   0:00.29 sshd        
  630 root      20   0   11636   5268   4588 S   0.0  0.6   0:00.34 sshd        
 2561 root      20   0   11636   5224   4544 S   0.0  0.6   0:00.30 sshd        
 2506 root      20   0   11636   5208   4528 S   0.0  0.5   0:00.32 sshd        
 1466 root      20   0   11636   5148   4468 S   0.0  0.5   0:00.30 sshd        
 4500 www-data  20   0   11384   4772   2264 S   0.0  0.5   0:00.00 apache2     
 1264 matsu     20   0    6848   4500   2956 S   0.0  0.5   0:01.12 bash        
 2519 matsu     20   0    6848   4496   2956 S   0.0  0.5   0:00.55 bash    
どうやらリブート前は、dovecot(POPサーバー)のプロセスがメモリリークか何かでメモリを消費していた模様。メモリ消費量は80MBほどですが、1GBしかメモリがないラズパイでは、この差が効いたようです。リブート前は233日ほど表面的には問題なく動き続けていましたが、たまにはリブートした方が良いのかもしれません。






ラズパイでLet's Encryptの証明書更新に失敗

ラズパイを自宅用の各種サーバー(DNS、メール、ウェブ他)として使っています。httpsのサーバー証明書の発行・更新にはLet's Encriptを使っています。今日たまたま証明書を更新しようとすると下記のエラーに陥りました。何度やっても失敗します。 ma...