lnmp一键安装包源码分析

通过分析这个lnmp一键安装包的shell脚本,可以了解下shell语言如何有条理的组织大量的代码

项目地址:https://github.com/oneinstack/lnmp

shell脚本在执行运维和自动化任务时经常用到,在解决服务器问题时非常有用

目录结构

目录 作用
config nginx服务器里的预定义配置文件
include 执行实际安装任务的函数定义
src 下载的源码存放的目录
tools 执行数据库备份等任务的小工具合集
根目录 入口文件存放目录、安装入口文件install.sh、卸载入口文件uninstall.sh等

初始化过程分析

export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
clear
printf "
#######################################################################
#       OneinStack for CentOS/RedHat 6+ Debian 8+ and Ubuntu 14+      #
#       For more information please visit https://oneinstack.com      #
#######################################################################
"
# Check if user is root
[ $(id -u) != "0" ] && { echo "${CFAILURE}Error: You must be root to run this script${CEND}"; exit 1; }

oneinstack_dir=$(dirname "`readlink -f $0`")
pushd ${oneinstack_dir} > /dev/null
. ./versions.txt
. ./options.conf
. ./include/color.sh
. ./include/check_os.sh
. ./include/check_dir.sh
. ./include/download.sh
. ./include/get_char.sh
Read more   2019/8/1 posted in  OPS

gogs,jenkins配置通过openldap用户系统进行登录

Read more   2019/7/28 posted in  CI/CD OPS

PHP微服务在k8s上落地实战

来自公司内部php的k8s落地实战总结分享,php的k8s微服务落地可以参考下面这个博客

k8s和docker关系图

关键点如下:
docker: 虚拟机软件
k8s: 管理docker里的这个虚拟机

Read more   2019/6/30 posted in  OPS CI/CD

使用资源配置工具terraform在aws上构建基础架构

Read more   2019/5/25 posted in  AWS OPS

使用prometheus + grafana + pushgateway搭建监控可视化系统

Read more   2019/5/21 posted in  监控 OPS

三分钟上手ssh命令批量执行工具ansible

ansible作为目前批量linux机器远控工具的老大、其易用性、管理成本低等远远超过了同类的Fabric等工具。

准备测试机器,这里就拿已有的kubernetes的机器做测试,机器和ip分配参考:java微服务 k8s生产环境搭建, 注意下面案例中的k8s机器的ip段已经改成 192.168.10,机器列表如下

机器 ip
K8s-ha-deploy 192.168.10.140
K8s-ha-master1 192.168.10.130
K8s-ha-master2 192.168.10.131
K8s-ha-master3 192.168.10.132
K8s-ha-node1 192.168.10.135
K8s-ha-node2 192.168.10.136
K8s-ha-node3 192.168.10.137

安装ansible

执行下面的命令在部署机上安装ansible

pip install pip --upgrade -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
pip install --no-cache-dir ansible -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

也可以使用docker一键启动

alias ansible="docker run --rm --name ansible -v ${HOME}/.ssh/config:/home/ansible/.ssh/config:ro -v ${HOME}/.ssh/known_hosts:/home/ansible/.ssh/known_hosts:ro -it williamyeh/ansible:alpine3 ansible"
ansible --version
Read more   2019/5/16 posted in  三分钟系列 OPS

k8s分布式存储,使用rancher将jenkins workspace部署在nfs上

安装nfs服务器

systemctl stop rpcbind.socket
systemctl stop rpcbind
systemctl disable rpcbind.socket 
systemctl disable rpcbind

mkdir -p ~/nfs/files
chmod -R 777 ~/nfs/files
echo "/files *(rw,sync,no_subtree_check,fsid=0,no_root_squash)" > ~/nfs/exports.txt

docker run --name nfs -d --rm\
  -v ~/nfs/files:/files\
  -v ~/nfs/exports.txt:/etc/exports:ro\
  --cap-add SYS_ADMIN\
  -p 2049:2049   -p 2049:2049/udp   \
  -p 111:111     -p 111:111/udp     \
  -p 32765:32765 -p 32765:32765/udp \
  -p 32767:32767 -p 32767:32767/udp \
  erichough/nfs-server

使用showmount命令查看效果

Read more   2019/4/15 posted in  OPS

rancher

30531939036831699

docker run -d --name=rancher --restart=unless-stopped \
  -p 8080:80 -p 8443:443 \
  -v ~/rancher:/var/lib/rancher
  rancher/rancher:latest
  
docker run -d --name=rancher --restart=unless-stopped \
  -p 8080:80 -p 8443:443 \
  rancher/rancher:latest
  
docker run -d --name=rancher --restart=unless-stopped \
  -p 8080:80 -p 8443:443 \
  -v ~/rancher:/var/lib/rancher \
  rancher/rancher:stable
  
docker run -d --name=rancher --restart=unless-stopped \
  -p 8880:80 -p 8843:443 \
  -v ~/rancher:/var/lib/rancher \
  rancher/rancher:stable
  
