Docker起服务和Dockerfile的学习

https://blog.csdn.net/u012787436/article/details/39722583 linux中对进程的管理
https://blog.csdn.net/qq_29755359/article/details/80589472 docker中可以使用systemctl

解决docker中centos7的报错:

There are no enabled repos.
Run "yum repolist all" to see the repos you have.
To enable Red Hat Subscription Management repositories:
     subscription-manager repos --enable <repo>
To enable custom repositories:
     yum-config-manager --enable <repo>
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

docker命令补全工具

yum install -y bash-completion

注意:必须要退出终端,重新登录一次才能生效。

RPM安装elasticsearch:

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.4.rpm

安装epel源:

yum -y install epel-release
yum list

docker安装elasticsearch

https://www.cnblogs.com/xiao987334176/p/9924564.html

开启端口映射,主机到虚拟机,制作一个镜像

docker run -dit --name="nginx01" -p8080:80  --privileged centos /usr/sbin/init

用bash交互的方式连接到容器

docker exec -it ID号 bash

编写dockerfile

FROM centos

RUN yum install -y wget java-1.8.0-openjdk && yum clean all && \
    wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.4.rpm && \
    rpm -ivh elasticsearch-6.2.4.rpm && rm -f elasticsearch-6.2.4.rpm && \
    sed -i '55s/#network.host: 192.168.0.1/network.host: 0.0.0.0/g' /etc/elasticsearch/elasticsearch.yml && \
    sed -i '59s/#http.port: 9200/http.port: 9200/g' /etc/elasticsearch/elasticsearch.yml
EXPOSE 9200
ENTRYPOINT runuser -s /bin/bash -l elasticsearch -c "/usr/share/elasticsearch/bin/elasticsearch"

来自镜像:centos
RUN:\进行分割操作
EXPOSE:暴露端口
ENTRYPOINT :镜像run起来之后,默认要执行的命令
runuser 指定用户执行命令。
-s 指定环境变量
-l 指定用户
-c 执行的命令

编排镜像:

docker build -t elasticsearch ~/dockerfile_elasticsearch(这里填自己创建的目录,目录里面vim dockerfile文件)

启动容器,进行端口映射

docker run -p 9200:9200 -d -it elasticsearch

默认会直接调用命令 runuser -s /bin/bash -l elasticsearch -c “/usr/share/elasticsearch/bin/elasticsearch”
-d:后台运行
-p 表示端口映射,hostPort : containerPort,左边是本机的,右边是容器的
它会一值hold住,监听9200端口

查看端口使用情况

netstat -anpt

docker 私有仓库搭建指南

docker pull registry
docker run -itd -v /data/registry:/var/lib/registry -p 5000:5000 --restart=always --name registry_new registry:latest
docker pull busybox
docker exec -it d837c72b876c /bin/sh
docker tag busybox:latest 192.168.137.128:5000/busybox:v1

配置私服仓库

cd /etc/docker
vim daemon.json
添加 "insecure-registries": ["192.168.137.128:5000"]
docker push 192.168.137.128:5000/busybox:v1

Prometheus介绍

Prometheus(普罗米修斯)是一套开源的监控&报警&时间序列数据库的组合,起始是由SoundCloud公司开发的。
现在最常见的Kubernetes容器管理系统中,通常会搭配Prometheus进行监控。
Prometheus基本原理是通过HTTP协议周期性抓取被监控组件的状态,这样做的好处是任意组件只要提供HTTP接口就可以接入监控系统,不需要任何SDK或者其他的集成过程。这样做非常适合虚拟化环境比如VM或者Docker 。(说白了就是不需要写SDK,直接HTTP就可以通信)
Prometheus应该是为数不多的适合Docker、Mesos、Kubernetes环境的监控系统之一。
输出被监控组件信息的HTTP接口被叫做exporter 。目前互联网公司常用的组件大部分都有exporter可以直接使用,比如Varnish、Haproxy、Nginx、MySQL、Linux 系统信息 (包括磁盘、内存、CPU、网络等等),具体支持的源看:https://github.com/prometheus。

优点:总之就是好,不用问为什么,小米也开源了一款具体叫什么自行百度

