docker构建php74+supervisor+cron

作者: 分类: php 时间: 2025-05-27 评论: 暂无评论

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 \
cron \
#添加工具ps 用于查看进程
lsof \
procps \
vim \
supervisor \
# 配置 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
COPY cronjobs /etc/cron.d/cronjobs

# 设置权限
RUN chmod 0644 /etc/cron.d/cronjobs && \
touch /var/log/cron.log

# 启动 Supervisor  supervisor 也是9000端口
CMD ["/usr/bin/supervisord", "-n", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
# /usr/bin/supervisord -n -c /etc/supervisor/conf.d/supervisord.conf

WORKDIR /www/monitor_plat

EXPOSE 9000

sources.list

deb http://mirrors.aliyun.com/debian/ bullseye main contrib non-free
eb 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;
}

}

cronjobs

# 每隔2小时重启队列进程(注意使用绝对路径)
0 */2 * * * root /usr/bin/supervisorctl restart queue

10 * * * * root /usr/bin/supervisorctl restart queue

# 每天8:03执行定时脚本(设置工作目录)
3 8 * * * root cd /www/monitor_plat && /usr/local/bin/php think day

docker-compose.yml

version: '3'

services:
  php74:
#    image: registry.cn-hangzhou.aliyuncs.com/gcpu/lnmp: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
    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

[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

[program:php-fpm]
daemonize = no  ; 禁止后台守护进程模式
command=/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

[program:cron]
command=cron -f  # 保持前台运行
autostart=true
autorestart=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

记录一个有意思的东西 varchar 长度

作者: 分类: php 时间: 2025-04-21 评论: 暂无评论

mysql 可以用 select length("中国") 计算varchar 长度。

eleme ui el-select 远程模糊搜索例子

作者: 分类: js 时间: 2025-04-09 评论: 暂无评论
Vue.component('MySuperGame', {
props: {
    // 双向绑定值
    value: {
        type: [String, Number, Array],
        default: ''
    },
    platform: {
        type: [String, Number],
        default: 0
    },
    // 初始默认值(用于回显)
    initialValue: {
        type: [String, Number, Array],
        default: ''
    },
    // 是否多选
    multiple: {
        type: Boolean,
        default: false
    },
    // 防抖时间(毫秒)
    debounceTime: {
        type: Number,
        default: 300
    },
    // 是否启用缓存
    enableCache: {
        type: Boolean,
        default: true
    },
    // 自定义结果格式化函数
    formatter: {
        type: Function,
        default: items => items.map(item => ({
            value: item.id+'',
            label: item.id+'-'+item.title+'-' + item.subtitle,
            gameInfo: item
        }))
    }
},
data() {
    return {
        localValue: this.value || this.initialValue,
        options: [],
        loading: false,
        searchKeyword: '',
        searchCache: new Map(),
        cancelToken: null
    }
},

watch: {
    'platform': {
        handler(newVal, oldVal) {
            if (newVal > 0 && newVal != oldVal) {
                this.localValue = '';                   // 清空绑定值
                this.options = [];                   // 清空绑定值
            }
        },
    },
    value(newVal) {
        this.localValue = newVal
    },
    localValue(newVal) {
        this.$emit('input', newVal)
        this.$emit('change', newVal)
        let gameNode=this.options.filter(item => {
            if(item.value==newVal){
                return true
            }
        })
        if(gameNode[0]){
            this.$emit('gamechange', gameNode[0].gameInfo)
        }
    }
},

created() {
    console.log('platform',this.platform)
    // 初始化加载默认值对应的选项
    if (this.localValue) {
        this.loadInitialValues()
    }
},

methods: {
    // 防抖搜索方法
    handleSearch: _.debounce( async function  (keyword) {
        this.searchKeyword = keyword.trim()
        if (!this.searchKeyword) {
            this.options = []
            return
        }
        this.loading = true
        this.cancelPreviousRequest()
        let response = await axios.post('/topic/card/getGameNodeSearch', {key: keyword, platform: this.platform,format:0})
            .catch(error => {
                if (!axios.isCancel(error)) {
                    console.error('Remote search failed:', error)
                    this.$message.error('搜索失败,请稍后重试')
                }
            })
            .finally(() => (this.loading = false))
        const items = this.formatter(response.data.data.gameList || [])
        if (this.enableCache) {
            this.searchCache.set(this.searchKeyword, items)
        }
        this.options = items

    }, 300),

    // 取消之前的请求
    cancelPreviousRequest() {
        if (this.cancelToken) {
            this.cancelToken('取消重复请求')
        }
    },

    // 加载初始值对应的选项
    async loadInitialValues() {
        try {
            this.loading = true
            const id =  this.localValue
            const response = await axios.post(`/topic/card/getGameNodeSearch`, {key: id, format: 0, platform: this.platform})
            this.options = this.formatter(response.data.data.gameList || [])
        } catch (error) {
            console.error('初始化加载失败:', error)
        } finally {
            this.loading = false
        }
    },

    // 高亮匹配文本
    highlight(label) {
        if (!this.searchKeyword) return label
        const regex = new RegExp(`(${this.searchKeyword})`, 'gi')
        return label.replace(regex, '<span class="highlight">$1</span>')
    }
},

template: `
  <el-select
      v-model="localValue"
      filterable
      clearable
      remote
      reserve-keyword
      placeholder="请输入游戏名或游戏ID"  
      :multiple="multiple"
      :remote-method="handleSearch"
      :loading="loading"
      :loading-text="'加载中...'"
      :no-match-text="'无匹配结果'"
      :no-data-text="'暂无数据'"
  >
    <el-option
        v-for="item in options"
        :key="item.value"
        :label="item.label"
        :value="item.value"
    >
      <span v-html="highlight(item.label)"></span>
    </el-option>
  </el-select>
`

})

k8s 利用 HPA自动缩容

作者: 分类: php 时间: 2025-04-03 评论: 暂无评论
# 定义 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 搭建步骤

作者: 分类: php 时间: 2025-04-03 评论: 暂无评论

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

Top ↑