在AWS的免费套餐中进行Twitter分析 – 环境准备篇

我利用AWS的免费套餐创建了一个环境,用于分析包含特定关键词的推文数量的时间序列。为了备忘录的目的,我总结了当时的操作。虽然我的目标是分析本文中的数据,但由于t2.micro的内存不足,我最终决定使用Python的tweepy来处理Logstash的部分。

我想做的事情

    • AWSの無料枠を活用してTwitterの分析を実施する

 

    • Logstashで特定のキーワードを含むツイートを取得

 

    • Logstashがツイートの情報をElasticsearchに格納

 

    Kibanaを使って可視化

这篇文章的内容( de )

    • AWSインスタンスの立ち上げと基本設定

 

    • Elasticsearchのインストールと初期設定

 

    • Kibanaのインストールと初期設定

 

    Logstashのインストール

由于这一点已经很庞大了,因此Twitter的分析将在另一篇文章中总结。

启动 AWS EC2 实例

首先,在AWS上启动EC2实例。

06_完了.png

EC2实例的基本设置

基本设定

首先,我们会进行基本设置和必要程序的安装。

% sudo apt-get update
% sudo apt-get -y upgrade
% sudo apt-get -y install vim zsh python3 python3-pip

登入用户的密码设置

接下来会设置用户的密码。如果不做这个操作,chsh命令会要求输入密码并且通不过。默认情况下,/etc/shadow文件中的密码字段会被设置为”*”,这样就不能通过密码认证。

% sudo passwd ubuntu
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully

将Shell改为zsh

将默认的Shell更改为zsh。这完全是出于个人偏好。

% chsh
Password: 
Changing the login shell for ubuntu
Enter the new value, or press ENTER for the default
    Login Shell [/bin/bash]: /usr/bin/zsh

Zsh和Vim的设置

我会为zsh和vim添加我喜欢的设置。这完全根据我的个人喜好。

    • zshの設定はこれ https://qiita.com/cielo1985/items/6d32e2ec7c74ee60dca7

vimの設定はこれ https://qiita.com/cielo1985/items/8b26cb292a8ef99255b7

然而,如果不将vim升级到8版本,就无法安装dein插件管理器,因此请按照以下步骤升级版本。
https://itsfoss.com/vim-8-release-install/

% sudo add-apt-repository ppa:jonathonf/vim
% sudo apt update
% sudo apt install vim

这样一来,基本设置就完成了。

安装Elasticsearch

首先需要安装Java。

% sudo apt install openjdk-8-jdk
% java -version
openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-8u181-b13-0ubuntu0.16.04.1-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)

安装Elasticsearch

我根据这个步骤进行了执行。听说apt比apt-get好,所以我尝试将所有apt-get替换为apt来执行,但中间有时候会忘记更改,导致apt和apt-get混用(很抱歉)。

下载并安装公共签名密钥。

% wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -

根据信息可能需要安装apt-transport-https。这次安装的已经是最新版本,因此不需要额外操作。

% sudo apt install apt-transport-https
Reading package lists... Done
Building dependency tree       
Reading state information... Done
apt-transport-https is already the newest version (1.2.27).
0 upgraded, 0 newly installed, 0 to remove and 3 not upgraded.

将仓库定义保存到/etc/apt/sources.list.d/elastic-6.x.list文件中。

% echo "deb https://artifacts.elastic.co/packages/6.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-6.x.list
deb https://artifacts.elastic.co/packages/6.x/apt stable main

安装Elasticsearch

% sudo apt update && sudo apt install elasticsearch

Elasticsearch的自动启动配置

首先确认正在运行的是SysV init还是systemd。

% ps -p 1
  PID TTY          TIME CMD
    1 ?        00:00:04 systemd

这个Ubuntu似乎正在运行systemd。我们将继续使用systemd的步骤。

% sudo /bin/systemctl daemon-reload
% sudo /bin/systemctl enable elasticsearch.service

第二个命令报错了,可能是由于本地设置不正确的原因。注意到看,好像只有警告。还出现了从/etc/systemd/system/multi-user.target.wants/elasticsearch.service到/usr/lib/systemd/system/elasticsearch.service的符号链接创建成功的信息,可能是在正常工作。

% sudo /bin/systemctl enable elasticsearch.service
Synchronizing state of elasticsearch.service with SysV init with /lib/systemd/systemd-sysv-install...
Executing /lib/systemd/systemd-sysv-install enable elasticsearch
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
    LANGUAGE = (unset),
    LC_ALL = (unset),
    LANG = "ja_JP.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
    LANGUAGE = (unset),
    LC_ALL = (unset),
    LANG = "ja_JP.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
Created symlink from /etc/systemd/system/multi-user.target.wants/elasticsearch.service to /usr/lib/systemd/system/elasticsearch.service.

在执行了export LC_ALL=C之后,一切都正常进行。

% export LC_ALL=C
% sudo /bin/systemctl enable elasticsearch.service
Synchronizing state of elasticsearch.service with SysV init with /lib/systemd/systemd-sysv-install...
Executing /lib/systemd/systemd-sysv-install enable elasticsearch

