Cấu hình Druid Indexer để xử lý nạp dữ liệu
Bước đầu tiên là thiết lập dịch vụ Indexer, thành phần chịu trách nhiệm đọc dữ liệu từ nguồn (S3, Kafka, Local file) và chuyển đổi thành các đoạn dữ liệu (segments) lưu trữ trong Deep Storage.
Tạo file cấu hình chính cho Indexer tại thư mục cấu hình của Druid. File này xác định các tham số kết nối ZooKeeper, lưu trữ sâu (Deep Storage), và giới hạn tài nguyên bộ nhớ cho các job nạp dữ liệu.
Thư mục cấu hình mặc định khi cài đặt từ source thường là /opt/druid/conf. Nếu bạn cài đặt theo cách khác, hãy điều chỉnh đường dẫn tương ứng.
sudo nano /opt/druid/conf/druid/ingestion/indexer.properties
Dưới đây là nội dung hoàn chỉnh của file indexer.properties được tối ưu cho môi trường Ubuntu 24.04 với Java 17. Các tham số quan trọng bao gồm địa chỉ ZooKeeper, cấu hình lưu trữ HDFS/S3, và giới hạn bộ nhớ RAM cho mỗi task.
druid.indexer.taskQueue=zookeeper
druid.indexer.taskQueue.zk.paths=/druid/indexer/taskQueue
druid.zk.connect=localhost:2181
druid.zk.paths=/druid
druid.indexer.storage.type=hdfs
druid.indexer.storage.basePath=/druid-deep-storage
druid.indexer.storage.type.local=false
druid.indexer.task.runner.type=local
druid.indexer.task.runner.maxTaskConcurrent=4
druid.indexer.task.runner.type.standalone=true
druid.indexer.task.runner.type.standalone.local.directory=/var/druid/ingestion
druid.indexer.task.runner.type.standalone.memory=4096
druid.indexer.task.runner.type.standalone.javaOpts=-Xms2G -Xmx4G
druid.indexer.taskRunner.type.standalone.log.dir=/var/log/druid/indexer
druid.http.server.dir=/var/druid/webapps
druid.http.port=8890
druid.http.servlet.contextPath=/druid/ingestion
druid.http.servlet.contextPath=/druid/indexer
druid.indexer.taskRunner.type.standalone.web.enabled=true
druid.indexer.taskRunner.type.standalone.web.port=8890
druid.indexer.taskRunner.type.standalone.web.contextPath=/druid/indexer
druid.indexer.taskRunner.type.standalone.web.maxThreads=10
Kết quả mong đợi: File cấu hình được lưu thành công. Dịch vụ Indexer sẽ khởi động trên port 8890, lắng nghe các yêu cầu nạp dữ liệu (ingestion specs) và quản lý các task chạy song song.
Khởi động dịch vụ Indexer. Trong môi trường sản xuất, bạn nên sử dụng systemd, nhưng để test nhanh, ta dùng script khởi động trực tiếp từ thư mục Druid.
cd /opt/druid/bin
sudo ./start-indexer.sh
Để xác nhận Indexer đã chạy, kiểm tra tiến trình và port đang lắng nghe. Bạn cần thấy tiến trình Java chứa tham số druid.indexer và port 8890 đang ở trạng thái LISTEN.
ps aux | grep druid | grep indexer
sudo ss -tlnp | grep 8890
Kết quả mong đợi: Xuất hiện dòng tiến trình chứa -Ddruid.indexer và lệnh ss trả về LISTEN 0 0 0.0.0.0:8890. Điều này chứng tỏ Indexer đã sẵn sàng nhận job.
Cài đặt và cấu hình Druid Web Console
Web Console (Druid Console) là giao diện quản lý trung tâm, nơi bạn có thể submit các spec nạp dữ liệu, xem metadata, và điều phối cluster. Mặc dù Indexer có giao diện riêng, Console tích hợp đầy đủ hơn.
Cấu hình Console cần trỏ đến cùng một ZooKeeper như các node khác để đồng bộ metadata. Port mặc định của Console là 8888.
Tạo file cấu hình console.properties trong thư mục cấu hình Console. File này xác định kết nối ZooKeeper, Deep Storage, và các tham số hiển thị trên giao diện.
sudo nano /opt/druid/conf/druid/0.24.0/console/console.properties
Thay đổi druid.zk.connect nếu bạn đang chạy ZooKeeper trên node khác. Đảm bảo đường dẫn druid.zk.paths khớp với các phần tử khác trong cluster (thường là /druid).
druid.zk.connect=localhost:2181
druid.zk.paths=/druid
druid.console.contextPath=/druid-console
druid.console.contextPath=/druid
druid.http.server.dir=/var/druid/webapps
druid.http.port=8888
druid.http.servlet.contextPath=/druid
druid.console.dataStorage.type=hdfs
druid.console.dataStorage.basePath=/druid-deep-storage
druid.console.indexer.type=local
druid.console.indexer.taskRunner.type.standalone=true
druid.console.indexer.taskRunner.type.standalone.local.directory=/var/druid/ingestion
druid.console.indexer.taskRunner.type.standalone.memory=4096
druid.console.indexer.taskRunner.type.standalone.javaOpts=-Xms2G -Xmx4G
druid.console.queryBroker.type=local
druid.console.queryBroker.http.port=8083
druid.console.metadataStore.type=zookeeper
druid.console.metadataStore.zk.paths=/druid/metadata
Kết quả mong đợi: File được lưu. Cấu hình này cho phép Console giao tiếp với Indexer để submit job và với Broker để xem kết quả query ngay trên giao diện.
Khởi động dịch vụ Web Console. Script này thường nằm trong thư mục bin của Druid.
cd /opt/druid/bin
sudo ./start-console.sh
Truy cập giao diện web trên trình duyệt để kiểm tra. Mở trình duyệt và gõ địa chỉ http://localhost:8888/druid-console hoặc http://IP_SERVER:8888/druid-console.
curl -I http://localhost:8888/druid-console
Kết quả mong đợi: Trình duyệt hiển thị giao diện Druid Console với menu bên trái (Datasets, Ingestion, Query, Metadata). Lệnh curl trả về HTTP/1.1 200 OK.
Cấu hình quyền truy cập và bảo mật cơ bản
Trong môi trường production, bạn không nên để Console mở hoàn toàn. Druid sử dụng cơ chế druid.http.auth để bảo vệ các endpoint nhạy cảm. Ở đây ta cấu hình cơ chế đơn giản nhất là Basic Auth hoặc hạn chế truy cập từ xa.
Chỉnh sửa file console.properties đã tạo ở phần trước để bật tính năng xác thực. Druid hỗ trợ nhiều module auth, ở đây ta dùng druid.http.auth.type để kích hoạt cơ chế mặc định hoặc cấu hình file auth.properties riêng nếu cần.
Tuy nhiên, cách đơn giản và hiệu quả nhất cho môi trường dev/test là giới hạn truy cập chỉ qua localhost hoặc cấu hình CORS. Nếu muốn bật Basic Auth, ta cần tạo file auth.properties và tham chiếu nó.
sudo nano /opt/druid/conf/druid/0.24.0/console/auth.properties
Dưới đây là nội dung file auth.properties để định nghĩa user và password cho truy cập Console. Lưu ý: Trong thực tế production, nên dùng LDAP hoặc Kerberos, nhưng đây là ví dụ cơ bản.
druid.auth.type=basic
druid.auth.basic.users=admin:password123,viewer:readonly123
druid.auth.basic.roles=admin:admin,viewer:viewer
Sau đó, cập nhật file console.properties để trỏ đến file auth này và bật xác thực.
sudo nano /opt/druid/conf/druid/0.24.0/console/console.properties
Thêm các dòng sau vào cuối file console.properties (hoặc chỉnh sửa nếu đã có):
druid.http.auth.type=basic
druid.http.auth.file=/opt/druid/conf/druid/0.24.0/console/auth.properties
druid.http.auth.enabled=true
Kết quả mong đợi: Khi truy cập lại http://localhost:8888/druid-console, trình duyệt sẽ hiện cửa sổ yêu cầu tên đăng nhập và mật khẩu.
Để verify bảo mật, thử truy cập một endpoint nhạy cảm mà không có auth (ví dụ: list datasets) và xem kết quả trả về 401 Unauthorized.
curl -i http://localhost:8888/druid-console/api/databases
Kết quả mong đợi: HTTP Status 401 Unauthorized. Sau đó thử lại với user đã tạo:
curl -i -u admin:password123 http://localhost:8888/druid-console/api/databases
Kết quả mong đợi: HTTP Status 200 OK và trả về danh sách các database (hoặc mảng rỗng nếu chưa có dữ liệu).
Cuối cùng, kiểm tra lại trạng thái của cả Indexer và Console để đảm bảo chúng chạy song song và không bị xung đột port.
sudo systemctl status druid-indexer
sudo systemctl status druid-console
sudo ss -tlnp | grep -E "8888|8890"
Kết quả mong đợi: Cả hai service đều ở trạng thái active (running) và các port 8888 (Console) cùng 8890 (Indexer) đang lắng nghe đúng yêu cầu.
Điều hướng series:
Mục lục: Series: Triển khai Database OLAP với Apache Druid trên Ubuntu 24.04
« Phần 5: Cấu hình các thành phần chính: Coordinator, Broker và Historical
Phần 7: Nạp dữ liệu mẫu và kiểm tra truy vấn SQL cơ bản »