Category: Linux

  • ss命令的使用

    centos6 下安装 iproute 包

    yum install -y iproute
    

    查看 6379 端口

    ss -nlp | grep redis

    查看连接的各种状态

    ss -nat | awk '{++s[$1]} END {for(k in s) print k,s[k]}'

    查看所有状态

    ss state all

    http://sharadchhetri.com/2014/09/27/ss-command-alternate-netstat/

  • shell json_reformat 工具

    在 shell 下使用 curl 与 api 交互,如果是 json 输出,为了更可读。可以安装

    CentOS

    yum install -y yajl

    OS X

    brew install yajl
  • vagrant使用笔记

    安装 vagrant

    https://www.vagrantup.com/downloads.html
    

    如果网络不好首先下载 box ,比如 centos6.box centos7.box

    注册一个 box,经常用的可以添加为 base,做为 default box

    vagrant box add base ~/osx-dmg/centos7.box
    vagrant box add centos6 ~/osx-dmg/centos6.box
    vagrant box add centos7 ~/osx-dmg/centos7.box
    

    查看已经导入的 box

    vagrant box list
    #或者
    ls -l ~/.vagrant.d/boxes
    

    如果网络条件好,添加官方的 centos/7 (注意写法)

    $ vagrant box add centos/7
    ==> box: Loading metadata for box 'centos/7'
        box: URL: https://vagrantcloud.com/centos/7
    This box can work with multiple providers! The providers that it
    can work with are listed below. Please review the list and choose
    the provider you will be working with.
    
    1) hyperv
    2) libvirt
    3) virtualbox
    4) vmware_desktop
    
    Enter your choice: 2
    

    工作目录为 ~/test,进入目录,创建一台 centos7 的机器,--minimal 生成的文件没有注释

    vagrant init centos/7 --minimal
    

    启动

    vagrant up

    连接 vagrant

    vagrant ssh
    

    执行 id 命令

    vagrant ssh -c 'id'

    查看当前 vagrant 管理的机器,最前面为机器 id

                                                                                    
    vagrant global-status
    

    修剪 vagrant 已经坏掉的机器

                                                       
    vagrant global-status --prune
    

    如果有不需要的 box

                                                                    
    vagrant destroy id
    

    准备多台虚拟机环境

      config.vm.define "web" do |web|                                               
        web.vm.box = "centos6"                                                      
      end                                                                           
                                                                                    
      config.vm.define "db" do |db|                                                 
        db.vm.box = "centos6"                                                       
      end
    

    查看机器的状态

    vagrant status

    查看 ssh 信息

    vagrant ssh-config

    关闭 vm

    vagrant halt

    不需要的 box 删除

    vagrant box remove centos/7

    vagrant 官方 centos7 box:

    https://vagrantcloud.com/centos/7/

    centos 提供的 box:

    http://cloud.centos.org/centos/7/vagrant/x86_64/images/

    windows 10 将 virtualbox 换为 hyperv
    上面获取 box 步骤时选 hyperv, 将原来 virtualbox 的相关 box 删除

    vagrant.exe box  remove centos/7 --provider virtualbox --all

    切到目录下,删除相关的 vm
    vagrant.exe destroy
    启动

    vagrant up --provider hyperv
  • ssh config 针对某些机器密码登陆

    一些机器不要公钥登陆,在每次连的时候提示密码

    Host kong
    Hostname 192.168.1.200
    User jpuyy                                                              
    Port 22                                                                         
    PreferredAuthentications password
    

    服务端,例如 sftp 可以允许密码登录的话

    Match user jpuyy address 192.168.1.0/24
        PasswordAuthentication yes
        # also since we want only sftp
        ForceCommand internal-sftp
    
    
  • rediscluster缩容和扩容

    rediscluster 配置的过量,浪费了资源。不够时又加加资源

    查看当前 rediscluster 的主从关系,并记下来 hash 值。

    redis-cli -c cluster nodes

    现在先进行扩容,192.168.11.3:6379 是当前 cluster 的一个 master 节点。

    添加 192.168.22.3:6379,从节点 192.168.22.4:6379

    查看 cluster 状态

    redis-trib.rb info 192.168.11.3:6379

    添加,默认为 master

    redis-trib.rb add-node 192.168.22.3:6379 192.168.11.3:6379

    添加 –slave, 主动找没有 slave 的 master 节点

    redis-trib.rb add-node --slave 192.168.22.4:6379 192.168.11.3:6379

    查看 cluster nodes

    redis-cli -c cluster nodes

    接下来确定哪些 master 的 slots 迁到新加的实例,找到新加的 master hash 值为 e0d88661607192a4ffac1b74412c6cd6d0932067

    从已有 slot 里分配 100 个 slots 到 e0d88661607192a4ffac1b74412c6cd6d0932067

    redis-trib.rb reshard --from 32f38c9e5e42f6a66e67abdd540cce72e4bf399c --to e0d88661607192a4ffac1b74412c6cd6d0932067 --slots 100 --yes 192.168.11.3:6379

    如果遇到失败,进行 fix

    redis-trib.rb fix 192.168.11.3:6379

    反复几次,并查看可以达到迁移目的。

    总是 fix 特别烦,写个有限循环搞定。

    for i in $(seq 20); do redis-trib.rb reshard --from 32f38c9e5e42f6a66e67abdd540cce72e4bf399c --to e0d88661607192a4ffac1b74412c6cd6d0932067 --slots 100 --yes 192.168.11.3:6379 || redis-trib.rb fix 192.168.11.3:6379; done
    

    缩容即是把里面的 slots 移到其他实例里,然后删除 node 对应的 hash

    redis-trib.rb del-node 192.168.11.3:6379 e0d88661607192a4ffac1b74412c6cd6d0932067

    查看当前实例是否还在提供服务

    redis-cli monitor

    当缩容或扩容完之后,如果 slots 的数量差的多的话, 可以对整个集群 rebalance

    redis-trib.rb rebalance 192.168.11.3:6379

    同样的如果中间中断,就 fix

    redis-trib.rb fix 192.168.11.3:6379

    到最后会提示

    *** No rebalancing needed! All nodes are within the 2.0% threshold.

    查看整个集群的情况

    #redis-cli -c cluster info
    
    cluster_state:ok
    cluster_slots_assigned:16384
    cluster_slots_ok:16384
    cluster_slots_pfail:0
    cluster_slots_fail:0
    cluster_known_nodes:12
    cluster_size:6
    cluster_current_epoch:46
    cluster_my_epoch:40
    cluster_stats_messages_sent:912305
    cluster_stats_messages_received:912227
    
    

    ok,到这里结束。

  • pidstat查看进程I/O读写

    查看一个进程的 io 读写

    每秒钟输出一次

    -d 参数:

    打印 I/O 信息,2.6.20及以后内核才支持。kB_rd/s 每秒读的 kB 数,kB_wr/s 每秒写的 kB 数,kB_ccwr/s 每秒取消写入磁盘的 kB 数。

    -l 参数:

    打印详细信息

    pidstat -dl 1 -p 29866