我实际上在使用Python时,尝试使用pip安装模块时遇到了类似的错误。
当我在Python中输入pip3 insatall [module_name]时,会出现以下错误。

locale.Error: unsupported locale setting

这个时候,如果你把以下内容放进去,问题就会解决。在.zshrc文件中写下会比较好。

% export LC_ALL=C

话题有些偏离,但现在已经设置自动启动了。现在,让我们来启动Elasticsearch吧。

sudo systemctl start elasticsearch.service

追加注释:2018.08.20 可使用systemctl status命令来确认启动状态。如果显示为active(running),则表示正常。

% sudo systemctl status elasticsearch
● elasticsearch.service - Elasticsearch
   Loaded: loaded (/usr/lib/systemd/system/elasticsearch.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2018-08-20 20:06:43 JST; 38min ago
     Docs: http://www.elastic.co
 Main PID: 1130 (java)
    Tasks: 50
   Memory: 527.2M
      CPU: 47.966s
   CGroup: /system.slice/elasticsearch.service
           ├─1130 /usr/bin/java -Xms256m -Xmx256m -XX:+UseConcMarkSweepGC -
〜後略〜

在中国,本地化的方式右如下:
systemctl命令不会显示执行是否成功。我们会通过查看日志来确认是否正确启动。

% sudo ls -la /var/log/elasticsearch
total 32
drwxr-x--- 2 elasticsearch elasticsearch  4096 Aug 16 05:12 ./
drwxr-xr-x 9 root          syslog         4096 Aug 16 05:01 ../
-rw-r--r-- 1 elasticsearch elasticsearch  1021 Aug 16 05:12 gc.log.0.current
-rw-r--r-- 1 elasticsearch elasticsearch 20197 Aug 16 05:12 hs_err_pid13055.log

出现错误。尝试查看hs_err_pid13055.log文件。

% sudo head /var/log/elasticsearch/hs_err_pid13055.log
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 986513408 bytes for committing reserved memory.
# Possible reasons:
#   The system is out of physical RAM or swap space
#   In 32 bit mode, the process size limit was hit
# Possible solutions:
#   Reduce memory load on the system
#   Increase physical memory or swap space
#   Check if swap backing store is full

似乎遇到了内存不足的问题。尝试分配986,513,408字节但似乎失败了。我们要检查一下这个实例分配了多少内存。

% free
              total        used        free      shared  buff/cache   available
Mem:        1014516       56780      236744        3320      720992      771952
Swap:             0           0           0

总共有1,014,516字节,也就是约1G字节。您试图分配986M字节,但显然会失败。因此,我们需要调整内存分配量。

% sudo vi /etc/elasticsearch/jvm.options    

#-Xms1g
#-Xmx1g
-Xms256m
-Xmx256m

我会重新启动并确认是否成功。

% sudo systemctl start elasticsearch.service
% sudo ls -la /var/log/elasticsearch 
total 60
drwxr-x--- 2 elasticsearch elasticsearch  4096 Aug 16 05:23 ./
drwxr-xr-x 9 root          syslog         4096 Aug 16 05:01 ../
-rw-r--r-- 1 elasticsearch elasticsearch  4435 Aug 16 05:23 elasticsearch.log
-rw-r--r-- 1 elasticsearch elasticsearch     0 Aug 16 05:23 elasticsearch_access.log
-rw-r--r-- 1 elasticsearch elasticsearch     0 Aug 16 05:23 elasticsearch_deprecation.log
-rw-r--r-- 1 elasticsearch elasticsearch     0 Aug 16 05:23 elasticsearch_index_indexing_slowlog.log
-rw-r--r-- 1 elasticsearch elasticsearch     0 Aug 16 05:23 elasticsearch_index_search_slowlog.log
-rw-r--r-- 1 elasticsearch elasticsearch 22949 Aug 16 05:23 gc.log.0.current
-rw-r--r-- 1 elasticsearch elasticsearch 20197 Aug 16 05:12 hs_err_pid13055.log

由于没有出现包含「err」的文件,所以看起来很顺利。试着对Elasticsearch执行GET操作。

% curl http://localhost:9200/
{
  "name" : "tznUzlS",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "mC6muENgRCa5j0cWh7mRYg",
  "version" : {
    "number" : "6.3.2",
    "build_flavor" : "default",
    "build_type" : "deb",
    "build_hash" : "053779d",
    "build_date" : "2018-07-20T05:20:23.451332Z",
    "build_snapshot" : false,
    "lucene_version" : "7.3.1",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

这感觉很不错!我们将安装kuromoji插件以便进行日语搜索。
(参考)
插件安装方法
分析-kuromoji

% sudo /usr/share/elasticsearch/bin/elasticsearch-plugin install analysis-kuromoji

在这一步,Elasticsearch的安装已经完成。接下来是Kibana。

安装Kibana

Kibana的安装

根据此步骤进行操作。由于在Elasticsearch安装步骤中已完成了存储库的注册,因此从apt-get install命令开始。

% sudo apt-get update && sudo apt-get install kibana

与Elasticsearch相同,我们将其注册到systemd中。

% sudo /bin/systemctl daemon-reload
% sudo /bin/systemctl enable kibana.service
Synchronizing state of kibana.service with SysV init with /lib/systemd/systemd-sysv-install...
Executing /lib/systemd/systemd-sysv-install enable kibana

启动Kibana。

% sudo systemctl start kibana.service

由于不确定启动是否成功,所以我将通过curl访问Kibana来检查。Kibana的默认端口号是5601。
[补充说明:2018.08.20]确认的好方法是使用sudo systemctl status kibana。

% curl http://localhost:5601
<script>var hashRoute = '/app/kibana';
var defaultRoute = '/app/kibana';

var hash = window.location.hash;
if (hash.length) {
  window.location = hashRoute + hash;
} else {
  window.location = defaultRoute;
}</script>%          

听上去进展不错呢。

以下为中国母语的类似表达方式:

Kibana的配置设置

为允许外部访问,将server.host设置为0.0.0.0并重新启动Kibana。

% sudo vi /etc/kibana/kibana.yml

#server.host: "localhost"                                                                                                                                                                               
server.host: "0.0.0.0"

% sudo systemctl restart kibana.service

编辑AWS安全组

修改AWS安全组设置,以便从外部也能够访问5601端口。

首先,确认正在运行Kibana的实例的安全组。

スクリーンショット 2018-08-16 14.36.06.png

我們將在該安全組的設定中添加一行,以允許TCP/5601的訪問。

スクリーンショット 2018-08-16 14.35.41.png

最后确认

我尝试访问Kibana。如果出现这样的界面,就表示成功!

スクリーンショット 2018-08-16 14.47.03.png

安装Logstash

最后我们需要安装Logstash。如果之前的步骤都完成了,只需要输入一行就可以了。

% sudo apt-get update && sudo apt-get install logstash

Using provided startup.options file: /etc/logstash/startup.options
OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000c5330000, 986513408, 0) failed; error='Cannot allocate memory' (errno=12)%] 
/usr/share/logstash/bin/system-install: line 88: #: command not found
chmod: cannot access '/etc/default/logstash': No such file or directory
dpkg: error processing package logstash (--configure):
 subprocess installed post-installation script returned error exit status 1
Errors were encountered while processing:
 logstash
E: Sub-process /usr/bin/dpkg returned an error code (1)

原本以为没有问题。看起来内存分配似乎没有成功。
因为jvm.options中设置了1g的分配,所以将其更改为256m。

% sudo vi /etc/logstash/jvm.options

#-Xms1g
#-Xmx1g
-Xms256m
-Xmx256m

我会再次执行。

% sudo apt install logstash
Reading package lists... Done
Building dependency tree       
Reading state information... Done
logstash is already the newest version (1:6.3.2-1).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
1 not fully installed or removed.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] y
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
    LANGUAGE = (unset),
    LC_ALL = (unset),
    LANG = "ja_JP.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
