Skip to content

Commit

Permalink
[ADD] Elasticsearch Full Cluster + Node Encryption พร้อมตั้งรหัสผ่าน …
Browse files Browse the repository at this point in the history
…Generate Cert
  • Loading branch information
wdrdres3qew5ts21 committed May 31, 2020
1 parent 1177105 commit 4283f65
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#ถ้า set ตัวนี้จะทำการ ovver default name ทันที
#COMPOSE_PROJECT_NAME=es
CERTS_DIR=/usr/share/elasticsearch/config/certificates
ELASTIC_PASSWORD=PleaseChangeMe
34 changes: 34 additions & 0 deletions create-cert-pem.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
version: '3.3'

services:
create_certs:
container_name: create_certs
image: docker.elastic.co/elasticsearch/elasticsearch:7.7.0
# bash -c คือการสั่งรัน command ที่อยู่ใน string single quote ทั้งหมด
# เขียน command หลายบรรทัด้วย fold > symbol ซึ่งใน step แรกคือการเช็คว่าถ้ายังไม่เคย gen cert มาก่อน
# จะทำการสั่ง gen cert ทั้ง pack ให้ใหม่โดยตำแหน่งที่เก็บ cert นั้นอยู่ที่ /certs/bundle.zip
# bin/elasticsearch-certutil cert --silent --pem --in config/certificates/instances.yml -out /certs/bundle.zip;
# สั่งทำการ gen cert แบบ silent mode ไม่มี interactive ใดๆทั้งนั้นทำให้สามารถทำเป้น script ได้เเพียงแต่ต้องกรอกข้อมูลให้ครบ
# โดยเลือกการ gen แบบเป็น pem ไฟล์และนำ --in คือ input file script มาจาก config/certificates/instances.yml ซึ่่งการเจนแบบ cert
# จะทำการสร้าง CA ให้อัตโนมัติด้วยถ้าหากระบุเอาไว้ตั้งแต่ตอนแรก และ output cert + ca ที่ gen ไปวางที่ /certs/bundle.zip
# ดังนั้นเมื่อ Gen เสร็จแล้วครั้งหน้าก้จะไม่ gen อีกเพราะเราเขียน logic
# --in ถ้าไม่มีก็จะได้ default templte ใช้ได้กับทุกอย่าง
command: >
bash -c '
if [[ ! -f /certs/bundle.zip ]]; then
bin/elasticsearch-certutil cert --silent --pem -out /certs/bundle.zip;
unzip /certs/bundle.zip -d /certs;
fi;
chown -R 1000:0 /certs
'
user: "0"
working_dir: /usr/share/elasticsearch
# สังเกตดุว่า volume นั้นถูก mount เอาไว้ทำให้เราสามารถนำไฟล์ cert + ca ที่ gen ออกมาไปวางที่ container อื่นที่ share volume ได้
# และตำแหน่งที่สองคือตำแหน่งของ instance.yml จาก local ถูกส่งเข้าไปวางที่ config/certificates/instances.yml นั่นเองจาก bind volume !
volumes:
- certs:/certs
- .:/usr/share/elasticsearch/config/certificates

