supervisord 与 worker 的应用

Linux Supervisor supervisord 与 worker 的应用 首先,目前使用 nohup 的方式启动,比较难托管,日志也是个问题,好处是方便灵活,同时只能通过 node_exporter 的 text-collector 或者 process_exporter 的方式进行进程监控。 换个思路,使用 supervisord 对 worker 进程进行托管,所有的环境变量都可以配置在 conf 文件中,版本变更,只需要更新二进制文件即可,启动过程直接 stop 或者 restart,同时可以添加预检测脚本。node_exporter 有一个 flag 即可检测 supervisourd 服务。 以 127.0.0.1 作为测试机器,已经测通。可以实现任务启动,日志重定输出,自动重启,进程监控。 首先给出 worker.conf 的配置。 # /etc/supervisor/conf.d/worker.conf [program:lotus-worker] command=/opt/cache/lotus-worker run --address=127.0.0.1:6969 directory=/opt/cache autorstart=true autorestart=true user=Development stopsignal=QUIT redirect_stderr = true stdout_logfile_backups = 10 stdout_capture_maxbytes = 100MB stdout_logfile=/opt/cache/log/%(program_name)s_log.log stderr_logfile_backups = 10 stderr_capture_maxbytes = 100MB stderr_logfile=/opt/cache/log/%(program_name)s_error.log environment = PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games", NLS_LANG=".AL32UTF8", IP=127.0.0.1, LOTUS_PATH=/opt/cache/repo, LOTUS_STORAGE_PATH=/opt/cache/lotusstorage, WORKER_PATH=/opt/cache/worker-path, FIL_PROOFS_PARAMETER_CACHE=/opt/lotus/v28-proof/, TMPDIR=/opt/cache, FIL_PROOFS_MAXIMIZE_CACHING=1, FIL_PROOFS_USE_GPU_COLUMN_BUILDER=1, FIL_PROOFS_PARENT_CACHE=/opt/cache/filecoin-parent/, MIN_AVAILABLE_MEMORY=640, SDR_WAIT_TIME=90, IPFS_GATEWAY="https://proof-parameters.s3.cn-south-1.jdcloud-oss.com/ipfs/", FIL_PROOFS_USE_GPU_TREE_BUILDER=1, RUST_LOG=Debug, STORAGE_API_INFO=xxxyy:/ip4/10.0.0.1/tcp/10002/http 这里面会提到IP指定,所以需要依赖于 ansible 做模版,使用 ansible_facts[‘default_ipv4’][‘address’] 作为key。 ...

systemd service 配置 ulimit 限制

Linux systemd service 配置 ulimit 限制 在 bash 中,有个 ulimit 命令,提供了对 shell 及该 shell 启动的进程的可用资源控制。主要包括打开文件描述符数量、用户的最大进程数量、coredump 文件的大小等。 在 CentOS 5/6 等版本中,资源限制的配置可以在 /etc/security/limits.conf 设置,针对 root/user 等各个用户或者 * 代表所有用户来设置。 当然,/etc/security/limits.d/ 中可以配置,系统是先加载 limits.conf 然后按照英文字母顺序加载 limits.d 目录下的配置文件,后加载配置覆盖之前的配置。 一个配置示例如下: soft nofile 100000 * hard nofile 100000 * soft nproc 100000 * hard nproc 100000 * soft core 100000 * hard core 100000 不过,在 CentOS 7 or RHEL 7 的系统中,使用 Systemd 替代了之前的 SysV,因此 /etc/security/limits.conf 文件的配置作用域缩小了一些。limits.conf 这里的配置,只适用于通过 PAM 认证登录用户的资源限制,它对 systemd 的 service 的资源限制不生效。登录用户的限制,与上面讲的一样,通过 /etc/security/limits.conf 和 limits.d 来配置即可。 对于systemd service的资源限制,如何配置呢? ...

systemd 的使用

