【原创】运维基础之Docker(2)通过docker部署zookeeper nginx tomcat redis kibana/elasticsearch/logstash mysql kafka mesos/marathon

时间:2023-03-09 18:47:37
【原创】运维基础之Docker(2)通过docker部署zookeeper nginx tomcat redis kibana/elasticsearch/logstash mysql kafka mesos/marathon

通过docker可以从头开始构建集群,也可以将现有集群(配置以及数据)平滑的迁移到docker部署;

1 docker部署zookeeper

# usermod -G docker zookeeper
# mkdir /data
# chown zookeeper.zookeeper /data
# echo 1 > /data/myid
# su - zookeeper
$ docker run --name zookeeper --restart always -p 2181:2181 -p 2888:2888 -p 3888:3888 -e ZOO_LOG4J_PROP="INFO,ROLLINGFILE" -v /var/log/zookeeper:/logs -v /conf/zoo.cfg:/conf/zoo.cfg -v /data:/data -v /etc/hosts:/etc/hosts:ro -d zookeeper

其中zoo.cfg使用正常的集群配置即可,需要挂载配置文件、日志目录、数据目录、host文件等,

注意:

1)zoo.cfg中的server.*配置可以通过 -e ZOO_SERVERS=server.1=0.0.0.0:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888 代替,其中本机的ip要配置为0.0.0.0,否则启动的时候会绑定端口失败

2)/data/myid文件也可以通过 -e ZOO_MY_ID=1 代替

参考:https://docs.docker.com/samples/library/zookeeper/

2 docker部署nginx

# docker run --name nginx --restart always -p 80:80 -v /etc/nginx/nginx.conf:/etc/nginx/nginx.conf:ro -v /etc/nginx/passwd:/etc/nginx/passwd:ro -v /var/log/nginx:/var/log/nginx -d nginx

其中nginx.conf使用正常的配置即可,需要挂载配置文件、日志目录、密码文件等,有需要还可以挂载html目录 -v /some/content:/usr/share/nginx/html:ro;

参考:https://docs.docker.com/samples/library/nginx/

3 docker部署tomcat

$ docker run -d --name tomcat -p 8080:8080 tomcat:8.0

参考:https://docs.docker.com/samples/library/tomcat/

4 docker部署redis

$ docker run -d --name redis -p 6379:6379 -v /redis/data/dir:/data redis redis-server --appendonly yes

参考:https://docs.docker.com/samples/library/redis/

5 docker部署elk

由于kibana、elasticsearch、logstash3个image内部分别使用各自的用户启动,并且uid和gid均为1000,为了避免冲突,使用一个新的elk组,gid为1000,将3个用户都添加到组中,保证对目录的写权限

# groupadd -g 1000 elk
# usermod -G elk elasticsearch
# usermod -G elk logstash
# usermod -G elk kibana
# chgrp elk $dir

kibana

# usermod -G docker kibana
# su - kibana
$ docker run --name kibana -p 5601:5601 -v /var/log/kibana:/usr/share/kibana/log -v /etc/kibana/kibana.yml:/usr/share/kibana/config/kibana.yml -d kibana:6.6.0

其中kibana.yml使用正常配置即可,需要挂载配置文件等;

如果分配的内存太少,kibana可能进程直接消失,而且查不到任何logs,至少512m以上;

参考:

https://docs.docker.com/samples/library/kibana/

https://www.elastic.co/guide/en/kibana/current/docker.html

elasticsearch

# usermod -G docker elasticsearch
# chgrp -R elk /opt/lmm/es_data/
# su - elasticsearch
$ docker run -d --name logstash -p 9600:9600 -v /var/log/nginx:/var/log/nginx:ro -v /var/log/logstash:/usr/share/logstash/logs -v /etc/logstash/GeoLite2-City.mmdb:/usr/share/logstash/config/GeoLite2-City.mmdb -v /etc/logstash/nginx_access.conf:/usr/share/logstash/pipeline/logstash.conf docker.elastic.co/logstash/logstash:6.6.0

其中elasticsearch.yml使用正常配置即可,需要设置宿主机ip,挂载配置文件、数据目录、日志目录等;

注意:
1)一定要保证索引目录的owner或者group的id为1000(此处设置group为1000),否则会报failed to obtain node locks或者AccessDeniedException
2)一定要设置network.publish_host为宿主机ip

The container runs Elasticsearch as user elasticsearch using uid:gid 1000:1000. Bind mounted host directories and files, such as custom_elasticsearch.yml above, need to be accessible by this user. For the data and log dirs, such as /usr/share/elasticsearch/data, write access is required as well.

参考:

https://docs.docker.com/samples/library/elasticsearch/

