ElasticSearch訪問控制

image.png
# 安裝elasticsearch
# 調(diào)高JVM線程數(shù)限制數(shù)量
echo "vm.max_map_count=262144" >> /etc/sysctl.conf
sysctl -p

# 創(chuàng)建配置文件目錄
mkdir -p /etc/elasticsearch

# 創(chuàng)建數(shù)據(jù)目錄及權(quán)限
mkdir /data
chmod 777 /data

# 創(chuàng)建配置文件
cat <<"EOF" >/etc/elasticsearch/elasticsearch.yml
cluster.name: "elasticsearch-cluster"
node.name: elasticsearch-node
network.host: 0.0.0.0
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true
EOF

# 如果機器內(nèi)存比較小充易,可以調(diào)整JVM內(nèi)存
cat <<"EOF" >/etc/elasticsearch/jvm.options
## JVM configuration

################################################################
## IMPORTANT: JVM heap size
################################################################
##
## You should always set the min and max JVM heap
## size to the same value. For example, to set
## the heap to 4 GB, set:
##
## -Xms4g
## -Xmx4g
##
## See https://www.elastic.co/guide/en/elasticsearch/reference/current/heap-size.html
## for more information
##
################################################################

# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space

-Xms256m
-Xmx256m

################################################################
## Expert settings
################################################################
##
## All settings below this section are considered
## expert settings. Don't tamper with them unless
## you understand what you are doing
##
################################################################

## GC configuration
8-13:-XX:+UseConcMarkSweepGC
8-13:-XX:CMSInitiatingOccupancyFraction=75
8-13:-XX:+UseCMSInitiatingOccupancyOnly

## G1GC Configuration
# NOTE: G1 GC is only supported on JDK version 10 or later
# to use G1GC, uncomment the next two lines and update the version on the
# following three lines to your version of the JDK
# 10-13:-XX:-UseConcMarkSweepGC
# 10-13:-XX:-UseCMSInitiatingOccupancyOnly
14-:-XX:+UseG1GC
14-:-XX:G1ReservePercent=25
14-:-XX:InitiatingHeapOccupancyPercent=30

## DNS cache policy
# cache ttl in seconds for positive DNS lookups noting that this overrides the
# JDK security property networkaddress.cache.ttl; set to -1 to cache forever
-Des.networkaddress.cache.ttl=60
# cache ttl in seconds for negative DNS lookups noting that this overrides the
# JDK security property networkaddress.cache.negative ttl; set to -1 to cache
# forever
-Des.networkaddress.cache.negative.ttl=10

## optimizations

# pre-touch memory pages used by the JVM during initialization
-XX:+AlwaysPreTouch

## basic

# explicitly set the stack size
-Xss1m

# set to headless, just in case
-Djava.awt.headless=true

# ensure UTF-8 encoding by default (e.g. filenames)
-Dfile.encoding=UTF-8

# use our provided JNA always versus the system one
-Djna.nosys=true

# turn off a JDK optimization that throws away stack traces for common
# exceptions because stack traces are important for debugging
-XX:-OmitStackTraceInFastThrow

# flags to configure Netty
-Dio.netty.noUnsafe=true
-Dio.netty.noKeySetOptimization=true
-Dio.netty.recycler.maxCapacityPerThread=0

# log4j 2
-Dlog4j.shutdownHookEnabled=false
-Dlog4j2.disable.jmx=true

-Djava.io.tmpdir=${ES_TMPDIR}

## heap dumps

# generate a heap dump when an allocation from the Java heap fails
# heap dumps are created in the working directory of the JVM
-XX:+HeapDumpOnOutOfMemoryError

# specify an alternative path for heap dumps; ensure the directory exists and
# has sufficient space
-XX:HeapDumpPath=data

# specify an alternative path for JVM fatal error logs
-XX:ErrorFile=logs/hs_err_pid%p.log

## JDK 8 GC logging

8:-XX:+PrintGCDetails
8:-XX:+PrintGCDateStamps
8:-XX:+PrintTenuringDistribution
8:-XX:+PrintGCApplicationStoppedTime
8:-Xloggc:logs/gc.log
8:-XX:+UseGCLogFileRotation
8:-XX:NumberOfGCLogFiles=32
8:-XX:GCLogFileSize=64m

# JDK 9+ GC logging
9-:-Xlog:gc*,gc+age=trace,safepoint:file=logs/gc.log:utctime,pid,tags:filecount=32,filesize=64m
# due to internationalization enhancements in JDK 9 Elasticsearch need to set the provider to COMPAT otherwise
# time/date parsing will break in an incompatible way for some date patterns and locals
9-:-Djava.locale.providers=COMPAT

# temporary workaround for C2 bug with JDK 10 on hardware with AVX-512
10-:-XX:UseAVX=2
EOF