Linux systemd 的使用 systemd 是一个庞大的系统,在这篇里,专注于 systemd 在 service,target 以及日志系统的使用。 service service 的书写 配置文件放在 /usr/lib/systemd/system/ 或者 /lib/systemd/system/ 中,以 .service 结尾。Systemd 默认从目录 /etc/systemd/system/ 读取配置文件,但是,里面存放的大部分文件都是符号链接。 # 配置文件大概格式 [Unit] Description=ATD daemon [Service] Type=forking ExecStart=/usr/bin/atd [Install] WantedBy=multi-user.target 配置文件是由 ini 格式书写的,按照几个大的区块来写,Unit 是该模块基础信息,Service 是 service 型 Unit 专属,Install 附加信息。 [Unit]区块通常是配置文件的第一个区块,用来定义 Unit 的元数据,以及配置与其他 Unit 的关系。它的主要字段如下。 - Description:简短描述 - Documentation:文档地址 - Requires:当前 Unit 依赖的其他 Unit,如果它们没有运行,当前 Unit 会启动失败 - Wants:与当前 Unit 配合的其他 Unit,如果它们没有运行,当前 Unit 不会启动失败 - BindsTo:与Requires类似,它指定的 Unit 如果退出,会导致当前 Unit 停止运行 - Before:如果该字段指定的 Unit 也要启动,那么必须在当前 Unit 之后启动 - After:如果该字段指定的 Unit 也要启动,那么必须在当前 Unit 之前启动 - Conflicts:这里指定的 Unit 不能与当前 Unit 同时运行 - Condition...:当前 Unit 运行必须满足的条件,否则不会运行 - Assert...:当前 Unit 运行必须满足的条件,否则会报启动失败 [Install]通常是配置文件的最后一个区块,用来定义如何启动,以及是否开机启动。它的主要字段如下。 - WantedBy:它的值是一个或多个 Target,当前 Unit 激活时(enable)符号链接会放入/etc/systemd/system目录下面以 Target 名 + .wants后缀构成的子目录中。一般挂载为 multi-user.target。 - RequiredBy:它的值是一个或多个 Target,当前 Unit 激活时,符号链接会放入/etc/systemd/system目录下面以 Target 名 + .required后缀构成的子目录中 - Alias:当前 Unit 可用于启动的别名 - Also:当前 Unit 激活(enable)时,会被同时激活的其他 Unit [Service]区块用来 Service 的配置,只有 Service 类型的 Unit 才有这个区块。它的主要字段如下。 - Type:定义启动时的进程行为。它有以下几种值。 - Type=simple:默认值,执行ExecStart指定的命令,启动主进程 - Type=forking:以 fork 方式从父进程创建子进程,创建后父进程会立即退出 - Type=oneshot:一次性进程,Systemd 会等当前服务退出,再继续往下执行 - Type=dbus:当前服务通过D-Bus启动 - Type=notify:当前服务启动完毕,会通知Systemd,再继续往下执行 - Type=idle:若有其他任务执行完毕,当前服务才会运行 - ExecStart:启动当前服务的命令 - ExecStartPre:启动当前服务之前执行的命令 - ExecStartPost:启动当前服务之后执行的命令 - ExecReload:重启当前服务时执行的命令 - ExecStop:停止当前服务时执行的命令 - ExecStopPost:停止当其服务之后执行的命令 - RestartSec:自动重启当前服务间隔的秒数 - Restart:定义何种情况 Systemd 会自动重启当前服务,可能的值包括always(总是重启)、on-success、on-failure、on-abnormal、on-abort、on-watchdog - TimeoutSec:定义 Systemd 停止当前服务之前等待的秒数 - Environment:指定环境变量 service 的管理 # 查看系统所有安装的服务项 systemctl list-unit-files --type=service # 查看系统所有运行的服务项 systemctl list-units --type=service # 查看系统所有开机自启动的服务项 systemctl list-unit-files --type=service | grep enabled # 查看指定服务项状态 systemctl status <服务项名称> # 查看服务项的依赖关系 systemctl list-dependencies <服务项名称> # 查看出错的服务 systemctl list-units --type=service --state=failed # 清除服务项的错误状态 systemctl reset-failed <服务项名称> # 查看系统启动耗时 systemd-analyze # 查看各项服务启动耗时 systemd-analyze blame | grep .service # 启动服务 systemctl start <服务项名称> # 停止服务 systemctl stop <服务项名称> # 重启服务 systemctl restart <服务项名称> # 重新读取配置文件 systemctl reload <服务项名称> # 使服务开机自启动 ## 需要配置 install systemctl enable <服务项名称> # 使服务不要开机自启动 systemctl disable <服务项名称> # 禁用服务 systemctl mask <服务项名称> # 启用服务 systemctl unmask <服务项名称> # 重新读取所有服务项 systemctl daemon-reload # 服务状态 enabled:已建立启动链接 disabled:没建立启动链接 static:该配置文件没有[Install]部分(无法执行),只能作为其他配置文件的依赖 masked:该配置文件被禁止建立启动链接 target Target 就是一个 Unit 组,包含许多相关的 Unit 。启动某个 Target 的时候,Systemd 就会启动里面所有的 Unit。从这个意义上说,Target 这个概念类似于"状态点",启动某个 Target 就好比启动到某种状态。 ...