Setting up logstash (1:6.3.2-1) ...
Using provided startup.options file: /etc/logstash/startup.options
Successfully created system startup script for Logstash

这次似乎顺利了。

% sudo systemctl start logstash
% sudo systemctl status logstash
% sudo systemctl status logstash.service
● logstash.service - logstash
   Loaded: loaded (/etc/systemd/system/logstash.service; disabled; vendor preset: enabled)
   Active: active (running) since Sat 2018-08-18 21:10:37 UTC; 2min 21s ago
 Main PID: 1717 (java)
    Tasks: 13
   Memory: 192.3M
      CPU: 7.730s
   CGroup: /system.slice/logstash.service
           └─1717 /usr/bin/java -Xms256m -Xmx256m -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly 

Aug 18 21:10:37 ip-172-31-38-107 systemd[1]: Started logstash.

仅限于免费套餐所特有的问题

我已经按照上述步骤成功安装了Elasticsearch、Kibana和Logstash。然而,当我全部启动时,内存不足,甚至无法进行ssh连接。我尝试在jvm.options中将内存从256m减少到128m等等,但是Elasticsearch使用了大约50%的内存(即使Xmx和Xms设置为128m也没有减少),Kibana使用了17%的内存并且其他服务已经无法正常运行了,就是这样的情况。

因此,我决定放弃Logstash,改用Python的tweepy来获取Twitter的数据。

[补充:2018年08月20日] Elasticsearch和Kibana单独使用就已经占用了近900M的内存,有时候甚至无法连接ssh。在这种情况下,我只能重新启动来恢复正常,但是一段时间后又会卡住。也许使用免费账户无法承受如此重的负荷。

关于未来

迄今为止,由于环境准备通常是一拍即合的,很难再现,我详细总结了一下。为了总结这些内容,我重新启动了大约三次实例,并进行了相同的步骤。即使我认为我已经详尽地记录了所有步骤,但仍有些遗漏,每次都会有几次这样的情况发生:“啊,我忘记了这个”。我认为总结到这一步骤就可以了,所以我终于可以开始进行下一次真实的Twitter分析了。由于内容变得很长,我将会分开成多篇文章。

广告
将在 10 秒后关闭
bannerAds