Grafana、Prometheus外加各類的Exporter目前已經(jīng)成了監(jiān)控領(lǐng)域的三劍客。
Grafana是一款多平臺(tái)、開(kāi)源的分析和交互式可視化Web應(yīng)用程序,配合后端數(shù)據(jù)源可以方便地進(jìn)行各類圖表展示,后端數(shù)據(jù)源一般是各類時(shí)序數(shù)據(jù)庫(kù),如InfluxDB、Prometheus、Graphite等。
Grafana自帶身份認(rèn)證系統(tǒng),它也可以集成外部的認(rèn)證系統(tǒng),如Github OAuth、Google OAuth和LDAP等。
本文的LDAP使用FreeIPA實(shí)現(xiàn),F(xiàn)reeIPA是一個(gè)用于Linux/Unix環(huán)境的開(kāi)源身份管理系統(tǒng),它提供集中式的賬號(hào)管理和身份驗(yàn)證,其集成389目錄服務(wù)器(一種LDAP實(shí)現(xiàn))、NTP、DNS、MIT Kerberos等。
- FreeIPA安裝
FreeIPA是紅帽公司的產(chǎn)品,如果操作系統(tǒng)是CentOS的話,可以采用YUM包安裝的方式,比較簡(jiǎn)單,如果是其他操作系統(tǒng),可以選擇使用容器的部署方式(正式環(huán)境建議使用YUM包的方式安裝,為了安裝它,專門部署CentOS系統(tǒng),也是值得的)。本文采用容器的安裝方式。容器運(yùn)行命令:
sudo docker run -it -d -h ipa.bigcompany.com -p 53:53/udp -p 53:53 \
-p 127.0.0.1:80:80 -p 127.0.0.1:443:443 -p 389:389 -p 636:636 -p 88:88 -p 464:464 \
-p 88:88/udp -p 464:464/udp -p 123:123/udp \
--sysctl net.ipv6.conf.lo.disable_ipv6=0 \
-v /sys/fs/cgroup:/sys/fs/cgroup:ro \
-v /home/aneirin/freeipa/ipa-data:/data:Z --name freeipa \
freeipa/freeipa-server:centos-7-4.6.8
初次運(yùn)行切記去掉“-d”選項(xiàng),不要讓容器在后臺(tái)運(yùn)行,因?yàn)槲覀冃枰诮换ナ侥J较峦瓿梢恍┲匾獏?shù)的配置,如是否啟用BIND、配置服務(wù)器的FQDN、域名、Kerberos的Realm name、目錄服務(wù)和IPA的管理員密碼等。后面啟動(dòng)容器可以加上“-d”選項(xiàng)。
服務(wù)監(jiān)聽(tīng)的端口:
HTTP/HTTPS:80/443
LDAP/LDAPS:389/636
Kerberos:88/464
DNS:53
NTP:123
你可以選擇是否對(duì)外暴露指定端口,比如我們正式環(huán)境使用的FreeIPA并沒(méi)有啟用DNS,53端口就不需要配置。如果正式環(huán)境使用Nginx作為前端,容器的80和443端口監(jiān)聽(tīng)在本地環(huán)回地址上,如不用Nginx,配置FreeIPA監(jiān)聽(tīng)在服務(wù)器的對(duì)外網(wǎng)口上。
Nginx的配置
server {
listen 192.168.1.2:443 ssl http2;
server_name ipa.bigcompany.com;
root /usr/share/nginx/html;
ssl_certificate "/etc/pki/nginx/ipa.bigcompany.com.pem";
ssl_certificate_key "/etc/pki/nginx/private/ipa.bigcompany.com-key.pem";
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
auth_basic "off";
proxy_pass https://localhost:443;
proxy_set_header Host $host;
proxy_set_header Referer https://ipa.bigcompany.com/ipa/ui;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_cookie_domain realipa.bigcompany.com ipa.bigcompany.com;
proxy_connect_timeout 150;
proxy_send_timeout 100;
proxy_read_timeout 100;
proxy_buffers 4 32k;
client_max_body_size 200M;
client_body_buffer_size 512k;
keepalive_timeout 5;
add_header Strict-Transport-Security max-age=63072000;
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
}
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
上面的操作完成后,使用IPA管理員賬號(hào)登錄FreeIPA的Web UI,添加用戶賬號(hào),每個(gè)賬號(hào)歸屬在創(chuàng)建好的組中,比如公司有開(kāi)發(fā)和運(yùn)維兩個(gè)團(tuán)隊(duì)需要訪問(wèn)Grafana,創(chuàng)建兩個(gè)組“developer”和“infra”(下圖中其他的組是FreeIPA自帶的):