timeit模块的使用

Python Python Module timeit模块的使用 timeit 主要是为了测试代码运行速度。 它主要有两个方法,即 timeit 和 repeat。 测试一段代码的运行时间,在 python 里面有个很简单的方法,就是使用 timeit 模块,使用起来超级方便 下面简单介绍一个 timeit 模块中的函数。 主要就是这两个函数: timeit(stmt='pass', setup='pass', timer=<defaulttimer>, number=1000000) 返回: 返回执行stmt这段代码number遍所用的时间,单位为秒,float型 参数: stmt:要执行的那段代码 setup:执行代码的准备工作,不计入时间,一般是import之类的 timer:这个在win32下是time.clock(),linux下是time.time(),默认的,不用管 number:要执行stmt多少遍 repeat(stmt='pass', setup='pass', timer=<defaulttimer>, repeat=3, number=1000000) 这个函数比 timeit 函数多了一个 repeat 参数而已,表示重复执行 timeit 这个过程多少遍,返回一个列表,表示执行每遍的时间。 当然,为了方便,python 还用了一个 Timer 类,Timer 类里面的函数跟上面介绍的两个函数是一样一样的 class timeit.Timer(stmt='pass', setup='pass',timer=<timer function>) Timer.timeit(number=1000000) Timer.repeat(repeat=3,number=1000000) 看懂了吧,一样的,使用的时候哪种方便用哪种。 就相当于 timeit(stmt='pass', setup='pass', timer=<defaulttimer>, number=1000000) = Timer(stmt='pass', setup='pass', timer=<timerfunction>).timeit(number=1000000) repeat(stmt='pass', setup='pass', timer=<defaulttimer>, repeat=3, number=1000000) = Timer(stmt='pass', setup='pass', timer=<timerfunction>).repeat(repeat=3, number=1000000)

Ubuntu 18.04升级内核以及内存性能研究

Linux Ubuntu Ubuntu 18.04升级内核以及内存性能研究 自从用上了 AMD 7002 CPU之后,服务器上出现了 EDAC amd64: Error: F0 not found, device 0x1460 (broken BIOS?)。 通过 suse 的论坛支持,确定这是因为 “AMD ROMA” 这一代太高级了,Linux Kernel 没跟上,所以会出现这个问题。Ubuntu 18.04 的内核版本是 4.15.0-55-generic;Ubuntu 20.04 的内核版本是 5.4.0-33-generic;官网提供的稳定版内核版本是 5.7.2-050702-generic。 那么就要评估要不要升级内核了?是直接升级操作系统还是仅升级内核? 要不要升级内核? 这需要判断这个错误对 性能 和 稳定性 的影响程度。 先整理出两台机器: mtest01:7542, 64G 2933 DDR4 * 8,内核 4.15.0-55-generic mtest02: 7542, 64G 2933 DDR4 * 8,内核 5.7.2-050702-generic 重启后,通过 dmesg 检测,发现 mtest01 出现 EDAC amd64: Error: F0 not found, device 0x1460 (broken BIOS?) 问题,而 mtest02 没出现这个问题。 ...

Ubuntu 18.4 通用配置脚本

Linux Ubuntu Ubuntu 18.4 通用配置脚本 配置 ip 先决条件:网口名为 eno1 且为联通状态,若不是,请手动修改。 #! /bin/bash # config_ip.sh # sh config_ip.sh eno1 192.168.1.xxx 192.168.1.1 mv /etc/netplan /etc/netplan.bk mkdir -p /etc/netplan/ touch /etc/netplan/01-netcfg.yaml cat > /etc/netplan/01-netcfg.yaml<<EOF # This file describes the network interfaces available on your system # For more information, see netplan(5). network: version: 2 renderer: networkd ethernets: $1: addresses: [$2/24] gateway4: $3 nameservers: addresses: - "$3" EOF cat /etc/netplan/01-netcfg.yaml echo 'Finish!' netplan apply if [ $? -eq 0] then echo 'Apply Success!' else echo 'Apply Failure!' fi 配置 DNS #! /bin/bash # config_dns.sh # sh config_dns.sh 192.168.1.x mv /etc/systemd/resolved.conf /etc/systemd/resolved.conf.bk cat > /etc/systemd/resolved.conf <<EOF [Resolve] DNS=$1 #FallbackDNS= #Domains= #LLMNR=no #MulticastDNS=no #DNSSEC=no #Cache=yes #DNSStubListener=yes EOF 配置源 #! /bin/bash # config_source.sh # sh config_source.sh mv /etc/apt/sources.list /etc/apt/sources.list.bk cat > /etc/apt/sources.list <<EOF # tsinghua deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse deb https://packages.grafana.com/oss/deb stable main deb-src https://packages.grafana.com/oss/deb stable main deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse # aliyun deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse # ubuntu deb http://archive.ubuntu.com/ubuntu/ trusty main restricted universe multiverse deb http://archive.ubuntu.com/ubuntu/ trusty-security main restricted universe multiverse deb http://archive.ubuntu.com/ubuntu/ trusty-updates main restricted universe multiverse deb http://archive.ubuntu.com/ubuntu/ trusty-proposed main restricted universe multiverse deb http://archive.ubuntu.com/ubuntu/ trusty-backports main restricted universe multiverse EOF apt-get update 配置集群免密登陆 生成默认密钥对 ssh-keygen -t rsa -C "default_key@default.namespace" ls ~/.ssh/ 获取认证文件 ssh-copy-id -i ~/.ssh/id_rsa xxx@xxx 通过生成的密钥通过 ssh-copy-id 指定一台机器,那么那台机器的 ~/.ssh/authorized_keys 就会更新,录入一条新的数据。 ...

