需求

在vue单页应用开发中,如果为了省事PHP代码和vue代码都放在同一个GIT仓库中
那么如何进行vue前端代码和php Api 后端代码组织呢?
下面提供一个组织方式

项目目录结构

frontend
public
.gitignore
.travis.yml
application
build.php
composer.json
extend
LICENSE.txt
phpunit.xml
README.md
runtime
tests
think
thinkphp
vendor

其中
frontend 为vue前端项目目录
public 为php的web目录,index.php入口文件在这里,nginx也配置的是public

vue 配置

文件 frontend/config/index.js

module.exports = {
  build: {
    env: require('./prod.env'),
    index: path.resolve(__dirname, '../../public/ui/index.html'),
    assetsRoot: path.resolve(__dirname, '../../public/ui'),
    assetsSubDirectory: 'static',
    assetsPublicPath: '/ui/',
    .........其他代码.......
  },

前台访问

api接口为
http://appweb.dev/v1/api

访问/ui/即可
http://appweb.dev/ui/

需要注意的问题

public/ui 目录为前端项目build后的文件,不适宜纳入git管理
线上发布的通过CI&CD工具解决
目前适用于PHP项目的CI&CD工具有
Jenkins
Walle 瓦力上线部署系统
Piplin 持续集成与部署系统

未完待续

img

服务注册发现(Service Discovery)

  • CoreDNS
    https://coredns.io
  • Consul
    https://www.hashicorp.com/products/consul
  • Etcd
    https://github.com/coreos/etcd
  • Zookeeper
    https://zookeeper.apache.org
  • Eureka
    https://github.com/Netflix/eureka

负载均衡(Load Balancer)

  • Nginx
  • Haproxy

网关(API Gateway)

  • Kong

熔断(Circuit breaker)

  • Hystrix

分布式跟踪(Distributed Tracing)

  • Opentracing
    http://opentracing.io
    Opentracing是一个调用链路追踪规范,它类似于Zipkin和Google Dapper。
    与Zipkin不同的是,它定义了协议,并提供了多种语言的客户端库,但是没有提供最终存储和展示的实现。 用户可以自定义对接到不同的后端兼容层上,只要其兼容于Opentracing协议即可。
  • Jaeger
    https://github.com/jaegertracing/jaeger

参考资料

OpenTracing官方标准-中文版 https://github.com/opentracing-contrib/opentracing-specification-zh
Opentracing http://dmdgeeker.com/goBook/docs/ch11/opentracing.html
Uber Jaeger安装与使用 http://dmdgeeker.com/post/uber_jaeger/

监控(Monitoring)

  • Prometheus
    https://prometheus.io/

参考资料

Grafana https://grafana.com 监控展示面板

配置

  • confd

扩展资料

其他资料

CNCF(Cloud Native Computing Foundation)于 2015 年 7 月成立,隶属于 Linux 基金会,初衷围绕“云原生”服务云计算,致力于维护和集成开源技术,支持编排容器化微服务架构应用。

Projects


还不了解 CNCF?关于 CNCF 的三问三答! http://blog.daocloud.io/cncf-3/
Go使用grpc+http打造高性能微服务 https://mp.weixin.qq.com/s?__biz=MjM5OTcxMzE0MQ==&mid=2653370431&idx=1&sn=59175120599a0974eb32364c26421c09&chksm=bce4d8258b935133c80658431cde2dd5cfecb2cc6bd450d305e8242dc37288b9c0a9956e8423&mpshare=1&scene=1&srcid=1122W2gv4zaFKq6DaTg8jA0C#rd
关于负载均衡和服务发现,Google的经验在这里 http://blog.shurenyun.com/untitled-95/
12-Factor 软件设计12要素中文版 https://12factor.net/zh_cn/
Spring Cloud for Microservices Compared to Kubernetes

一些有用的综合Docker用法

crontab计划任务里面执行 docker中的程序,并指定账户

*/1 * * * * docker exec –user=www php-5.6-fpm-alpine bash -c “cd /tmp && pwd && ls && php -v > phpversion.log”

运行一个docker 命名且挂载目录映射端口

docker run -d –name php-5.6-fpm-alpine –restart=always -p 127.0.0.1:9001:9000 -v /tmp:/tmp -v /data:/data registry.cn-shanghai.aliyuncs.com/wwek/php:5.6-fpm-alpine

Docker 镜像加速

  • Docker 中国官方镜像加速 https://www.docker-cn.com/registry-mirror
    您可以配置 Docker 守护进程默认使用 Docker 官方镜像加速。这样您可以默认通过官方镜像加速拉取镜像,而无需在每次拉取时指定 registry.docker-cn.com。
    您可以在 Docker 守护进程启动时传入 –registry-mirror 参数:
$ docker --registry-mirror=https://registry.docker-cn.com daemon

为了永久性保留更改,您可以修改 /etc/docker/daemon.json 文件并添加上 registry-mirrors 键值。

{
  "registry-mirrors": ["https://registry.docker-cn.com"]
}

修改保存后重启 Docker 以使配置生效。

  • 阿里云 镜像加速器
    登录阿里云控制台 > 容器镜像服务 > 镜像加速器 按照后台给出的操作文档进行设置

免费的Docker私有镜像仓库

  • 阿里云
    登录阿里云控制台 > 容器镜像服务
    先设置”命名空间”,再在”镜像列表”,”创建镜像仓库”
    阿里云提供 阿里云Code,GitHub Bitbucket 私有GitLab 本地仓库 的源代码仓库的集成

  • 腾讯云
    登录腾讯云控制台 > 容器服务 > 镜像仓库
    先在”我的镜像”设置”命名空间”,再在”我的镜像”,”新建”
    腾讯云提供 Github GitLab 的源代码仓库的集成

管理工具

  • scope
    https://github.com/weaveworks/scope
    Monitoring, visualisation & management for Docker & Kubernetes
    可视化监控管理 Docker

  • kitematic
    Docker 官方桌面管理工具

如何在一个Docker中运行多个程序进程?

我们都知道Docker容器的哲学是一个Docker容器只运行一个进程,但是有时候我们就是需要在一个Docker容器中运行多个进程
那么基本思路是在Dockerfile 的CMD 或者 ENTRYPOINT 运行一个”东西”,然后再让这个”东西”运行多个其他进程
简单说来是用Bash Shell脚本或者三方进程守护 (Monit,Skaware S6,Supervisor),其他没讲到的三方进程守护工具同理

Bash Shell脚本

入口文件运行一个Bash Shell 脚本, 然后在这个脚本内去拉起多个进程
注意最后要增加一个死循环不要让这个脚本退出,否则拉起的进程也退出了
run.sh

#!/bin/bash

# start 1
start1  > /var/log/start1.log 2>&1 &
# start 2
start2 > /var/log/start2.log 2>&1 &

# just keep this script running
while [[ true ]]; do
    sleep 1
done

在Dockerfile的入口中运行run.sh

ENTRYPOINT  ["run.sh"]

用Bash Shell 的方式,任意发行版的linux都支持,缺点是不能拉起crash的进程,也就是只能拉起运行不能”守护”
如果不关心进程crash问题那么可以用这种方式

三方容器进程初始化之-dumb-init

dumb-init官方
A minimal init system for Linux containers
一个最小化的Linux容器初始化系统
dumb-init是一个简单的进程监控器和init系统,设计为在最小容器环境(如Docker)中作为PID 1运行。它被部署为一个用C编写的小型静态链接二进制文件。
Dockerfile 参考

# Runs "/usr/bin/dumb-init -- /my/script --with --args"
ENTRYPOINT ["/usr/bin/dumb-init", "--"]

# or if you use --rewrite or other cli flags
# ENTRYPOINT ["dumb-init", "--rewrite", "2:3", "--"]

CMD ["/my/script", "--with", "--args"]

ServiceMesh的数据平面Envoy Proxy的容器镜像就是使用的dumb-init

三方容器进程初始化之-tini

tini官方
A tiny but valid init for containers
容器的一个小而有效的init

三方进程守护之-Monit

Monit和Supervisor还是有很大区别的,Supervisor管理的都是前台执行的进程,Monit既可以管理前台进程也可以管理后台进程,简单的说,在CentOS中使用service xxx start 启动的程序,使用Monit可以直接管理,这就解决了很多没有前台方式启动的程序不能用Supervisor来管理的问题。
Dockerfile 参考

ENTRYPOINT ["/usr/bin/monit","-I"]

三方进程守护之-Supervisor

大名鼎鼎的 Supervisor
如果有多种版本的linux要用那么可以用Supervisor做统一进程守护管理,网上资料一大堆
注意要以前台程序运行,配置文件中要有,如果是后台的方式docker会退出

[supervisord]
nodaemon=true

Dockerfile 参考

ENTRYPOINT ["supervisord", "-c", "/etc/supervisor/conf.d/youconf.conf"]

三方进程守护之-Skaware S6

Supervisor是常见的进程守护程序,不过程序文件太大,想要容器镜像尽量小,在特别是用Alpine作为基础镜像的时候推荐使用Skaware S6
参考这个微服务基础镜像 https://github.com/nicholasjackson/microservice-basebox 他就是用 Skaware 作为进程守护程序运行多个进程的
如果基础容器镜像是本身就是Alpine,那就再合适不过了
Dockerfile 参考

# skaware s6 daemon runner
RUN mkdir /s6 \
 && wget --no-check-certificate https://github.com/just-containers/skaware/releases/download/v1.21.2/s6-2.6.1.1-linux-amd64-bin.tar.gz \
 && tar -xvzf s6-2.6.1.1-linux-amd64-bin.tar.gz --directory /s6 --strip-components=1 \
 && mv /s6/bin/* /usr/bin \
 && rm -rf s6-2.6.1.1-linux-amd64-bin.tar.gz \
 && rm -rf /s6 \
 && cd /usr/bin/ \
 && chmod -R 755 s6-accessrules-cdb-from-fs s6-accessrules-fs-from-cdb \
    s6-applyuidgid s6-cleanfifodir s6-connlimit s6-envdir s6-envuidgid \
    s6-fdholder-daemon s6-fdholder-delete s6-fdholder-deletec s6-fdholder-getdump \
    s6-fdholder-getdumpc s6-fdholder-list s6-fdholder-listc s6-fdholder-retrieve \
    s6-fdholder-retrievec s6-fdholder-setdump s6-fdholder-setdumpc s6-fdholder-store \
    s6-fdholder-storec s6-fdholder-transferdump s6-fdholder-transferdumpc s6-fdholderd \
    s6-fghack s6-ftrig-listen s6-ftrig-listen1 s6-ftrig-notify s6-ftrig-wait s6-ftrigrd \
    s6-ioconnect s6-ipcclient s6-ipcserver s6-ipcserver-access s6-ipcserver-socketbinder \
    s6-ipcserverd s6-log s6-mkfifodir s6-notifyoncheck s6-setlock s6-setsid s6-setuidgid \
    s6-softlimit s6-sudo s6-sudoc s6-sudod s6-supervise s6-svc s6-svlisten s6-svlisten1 \
    s6-svok s6-svscan s6-svscanctl s6-svstat s6-svwait s6-tai64n s6-tai64nlocal s6lockd ucspilogd \
 && cd -

# 预处理s6配置文件
RUN mkdir -p /etc/s6/.s6-svscan  \
 && ln -s /bin/true /etc/s6/.s6-svscan/finish  \
 && mkdir -p /etc/s6/cron \
 && mkdir -p /etc/s6/app \
 && ln -s /bin/true /etc/s6/cron/finish \
 && ln -s /bin/true /etc/s6/app/finish

# corotab 文件内容
ADD cronfile /var/spool/cron/root
# 运行Bash 脚本
ADD cron.run /etc/s6/cron/run
ADD app.run /etc/s6/app/run

ENTRYPOINT ["/usr/bin/s6-svscan","/etc/s6"]

cron.run example

#!/usr/bin/env bash
exec crond -n

app.run example

#!/usr/bin/env bash
exec app

三方进程守护之-s6-overlay

s6-overlay 是基于 Skaware S6适用于容器的进程守护工具
s6-overlay 官网 https://github.com/just-containers/s6-overlay
Dockerfile 参考

# Install s6-overlay 进程守护管理.
ENV S6_VERSION v1.21.4.0
RUN curl -L "https://github.com/just-containers/s6-overlay/releases/download/$S6_VERSION/s6-overlay-amd64.tar.gz" > /tmp/s6-overlay-amd64.tar.gz
RUN tar xzf /tmp/s6-overlay-amd64.tar.gz -C / --exclude="./bin" && \
    tar xzf /tmp/s6-overlay-amd64.tar.gz -C /usr ./bin \
    && rm /tmp/s6-overlay-amd64.tar.gz
ENV S6_BEHAVIOUR_IF_STAGE2_FAILS 1

# Set up a standard volume for logs.
VOLUME ["/var/log/services"]


# 设置入口为 s6-based init.
ENTRYPOINT ["/init"]

三方进程守护之-runit

runit官网http://smarden.org/runit/
具体的使用方法见官网
在Docker生态圈, phusion/baseimage-docker, gitlab 在使用runit作为进程管理工具

下面以要运行cron 和 ssh 为例
/etc/service/ 为配置文件目录

/etc/service/sshd 为要运行的程序目录
/etc/service/sshd/run 为需要运行的程序入口脚本文件
cat run

#!/bin/sh
set -e
exec /usr/sbin/sshd -D

/etc/service/cron 为要运行的程序目录
/etc/service/cron/run 为需要运行的程序入口脚本文件
cat run

#!/bin/sh
exec /usr/sbin/cron -f

Dockerfile 参考

ENTRYPOINT ["/usr/bin/runsvdir","-P","/etc/service"]

三方进程守护之-Systemd

在 docker 中使用 Systemd 需要在 docker run 的时候开启特权模式 –privileged ,所以不推荐
这个直接放弃了
Dockerfile 参考

ENTRYPOINT ["/usr/sbin/init"]

参考资料

Alpine里的go应用,你猜他能有多小? http://blog.csdn.net/sisiy2015/article/details/50350261
如何运行多进程Docker容器? http://dockone.io/article/951
在Docker Container中启动定时任务 http://dockone.io/article/1070
Docker容器内多进程管理(一)-Supervisor http://www.linuxprobe.com/docker-process-management1.html
Docker容器内多进程管理(二)-Monit http://www.linuxprobe.com/docker-process-management2.html
关于S6和Runit的论坛讨论 S6 or Runit, not systemd https://www.linuxquestions.org/questions/slackware-14/s6-or-runit-not-systemd-4175465428/
[译] runit 快速入门 https://segmentfault.com/a/1190000006644578

mysql雪崩

故事

小王刚下班回家正准备露一手做2个菜。
手机就收到监控报警,网站打不开报警,mysql IO报警,xx报警
还没等开电脑,公司的人就来电话了
老板:小王啊,网站怎么又打不开了?快点解决啊!损失很大啊!
小王:好的马上处理。
凭借小王丰富的经验,mysql IO都那么高了,肯定是什么sql查询把IO都耗干净了,估计还卡了一堆查询
熟练的用show processlist看了下mysql正在处理的查询,果不其然,这查询队列都长到姥姥家了
苦逼的小心易易的结束了卡的时间很长的SELECT查询,这他妈结束的数据远远跟不上新增加的慢查询的速度呀
应用层程序也来不及改呀
最后小王还是恢复了数据库,可时间已经过去了整整1小时。

事后
老版:这次网站挂了1小时,以后别这样,的解决啊!
小王解释道:网站被恶意爬虫了,爬到了耗IO的sql查询,这个时候业务又是高峰期,导致数据库卡死了。
(对于业务来说,更多时候只关注结果,业务不能离线)

其他公司怎么做的?

  • 数据库中间件处理
  • 缓存机制保证不会让数据库太难受
  • 应用层熔断机制
    等等

纯PHP + MYSQL的项目如何应对数据库雪崩?

  • 数据库中间件处理
    我想绝大用PHP + MYSQL的中小公司是没有MYSQL中间件的,有中间件的可以在数据库中间件处理
    有用Swoole写MYSQL中间件的案例
  • 缓存要顶用
    设计好Redis机制,最小量的请求落在MYSQL上
  • 用MySQL Query Rewrite Plugin 进行SQL语句重写
    MySQL · 5.7新特性 · Query Rewrite Plugin
  • 用pt-kill自动杀掉过长的慢查询
    pt-kill是Percona-Toolkit系列之一,Percona公司出品的工具都没用过,绝对有相见恨晚的感觉。
    pt-kill杀掉慢查询,这算是一个简单粗暴的“熔断方案”,都已经到了让数据库雪崩的阶段,与其业务都不可用,还不如有损服务。
    需要注意的是只结束从库的SELECT慢查询

直接上手,pt-kill常驻进程自动结束SQL慢查询实例

以下实例作用是
* 127.0.0.1这台msyql实例上
* 字符集指定为 utf8
* 查询中有关键字 “SELECT|select” (支持正则)
* mysql帐号是 “业务mysql帐号”
* 查询时间大于60秒
* kill掉查询,不kill mysql连接
* 表示从匹配的结果中选择,类似SQL中的where部分,all是全部的查询
* 10秒检查一次
* 日志记录位置
* 打印日志
* 守护进程的方式运行

pt-kill \
--host 127.0.0.1 --port 3306 --user 'root' --password 'password' \
--charset utf8 \
--match-info "SELECT|select" \
--match-user 业务mysql帐号 \
--busy-time 60 \
--kill-query \
--victims all \
--interval 10 \
--log=/var/log/mysql-pt-kill.log \
--print \
--daemonize

常用参数说明

  • host 连接数据库的地址
  • port 连接数据库的端口
  • user 连接数据库的用户名
  • passowrd 连接数据库的密码
  • charset 指定字符集
  • match-command 指定杀死的查询类型
  • match-user 指定杀死的用户名,即杀死该用户的查询
  • match-info 匹配查询的信息
  • busy-time 指定杀死超过多少秒的查询
  • kill 执行kill命令
  • kill-query 与kill匹配查询的连接不同,此选项只会kill查询,而不会杀死连接
  • victims 表示从匹配的结果中选择,类似SQL中的where部分,all是全部的查询
  • interal 每隔多少秒检查一次
  • print 把kill的查询打印出来
  • log 输出日志到文件仅在 daemonize 守护进程的时候
  • daemonize 守护进程(常驻进程)
    更多参考官方pt-kill手册

监控多个mysql

如果多个mysql都需要用pt-kill监控慢查询并干掉,那么运行多个即可

/usr/bin/pt-kill \
--host 10.10.0.1 \
--port 3306 \
--user ptkill \ 
--password ptkill账号密码 \
--charset utf8 \
--match-info 'SELECT|select' \ 
--match-user 业务mysql帐号 \
--busy-time 20 \
--victims all \
--interval 10 \
--log=/var/log/pt-kill-mysql-slave1.log \
--pid=/var/run/pt-kill-mysql-slave1.pid \
--kill \
--print \ 
--daemonize

/usr/bin/pt-kill \
--host 10.10.0.2 \
--port 3306 \
--user ptkill \ 
--password ptkill账号密码 \
--charset utf8 \
--match-info 'SELECT|select' \ 
--match-user 业务mysql帐号 \
--busy-time 20 \
--victims all \
--interval 10 \
--log=/var/log/pt-kill-mysql-slave2.log \
--pid=/var/run/pt-kill-mysql-slave2.pid \
--kill \
--print \ 
--daemonize

pt-kill守护进程

虽然pt-kill自带参数daemonize可以后台守护运行,但是很多时候会莫名其妙的进程挂掉,
那么还需要一个外部的进程守护工具来守护他,在pt-kill自动退出后再次把进程拉起来
这里进程守护工具我们使用 Monit
配置文件参考为

#pt-kill-mysql 从库1守护
check process pt-kill-mysql-slave1 with pidfile /var/run/pt-kill-mysql-slave1.pid
   start program  "/usr/bin/pt-kill --host 10.10.0.1 --port 3306 --user ptkill --password ptkill账号密码 --charset utf8 --match-info 'SELECT|select' --match-user 业务mysql帐号 --busy-time 20 --victims all --interval 10 --log=/var/log/pt-kill-mysql-slave1.log --kill --print --pid=/var/run/pt-kill-mysql-slave1.pid --daemonize"
   stop program  "kill `cat /var/run/pt-kill-mysql-slave1.pid`"
   if changed pid then start

#pt-kill-mysql 从库2守护
check process pt-kill-mysql-slave2 with pidfile /var/run/pt-kill-mysql-slave2.pid
   start program "/usr/bin/pt-kill --host 10.10.0.2 --port 3306 --user ptkill --password ptkill账号密码 --charset utf8 --match-info 'SELECT|select' --match-user 业务mysql帐号 --busy-time 20 --victims all --interval 10 --log=/var/log/pt-kill-mysql-slave2.log --kill --print --pid=/var/run/pt-kill-mysql-slave2.pid --daemonize"
   stop program  "kill `cat /var/run/pt-kill-mysql-slave2.pid`"
   if changed pid then start

参考

官方pt-kill手册