elasticsearch部署
推薦到官網下載ES源碼的打包,個人覺得比起通過yum等包管理工具安裝更靈活,且方便管理,比如
- 目錄自主可控,便于配置
- 安裝插件更方便
- 同義詞等詞庫詞庫維護
##ES長期運行
推薦使用進程管理工具來運行ES,這里使用supervisor,將es作為supervisor的子進程運行。
配置supervisord
[supervisord] ; .... nodaemon=false ; (start in foreground if true;default false)[program:elk_01] directory=/yikaoyan/elasticsearch-7.2.1 command=/usr/bin/bash bin/elasticsearch user=elk ; 注意,ES不能以root身份運行注意,上面的nodaemon=false,如果為true,通過systemctl命令將無法啟動supervisord服務。
安裝并設置supervisord服務開機自啟
systemctl enable supervisord // 允許開機啟動允許開機啟動后,將在/lib/systemd/system目錄下生成supervisord.service文件,編輯該文件,可以編輯supervisord的啟動命令,這里重新制定supervisor的配置文件目錄:
[Service] Type=forking ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf注意,每次修改完配置文件后(包括修改以上文件和supervisord.conf),都需要通過如下命令重載后再啟動服務:
systemctl daemon-reload systemctl start supervisord這樣每次啟動supervisord,就可以自動運行ES
ES非root用戶運行問題
es默認不允許以root身份運行,可以通過添加用戶并授權該用戶對es目錄的操作權限來解決:
# 添加用戶 useadd elk # 授權該用戶和用戶組對es目錄的操作權限 chown -R elk:elk elasticsearch-folder##ES認證和NGINX
es默認沒有認證,有未授權訪問漏洞的危險。商業套件x-pack中提供認證功能,但是需要授權,試用只有一個月。另外ES也有第三方插件search-guard提供安全方面的功能,但是生產環境中配置比較復雜。這里我采用nginx的Http Basic Authentication來做認證功能(官網文檔參考),并利用nginx將搜索請求通過端口轉發給es。下面是我的nginx配置參考:
upstream elk {server 127.0.0.1:9200; }server {listen 8080; # 監聽8080端口,并將請求轉發到本機9200端口access_log /var/log/nginx/statistics_access.log;client_max_body_size 4G;fastcgi_buffers 64 8K;client_body_buffer_size 1024k;keepalive_timeout 5;client_body_timeout 15;client_header_timeout 15;send_timeout 15;sendfile on;tcp_nopush on;tcp_nodelay on;location / {proxy_pass http://elk; # 重要,http://不可少proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_buffers 8 32k;proxy_buffer_size 64k;# 配置訪問賬號密碼auth_basic "auth required";auth_basic_user_file /etc/nginx/conf.d/htpasswd/es;} }出于安全考慮,再采取如下兩個措施:
-
編輯ES配置文件.../elasticsearch-7.2.1/config/elasticsearch.yml,綁定ES僅限本機訪問:
# ---------------------------------- Network ----------------------------------- # # Set the bind address to a specific IP (IPv4 or IPv6): # network.host: 127.0.0.1 # # Set a custom port for HTTP: # # http.port: 9200 -
在安全組中關閉其他端口的訪問,只開放上面8080端口的訪問。
連接ES
Kibana修改連接配置:
# elasticsearch.hosts: ["http://localhost:9200"] elasticsearch.hosts: ["http://xx.xx.xx.xxx:8080"]# ...# If your Elasticsearch is protected with basic authentication, these settings provide # the username and password that the Kibana server uses to perform maintenance on the Kibana # index at startup. Your Kibana users still need to authenticate with Elasticsearch, which # is proxied through the Kibana server. elasticsearch.username: "user" elasticsearch.password: "pwd"Python客戶端連接:
ES_CLIENT = Elasticsearch(['http://user:pwd@xx.xx.xx.xxx:8080'])注意,ES貌似不支持通過域名的方式訪問,必須指定IP和端口。
總結
以上是生活随笔為你收集整理的elasticsearch部署的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Cocos2d-x 在缓存创建图片
- 下一篇: CS0656缺少编译器要求的成员“Mic