Ubuntu 使用光纤设备遇到的问题

Linux Ubuntu Network Ubuntu 使用光纤设备遇到的问题 已有的设备: 一台全新的带有光口的交换机 普通服务器(无光模块) 不知生产日期的 8Gb/s 的光网卡 不知生产日期的 8Gb/s 的光模块 几根 LC-LC 光纤线 该接的线都接了,连上机器发现根本没识别网卡,光模块上的三个灯一直闪烁,我寻思👴没用过光纤,这到底该怎么排查呢。根据这篇文档进行操作。 识别 HBA 卡 # lspci | grep -i ql <== 由于lspci显示连接的pci设备过多,通过grep查找 82:01.0 Ethernet controller: QLogic Corp. ISP4032-based Ethernet IPv6 NIC 82:01.1 Network controller: QLogic Corp. ISP4032-based iSCSI TOE IPv6 HBA 查看是否有驱动 # lsmod | grep -i ql qla2xxx 260462 0 iscsi_boot_sysfs 16000 1 qla2xxx libiscsi 57278 4 qla2xxx,libiscsi_tcp,iscsi_tcp,ib_iser scsi_transport_iscsi 89279 5 qla2xxx,iscsi_tcp,ib_iser,libiscsi 驱动可能有点老,但毕竟也加载了。 查看使用信息 祭出 dmesg。 $ dmesg | grep -i ql xxxxxx xxxxxx xxxxxxx cable unplugged 电缆不通电?继续查,都是说线没插好或者驱动有问题。重新安装驱动再重启网络服务,无效。 ...

Ubuntu 修改时区和更新时间

Linux Ubuntu Ubuntu 修改时区和更新时间 修改时区 先查看当前系统时区。 root@ubuntu:/# date -R Wed, 06 May 2020 04:49:31 -0400 结果时区是:-0400 我需要的是东八区,这儿显示不是,所以需要设置一个时区。 # 运行tzselect root@ubuntu:/# tzselect # 选择亚洲 Asia,确认之后选择中国(China),最后选择北京(Beijing) Please identify a location so that time zone rules can be set correctly. Please select a continent, ocean, "coord", or "TZ". 1) Africa 2) Americas 3) Antarctica 4) Asia 5) Atlantic Ocean 6) Australia 7) Europe 8) Indian Ocean 9) Pacific Ocean 10) coord - I want to use geographical coordinates. 11) TZ - I want to specify the time zone using the Posix TZ format. #? 4 Please select a country whose clocks agree with yours. 1) Afghanistan 18) Israel 35) Palestine 2) Armenia 19) Japan 36) Philippines 3) Azerbaijan 20) Jordan 37) Qatar 4) Bahrain 21) Kazakhstan 38) Russia 5) Bangladesh 22) Korea (North) 39) Saudi Arabia 6) Bhutan 23) Korea (South) 40) Singapore 7) Brunei 24) Kuwait 41) Sri Lanka 8) Cambodia 25) Kyrgyzstan 42) Syria 9) China 26) Laos 43) Taiwan 10) Cyprus 27) Lebanon 44) Tajikistan 11) East Timor 28) Macau 45) Thailand 12) Georgia 29) Malaysia 46) Turkmenistan 13) Hong Kong 30) Mongolia 47) United Arab Emirates 14) India 31) Myanmar (Burma) 48) Uzbekistan 15) Indonesia 32) Nepal 49) Vietnam 16) Iran 33) Oman 50) Yemen 17) Iraq 34) Pakistan #? 9 Please select one of the following time zone regions. 1) Beijing Time 2) Xinjiang Time #? 1 The following information has been given: China Beijing Time Therefore TZ='Asia/Shanghai' will be used. Selected time is now: Wed May 6 16:49:49 CST 2020. Universal Time is now: Wed May 6 08:49:49 UTC 2020. Is the above information OK? 1) Yes 2) No #? 1 You can make this change permanent for yourself by appending the line TZ='Asia/Shanghai'; export TZ to the file '.profile' in your home directory; then log out and log in again. Here is that TZ value again, this time on standard output so that you can use the /usr/bin/tzselect command in shell scripts: Asia/Shanghai 复制文件到 /etc 目录下。 ...