工作原理:它的服务过程是这样的Prometheus daemon负责定时去目标上抓取metrics(指标) 数据,每个抓取目标需要暴露一个http服务的接口给它定时抓取。
Prometheus:支持通过配置文件、文本文件、zookeeper、Consul、DNS SRV lookup等方式指定抓取目标。支持很多方式的图表可视化,例如十分精美的Grafana,自带的Promdash,以及自身提供的模版引擎等等,还提供HTTP API的查询方式,自定义所需要的输出。
Alertmanager:是独立于Prometheus的一个组件,可以支持Prometheus的查询语句,提供十分灵活的报警方式。
PushGateway:这个组件是支持Client主动推送metrics到PushGateway,而Prometheus只是定时去Gateway上抓取数据。
如果有使用过statsd的用户,则会觉得这十分相似,只是statsd是直接发送给服务器端,而Prometheus主要还是靠进程主动去抓取。
大多数Prometheus组件都是用Go编写的,它们可以轻松地构建和部署为静态二进制文件。访问prometheus.io以获取完整的文档,示例和指南。
Prometheus从根本上所有的存储都是按时间序列去实现的,相同的metrics(指标名称) 和label(一个或多个标签) 组成一条时间序列,不同的label表示不同的时间序列。为了支持一些查询,有时还会临时产生一些时间序列存储。
metrics name&label指标名称和标签
每条时间序列是由唯一的”指标名称”和一组”标签(key=value)”的形式组成。
指标名称:一般是给监测对像起一名字,例如http_requests_total这样,它有一些命名规则,可以包字母数字_之类的的。通常是以应用名称开头_监测对像_数值类型_单位这样。例如:push_total、userlogin_mysql_duration_seconds、app_memory_usage_bytes。
标签:就是对一条时间序列不同维度的识别了,例如一个http请求用的是POST还是GET,它的endpoint是什么,这时候就要用标签去标记了。最终形成的标识便是这样了:http_requests_total{method=”POST”,endpoint=”/api/tracks”}。
记住,针对http_requests_total这个metrics name无论是增加标签还是删除标签都会形成一条新的时间序列。

查询语句就可以跟据上面标签的组合来查询聚合结果了。

如果以传统数据库的理解来看这条语句,则可以考虑http_requests_total是表名,标签是字段,而timestamp是主键,还有一个float64字段是值了。(Prometheus里面所有值都是按float64存储)。

Prometheus四种数据类型

1.Counter

Counter用于累计值,例如记录请求次数、任务完成数、错误发生次数。一直增加,不会减少。重启进程后,会被重置。

例如:http_response_total{method=”GET”,endpoint=”/api/tracks”} 100,10秒后抓取http_response_total{method=”GET”,endpoint=”/api/tracks”} 100。

2.Gauge

Gauge常规数值,例如 温度变化、内存使用变化。可变大,可变小。重启进程后,会被重置。

例如: memory_usage_bytes{host=”master-01″} 100 < 抓取值、memory_usage_bytes{host=”master-01″} 30、memory_usage_bytes{host=”master-01″} 50、memory_usage_bytes{host=”master-01″} 80 < 抓取值。

3.Histogram

Histogram(直方图)可以理解为柱状图的意思,常用于跟踪事件发生的规模,例如:请求耗时、响应大小。它特别之处是可以对记录的内容进行分组,提供count和sum全部值的功能。

例如:{小于10=5次,小于20=1次,小于30=2次},count=7次,sum=7次的求和值。

4.Summary

Summary和Histogram十分相似,常用于跟踪事件发生的规模,例如:请求耗时、响应大小。同样提供 count 和 sum 全部值的功能。

例如:count=7次,sum=7次的值求值。

它提供一个quantiles的功能,可以按%比划分跟踪的结果。例如:quantile取值0.95,表示取采样值里面的95%数据。

安装运行Prometheus(docker版)

下面介绍如何使用Prometheus和Grafana对本机服务器性能进行监控。
监控本机,只需要一个exporter
node_exporter – 用于机器系统数据收集

Grafana是一个开源的功能丰富的数据可视化平台,通常用于时序数据的可视化。它内置了以下数据源的支持:

Prometheus:9090 Grafana:3000

docker pull prom/node-exporter
docker pull prom/prometheus
docker pull grafana/grafana
docker run -d -p 9100:9100 \
  -v "/proc:/host/proc:ro" \
  -v "/sys:/host/sys:ro" \
  -v "/:/rootfs:ro" \
  --net="host" \
  prom/node-exporter
netstat -anpt

浏览器访问查看

http://192.168.137.144:9100/metrics(填你自己虚拟机的ip地址)

启动prometheus

新建目录prometheus,编辑配置文件prometheus.yml

mkdir /opt/prometheus
cd /opt/prometheus/
vim prometheus.yml
global:
  scrape_interval:     60s
  evaluation_interval: 60s
 
scrape_configs:
  - job_name: prometheus
    static_configs:
      - targets: ['localhost:9090']
        labels:
          instance: prometheus
 
  - job_name: linux
    static_configs:
      - targets: ['192.168.91.132:9100']
        labels:
          instance: localhost

启动prometheus

docker run  -d \
  -p 9090:9090 \
  -v /opt/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml  \
  prom/prometheus
netstat -anpt

访问:http://192.168.137.144:9090/graph
访问:http://192.168.137.144:9090/targets

启动grafana

新建空文件夹grafana-storage,用来存储数据

mkdir /opt/grafana-storage

设置权限

chmod 777 -R /opt/grafana-storage

启动grafana

docker run -d \
  -p 3000:3000 \
  --name=grafana \
  -v /opt/grafana-storage:/var/lib/grafana \
  grafana/grafana

访问: http://192.168.137.144:3000/

默认密码都是admin
然后进去修改密码

name名字写Prometheus

type 选择Prometheus,因为数据都从它那里获取

url 输入Prometheus的ip+端口

点击 Graph

点击标题下方的编辑

输入cpu,底部会有提示

https://www.cnblogs.com/xiao987334176/p/9930517.html 感谢大佬的分享

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值