volumes:
certs:
driver: local
45 changes: 40 additions & 5 deletions docker-compose-elastic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,27 @@ services:
# หลังติดตั้งเสร็จแล้วต้องรันคำสั่ง bin/elasticsearch-setup-passwords interactive
# เพื่อให้ xpack module ที่ลงไว้ทำการตั้ง password ตามที่เราตั้งด้วยนั่นเอง
# ถ้าใช้ xpack บน cluster mode จะโดน bootstrap สั่งให้ทำ TLS ด้วยทันทีไม่งั้นจะ bootstrap ไม่ผ่าน
# - xpack.security.enabled=true
- ELASTIC_PASSWORD=$ELASTIC_PASSWORD
- xpack.security.enabled=true
- xpack.security.http.ssl.enabled=true
- xpack.security.http.ssl.key=$CERTS_DIR/instance/instance.key
- xpack.security.http.ssl.certificate_authorities=$CERTS_DIR/ca/ca.crt
- xpack.security.http.ssl.certificate=$CERTS_DIR/instance/instance.crt
- xpack.security.transport.ssl.enabled=true
- xpack.security.transport.ssl.verification_mode=certificate
- xpack.security.transport.ssl.certificate_authorities=$CERTS_DIR/ca/ca.crt
- xpack.security.transport.ssl.certificate=$CERTS_DIR/instance/instance.crt
- xpack.security.transport.ssl.key=$CERTS_DIR/instance/instance.key
# จำเป็นต้องปิดการ swap memory เมื่อทำ cluster node เพราะว่า
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- data01:/usr/share/elasticsearch/data
# volume certs นี้ยังแชร์กับ create-cert.yml service อีกด้วยเพราะว่า namespace ชื่อเดียวกัน
# ทำให้ทำการ mount cert เข้าไปหาทุกๆ node ได้อีกด้วย !!!
- certs:$CERTS_DIR
networks:
- elastic
ports:
Expand All @@ -52,14 +65,25 @@ services:
# หลังติดตั้งเสร็จแล้วต้องรันคำสั่ง bin/elasticsearch-setup-passwords interactive
# เพื่อให้ xpack module ที่ลงไว้ทำการตั้ง password ตามที่เราตั้งด้วยนั่นเอง
# ถ้าใช้ xpack บน cluster mode จะโดน bootstrap สั่งให้ทำ TLS ด้วยทันทีไม่งั้นจะ bootstrap ไม่ผ่าน
# - xpack.security.enabled=true
- ELASTIC_PASSWORD=$ELASTIC_PASSWORD
- xpack.security.enabled=true
- xpack.security.http.ssl.enabled=true
- xpack.security.http.ssl.key=$CERTS_DIR/instance/instance.key
- xpack.security.http.ssl.certificate_authorities=$CERTS_DIR/ca/ca.crt
- xpack.security.http.ssl.certificate=$CERTS_DIR/instance/instance.crt
- xpack.security.transport.ssl.enabled=true
- xpack.security.transport.ssl.verification_mode=certificate
- xpack.security.transport.ssl.certificate_authorities=$CERTS_DIR/ca/ca.crt
- xpack.security.transport.ssl.certificate=$CERTS_DIR/instance/instance.crt
- xpack.security.transport.ssl.key=$CERTS_DIR/instance/instance.key
# จำเป็นต้องปิดการ swap memory เมื่อทำ cluster node เพราะว่า
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- data02:/usr/share/elasticsearch/data
- certs:$CERTS_DIR
networks:
- elastic

Expand All @@ -86,13 +110,24 @@ services:
# หลังติดตั้งเสร็จแล้วต้องรันคำสั่ง bin/elasticsearch-setup-passwords interactive
# เพื่อให้ xpack module ที่ลงไว้ทำการตั้ง password ตามที่เราตั้งด้วยนั่นเอง
# ถ้าใช้ xpack บน cluster mode จะโดน bootstrap สั่งให้ทำ TLS ด้วยทันทีไม่งั้นจะ bootstrap ไม่ผ่าน
# - xpack.security.enabled=true
- ELASTIC_PASSWORD=$ELASTIC_PASSWORD
- xpack.security.enabled=true
- xpack.security.http.ssl.enabled=true
- xpack.security.http.ssl.key=$CERTS_DIR/instance/instance.key
- xpack.security.http.ssl.certificate_authorities=$CERTS_DIR/ca/ca.crt
- xpack.security.http.ssl.certificate=$CERTS_DIR/instance/instance.crt
- xpack.security.transport.ssl.enabled=true
- xpack.security.transport.ssl.verification_mode=certificate
- xpack.security.transport.ssl.certificate_authorities=$CERTS_DIR/ca/ca.crt
- xpack.security.transport.ssl.certificate=$CERTS_DIR/instance/instance.crt
- xpack.security.transport.ssl.key=$CERTS_DIR/instance/instance.key
# จำเป็นต้องปิดการ swap memory เมื่อทำ cluster node เพราะว่า
ulimits:
memlock:
soft: -1
hard: -1
# volumes:
volumes:
- certs:$CERTS_DIR
# - data03:/usr/share/elasticsearch/data
networks:
- elastic
Expand Down Expand Up @@ -121,5 +156,5 @@ volumes: # volume แบบใช้ในตัว dcoker machine ให้จ
driver: local
data02:
driver: local
data03:
certs:
driver: local
14 changes: 14 additions & 0 deletions instances.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
instances:
- name: es01
dns:
- es01
- localhost
ip:
- 127.0.0.1

- name: es02
dns:
- es02
- localhost
ip:
- 127.0.0.1

0 comments on commit 4283f65

Please sign in to comment.