https://www.elastic.co/guide/en/elasticsearch/reference/6.6/docker.html

logstash

# usermod -G docker logstash
# chgrp -R elk /var/log/logstash
# su - logstash
$ docker run -d --name logstash -p 9600:9600 -v /var/log/nginx:/var/log/nginx:ro -v /var/log/logstash:/usr/share/logstash/logs -v /etc/logstash/GeoLite2-City.mmdb:/usr/share/logstash/config/GeoLite2-City.mmdb -v /etc/logstash/nginx_access.conf:/usr/share/logstash/pipeline/logstash.conf docker.elastic.co/logstash/logstash:6.6.0

其中logstash.conf使用正常的配置即可;要保证对日志目录可写;

Bind-mounted configuration files will retain the same permissions and ownership within the container that they have on the host system. Be sure to set permissions such that the files will be readable and, ideally, not writeable by the container’s logstash user (UID 1000).

参考:

https://docs.docker.com/samples/library/logstash/

https://www.elastic.co/guide/en/logstash/current/docker-config.html

6 docker部署mysql

$ docker run -d --name mysql -p 3306:3306 -v /var/lib/mysql:/var/lib/mysql -v /etc/my.cnf:/etc/mysql/my.cnf -e MYSQL_ROOT_PASSWORD=my-secret-pw mysql:5.7.25

其中my.cnf使用正常配置即可,需要挂载数据目录;

参考:https://docs.docker.com/samples/library/mysql/

7 docker部署kafka

$ docker run -d --name kafka -p 9092:9092 -e KAFKA_BROKER_ID=1 -e KAFKA_LOG_DIRS=/kafka/kafka-logs -e KAFKA_ZOOKEEPER_CONNECT=$zk_server:2181 -e KAFKA_ADVERTISED_HOST_NAME=$host_ip -e KAFKA_AUTO_CREATE_TOPICS_ENABLE=true -v /data/kafka-logs:/kafka/kafka-logs wurstmeister/kafka

不使用配置文件,配置通过参数传递,需要挂载数据目录;

参考:https://hub.docker.com/r/wurstmeister/kafka

8 docker部署mesos

mesos-master

$ docker run -d --name mesos-master1 --net=host --restart=always -p 5050:5050 -e MESOS_HOSTNAME=192.168.0.1 -e MESOS_IP=192.168.0.1 -e MESOS_ZK=zk://192.168.0.1:2181,192.168.0.2:2181,192.168.0.3:2181/mesos -e MESOS_PORT=5050 -e MESOS_LOG_DIR=/var/log/mesos -e MESOS_QUORUM=1 -e MESOS_REGISTRY=in_memory -e MESOS_WORK_DIR=/var/lib/mesos mesoscloud/mesos-master

逐台修改MESOS_HOSTNAME、MESOS_IP

参考:https://hub.docker.com/r/mesosphere/mesos-master

mesos-slave

$ docker run -d --name=mesos-slave1 --net=host --restart=always --pid=host --privileged=true -e MESOS_HOSTNAME=192.168.0.1 -e MESOS_IP=192.168.0.1 -e MESOS_MASTER=zk://192.168.0.1:2181,192.168.0.2:2181,192.168.0.3:2181/mesos -e MESOS_CONTAINERIZERS=docker,mesos -v /usr/bin/docker:/usr/bin/docker -v /dev:/dev -v /var/run/docker.sock:/var/run/docker.sock -v /var/log/mesos:/var/log/mesos -v /tmp/mesos:/tmp/mesos mesoscloud/mesos-slave

逐台修改MESOS_HOSTNAME、MESOS_IP

参考:https://hub.docker.com/r/mesosphere/mesos-slave

marathon

$ docker run -d --name marathon --net host --restart=always -e MARATHON_HOSTNAME=192.168.0.1 -e MARATHON_HTTPS_ADDRESS=192.168.0.1 -e MARATHON_HTTP_ADDRESS=192.168.0.1 -e MARATHON_MASTER=zk://192.168.0.1:2181,192.168.0.2:2181,192.168.0.3:2181/mesos -e MARATHON_ZK=zk://192.168.0.1:2181,192.168.0.2:2181,192.168.0.3:2181/marathon mesoscloud/marathon

逐台修改MARATHON_HOSTNAME、MARATHON_HTTPS_ADDRESS、MARATHON_HTTP_ADDRESS

参考:https://hub.docker.com/r/mesosphere/marathon

9 其他

https://docs.docker.com/samples/library/cassandra/

https://docs.docker.com/samples/library/flink/

https://docs.docker.com/samples/library/influxdb/

https://docs.docker.com/samples/library/alpine/