Skip to content

Commit

Permalink
[ADD] Elasticsearch TLS เฉพาะ Cluster ที่ Transport Layer ส่วน Traffi…
Browse files Browse the repository at this point in the history
…c ข้างนอกใช้ HTTP
  • Loading branch information
wdrdres3qew5ts21 committed Jun 4, 2020
1 parent d0a0bc9 commit e47cd4c
Show file tree
Hide file tree
Showing 5 changed files with 231 additions and 12 deletions.
19 changes: 19 additions & 0 deletions changePassword.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash
curl -X POST -v -k --cacert /usr/share/elasticsearch/config/certificates/ca/ca.crt "https://elasticsearch1:9200/_security/user/kibana/_password?pretty" -u elastic:PleaseChangeMe -H 'Content-Type: application/json' -d' { "password" : "PleaseChangeMe" } '

if [[ $? == 51 ]]
then
echo "Connect but it not secure So fail to change Password"
else
echo "Skip SSL Secure Connection by -k argument so Change password Success"
fi

echo "======="
echo "Variable two sign : $$?"
echo "Variable single sign: $?"

#!/bin/bash

curl -k https://elasticsearch1:9200

echo "Yeah $?"
179 changes: 179 additions & 0 deletions docker-compose-elastic-http.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
version: '3.3'
services:
elasticsearch1:
image: docker.elastic.co/elasticsearch/elasticsearch:7.7.0
container_name: elasticsearch1
restart: always
environment:
- node.name=elasticsearch1
# จะทำให้มันไม่ยอมเข้าไปอยู่ใน cluster และเอาตัวเองเป้น master เสมอเลย
# - discovery.type=single-node
- cluster.name=es-docker-cluster
# เวลา scan Elasticsearch จะเช็ค Auto internal Transport Layer บน Port 9300-9305 ทันทีและรวบเข้า Cluster
# ระหว่างทางสามารถเพิ่มลดได้ตามใจชอบระบบจะ Detect เองอัตโนมัติ
- discovery.seed_hosts=elasticsearch1,elasticsearch2,elasticsearch3
- cluster.initial_master_nodes=elasticsearch1,elasticsearch2,elasticsearch3
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- http.cors.enabled=true
- http.cors.allow-origin=*
# ผูกเข้ากับ interface คอมเราผ่าน ifconfig
- network.host=_eth0_
# หลังติดตั้งเสร็จแล้วต้องรันคำสั่ง bin/elasticsearch-setup-passwords interactive
# เพื่อให้ xpack module ที่ลงไว้ทำการตั้ง password ตามที่เราตั้งด้วยนั่นเอง
# ถ้าใช้ xpack บน cluster mode จะโดน bootstrap สั่งให้ทำ TLS ด้วยทันทีไม่งั้นจะ bootstrap ไม่ผ่าน
- ELASTIC_PASSWORD=$ELASTIC_PASSWORD
- xpack.security.enabled=true
- xpack.security.http.ssl.enabled=false
# - 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:
- 9200:9200
# healthcheck:
# test: curl -k https://elasticsearch1:9200; if [[ $? == 0 ]]; then echo 0; else echo 1; fi
# interval: 30s
# timeout: 10s
# retries: 5

elasticsearch2:
image: docker.elastic.co/elasticsearch/elasticsearch:7.7.0
container_name: elasticsearch2
restart: always
environment:
- node.name=elasticsearch2
- cluster.name=es-docker-cluster
- discovery.seed_hosts=elasticsearch1,elasticsearch2,elasticsearch3
- cluster.initial_master_nodes=elasticsearch1,elasticsearch2,elasticsearch3
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- http.cors.enabled=true
- http.cors.allow-origin=*
- network.host=_eth0_
# หลังติดตั้งเสร็จแล้วต้องรันคำสั่ง bin/elasticsearch-setup-passwords interactive
# เพื่อให้ xpack module ที่ลงไว้ทำการตั้ง password ตามที่เราตั้งด้วยนั่นเอง
# ถ้าใช้ xpack บน cluster mode จะโดน bootstrap สั่งให้ทำ TLS ด้วยทันทีไม่งั้นจะ bootstrap ไม่ผ่าน
- ELASTIC_PASSWORD=$ELASTIC_PASSWORD
- xpack.security.enabled=true
- xpack.security.http.ssl.enabled=false
# - 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


