利用Docker搭建你的第一个应用栈以及部署监控:Haproxy+Django+Redis+Cadvisor+Consul+Prometheus+Grafana

本文详细记录了使用Docker在CentOS 7上搭建Haproxy、Django、Redis应用栈,以及通过Consul、Prometheus和Grafana进行监控的全过程。涉及的关键步骤包括Docker的安装、数据库主从配置、APP容器创建、haproxy代理设置和监控组件的部署。在部署过程中遇到了如配置文件生效、端口映射、服务注册等问题,并给出了相应的解决方案。
摘要由CSDN通过智能技术生成

在拜读浙江大学SEL实验室著作的《Docker容器与容器云》第二版时,自己按照书中在自己的虚拟机中搭建了一个由docker容器构建的应用栈以及部署了对其的监控。由于docker hub中镜像版本的的更迭,按照书中的步骤进行部署,不免会遇到一些问题,在此记录下部署过程也分享下部署过程中踩过的坑。

搭建的环境是:

1:虚拟机工具是VMware Workstations

2:操作系统是CentOS Linux release 7.5.1804 (Core)

3:linux内核是3.10.0-862.el7.x86_64

整个部署过程可以分为三部分:

1.安装docker

2.Haproxy+Django+Redis搭建应用栈

3.Cadvisor+Consul+Prometheus+Grafana部署监控

以下操作均在root用户下进行,其他用户请注意授予sudo权限

第一部分:安装docker

执行以下命令:

yum install docker

回显信息:

执行命令:

[root@localhost ~]# systemctl start docker

查看docker基本信息:

[root@localhost ~]# docker info

回显信息:

Containers: 0
 Running: 0
 Paused: 0
 Stopped: 0
Images: 0
Server Version: 1.13.1
...

docker安装完毕!

第二部分:Haproxy+Django+Redis搭建应用栈

  • 准备工作

1.拉取最新版本的镜像,执行以下命令:

[root@localhost ~]# docker pull redis
[root@localhost ~]# docker pull django
[root@localhost ~]# docker pull haproxy

2.查看拉取的镜像,执行以下命令