之所以創(chuàng)建不同的組是為了配置Grafana根據(jù)組來(lái)做權(quán)限的下發(fā),具體見(jiàn)下。
- Grafana安裝與配置
Grafana安裝使用容器的方式,管理起來(lái)會(huì)方便很多,“docker-compose.yml”文件:
version: "3"
services:
grafana:
image: grafana/grafana:latest
container_name: monitoring_grafana
restart: unless-stopped
user: "2302"
ports:
- 3000:3000
links:
- prometheus:prometheus
volumes:
- ./grafana/data:/var/lib/grafana
- ./grafana/config/ldap.toml:/etc/grafana/ldap.toml
environment:
- GF_SECURITY_ADMIN_PASSWORD=admin
- GF_USERS_ALLOW_SIGN_UP=false
- GF_SERVER_DOMAIN=bigcompany.com
- GF_AUTH_LDAP_ENABLED=true
- GF_AUTH_LDAP_ALLOW_SIGN_UP=true
“docker-compose.yml”文件注意一點(diǎn),因?yàn)槟夸洝?/grafana/data”屬主的用戶ID為2302,所以鍵“user”的值配置為2302,讀者配置時(shí),根據(jù)實(shí)際情況做調(diào)整即可。
ldap.toml
[[servers]]
# Ldap server host (specify multiple hosts space separated)
host = "192.168.1.2"
# Default port is 389 or 636 if use_ssl = true
port = 389
# Set to true if LDAP server should use an encrypted TLS connection (either with STARTTLS or LDAPS)
use_ssl = false
# If set to true, use LDAP with STARTTLS instead of LDAPS
start_tls = false
# set to true if you want to skip SSL cert validation
ssl_skip_verify = false
# set to the path to your root CA certificate or leave unset to use system defaults
# root_ca_cert = "/path/to/certificate.crt"
# Authentication against LDAP servers requiring client certificates
# client_cert = "/path/to/client.crt"
# client_key = "/path/to/client.key"
# Search user bind dn
bind_dn = "uid=%s,cn=users,cn=compat,dc=bigcompany,dc=com"
# Search user bind password
# If the password contains # or ; you have to wrap it with triple quotes. Ex """#password;"""
#bind_password = 'grafana'
# User search filter, for example "(cn=%s)" or "(sAMAccountName=%s)" or "(uid=%s)"
# Allow login from email or username, example "(|(sAMAccountName=%s)(userPrincipalName=%s))"
search_filter = "(uid=%s)"
# An array of base dns to search through
search_base_dns = ["cn=users,cn=compat,dc=bigcompany,dc=com"]
group_search_filter = "(&(objectClass=posixGroup)(memberUid=%s))"
group_search_filter_user_attribute = "uid"
group_search_base_dns = ["cn=groups,cn=compat,dc=bigcompany,dc=com"]
[[servers.group_mappings]]
group_dn = "cn=infra,cn=groups,cn=compat,dc=bigcompany,dc=com"
org_role = "Admin"
grafana_admin = true # Available in Grafana v5.3 and above
[[servers.group_mappings]]
group_dn = "*"
org_role = "Viewer"
# Specify names of the LDAP attributes your LDAP uses
[servers.attributes]
username = "uid"
surname = "sn"
email = "email"
注意:
1,在使用LDAP服務(wù)時(shí),首先需要bind,即認(rèn)證的意思,因?yàn)樘峁┑谋磉_(dá)式匹配所有用戶,這里就不用配置bind密碼了。
2,根據(jù)用戶所屬的組配置權(quán)限,Grafana如何知道用戶所屬的組呢?所以配置組的查詢過(guò)濾表達(dá)式:“(&(objectClass=posixGroup)(memberUid=%s))”,如果你不確定自己配置的是否正確,可以運(yùn)行命令驗(yàn)證:ldapsearch -x -h 192.168.1.2 -b dc=bigcompany,dc=com "(&(objectClass=posixGroup)(memberUid=username))"。如果輸出了組信息,就說(shuō)明沒(méi)有問(wèn)題,
[aneirin@ipa ~]$ldapsearch -x -h 192.168.1.2 -b dc=bigcompany,dc=com "(&(objectClass=posixGroup)(memberUid=liudehua))"
# extended LDIF
#
# LDAPv3
# base <dc=bigcompany,dc=com> with scope subtree
# filter: (&(objectClass=posixGroup)(memberUid=liudehua))
# requesting: ALL
#
# infra, groups, compat, bigcompany.com
dn: cn=infra,cn=groups,cn=compat,dc=bigcompany,dc=com
objectClass: posixGroup
objectClass: ipaOverrideTarget
objectClass: ipaexternalgroup
objectClass: top
gidNumber: 235600003
memberUid: liudehua
ipaAnchorUUID:: OklQQTp5b3V4dWVwYWkub3JnOjdmYTA2YzdlLWNiNTUtMTFlYi04NWQ4LTAyND
JhYzExMDAwMg==
cn: infra
# search result
search: 2
result: 0 Success
# numResponses: 2
# numEntries: 1
總結(jié)
很多服務(wù)都支持集成LDAP的認(rèn)證,目前開(kāi)源的提供LDAP服務(wù)的軟件,F(xiàn)reeIPA無(wú)疑是其中的佼佼者,安裝簡(jiǎn)便,集成的服務(wù)豐富,不過(guò)正式環(huán)境一定注意數(shù)據(jù)的高可用性,必要的話,可以部署主從來(lái)提高穩(wěn)定性。