Ubuntu 安装 ftp 服务

Linux Ubuntu Network Ubuntu 安装 ftp 服务 这是一个很简单的需求,相关的博文也相当多,安装的过程非常简单,但是配置相关的说明有些分散。 主要几步: 可选:创建目录 安装服务 写配置文件 访问服务 最麻烦的在于 写配置文件,ftp 通过文本格式的配置文件实现了权限管理,用户管理,目录管理和网络管理。 安装服务 $ apt-get install vsftpd vsftpd 意思为 “very secure FTP daemon(非常安全的FTP进程)”,当然只有更安全没有最安全。 那它到底安全在哪里呢,主要体现在以下两点: 权限控制,vsftpd 以一般用户登录,用户权限相对较小,对于系统就越安全,对于用户需要的系统级指令大部分被整合到 vsftpd 中了,用户不需要申请更高权限就足以完成绝大部分 ftp 指令;此外对于 ftp 本身内部的读写控制,vsftpd 也足以通过配置文件控制了; 目录限制,vsftpd 通过 chroot 可以控制 ftp 登录用户所能看到的目录范围,即限定 ftp 用户看到的根目录为系统中某一个目录,如此一个 ftp 用户就除了看到自己的 ftp 根目录不能看到其他比如配置文件、系统更目录等,保护了系统。 写配置文件 从安全的角度来看,尽量不要启用匿名;同时尽量使用一个统一的目录,方便管理(这样的考虑是因为将 ftp 作为一个统一的文件传输管理系统,如果每个用户单独目录就很难提到共享这个概念)。 还有个 主动模式/被动模式 的区别,具体的区别可以参考 Ubuntu 14.04 配置vsftpd实现FTP服务器 - 通过FTP连接AWS 的第 2 章。我的需求是使用被动模式。 从上述所说,重要的配置如下: anonymous_enable=NO # 不允许匿名用户登录 local_enable=YES # 允许本地用户组的用户登录 write_enable=YES # 允许本地用户上传文件 local_root = /home/xxxx/ftp # 本地用户登入时,将被切换到定义的目录下,默认值为各用户的家目录,通过这个指定目录,该目录需要手动创建 #使用被动模式 pasv_enable=YES pasv_min_port=1024 pasv_max_port=1048 pasv_address=你的访问IP(服务器外网IP) 更多的配置信息可以参考文末的参考链接前两项。 ...

Ubuntu 搭建 Gitlab

Linux Ubuntu [[GitLab]] Ubuntu 搭建 Gitlab 准备工作 $ sudo apt-get install ruby vim curl openssh-server ca-certificates $ gem sources --add https://mirrors.tuna.tsinghua.edu.cn/rubygems/ --remove https://rubygems.org/ $ bundle config mirror.https://rubygems.org https://mirrors.tuna.tsinghua.edu.cn/rubygems 安装 Gitlab Gitlab 官网找到的下载链接都是 30 天适用的企业版本,其实把 gitlab-ee 改成 gitlab-ce 即可。 curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.deb.sh | sudo bash # GitLab: 13.0.1 (74623c80da9) FOSS # GitLab Shell: 13.2.0 # PostgreSQL: 11.7 sudo apt-get install gitlab-ce 后面就通过浏览器访问,配置自定义项目。 关于公钥密钥数据传输,这里掠过不谈。 备份数据 # 会自动在 /var/opt/gitlab/backups 目录下生成一个备份文件 gitlab-rake gitlab:backup:create # 修改备份地址 vi /etc/gitlab/gitlab.rb ... gitlab_rails['backup_path'] = "/var/opt/gitlab/backups" ... vi /etc/crontab # 添加定时任务,每天凌晨两点,执行 gitlab 备份 0 2 * * * root /opt/gitlab/bin/gitlab-rake gitlab:backup:create CRON=1 恢复数据 确认新老机器的gitlab服务版本一致。 ...