为了找出响应速度慢,超时失败率高的接口,并定位出性能瓶颈的卡壳所在,我们需要一个apm性能监测工具。php是一个单进程的语言,性能卡壳主要就在于myql、redis、memcache等数据库操作以及curl等微服务调用过程,我们需要一个工具,能够帮助我们系统的看到整个接口的请求——响应过程,找出过程中的耗时点,有针对性的做出优化。skywalking是国产开源的apm性能管理工具,本教程在前面内容的基础上,基于k8s搭建一个自己的skywalking服务端,ui工具以及php的skywalking agent
我们的skywalking使用外部mysql作为数据存储仓库
mysql存储卷
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-data
namespace: datacenter
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 2Gi
storageClassName: local-path
volumeMode: Filesystem
myql deployment中用环境变量指定时区和root密码,root密码来自于k8s配置的密钥
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
k8s.kuboard.cn/layer: db
k8s.kuboard.cn/name: mysql
name: mysql
namespace: datacenter
spec:
selector:
matchLabels:
k8s.kuboard.cn/layer: db
k8s.kuboard.cn/name: mysql
template:
metadata:
labels:
k8s.kuboard.cn/layer: db
k8s.kuboard.cn/name: mysql
spec:
containers:
- env:
- name: TZ
value: Asia/Shanghai
envFrom:
- secretRef:
name: passwords
image: 'mysql:8.0'
name: mysql
ports:
- containerPort: 3306
name: mysql
protocol: TCP
volumeMounts:
- mountPath: /var/lib/mysql
name: volume-3k3ah
volumes:
- name: volume-3k3ah
persistentVolumeClaim:
claimName: mysql-data
skywalking连接mysql,需要自己下载mysql的jar包放到/skywalking/ext-libs 目录中,我这里使用共享数据卷的方式放入。在环境变量中配置连接方式为mysql以及mysql的连接信息从k8s的密钥中读取,时区设置一下
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
k8s.kuboard.cn/layer: monitor
k8s.kuboard.cn/name: skywalking
name: skywalking
namespace: promethues
spec:
selector:
matchLabels:
k8s.kuboard.cn/layer: monitor
k8s.kuboard.cn/name: skywalking
template:
metadata:
labels:
k8s.kuboard.cn/layer: monitor
k8s.kuboard.cn/name: skywalking
spec:
containers:
- env:
- name: TZ
value: Asia/Shanghai
- name: SW_STORAGE
value: mysql
envFrom:
- secretRef:
name: skywalking-db
image: 'apache/skywalking-oap-server:9.0.0'
name: skywalking
ports:
- containerPort: 12800
name: sky-http
protocol: TCP
- containerPort: 11800
name: sky-grpc
protocol: TCP
volumeMounts:
- mountPath: /skywalking/ext-libs
name: volume-r8etc
volumes:
- name: volume-r8etc
persistentVolumeClaim:
claimName: skywalking-jars
skywalking-ui是skywalking的界面展示工具,需要连接到skywalking才能使用,环境变量中设置一下时区
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
k8s.kuboard.cn/layer: web
k8s.kuboard.cn/name: skywalking-ui
name: skywalking-ui
namespace: promethues
spec:
selector:
matchLabels:
k8s.kuboard.cn/layer: web
k8s.kuboard.cn/name: skywalking-ui
template:
metadata:
labels:
k8s.kuboard.cn/layer: web
k8s.kuboard.cn/name: skywalking-ui
spec:
containers:
- env:
- name: SW_OAP_ADDRESS
value: 'http://skywalking:12800'
- name: TZ
value: Asia/Shanghai
image: 'apache/skywalking-ui:8.9.0'
name: skywalking-ui
ports:
- containerPort: 8080
name: skywalking-ui
protocol: TCP
skywalking php客户端的编译需要依赖cargo 和 rust 环境 以及libcurl库
cargo和rust
export RUSTUP_HOME=/tmp/rustup && \
export CARGO_HOME=/tmp/cargo && \
curl https://sh.rustup.rs -sSf | sh -s -- -y --no-modify-path && \
ln -s $CARGO_HOME/bin/rustup /usr/local/bin/rustup && \
ln -s $CARGO_HOME/bin/rustc /usr/local/bin/rustc && \
ln -s $CARGO_HOME/bin/cargo /usr/local/bin/cargo && \
ln -s $CARGO_HOME/bin/rustfmt /usr/local/bin/rustfmt
libcurl
apt install -y libcurl4-openssl-dev
安装skywalking
rustup default stable && pecl install skywalking
skywaling的php配置文件内容如下
extension=skywalking
skywalking.enable = ${PHP_SKYWALKING_ENABLE}
skywalking.service = ${APP_NAME}
skywalking.oap_version = 9.0.0
skywalking.grpc_address = ${PHP_SKYWALKING_SERVER_ADDR}
skywalking.log_level = ${PHP_SKYWALKING_LOG_LEVEL}
skywalking.log_path = ${PHP_SKYWALKING_LOG_FILE}
skywalking.sample_n_per_3_secs = ${PHP_SKYWALKING_SAMPLE_PER_SECS}
skywalking.curl_response_enable = 1
具体内容从环境变量中读取
apiVersion: v1
data:
PHP_SKYWALKING_ENABLE: 'On'
PHP_SKYWALKING_LOG_FILE: /tmp/skywalking_agent.log
PHP_SKYWALKING_LOG_LEVEL: INFO
PHP_SKYWALKING_SERVER_ADDR: 'skywalking.promethues:11800'
PHP_SKYWALKING_WORKER_THREADS: '1'
PHP_SKYWALKING_SAMPLE_PER_SECS: '3'
kind: ConfigMap
metadata:
name: test-api-env
namespace: test-project1
skywalking 的 php agent通过 sky的grpc端口 11800 发送链路追踪数据。skywalking连接mysql存储这些数据,skywalking-ui 调用 sky的http端口 12800 查询数据做成报表。