Ubuntu18配置elasticsearch服务开机自启踩坑

文章目录

      • Ubuntu18.4TLS实现ES服务自启动的详细步骤
      • 典型报错:
        • 各种上限问题
        • elasticsearch访问外网
        • 端口占用
      • 引用

Ubuntu18.4TLS实现ES服务自启动的详细步骤

首先你应该确定当前系统的初始化系统使用的那种类型
常见的用sysvinit、UpStart、systemd这三种
通过运行ps ax | grep init
如果出现了进程号为1的init进程,说明是前两者,然后看/etc/initlab文件是否存在,如果存在,则为sysvinit。否则为UpStart。
如果没有则 ps ax | grep systemd ,出现进程号为1的systemd进程,则为systemd初始化系统。
不同的初始化系统的自定义配置服务的方式不同

  1. 在/etc/init.d目录下,添加执行的脚本,并指明RUNLEVEL以及abort等内容

    避免类似下面的报错

    update-rc.d: error: mydaemonDefault-Start contains no runlevels, aborting
    

    给出elasticsearch的样例脚本
    elasticsearch.service

    #!/bin/sh
    #description: elasticsearch
    ### BEGIN INIT INFO
    # Provides:          elasticsearch
    # Required-Start:    $all
    # Required-Stop:
    # Default-Start:     2 3 4 5
    # Default-Stop:
    # Short-Description: start elasticsearch
    ### END INIT INFO
    
    
    case "$1" in
    start)
        cd /home/fengyuluo/local/elasticsearch-6.3.2
        ./bin/elasticsearch -d
    !
        echo "elasticsearch startup"
        ;;
    stop)
        es_pid=`ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}'`
        kill -9 $es_pid
        echo "elasticsearch stopped"
        ;;
    restart)
        es_pid=`ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}'`
        kill -9 $es_pid
        echo "elasticsearch stopped"
        cd /home/fengyuluo/local/elasticsearch-6.3.2
        ./bin/elasticsearch -d
    !
        echo "elasticsearch startup"
        ;;
    *)
        echo "start|stop|restart"
        ;;
    esac
    
    exit $?
    
    
  2. 去/lib/systemd/system目录下添加服务脚本
    elasticsearch.serivce

    [Unit]
    Description=elasticsearch
    After=network.target
    
    [Service]
    Type=simple
    ExecStart=/bin/bash /home/fengyuluo/local/elasticsearch-6.3.2/bin/elasticsearch
    Restart=always
    User=fengyuluo
    Group=fengyuluo
    WorkingDirectory=/home/fengyuluo/local/elasticsearch-6.3.2
    [Install]
    WantedBy=mutil-user.target
    

    ​ 指明user和group,普通用户,避免elasticsearch因为root用户运行而报错

  3. 执行sudo systemctl daemon-reload

  4. 重启服务
    sudo systemctl elasticsearch.service

  5. 设置服务开机自启
    sudo systemctl enable elasticsearch.service

典型报错:

各种上限问题

max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
max number of threads [1024] for user [hadoop] is too low, increase to at least [2048]
max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

解决方案:

  • max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]

修改 /etc/systemd/user.conf 及 /etc/systemd/system.conf 中如下面这行的配置项(这将处理图形登录):

DefaultLimitNOFILE=65536

修改 /etc/security/limits.conf 中如下面这几行(这将处理非图形登录):

* soft nofile 65536 
* hard nofile 65536

这三个地方都要修改,然后重启计算机生效

  • max number of threads [1024] for user [hadoop] is too low, increase to at least [2048]

    vim /etc/security/limits.d/90-nproc.conf

    找到如下内容:

    soft nproc 1024

    修改为

    soft nproc 2048

  • max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

    vi /etc/sysctl.conf

    添加下面配置:

    vm.max_map_count=655360

    并执行命令:

    sysctl -p

elasticsearch访问外网

​ 如果elasticsearch有需要外网访问的需求,更改elasticsearch.yml文件

network.host: 0.0.0.0

​ 保存然后运行,应该就会报第六条中列出的错误,照解决方案修改

端口占用

Elasticsearch会占用9200用于外部通迅
9300用于集群内部节点之间的通讯

引用

Elasticsearch开机启动脚本
Systemd 入门教程:实战篇
Ubuntu 18.04 LTS ulimit 修改不生效的问题

你可能感兴趣的:(大数据,操作系统,Elastic,Stack)