elasticsearch3:
image: docker.elastic.co/elasticsearch/elasticsearch:7.7.0
container_name: elasticsearch3
restart: always
environment:
- node.name=elasticsearch3
- cluster.name=es-docker-cluster
- discovery.seed_hosts=elasticsearch1,elasticsearch2,elasticsearch3
- cluster.initial_master_nodes=elasticsearch1,elasticsearch2,elasticsearch3
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- http.cors.enabled=true
- http.cors.allow-origin=*
- network.host=_eth0_
# ด้วยสาม parameter นี้จะทำให้ Elasticsearch Node นี้เป็นแค่ Coordinate ใช้ประสานคำสั่งเฉยๆ
# จะไม่มีการเก้บข้อมูลใดๆที่ตัวมันดังนั้น Volume จึงไม่ทำงานและห้ามใช้ด้วยนั่นเอง ถ้าใช้ก็จะบึ้มเพราะเป็น Coordinate Node
- node.master=false
- node.data=false
- node.ingest=false
# หลังติดตั้งเสร็จแล้วต้องรันคำสั่ง bin/elasticsearch-setup-passwords interactive
# เพื่อให้ xpack module ที่ลงไว้ทำการตั้ง password ตามที่เราตั้งด้วยนั่นเอง
# ถ้าใช้ xpack บน cluster mode จะโดน bootstrap สั่งให้ทำ TLS ด้วยทันทีไม่งั้นจะ bootstrap ไม่ผ่าน
- ELASTIC_PASSWORD=$ELASTIC_PASSWORD
- xpack.security.enabled=true
- xpack.security.http.ssl.enabled=false
# - 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:
- certs:$CERTS_DIR
# - data03:/usr/share/elasticsearch/data
networks:
- elastic

kibana:
image: docker.elastic.co/kibana/kibana:7.7.0
container_name: kibana
restart: always
environment:
# env ที่อยู่ตรงนี้คือตัวเดียวกับใน kibana.yml แต่จะเขียนด้วย format KEY_PROPERTY: value
# ซึ่งถ้าเป้นใน kibana.yml จะเขียนด้วย key.property: value แทนนั่นเอง มีค่าเท่ากัน
# SERVER_NAME: localhost
# ELASTICSEARCH_URL: http://elasticsearch:9200
- ELASTICSEARCH_PASSWORD=$ELASTIC_PASSWORD
ports:
- 5601:5601
volumes:
- ./kibana.yml:/usr/share/kibana/config/kibana.yml
- certs:$CERTS_DIR
networks:
- elastic
# command: >
# bash -c '
# curl -X POST -v -k --cacert /usr/share/elasticsearch/config/certificates/ca/ca.crt "https://elasticsearch1:9200/_security/user/kibana/_password?pretty" -u elastic:PleaseChangeMe -H 'Content-Type: application/json' -d' { "password" : "PleaseChangeMe" } '

# if [[ $? == 51 ]]
# then
# echo "Connect but it not secure So fail to change Password"
# else
# echo "Skip SSL Secure Connection by -k argument so Change password Success"
# fi
# '
# depends_on: {"elasticsearch1": {"condition": "service_healthy"}}

networks:
elastic:

volumes: # volume แบบใช้ในตัว dcoker machine ให้จัดการเอง
data01:
driver: local
data02:
driver: local
certs:
driver: local
30 changes: 24 additions & 6 deletions docker-compose-elastic.yml → docker-compose-elastic-ssl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ services:
- cluster.name=es-docker-cluster
# เวลา scan Elasticsearch จะเช็ค Auto internal Transport Layer บน Port 9300-9305 ทันทีและรวบเข้า Cluster
# ระหว่างทางสามารถเพิ่มลดได้ตามใจชอบระบบจะ Detect เองอัตโนมัติ
# sudo sysctl -w vm.max_map_count=262144 ถ้าทำ cluster ต้องขยาย memmory ด้วยไม่งั้นจะ fail
- discovery.seed_hosts=elasticsearch1,elasticsearch2,elasticsearch3
- cluster.initial_master_nodes=elasticsearch1,elasticsearch2,elasticsearch3
- bootstrap.memory_lock=true
Expand All @@ -24,11 +25,11 @@ services:
# ถ้าใช้ xpack บน cluster mode จะโดน bootstrap สั่งให้ทำ TLS ด้วยทันทีไม่งั้นจะ bootstrap ไม่ผ่าน
- ELASTIC_PASSWORD=$ELASTIC_PASSWORD
- xpack.security.enabled=true
- xpack.security.http.ssl.enabled=true
- xpack.security.http.ssl.enabled=false
- 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.enabled=false
- 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
Expand All @@ -47,6 +48,11 @@ services:
- elastic
ports:
- 9200:9200
# healthcheck:
# test: curl -k https://elasticsearch1:9200; if [[ $? == 0 ]]; then echo 0; else echo 1; fi
# interval: 30s
# timeout: 10s
# retries: 5