https://172.16.123.1:8843/v3/import/v2d7znsxv8vvjzfl6gcwkglvkvnm759f98zt227h9plzm9vdwlh56g.yaml

image-20181127123429327-3293269

curl --insecure -sfL https://172.16.123.1:8443/v3/import/kpb77vt4zl64bdxgz4jpxt2s2x8h94ng2tdlfqcmntv762vc4bf987.yaml | kubectl apply -f -

image-20181127125127117-3294287

curl -X "POST" "http://127.0.0.1:8080/order/get-list" \
     -H 'Content-Type: application/x-www-form-urlencoded' \
     --data-urlencode "user_id=9" \
     --data-urlencode "status=1,2,3,4,5,6" \
     --data-urlencode "order_type=1"
2018/5/20 posted in  OPS

app单点部署流程

部署流程

备份chupinxiu.com.conf配置文件

在web1上创建负载切换的lua文件:/opt/openresty-1.13.6.1/lua/php-fpm-upstream.lua

if ngx.var.project == "apipro" then
    return ngx.exec("@single")
else
    return ngx.exec("@mutiple")
end

编辑nginx配置文件: /opt/openresty-1.13.6.1/nginx/conf/vhost/chupinxiu.com.conf

配置两个php的upstream

upstream php-fpm-backend-mutiple {
    server unix:/var/run/php-fpm.sock weight=5; # web1
    server 10.135.216.41:9000 weight=5; # web2
}
upstream php-fpm-backend-single {
    server unix:/var/run/php-fpm.sock; # web1
}

为两个upstream配置两个location

    location @mutiple {
        root $dest;
        proxy_set_header Host $host;
        fastcgi_pass   php-fpm-backend-mutiple;
        fastcgi_index  index.php;
        include        fastcgi.conf;
    }

    location @single {
        root $dest;
        proxy_set_header Host $host;
        fastcgi_pass   php-fpm-backend-single;
        fastcgi_index  index.php;
        include        fastcgi.conf;
    }

normal location中使用php-fpm-upstream.lua选择upstream

    location @normal {
        content_by_lua_file    /opt/openresty-1.13.6.1/lua/php-fpm-upstream.lua;
    }

重启nginx

回滚流程

删除chupinxiu.com.conf

将chupinxiu.com.conf配置文件恢复

删除 /opt/openresty-1.13.6.1/lua/php-fpm-upstream.lua;

重启nginx

2018/5/20 posted in  OPS

数据库切换nginx部署流程

部署流程

将维护页面上传到/var/www/maintain目录,并命名为index.html,设置权限为www-data:deploy用户

备份chupinxiu.com.conf配置文件

修改chupinxiu.com.conf的location /代码段为

    location / {
        index  index.php index.html index.htm;
        try_files /index.html =404;

        # kill cache
        add_header Last-Modified $date_gmt;
        add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
        if_modified_since off;
        expires off;
        etag off;
    }

备份/opt/openresty-1.13.6.1/lua/grayroot.lua

在web1上修改设置root的lua文件:/opt/openresty-1.13.6.1/lua/grayroot.lua

local real_path = "/var/www/maintain"
return real_path

重启nginx

恢复流程

删除/opt/openresty-1.13.6.1/lua/grayroot.lua文件并从备份文件恢复

删除chupinxiu.com.conf并从备份文件恢复

重启nginx

2018/5/20 posted in  OPS

灰度发布

CREATE TABLE `gray` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `project` varchar(45) NOT NULL,
  `shopId` text,
  `status` tinyint(1) unsigned NOT NULL DEFAULT '1',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4;
INSERT INTO `cpx_prod`.`gray` (`id`, `project`, `shopId`, `status`, `created_at`, `updated_at`) VALUES ('2', 'kufang', '1804', '1', '2018-05-28 17:04:25', '2018-05-28 17:04:25');

灰度开发方案

  1. 设计灰度表结构和存储位置
  2. 设计redis键,选择灰度发布使用的redis服务器
  3. 在ops平台上开发店铺灰度灰度状态管理页面
  4. 使用ops现有的指定发布功能发布灰度代码
  5. nginx lua根据店铺的灰度状态走不同的项目路径

灰度缓存更新

  1. ops灰度系统设置了店铺灰度后更新灰度缓存
  2. lua检测到redis里没有缓存key,从mysql中读取店铺灰度数据,缓存到redis中,redis使用一个key保存所有灰度店铺的id,使用json序列化

灰度代码发布

  1. 灰度代码发布复用ops上的指定发布
Read more   2018/5/20 posted in  OPS LUA OpenResty

查看网卡流量

sar -n DEV 1

比较重要的字段如下

rxpck/s:每秒钟接收的数据包
txpck/s:每秒钟发送的数据包
rxbyt/s:每秒钟接收的字节数
txbyt/s:每秒钟发送的字节数
rxcmp/s:每秒钟接收的压缩数据包
txcmp/s:每秒钟发送的压缩数据包
rxmcst/s:每秒钟接收的多播数据包
2018/5/20 posted in  OPS