docker save -o hy_dev.tar 2d4567b582e85ded3f
docker load -i hy_dev.tar
docker tag xxx xxx
docker 导入导出镜像
docker构建php74+supervisor+cron
Dockerfile
FROM php:7.4.33-fpm
COPY sources.list /etc/apt/sources.list
# 安装编译 Redis 扩展所需的依赖
RUN apt-get update && apt-get install -y \
autoconf \
g++ \
make \
libssl-dev \
libzip-dev \
libpng-dev \
libjpeg-dev \
libfreetype6-dev \
# 补充运行时依赖库
libfreetype6 \
libjpeg62-turbo \
libpng16-16 \
#添加工具ps 用于查看进程
lsof \
procps \
vim \
supervisor \
&& apt-get install -y --no-install-recommends cron \
# 配置 GD 扩展(明确指定路径)
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) gd \
&& docker-php-ext-install pdo_mysql zip \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
# 下载并解压 Redis 扩展源码(版本 5.3.7)
# 注意:需提前将源码包放在构建上下文的 `redis/` 目录下
COPY redis/redis-5.3.7.tgz /tmp/
RUN mkdir -p /usr/src/php/ext/redis \
&& tar xfz /tmp/redis-5.3.7.tgz -C /usr/src/php/ext/redis --strip-components=1 \
&& rm /tmp/redis-5.3.7.tgz
# 编译安装 Redis 扩展
RUN docker-php-ext-install redis
#
## 复制配置
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
#用文件会出错 deepseek说是少了换行导致的
RUN echo "* * * * * root echo 'cron is running' > /proc/1/fd/1 2>&1" >> /etc/cron.d/custom-cron
RUN echo "0 */2 * * * root /usr/bin/supervisorctl restart queue" >> /etc/cron.d/custom-cron
RUN echo "*/30 * * * * root /usr/bin/supervisorctl update" >> /etc/cron.d/custom-cron
RUN echo "3 8 * * * root cd /www/monitor_plat && /usr/local/bin/php think day" >> /etc/cron.d/custom-cron
# 设置权限
RUN chmod 0644 /etc/cron.d/custom-cron && \
touch /var/log/cron.log &&\
chmod 0777 /var/log/cron.log && \
chgrp crontab /etc/cron.d/custom-cron # 添加crontab组权限
# 启动 Supervisor supervisor 也是9000端口 主进程 supervisord 不建议重启
# /usr/bin/supervisord -n -c /etc/supervisor/conf.d/supervisord.conf
# CMD ["/usr/bin/supervisord", "-n", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
# 复制docker-entrypoint.sh到镜像中,并赋予可执行权限
COPY docker-entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
CMD ["/usr/local/bin/docker-entrypoint.sh"]
WORKDIR /www/monitor_plat
EXPOSE 9000
sources.list
deb http://mirrors.aliyun.com/debian/ bullseye main contrib non-free
deb http://mirrors.aliyun.com/debian-security/ bullseye-security main
deb http://mirrors.aliyun.com/debian/ bullseye-updates main contrib non-free
default.conf
server {
listen 80;
index index.php index.html;
server_name smart.u3733.com;
root /www/monitor_plat/public;
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?s=$1 last; break;
}
}
location ~ \.php$ {
fastcgi_pass php74:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
docker-compose.yml
version: '3'
services:
php74:
image: my-php-fpm:7.4-supervisord
build:
context: .
dockerfile: Dockerfile
container_name: php74
ports:
- "9001:9000"
volumes:
- ./../:/www/monitor_plat
- ./php74/php.ini:/usr/local/etc/php/php.ini
- ./supervisord.conf:/etc/supervisor/conf.d/supervisord.conf
networks:
- smart-network
nginx:
image: registry.cn-hangzhou.aliyuncs.com/gcpu/lnmp:nginx_1.27.5
container_name: nginx
ports:
- "9090:80"
volumes:
- ./../:/www/monitor_plat
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
networks:
- smart-network
depends_on:
- php74
networks:
smart-network:
####
## docker-compose build --no-cache # 强制完全重建
## docker-compose up -d --force-recreate # 重启容器
supervisord 有两种模式,一种是端口的占用9000,一种是unix套接字
supervisord.conf
[inet_http_server]
port=0.0.0.0:9096
[unix_http_server]
file = /var/run/supervisor.sock ; (the path to the socket file)
chmod = 0700 ; sockef file mode (default 0700)
[supervisord]
nodaemon=true
logfile=/var/log/supervisord.log
pidfile=/var/run/supervisord.pid
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///var/run/supervisor.sock
[program:php-fpm]
daemonize = no ; 禁止后台守护进程模式
command=bash -c "fuser -k 9000/tcp || true; /usr/local/sbin/php-fpm" # 终止占用端口的进程
autostart=true
autorestart=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
[program:queue]
command=php /www/monitor_plat/think queue:listen --queue=default
directory=/www/monitor_plat
autostart=true
autorestart=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
user=www-data
numprocs= 4 ; 启动8个进程
process_name=%(program_name)s_%(process_num)02d ; 动态生成进程名
docker-entrypoint.sh
#!/bin/sh
set -e
# 停止 cron 服务(如果正在运行)
pkill cron || true
# 创建运行时目录并设置适当权限
mkdir -p /www/monitor_plat/runtime
chmod -R 775 /www/monitor_plat/runtime
# 重启 cron 服务
service cron restart
# 后台输出 cron 日志
tail -F /var/log/cron.log &
# 启动主进程
exec /usr/bin/supervisord -n -c /etc/supervisor/conf.d/supervisord.conf
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