# 拉取鏡像
docker pull elasticsearch:6.8.7
# 運行
docker run -d --restart=always -p 9200:9200 -p 9300:9300 -v /etc/elasticsearch/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /etc/elasticsearch/jvm.options:/usr/share/elasticsearch/config/jvm.options -v /data:/usr/share/elasticsearch/data --name elasticsearch elasticsearch:6.8.7

# 配置TLS
docker exec -it elasticsearch bash
bin/elasticsearch-certutil cert -out config/elastic-certificates.p12 -pass ""
exit
# 復制證書到物理機
docker cp elasticsearch:/usr/share/elasticsearch/config/elastic-certificates.p12 /etc/elasticsearch/elastic-certificates.p12
# 修改證書權(quán)限
chmod 660 /etc/elasticsearch/elastic-certificates.p12

# 修改elasticsearch配置 添加以下配置
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: elastic-certificates.p12

# 重新部署
docker stop elasticsearch
docker rm elasticsearch
docker run -d --restart=always -p 9200:9200 -p 9300:9300 -v /etc/elasticsearch/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /etc/elasticsearch/jvm.options:/usr/share/elasticsearch/config/jvm.options -v /etc/elasticsearch/elastic-certificates.p12:/usr/share/elasticsearch/config/elastic-certificates.p12 -v /data:/usr/share/elasticsearch/data --name elasticsearch elasticsearch:6.8.7

# 設(shè)置密碼(可隨機可手動指定)
docker exec -it elasticsearch bash
bin/elasticsearch-setup-passwords auto # 自動生成隨機密碼
bin/elasticsearch-setup-passwords interactive # 手動配置

Initiating the setup of passwords for reserved users elastic,apm_system,kibana,logstash_system,beats_system,remote_monitoring_user.
The passwords will be randomly generated and printed to the console.
Please confirm that you would like to continue [y/N]y


Changed password for user apm_system
PASSWORD apm_system = QG0I9LS9ytRKXOEwzeHs

Changed password for user kibana
PASSWORD kibana = hwc02uXgKdHgQPqAQbIL

Changed password for user logstash_system
PASSWORD logstash_system = njSslSbuVPfPLb3HCbj2

Changed password for user beats_system
PASSWORD beats_system = UCAwd9Y6ZMEZVTV1OrZ4

Changed password for user remote_monitoring_user
PASSWORD remote_monitoring_user = gmCVf8oFC3BaxOBI2M0f

Changed password for user elastic
PASSWORD elastic = mCO21RPJQYBmAze7x5R0

# 部署集群啟動其他節(jié)點即可

# 訪問測試
# 直接訪問拒絕
curl localhost:9200
{"error":{"root_cause":[{"type":"security_exception","reason":"missing authentication token for REST request [/]","header":{"WWW-Authenticate":"Basic realm=\"security\" charset=\"UTF-8\""}}],"type":"security_exception","reason":"missing authentication token for REST request [/]","header":{"WWW-Authenticate":"Basic realm=\"security\" charset=\"UTF-8\""}},"status":401}[root@izs3l77ihmekj0z ~]

