本次安装ES和Kibana版本如下:
- elasticsearch 6.8.3
- kibana 6.8.3
- elasticsearch-head 5 前端框架
1.安装elasticsearch6.8.3
1)拉取镜像
docker pull elasticsearch:6.8.3
2)创建配置文件
想要建几台机器的集群就创建几个配置文件,需要修改的参数为node.name、http.port、transport.tcp.port、discovery.zen.ping.unicast.hosts
本次创建2台docker的es集群,使用docker容器所在服务器的ip:192.168.130.131,端口分别使用9200、9300、9201、9301
#开放防火墙端口
firewall-cmd --add-port=9300/tcp
firewall-cmd --add-port=9301/tcp
#创建文件夹
mkdir /home/es
cd /home/es/
#创建配置文件夹
mkdir /home/es/config
#创建数据挂载文件夹
mkdir data1
mkdir data2
#赋权限
chmod 777 data1 data2
#创建插件挂载文件夹
mkdir plugins1
mkdir plugins2
#创建配置文件
cd /home/es/config
vi es1.yml
vi es2.yml
然后创建一个es1.yml配置文件里面的内容为
cluster.name: elasticsearch-cluster
node.name: es-node1
network.host: 0.0.0.0
network.publish_host: 192.168.130.131
http.port: 9200
transport.tcp.port: 9300
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true
discovery.zen.ping.unicast.hosts: ["192.168.130.131:9300","192.168.130.131:9301"]
discovery.zen.minimum_master_nodes: 1
然后创建一个es2.yml配置文件里面的内容为
cluster.name: elasticsearch-cluster
node.name: es-node2
network.host: 0.0.0.0
network.publish_host: 192.168.130.131
http.port: 9201
transport.tcp.port: 9301
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true
discovery.zen.ping.unicast.hosts: ["192.168.130.131:9300","192.168.130.131:9300"]
discovery.zen.minimum_master_nodes: 1
3)调高JVM线程数限制数量
在centos窗口中,修改配置sysctl.conf
vi /etc/sysctl.conf
在最后加入如下内容:
vm.max_map_count=262144
启用配置:
sysctl -p
注:这一步是为了防止启动容器时,报出如下错误:
bootstrap checks failed max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144]
4)运行镜像
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -dit -p 9200:9200 -p 9300:9300 \
-v /home/es/config/es1.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v /home/es/plugins1:/usr/share/elasticsearch/plugins \
-v /home/es/data1:/usr/share/elasticsearch/data --name ES01 elasticsearch:6.8.3
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9201:9201 -p 9301:9301 \
-v /home/es/config/es2.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v /home/es/plugins2:/usr/share/elasticsearch/plugins \
-v /home/es/data2:/usr/share/elasticsearch/data --name ES02 elasticsearch:6.8.3
5)测试站点是否成功
在浏览器地址栏访问http://192.168.130.131:9200/_cat/nodes?pretty查看节点状态
如果成功,显示如下:
*6)增加用户名密码验证
获取集群证书
#创建文件夹
cd /home/es
mkdir /home/es/certificates
cd certificates
#查看启动的容器
[root@localhost nacos]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c2a00c744d2e elasticsearch:6.8.3 "/usr/local/bin/dock…" 22 hours ago Up 3 seconds 9200/tcp, 0.0.0.0:9201->9201/tcp, 9300/tcp, 0.0.0.0:9301->9301/tcp ES02
5f477f1a7a7e elasticsearch:6.8.3 "/usr/local/bin/dock…" 22 hours ago Up 5 seconds 0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp ES01
#进入其中一个容器,如ES01-->5f477f1a7a7e
docker exec -it ES01 /bin/bash
#配置生成SSL,在主服务器上生成就行
bin/elasticsearch-certgen
#####################################
Please enter the desired output file [certificate-bundle.zip]: cert.zip (压缩包名称)
Enter instance name: es-app(实例名)
Enter name for directories and files [p4mES]: es(文件夹名)
Enter IP Addresses for instance (comma-separated if more than one) []: 192.168.130.131(实例ip,多个ip用逗号隔开)
Enter DNS names for instance (comma-separated if more than one) []: es-node1,es-node2(节点名,多个节点用逗号隔开)
Would you like to specify another instance? Press 'y' to continue entering instance information: (到达这一步,不需要按y重新设置,按空格键就完成了)
Certificates written to /usr/local/elasticsearch/bin/cert.zip(这个是生成的文件存放地址,不用填写)
#退出容器
exit
#从容器中拷贝证书
docker cp ES01:/usr/share/elasticsearch/cert.zip .
#解压
unzip cert.zip
复制yml配置文件,在es01.yml、es02.yml中添加下列内容,生成es1-auth.yml、es2-auth.yml
es1-auth.yml内容添加:
# x-head 访问配置
http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type
# 开通高级权限后,打开安全配置功能
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
# 配置ssl和CA证书配置
xpack.ssl.key: /usr/share/elasticsearch/config/certificates/es/es.key
xpack.ssl.certificate: /usr/share/elasticsearch/config/certificates/es/es.crt
xpack.ssl.certificate_authorities: /usr/share/elasticsearch/config/certificates/ca/ca.crt
es2-auth.yml内容添加:
# 开通高级权限后,打开安全配置功能
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
# 配置ssl和CA证书配置
xpack.ssl.key: /usr/share/elasticsearch/config/certificates/es/es.key
xpack.ssl.certificate: /usr/share/elasticsearch/config/certificates/es/es.crt
xpack.ssl.certificate_authorities: /usr/share/elasticsearch/config/certificates/ca/ca.crt
停止并删除容器
#停止
docker stop ES01
docker stop ES02
#删除
docker rm ES01
docker rm ES02
用以下命令重新启动
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -dit -p 9200:9200 -p 9300:9300 \
-v /home/es/config/es1-auth.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v /home/es/plugins1:/usr/share/elasticsearch/plugins \
-v /home/es/data1:/usr/share/elasticsearch/data \
-v /home/es/certificates/:/usr/share/elasticsearch/config/certificates/ \
--name ES01 elasticsearch:6.8.3
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9201:9201 -p 9301:9301 \
-v /home/es/config/es2-auth.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v /home/es/plugins2:/usr/share/elasticsearch/plugins \
-v /home/es/data2:/usr/share/elasticsearch/data \
-v /home/es/certificates/:/usr/share/elasticsearch/config/certificates/ \
--name ES02 elasticsearch:6.8.3
宿主机的端口允许其它计算机访问,但是不允许来自本机的 Docker 容器访问。必须通过设置 firewalld 规则允许本机的 Docker 容器访问,可以通过在 /etc/firewalld/zones/public.xml 中添加防火墙规则避免这个问题:
注意这里的 172.17.0.0/16 可以匹配 172.17.xx.xx IP 段的所有 IP。
之后重启下防火墙:
systemctl restart firewalld
开通高级功能后,如何设置登录账号和密码,开启登录验证功能
# 进入主容器
docker exec -it ES01 /bin/bash
#自定义设置elastic、kibana…等所有工具的登录密码
bin/elasticsearch-setup-passwords interactive
完成
2.安装elasticsearch-head前端框架
1)拉取镜像
docker pull mobz/elasticsearch-head:5
2)新建Gruntfile.js
cd /home/es/config
vi Gruntfile.js
内容如下:
module.exports = function(grunt) {
var fileSets = require("./grunt_fileSets.js");
// Project configuration.
grunt.initConfig({
clean: {
_site: {
src: ['_site']
}
},
concat: {
vendorjs: {
src: fileSets.vendorJs,
dest: '_site/vendor.js'
},
vendorcss: {
src: fileSets.vendorCss,
dest: '_site/vendor.css'
},
appjs: {
src: fileSets.srcJs,
dest: '_site/app.js'
},
appcss: {
src: fileSets.srcCss,
dest: '_site/app.css'
}
},
copy: {
site_index: {
src: 'index.html',
dest: '_site/index.html',
options: {
process: function( src ) {
return src.replace(/_site\//g, "");
}
}
},
base: {
expand: true,
cwd: 'src/app/base/',
src: [ '*.gif', '*.png', '*.css' ],
dest: '_site/base/'
},
iconFonts: {
expand: true,
cwd: 'src/vendor/font-awesome/fonts/',
src: '**',
dest: '_site/fonts'
},
i18n: {
src: 'src/vendor/i18n/i18n.js',
dest: '_site/i18n.js'
},
lang: {
expand: true,
cwd: 'src/app/lang/',
src: '**',
dest: '_site/lang/'
}
},
jasmine: {
task: {
src: [ fileSets.vendorJs, 'src/vendor/i18n/i18n.js', 'src/app/lang/en_strings.js', fileSets.srcJs ],
options: {
specs: 'src/app/**/*Spec.js',
helpers: 'test/spec/*Helper.js',
display: "short",
summary: true
}
}
},
watch: {
"scripts": {
files: ['src/**/*', 'test/spec/*' ],
tasks: ['default'],
options: {
spawn: false
}
},
"grunt": {
files: [ 'Gruntfile.js' ]
}
},
connect: {
server: {
options: {
port: 9400,
base: '.',
keepalive: true
}
}
}
});
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-jasmine');
// Default task(s).
grunt.registerTask('default', ['clean', 'concat', 'copy', 'jasmine']);
grunt.registerTask('server', ['connect:server']);
grunt.registerTask('dev', [ 'default', 'watch' ]);
};
修改端口
3)启动容器
docker run -d -p 9400:9400 -v /home/es/config/Gruntfile.js:/usr/src/app/Gruntfile.js --name es-manager mobz/elasticsearch-head:5
5)浏览器访问http://192.168.130.131:9400/
3.安装kibana
#查看ES01的docker内容器
[root@localhost es]# docker inspect ES01 | grep IPAddress
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.2",
"IPAddress": "172.17.0.2",
#运行容器
docker run --name es-kibana -e ELASTICSEARCH_URL=http://172.17.0.2:9200 -p 5601:5601 -d kibana:6.8.3
参数ELASTICSEARCH_URL一定要是docker里面的内部地址,否则Kibana访问不到Elasticsearch。
成功如下图: