From 4283f65d2f45ff16cd928b515cefbe562c955493 Mon Sep 17 00:00:00 2001 From: NaomiLin Date: Sun, 31 May 2020 23:17:30 +0700 Subject: [PATCH] =?UTF-8?q?[ADD]=20Elasticsearch=20Full=20Cluster=20+=20No?= =?UTF-8?q?de=20Encryption=20=E0=B8=9E=E0=B8=A3=E0=B9=89=E0=B8=AD=E0=B8=A1?= =?UTF-8?q?=E0=B8=95=E0=B8=B1=E0=B9=89=E0=B8=87=E0=B8=A3=E0=B8=AB=E0=B8=B1?= =?UTF-8?q?=E0=B8=AA=E0=B8=9C=E0=B9=88=E0=B8=B2=E0=B8=99=20Generate=20Cert?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 4 ++++ create-cert-pem.yml | 34 ++++++++++++++++++++++++++++ docker-compose-elastic.yml | 45 +++++++++++++++++++++++++++++++++----- instances.yml | 14 ++++++++++++ 4 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 .env create mode 100644 create-cert-pem.yml create mode 100644 instances.yml diff --git a/.env b/.env new file mode 100644 index 0000000..8df4a2e --- /dev/null +++ b/.env @@ -0,0 +1,4 @@ +#ถ้า set ตัวนี้จะทำการ ovver default name ทันที +#COMPOSE_PROJECT_NAME=es +CERTS_DIR=/usr/share/elasticsearch/config/certificates +ELASTIC_PASSWORD=PleaseChangeMe \ No newline at end of file diff --git a/create-cert-pem.yml b/create-cert-pem.yml new file mode 100644 index 0000000..e5a2864 --- /dev/null +++ b/create-cert-pem.yml @@ -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 diff --git a/docker-compose-elastic.yml b/docker-compose-elastic.yml index 93ff54d..a0302b5 100644 --- a/docker-compose-elastic.yml +++ b/docker-compose-elastic.yml @@ -22,7 +22,17 @@ 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: @@ -30,6 +40,9 @@ services: hard: -1 volumes: - data01:/usr/share/elasticsearch/data + # volume certs นี้ยังแชร์กับ create-cert.yml service อีกด้วยเพราะว่า namespace ชื่อเดียวกัน + # ทำให้ทำการ mount cert เข้าไปหาทุกๆ node ได้อีกด้วย !!! + - certs:$CERTS_DIR networks: - elastic ports: @@ -52,7 +65,17 @@ 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: @@ -60,6 +83,7 @@ services: hard: -1 volumes: - data02:/usr/share/elasticsearch/data + - certs:$CERTS_DIR networks: - elastic @@ -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 @@ -121,5 +156,5 @@ volumes: # volume แบบใช้ในตัว dcoker machine ให้จ driver: local data02: driver: local - data03: + certs: driver: local diff --git a/instances.yml b/instances.yml new file mode 100644 index 0000000..9de371f --- /dev/null +++ b/instances.yml @@ -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 \ No newline at end of file