# 帶密碼訪問成功
curl localhost:9200/ --user elastic:mCO21RPJQYBmAze7x5R0
{
  "name" : "elasticsearch-node",
  "cluster_name" : "elasticsearch-cluster",
  "cluster_uuid" : "ESg1ZrTiSsOeNeWCQmJNdg",
  "version" : {
    "number" : "6.8.7",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "c63e621",
    "build_date" : "2020-02-26T14:38:01.193138Z",
    "build_snapshot" : false,
    "lucene_version" : "7.7.2",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

# 查看集群狀態(tài)
curl localhost:9200/_cat/health?v --user elastic:mCO21RPJQYBmAze7x5R0
epoch      timestamp cluster               status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1585486040 12:47:20  elasticsearch-cluster green           1         1      1   1    0    0        0             0                  -                100.0%
# 查看索引狀態(tài) 密碼就存儲在.security-6這個索引中
curl localhost:9200/_cat/indices?v --user elastic:mCO21RPJQYBmAze7x5R0      
health status index       uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   .security-6 ECm7arRxRLqY0meJFf5ppA   1   0          6            0       19kb           19kb

# 安裝kibana
# 生成kibana配置文件
mkdir -p /etc/kibana
cat <<"EOF" >/etc/kibana/kibana.yml
# ** THIS IS AN AUTO-GENERATED FILE **
#

# Default Kibana configuration for docker target
server.name: kibana
server.host: "0"
elasticsearch.hosts: [ "http://172.24.35.68:9200" ]
xpack.monitoring.ui.container.elasticsearch.enabled: true
elasticsearch.username: "kibana"
elasticsearch.password: "hwc02uXgKdHgQPqAQbIL"
EOF
# 修改權(quán)限
chmod 777 /etc/kibana/kibana.yml

# 拉取鏡像
docker pull kibana:6.8.7

# 運行
docker run -d --restart=always --name=kibana -p 5601:5601 -v /etc/kibana/kibana.yml:/usr/share/kibana/config/kibana.yml kibana:6.8.7

# 訪問測試
kibana
kibana
# 安裝elasticsearch-head

# 下載源碼解壓
wget https://codeload.github.com/mobz/elasticsearch-head/zip/master -O elasticsearch-head-master.zip
unzip elasticsearch-head-master.zip
cd elasticsearch-head-master

# 制作elasticsearch-head鏡像
docker build -t elasticsearch-head:alpine -f Dockerfile-alpine .
Sending build context to Docker daemon  3.027MB
Step 1/6 : FROM node:alpine
 ---> 483343d6c5f5
Step 2/6 : WORKDIR /usr/src/app
 ---> Using cache
 ---> 6a4ff9cfd803
Step 3/6 : RUN npm install http-server
 ---> Using cache
 ---> d70acd0b5ac3
Step 4/6 : COPY . .
 ---> 9754e9da891e
Step 5/6 : EXPOSE 9100
 ---> Running in d1e07d5c93a9
Removing intermediate container d1e07d5c93a9
 ---> 89573a689ca3
Step 6/6 : CMD node_modules/http-server/bin/http-server _site -p 9100
 ---> Running in 7f6987a0240f
Removing intermediate container 7f6987a0240f
 ---> 9d4f61595780
Successfully built 9d4f61595780
Successfully tagged elasticsearch-head:alpine
# 安裝
docker run -d --restart=always -p 9100:9100 --name=elasticsearch-head elasticsearch-head:alpine

# 修改elasticsearch配置
http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type

# 重啟elasticsearch
docker restart elasticsearch

# 訪問測試
http://59.110.233.231:9100/?auth_user=elastic&auth_password=mCO21RPJQYBmAze7x5R0
elasticsearch-head
elasticsearch-head
# 參考文檔
https://www.elastic.co/cn/blog/getting-started-with-elasticsearch-security
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌卵迂,老刑警劉巖底扳,帶你破解...
    沈念sama閱讀 218,941評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件寂嘉,死亡現(xiàn)場離奇詭異,居然都是意外死亡班挖,警方通過查閱死者的電腦和手機鲁捏,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來萧芙,“玉大人碴萧,你說我怎么就攤上這事∧┕海” “怎么了破喻?”我有些...
    開封第一講書人閱讀 165,345評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長盟榴。 經(jīng)常有香客問我曹质,道長,這世上最難降的妖魔是什么擎场? 我笑而不...
    開封第一講書人閱讀 58,851評論 1 295
  • 正文 為了忘掉前任羽德,我火速辦了婚禮,結(jié)果婚禮上迅办,老公的妹妹穿的比我還像新娘宅静。我一直安慰自己,他們只是感情好站欺,可當我...
    茶點故事閱讀 67,868評論 6 392
  • 文/花漫 我一把揭開白布姨夹。 她就那樣靜靜地躺著,像睡著了一般矾策。 火紅的嫁衣襯著肌膚如雪磷账。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,688評論 1 305
  • 那天贾虽,我揣著相機與錄音逃糟,去河邊找鬼。 笑死蓬豁,一個胖子當著我的面吹牛绰咽,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播地粪,決...
    沈念sama閱讀 40,414評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼取募,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了驶忌?” 一聲冷哼從身側(cè)響起矛辕,我...
    開封第一講書人閱讀 39,319評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎付魔,沒想到半個月后聊品,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,775評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡几苍,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年翻屈,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片妻坝。...
    茶點故事閱讀 40,096評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡伸眶,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出刽宪,到底是詐尸還是另有隱情厘贼,我是刑警寧澤,帶...
    沈念sama閱讀 35,789評論 5 346
  • 正文 年R本政府宣布圣拄,位于F島的核電站嘴秸,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏庇谆。R本人自食惡果不足惜岳掐,卻給世界環(huán)境...
    茶點故事閱讀 41,437評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望饭耳。 院中可真熱鬧串述,春花似錦、人聲如沸寞肖。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,993評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春淫僻,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背频祝。 一陣腳步聲響...
    開封第一講書人閱讀 33,107評論 1 271
  • 我被黑心中介騙來泰國打工沧踏, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人欣簇。 一個月前我還...
    沈念sama閱讀 48,308評論 3 372
  • 正文 我出身青樓规脸,卻偏偏與公主長得像,于是被迫代替她去往敵國和親熊咽。 傳聞我的和親對象是個殘疾皇子莫鸭,可洞房花燭夜當晚...
    茶點故事閱讀 45,037評論 2 355

推薦閱讀更多精彩內(nèi)容