elasticsearch2:
image: docker.elastic.co/elasticsearch/elasticsearch:7.7.0
Expand All @@ -67,11 +73,11 @@ services:
# ถ้าใช้ xpack บน cluster mode จะโดน bootstrap สั่งให้ทำ TLS ด้วยทันทีไม่งั้นจะ bootstrap ไม่ผ่าน
- ELASTIC_PASSWORD=$ELASTIC_PASSWORD
- xpack.security.enabled=true
- xpack.security.http.ssl.enabled=true
- xpack.security.http.ssl.enabled=false
- 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.enabled=false
- 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
Expand Down Expand Up @@ -112,11 +118,11 @@ services:
# ถ้าใช้ xpack บน cluster mode จะโดน bootstrap สั่งให้ทำ TLS ด้วยทันทีไม่งั้นจะ bootstrap ไม่ผ่าน
- ELASTIC_PASSWORD=$ELASTIC_PASSWORD
- xpack.security.enabled=true
- xpack.security.http.ssl.enabled=true
- xpack.security.http.ssl.enabled=false
- 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.enabled=false
- 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
Expand Down Expand Up @@ -149,7 +155,19 @@ services:
- certs:$CERTS_DIR
networks:
- elastic
# command: >
# bash -c '
# curl -X POST -v -k --cacert /usr/share/elasticsearch/config/certificates/ca/ca.crt "https://elasticsearch1:9200/_security/user/kibana/_password?pretty" -u elastic:PleaseChangeMe -H 'Content-Type: application/json' -d' { "password" : "PleaseChangeMe" } '

# if [[ $? == 51 ]]
# then
# echo "Connect but it not secure So fail to change Password"
# else
# echo "Skip SSL Secure Connection by -k argument so Change password Success"
# fi
# '
# depends_on: {"elasticsearch1": {"condition": "service_healthy"}}

networks:
elastic:

Expand Down
3 changes: 3 additions & 0 deletions init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
sysctl -w vm.max_map_count=262144

12 changes: 6 additions & 6 deletions kibana.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ server.host: 0.0.0.0
# แทนที่จะเป็น ชื่อ Container ซึ่งการแก้ก็คือนำ Kubernetes มาสร้าง Network Overlay ไว้นั่นเอง !!! เพราะ K8s จะรวม
# Pods หลายๆตัวให้เป้น Service และใช้ URL Domain นั้นแทนได้ !!!
# elasticsearch.hosts: [ "http://elasticsearch1:9200"]
elasticsearch.hosts: ["https://elasticsearch1:9200" ]
elasticsearch.hosts: ["http://elasticsearch1:9200" ]
# user ต้องเป้น user ชื่อ kibana เท่านั้นเพราะเงื่อนไขการติดต่อคือ
# user ต้องตรงกับ app เช่น apm ต้องใช้ apm ไม่งั้นจะเข้าไมไ่ด้ !!!
# ส่วน user elastic มีไว้ใช้ login กับ elastic ไม่เกีย่วกับ user ของ kibana !!!
elasticsearch.username: "kibana"
elasticsearch.password: "PleaseChangeMe"
server.ssl.enabled: true
server.ssl.key: /usr/share/elasticsearch/config/certificates/instance/instance.key
server.ssl.certificate: /usr/share/elasticsearch/config/certificates/instance/instance.crt
elasticsearch.ssl.certificateAuthorities: /usr/share/elasticsearch/config/certificates/ca/ca.crt
elasticsearch.ssl.verificationMode: certificate
# server.ssl.enabled: false
# server.ssl.key: /usr/share/elasticsearch/config/certificates/instance/instance.key
# server.ssl.certificate: /usr/share/elasticsearch/config/certificates/instance/instance.crt
# elasticsearch.ssl.certificateAuthorities: /usr/share/elasticsearch/config/certificates/ca/ca.crt
# elasticsearch.ssl.verificationMode: certificate

0 comments on commit e47cd4c

Please sign in to comment.