mysql 可以用 select length("中国") 计算varchar 长度。
k8s 利用 HPA自动缩容
# 定义 Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
#replicas: 2 # 指定 3 个副本
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.25.3 # 使用官方稳定版本
ports:
- containerPort: 80 # 容器监听的端口
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
# 健康检查(可选)
livenessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 5
periodSeconds: 10
readinessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 2
periodSeconds: 5
---
---
# 定义 Service
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: NodePort # 通过节点端口暴露服务
selector:
app: nginx # 必须与 Deployment 的标签匹配
ports:
- protocol: TCP
port: 80 # Service 的端口
targetPort: 80 # 容器的端口
nodePort: 30080 # 节点的端口(范围 30000-32767)
---
# nginx-hpa.yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: nginx-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nginx-deployment
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 10 # 目标 CPU 使用率 50%
# - type: Pods
# pods:
# metric:
# name: requests-per-second
# target:
# type: AverageValue
# averageValue: 20
behavior:
scaleDown:
stabilizationWindowSeconds: 20
selectPolicy: Max
policies:
- type: Percent
value: 50
periodSeconds: 10
scaleUp:
stabilizationWindowSeconds: 20
selectPolicy: Max
policies:
- type: Percent
value: 50
periodSeconds: 10
Deployment
apiVersion: apps/v1:指定 API 版本。
kind: Deployment:指定资源类型为 Deployment。
metadata:元数据部分。
name: nginx-deployment:Deployment 的名称。
labels:标签,用于标识 Deployment。
app: nginx:标签键值对,键为 app,值为 nginx。
spec:规范部分。
replicas: 1:指定副本数为 1。
selector:选择器,用于选择要管理的 Pods。
matchLabels:匹配标签。
app: nginx:匹配标签键值对,键为 app,值为 nginx。
template:Pod 模板。
metadata:Pod 的元数据。
labels:Pod 的标签。
app: nginx:标签键值对,键为 app,值为 nginx。
spec:Pod 的规范。
containers:容器列表。
name: nginx:容器名称。
image: nginx:1.25.3:使用的镜像。
ports:端口列表。
containerPort: 80:容器监听的端口。
resources:资源请求和限制。
requests:资源请求。
memory: "64Mi":请求的内存。
cpu: "250m":请求的 CPU。
limits:资源限制。
memory: "128Mi":限制的内存。
cpu: "500m":限制的 CPU。
livenessProbe:存活性探针。
httpGet:HTTP GET 请求。
path: /:请求路径。
port: 80:请求端口。
initialDelaySeconds: 5:初始延迟。
periodSeconds: 10:检查间隔。
readinessProbe:就绪性探针。
httpGet:HTTP GET 请求。
path: /:请求路径。
port: 80:请求端口。
initialDelaySeconds: 2:初始延迟。
periodSeconds: 5:检查间隔。
Service
apiVersion: v1:指定 API 版本。
kind: Service:指定资源类型为 Service。
metadata:元数据部分。
name: nginx-service:Service 的名称。
spec:规范部分。
type: NodePort:通过节点端口暴露服务。
selector:选择器,用于选择要暴露的 Pods。
app: nginx:选择标签键值对,键为 app,值为 nginx。
ports:端口列表。
protocol: TCP:协议类型。
port: 80:Service 的端口。
targetPort: 80:容器的端口。
nodePort: 30080:节点的端口(范围 30000-32767)。
Horizontal Pod Autoscaler (HPA)
apiVersion: autoscaling/v2:指定 API 版本。
kind: HorizontalPodAutoscaler:指定资源类型为 HPA。
metadata:元数据部分。
name: nginx-hpa:HPA 的名称。
spec:规范部分。
scaleTargetRef:扩展目标引用。
apiVersion: apps/v1:目标 API 版本。
kind: Deployment:目标资源类型。
name: nginx-deployment:目标 Deployment 的名称。
minReplicas: 2:最小副本数。
maxReplicas: 10:最大副本数。
metrics:指标列表。
type: Resource:资源类型指标。
resource:资源名称。
name: cpu:CPU 资源。
target:目标设置。
type: Utilization:使用率类型。
averageUtilization: 50:目标 CPU 使用率为 50%。
type: Pods:Pods 类型指标。
pods:Pods 指标。
metric:指标名称。
name: requests-per-second:每秒请求数。
target:目标设置。
type: AverageValue:平均值类型。
averageValue: 20:目标每秒平均请求数为 20。
behavior:行为设置。
scaleDown:缩减设置。
stabilizationWindowSeconds: 300:稳定窗口时间(秒)。
selectPolicy: Max:选择策略为最大值。
policies:策略列表。
type: Percent:百分比类型。
value: 50:缩减百分比为 50%。
periodSeconds: 60:策略周期(秒)。
scaleUp:扩展设置。
stabilizationWindowSeconds: 600:稳定窗口时间(秒)。
selectPolicy: Max:选择策略为最大值。
policies:策略列表。
type: Percent:百分比类型。
value: 100:扩展百分比为 100%。
periodSeconds: 60:策略周期(秒)。
MicroK8s 搭建步骤
deban系统安装MicroK8s
apt update && sudo apt upgrade -y
apt install snapd
snap install core
unbantu
sudo snap install microk8s --classic
sudo snap alias microk8s.kubectl kubectl
sudo snap alias microk8s.ctr ctr
sudo snap alias microk8s.helm helm
sudo snap alias microk8s.helm3 helm3
关于mysql FIND_IN_SET 的性能
caiji_soft 5000条数据
select id FROM caiji_soft where FIND_IN_SET(21,tag) or FIND_IN_SET(32,tag)
OK
时间: 0.021s
Thinkphp8 laravel加速方法
- 启用路由缓存
在 config/route.php 文件中启用路由缓存,可以减少路由匹配的时间。
return [
// 其他路由配置...
'url_cache' => true,
];
- 启用配置缓存
在 config.php 文件中启用配置缓存,将所有配置文件合并并缓存,减少文件读取和解析的时间。 - 数据库查询缓存
对于频繁查询且不经常变化的数据,可以使用查询缓存来提高性能。在数据库配置文件 config/database.php 中启用查询缓存。
return [
// 其他数据库配置...
'cache' => true,
];
- 启用 OPcache
确保 PHP 的 OPcache 已启用并正确配置。在 php.ini 文件中启用 OPcache。
opcache.enable=1
opcache.enable_cli=1
opcache.memory_consumption=128
opcache.max_accelerated_files=4000
opcache.save_comments=1
opcache.revalidate_freq=60
- 使用静态内容
将不经常变化的 CSS、JavaScript 和图片等静态资源缓存到客户端,通过设置合理的缓存头(Cache-Control)来实现。 - 数据压缩
使用 Gzip 或 Deflate 压缩输出内容,减少网络传输时间。在 Web 服务器配置中启用压缩输出。 - 优化模板
减少模板中的复杂逻辑和循环,尽量将逻辑处理放在 PHP 代码中,模板只负责展示。 - 使用 CDN
将静态资源部署到 CDN(内容分发网络),可以加快资源的加载速度,减少服务器压力。 - 数据库优化(很多)
使用合适的索引来加速查询。
定期清理和优化数据库表。
避免使用 SELECT *,只查询需要的字段。
- 使用 HTTP/2
如果可能,使用 HTTP/2 协议,它提供了多路复用、头部压缩等特性,可以显著提高页面加载速度。 - 调整 PHP-FPM 配置
如果你使用 PHP-FPM,可以调整其配置来提高性能。例如,增加 pm.max_children、pm.start_servers、pm.min_spare_servers 和 pm.max_spare_servers 的值,以适应更高的并发负载。 - 使用缓存服务器
使用 Redis 或 Memcached 作为缓存服务器,可以显著提高读写性能,特别是对于频繁访问的数据。 - 避免不必要的插件和扩展
禁用或卸载不必要的插件和扩展,减少系统开销。 - 使用性能分析工具
使用 Xdebug、Blackfire 或其他性能分析工具来检测瓶颈,并针对这些瓶颈进行优化。