[root@localhost ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/haproxy   latest              d23194a3929a        5 days ago          72 MB
docker.io/redis     latest              5d2989ac9711        2 weeks ago         95 MB
docker.io/django    latest              eb40dcf64078        2 years ago         436 MB

3.创建容器挂载点目录,之后要给容器挂在文件目录,书中没有提前挂载,在启动容器时,如果不使用-v参数指定挂载目录,docker会默认自己指定挂载目录。执行以下命令:

[root@localhost ~]# cd /home
[root@localhost home]# mkdir docker
[root@localhost home]# ls
docker
[root@localhost home]# cd docker/
[root@localhost docker]# mkdir redis django haproxy
[root@localhost docker]# ls
django  haproxy  redis
  • 应用栈数据库节点配置

数据库配置包括主数据库和从数据库的配置,分别创建主数据库和从数据库的文件挂载点,执行以下命令:

[root@localhost ~]# cd /home/docker/redis
[root@localhost redis]# mkdir master slave1 slave2

1.master数据库的配置如下:

启动redis-master容器,执行以下命令:

[root@localhost redis]# docker run -it --name redis-master -v /home/docker/redis/master/:/data redis

--name 给容器进行命名,-v指定文件挂载目录

得到结果:

root@e54206d487ed:/data#

这个终端是容器虚拟出来的终端。

从容器终端中退出,使用快捷键Ctrl+D,或者执行以下命令:

root@e54206d487ed:/data# exit

切换到redis-master容器挂载的文件目录,创建redis的配置文件,文件名字是redis.conf配置文件内容,按照网上的模板书写就行,注意修改其中的参数。

[root@localhost ~] cd /home/docker/redis/master
[root@localhost ~] vim redis.conf

修改的参数为:

daemonize yes 注意是将yes修改为no,使Redis在容器前端运行,在前端运行时无法进行其他操作,可以再开一个终端。
pidfile /var/run/redis.pid

再次进入redis-master虚拟终端中,切换到容器中的volume目录:

[root@localhost master]# docker exec -it redis-master /bin/bash
root@e54206d487ed:/data# cd /data

复制启动配置文件从/data到redis工作目录/usr/local/bin下,执行以下命令:

root@e54206d487ed:/data# cp /data/redis.conf /usr/local/bin/
root@e54206d487ed:/data# ls
redis.conf
root@e54206d487ed:/data# cp /data/redis.conf /usr/local/bin/
root@e54206d487ed:/data# cd /usr/local/bin/
root@e54206d487ed:/usr/local/bin# ls
docker-entrypoint.sh  gosu  redis-benchmark  redis-check-aof  redis-check-rdb  redis-cli  redis-sentinel  redis-server	redis.conf

启动redis-master服务

root@e54206d487ed:/usr/local/bin# redis-server redis.conf 
31:C 14 Jan 2019 06:02:44.717 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
31:C 14 Jan 2019 06:02:44.717 # Redis versio
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一个典型的Haproxy_exporter的监控metric例子 包含指标 haproxy_backend_bytes_in_total haproxy_backend_bytes_out_total haproxy_backend_client_aborts_total haproxy_backend_compressor_bytes_bypassed_total haproxy_backend_compressor_bytes_in_total haproxy_backend_compressor_bytes_out_total haproxy_backend_connection_errors_total haproxy_backend_current_queue haproxy_backend_current_server haproxy_backend_current_session_rate haproxy_backend_current_sessions haproxy_backend_http_connect_time_average_seconds haproxy_backend_http_queue_time_average_seconds haproxy_backend_http_response_time_average_seconds haproxy_backend_http_responses_compressed_total haproxy_backend_http_responses_total haproxy_backend_http_total_time_average_seconds haproxy_backend_response_errors_total haproxy_backend_retry_warnings_total haproxy_backend_server_aborts_total haproxy_backend_server_selected_total haproxy_backend_sessions_total haproxy_backend_up haproxy_backend_weight haproxy_exporter_build_info haproxy_exporter_csv_parse_failures haproxy_exporter_total_scrapes haproxy_frontend_bytes_in_total haproxy_frontend_bytes_out_total haproxy_frontend_compressor_bytes_bypassed_total haproxy_frontend_compressor_bytes_in_total haproxy_frontend_compressor_bytes_out_total haproxy_frontend_connections_total haproxy_frontend_current_session_rate haproxy_frontend_current_sessions haproxy_frontend_http_requests_total haproxy_frontend_http_responses_compressed_total haproxy_frontend_http_responses_total haproxy_frontend_limit_session_rate haproxy_frontend_limit_sessions haproxy_frontend_max_session_rate haproxy_frontend_max_sessions haproxy_frontend_request_errors_total haproxy_frontend_requests_denied_total haproxy_frontend_sessions_total haproxy_server_bytes_in_total haproxy_server_bytes_out_total haproxy_server_check_duration_seconds haproxy_server_check_failures_total haproxy_server_client_aborts_total haproxy_server_connection_errors_total haproxy_server_current_queue haproxy_server_current_session_rate haproxy_server_current_sessions haproxy_server_downtime_seconds_total haproxy_server_http_responses_total haproxy_server_max_queue haproxy_server_max_session_rate haproxy_server_max_sessions haproxy_server_redispatch_warnings_total haproxy_server_response_errors_total haproxy_server_retry_warnings_total haproxy_server_server_aborts_total haproxy_server_server_selected_total haproxy_server_sessions_total haproxy_server_up haproxy_server_weight haproxy_up process_cpu_seconds_total process_max_fds process_open_fds process_resident_memory_bytes process_start_time_seconds process_virtual_memory_bytes promhttp_metric_handler_requests_in_flight promhttp_metric_handler_requests_total
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值