企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# Docker 方式安装 原文链接 : [https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html) 译文链接 : [http://apache.wiki/pages/editpage.action?pageId=4882734](http://apache.wiki/pages/editpage.action?pageId=4882734) 贡献者 : @漫步 ## Docker 方式安装 Elasticsearch也可作为一个docker镜像来安装。该镜像是需要 [X-Pack](https://www.elastic.co/guide/en/x-pack/5.0/index.html) 来built ## 安全注意事项[](https://github.com/elastic/elasticsearch/edit/5.0/docs/reference/setup/install/docker.asciidoc "编辑GitHub上此页") ![注意](https://img.kancloud.cn/50/1f/501f430e0c03baa87f0bbf9c0e08af0d_66x58.jpg) [X-Pack](https://www.elastic.co/guide/en/x-pack/5.0/index.html) 被在此图像中预装。请花几分钟时间来熟悉[的](https://www.elastic.co/guide/en/x-pack/5.0/security-getting-started.html)[X-Pack](https://www.elastic.co/guide/en/x-pack/5.0/index.html)[安全](https://www.elastic.co/guide/en/x-pack/5.0/security-getting-started.html)和如何更改默认密码。为默认密码`elastic`用户`changeme`。 ![注意](https://img.kancloud.cn/50/1f/501f430e0c03baa87f0bbf9c0e08af0d_66x58.jpg) [X-Pack](https://www.elastic.co/guide/en/x-pack/5.0/index.html) 包括30天的试用许可证。在这之后,你可以得到一个[可用的预订](https://www.elastic.co/subscriptions)或[停用安全](https://www.elastic.co/guide/en/x-pack/5.0/security-settings.html)。基本许可证是免费的,包括[监控](https://www.elastic.co/products/x-pack/monitoring)扩展。 获取Elasticsearch的码头工人是发出一个简单`docker pull`靠弹性泊坞窗注册表命令。 泊坞窗图像可以用下面的命令得到: ``` docker pull docker.elastic.co/elasticsearch/elasticsearch:5.0.1 ``` ## 在命令行中运行Elasticsearch ### 开发模式 Elasticsearch可以很快开始为开发或测试使用用下面的命令: ``` docker run -p 9200:9200 -e "http.host=0.0.0.0" -e "transport.host=127.0.0.1" docker.elastic.co/elasticsearch/elasticsearch:5.0.1 ``` ### 生产方式 ![重要](https://img.kancloud.cn/15/d7/15d74881ee7292bc56d26f6af9b642a8_66x58.jpg) `vm_max_map_count`内核设置需要被设置到至少`262144`用于生产。根据您的平台: * Linux 该`vm_map_max_count`设置应永久在/etc/sysctl.conf中进行设置: ``` $ grep的vm.max_map_count的/etc/sysctl.conf vm.max_map_count = 262144 ``` 应用设置在实时系统类型: `sysctl -w vm.max_map_count=262144` * OSX with [Docker for Mac](https://docs.docker.com/engine/installation/mac/#/docker-for-mac)`vm_max_map_count`设置必须与xhyve虚拟机中进行设置: ``` $ screen ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/tty ``` 登录,没有密码。然后配置`sysctl`设置: ``` sysctl的-w vm.max_map_count = 262144 ``` OSX with [Docker Toolbox](https://docs.docker.com/engine/installation/mac/#docker-toolbox) `vm_max_map_count`设置必须通过泊坞窗机进行设置: ``` docker-machine ssh sudo sysctl -w vm.max_map_count=262144 ``` 下面的例子带来了包括两个Elasticsearch节点的群集。要打开集群,使用[`docker-compose.yml`](https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html#docker-prod-cluster-composefile)和公正的类型: ``` docker-compose up ``` ![注意](https://img.kancloud.cn/50/1f/501f430e0c03baa87f0bbf9c0e08af0d_66x58.jpg) `docker-compose`没有预装泊坞窗上的Linux。说明安装它可以在找到[泊坞窗,撰写网页](https://docs.docker.com/compose/install/#install-using-pip)。 节点`elasticsearch1`监听`localhost:9200`,而`elasticsearch2`谈判`elasticsearch1`在码头工人的网络。 这个例子还使用[泊坞命名卷](https://docs.docker.com/engine/tutorials/dockervolumes),被称为`esdata1`和`esdata2`将,如果不存在创建。 `docker-compose.yml`: ``` version: '2' services: elasticsearch1: image: docker.elastic.co/elasticsearch/elasticsearch:5.0.1 container_name: elasticsearch1 environment: - cluster.name=docker-cluster - bootstrap.memory_lock=true - "ES_JAVA_OPTS=-Xms512m -Xmx512m" ulimits: memlock: soft: -1 hard: -1 nofile: soft: 65536 hard: 65536 mem_limit: 1g cap_add: - IPC_LOCK volumes: - esdata1:/usr/share/elasticsearch/data ports: - 9200:9200 networks: - esnet elasticsearch2: image: docker.elastic.co/elasticsearch/elasticsearch:5.0.1 environment: - cluster.name=docker-cluster - bootstrap.memory_lock=true - "ES_JAVA_OPTS=-Xms512m -Xmx512m" - "discovery.zen.ping.unicast.hosts=elasticsearch1" ulimits: memlock: soft: -1 hard: -1 nofile: soft: 65536 hard: 65536 mem_limit: 1g cap_add: - IPC_LOCK volumes: - esdata2:/usr/share/elasticsearch/data networks: - esnet volumes: esdata1: driver: local esdata2: driver: local networks: esnet: driver: bridge ``` 要停止集群类型`docker-compose down`。数据量将继续存在,因此有可能使用相同的数据与重新开始集群`docker-compose up`。要销毁集群**和数据量**只需键入`docker-compose down -v`。 ### 检查集群的现状 ``` curl -u elastic http://127.0.0.1:9200/_cat/health Enter host password for user 'elastic': 1472225929 15:38:49 docker-cluster green 2 2 4 2 0 0 0 0 - 100.0% ``` 将消息记录到控制台和配置多克尔记录驾驶员进行处理。默认情况下,你可以访问日志`docker logs`。 ##  docker配置Elasticsearch Elasticsearch从加载文件下它的配置`/usr/share/elasticsearch/config/`。这些配置文件都记录在[_配置Elasticsearch_](https://www.elastic.co/guide/en/elasticsearch/reference/current/settings.html "配置Elasticsearch")和[设置JVM系统属性](https://www.elastic.co/guide/en/elasticsearch/reference/current/setting-system-settings.html#es-java-opts "设置JVM系统属性")。 图像提供了多种方式与传统的方法是提供自定义的文件,即配置Elasticsearch设置`elasticsearch.yml`,但它也可以使用环境变量设置选项: #### A.通过docker环境变量传递参数 例如,要定义与群集名称`docker run`可以传递`-e "[cluster.name](http://cluster.name)=mynewclustername"`。双引号是必需的。 ![注意](https://img.kancloud.cn/50/1f/501f430e0c03baa87f0bbf9c0e08af0d_66x58.jpg) 有限定之间的差[的默认设置](https://www.elastic.co/guide/en/elasticsearch/reference/current/settings.html#_setting_default_settings "设置默认settingsedit")和正常设置。前者的前缀`default.`并不能覆盖正常的设置,如果定义。 #### B.绑定安装配置 创建自定义配置文件并安装这个在图像上的相应文件。例如,绑定安装一个`custom_elasticsearch.yml`带有`docker run`可以通过参数来实现: ``` -v full_path_to / custom_elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml ``` ![重要](https://img.kancloud.cn/15/d7/15d74881ee7292bc56d26f6af9b642a8_66x58.jpg) `custom_elasticsearch.yml` 应该是可读的UID:GID `1000:1000` #### C.自定义图像 在某些环境中,它可能会更有意义,以制备含有配置的自定义图像。一个`Dockerfile`实现,这可能是简单的: ``` FROM docker.elastic.co/elasticsearch/elasticsearch:5.0.1 ADD elasticsearch.yml /usr/share/elasticsearch/config/ USER root chown elasticsearch:elasticsearch config/elasticsearch.yml USER elasticsearch ``` 然后,您可以建立并尝试与类似的图像: ``` docker build --tag=elasticsearch-custom . docker run -ti -v /usr/share/elasticsearch/data elasticsearch-custom ``` #### D.覆盖图像的默认[CMD](https://docs.docker.com/engine/reference/run/#cmd-default-command-or-options) 选项可以作为命令行选项来Elasticsearch过程覆盖默认命令为图像传递。例如: ``` docker run <various parameters> bin/elasticsearch -Ecluster.name=mynewclustername ``` ## 默认生产和使用的注意事项 我们收集了一些供生产使用的最佳实践。 ![注意](https://img.kancloud.cn/50/1f/501f430e0c03baa87f0bbf9c0e08af0d_66x58.jpg) 以下提到的任何参数码头工人承担使用`docker run`。 1. 经由多克尔CLI正确设置的功能和ulimits是重要的。正如前面的例子中看出[docker-compose.yml](https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html#docker-prod-cluster-composefile),需要下列选项: ``` --cap-add=IPC_LOCK --ulimit memlock=-1:-1 --ulimit nofile=65536:65536 ``` 2. 确保`bootstrap.memory_lock`被设置为`true`如在“解释[禁用交换](https://www.elastic.co/guide/en/elasticsearch/reference/current/setup-configuration-memory.html "禁用交换") ”。 这可以通过任何的实现[的配置方法](https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html#docker-configuration-methods "与泊坞窗配置Elasticsearch"),例如,通过设置与适当的环境变量`-e "bootstrap.memory_lock=true"`。 3. 形象[暴露](https://docs.docker.com/engine/reference/builder/#/expose) TCP端口9200和9300。对于建议随机与发布的港口集群`--publish-all`,除非你是钉住每个主机一个容器。 4. 使用`ES_JAVA_OPTS`环境变量设置堆大小,如使用16GB的使用`-e ES_JAVA_OPTS="-Xms16g -Xmx16g"`与`docker run`。此外,还建议设置[内存限制](https://docs.docker.com/engine/reference/run/#user-memory-constraints)的容器。 5. 固定您的部署到Elasticsearch多克尔图像的特定版本,例如`[docker.elastic.co/elasticsearch/elasticsearch:5.0.1](http://docker.elastic.co/elasticsearch/elasticsearch:5.0.1)`。 6. 总是使用上结合的体积`/usr/share/elasticsearch/data`,如图中[生产例子](https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html#docker-cli-run-prod-mode "生产模式"),原因如下: 1. 如果容器杀了你elasticsearch节点的数据也不会丢失 2. Elasticsearch是I / O敏感和泊坞窗存储驱动程序是不理想的高速I / O 3. 它可以利用先进的[docker 容器插件](https://docs.docker.com/engine/extend/plugins/#volume-plugins) 7. 如果您使用的是devicemapper存储驱动程序(默认情况下至少红帽(RPM)的分布)确保你不使用默认`loop-lvm`模式。配置泊坞窗引擎使用[直接LVM](https://docs.docker.com/engine/userguide/storagedriver/device-mapper-driver/#configure-docker-with-devicemapper)来代替。 8. 请考虑使用不同的集中你的日志[记录的驱动程序](https://docs.docker.com/engine/admin/logging/overview/)。还要注意的是默认的JSON文件记录的驱动程序并不适合用于生产。 ## 下一步 现在,您有一个测试环境Elasticsearch成立。开始之前严重的发展或进入生产与Elasticsearch,你需要做一些额外的设置: * 了解如何[配置Elasticsearch](https://www.elastic.co/guide/en/elasticsearch/reference/current/settings.html "配置Elasticsearch")。 * 配置[重要Elasticsearch设置](https://www.elastic.co/guide/en/elasticsearch/reference/current/important-settings.html "重要Elasticsearch配置")。 * 配置[重要的系统设置](https://www.elastic.co/guide/en/elasticsearch/reference/current/system-config.html "重要的系统配置")。