Press "Enter" to skip to content

Tag: centos

Linux shell get local ip address

Linux shell中获得本机IP的相关命令, 你知道几个?

hostname -I     # 不同系统返回不一样, 有些可能需要cut一下
ifconfig eth0 | grep inet | grep -v inet6 | awk '{print $2}'
ifconfig | awk '/inet addr/{print substr($2,6)}'
ifconfig | sed -n 's/.*inet addr:\([0-9.]\+\)\s.*/\1/p'
ip a s|sed -ne '/127.0.0.1/!{s/^[ \t]*inet[ \t]*\([0-9.]\+\)\/.*$/\1/p}'
ifconfig | perl -nle 's/dr:(\S+)/print $1/e'
curl ifconfig.me      # 适用于有公网IP的情况
1 Comment

Linux下的双向文件同步工具

最近有在2台机器双向同步文件的需求, 于是有了本文. 上网找了一些双向文件同步的工具, 比较典型的有syncthingmirror. 本文介绍mirror的使用方法(不过好像syncthing使用更广泛一些).

  1. 在2台机器上安装好java1.8版本(ubuntu使用sudo apt install openjdk-8-jre命令即可安装);
  2. 在2台机器上安装好Watchman(安装方法);
  3. 在2台机器上安装mirror工具(安装方法见github页面)

假设你的mirror安装在/data/apps/mirror/目录下, 2台机器需要同步/data/sftp/文件夹.

在server端运行mirror程序

写入systemd系统服务

$ vim /etc/systemd/system/mirror.service    #写入如下内容
[Unit]
Description=Mirror (File sync for two sftp servers[10.16.19.21,10.16.19.24])
After=network-online.target
After=network-online.target
Requires=network-online.target

[Service]
Type=simple
ExecStart=/bin/bash -l -c '/data/apps/mirror/mirror server'
Restart=on-failure

[Install]
WantedBy=multi-user.targe

启动程序

$ systemctl daemon-reload
$ systemctl restart mirror.service && systemctl status mirror.service
$ systemctl enable mirror.service
1 Comment

docker pull offline

如果要在一个内网/网络很差的环境下运行docker pull, 应该会让人抓狂吧. 本文介绍一下怎么解决这个问题. 本文基于CentOS 7.

1, 使用Proxy

这是大多数人首先想到的方法. docker官网说export HTTPS_PROXY / export HTTP_PROXY以后运行docker pull即可使用代理, 但是我这里是无效的, 原因未知. 这里介绍一个解决办法:

$ vim /usr/lib/systemd/system/docker.service  #在[Service]段下添加如下2行
......
[Service]
Environment="HTTPS_PROXY=http://10.10.74.101:8888"
Environment="HTTP_PROXY=http://10.10.74.101:8888"
......
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker.service

然后再次运行docker pull就不会有问题了.

请注意: 使用本方法需要一个速度很棒的proxy_server . 如果你的proxy_server速度一般, 那么即使配置生效, 极慢的pull速度也会让你怀疑人生. 因此, 不妨考虑一下下面的方法.

2, 使用docker save/load进行镜像的导入导出

首先在一个网络不错的环境里, 进行docker pull, 并且将pull回来的docker save成一个image

$ docker pull docker.elastic.co/elasticsearch/elasticsearch:6.6.2
$ docker save -o es.img docker.elastic.co/elasticsearch/elasticsearch:6.6.2

然后将这个es.img文件拷贝到没有公网/网络很差的系统里

$ docker load -i es.img
Leave a Comment

解决df -h命令卡住

通过strace命令判断是在哪个mount point卡住

$ strace df
......
stat("/sys/fs/cgroup/devices", {st_mode=S_IFDIR|0755, st_size=0, ...}) = 0
stat("/sys/fs/cgroup/pids", {st_mode=S_IFDIR|0755, st_size=0, ...}) = 0
stat("/sys/fs/cgroup/hugetlb", {st_mode=S_IFDIR|0755, st_size=0, ...}) = 0
stat("/sys/fs/cgroup/blkio", {st_mode=S_IFDIR|0755, st_size=0, ...}) = 0
stat("/sys/fs/cgroup/freezer", {st_mode=S_IFDIR|0755, st_size=0, ...}) = 0
stat("/sys/fs/cgroup/cpuset", {st_mode=S_IFDIR|0755, st_size=0, ...}) = 0
stat("/sys/fs/cgroup/cpu,cpuacct", {st_mode=S_IFDIR|0755, st_size=0, ...}) = 0
stat("/sys/fs/cgroup/perf_event", {st_mode=S_IFDIR|0755, st_size=0, ...}) = 0
stat("/sys/kernel/config", {st_mode=S_IFDIR|0755, st_size=0, ...}) = 0
stat("/", {st_mode=S_IFDIR|0555, st_size=4096, ...}) = 0
stat("/proc/sys/fs/binfmt_misc",    #这里卡住了

解决/proc/sys/fs/binfmt_misc被卡住问题(以下方法任选其一)

1. 执行systemctl restart proc-sys-fs-binfmt_misc.automount;

2. 升级到最新 systemd-219-57 版本;

3. 按照红帽知识库的步骤对 proc-sys-fs-binfmt_misc.automount 进行 mask 操作, 只进行静态的 mount 操作;

Leave a Comment

Prometheus中rate和irate的区别

rate()

rate(v range-vector) calculates the per-second average rate of increase of the time series in the range vector.

rate()函数计算某个时间序列范围内的每秒平均增长率。

Breaks in monotonicity (such as counter resets due to target restarts) are automatically adjusted for.

自适应单调性中断(比如target重启导致的计数器重置).

Also, the calculation extrapolates to the ends of the time range, allowing for missed scrapes or imperfect alignment of scrape cycles with the range’s time period.

计算结果是推算到每个时间范围的最后而得出, 允许漏抓和抓取周期与时间范围的不完美结合.

The following example expression returns the per-second rate of HTTP requests as measured over the last 5 minutes, per time series in the range vector:

以下示例返回最后五分钟HTTP请求每秒增长率

rate(http_requests_total{job="api-server"}[5m])

rate should only be used with counters. It is best suited for alerting, and for graphing of slow-moving counters.

rate应该只和计数器一起使用。最适合告警和缓慢计数器的绘图。

Leave a Comment