<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Cronjob on Zayn's Blog</title><link>https://blog.treesir.pub/tags/cronjob/</link><description>Recent content in Cronjob on Zayn's Blog</description><generator>Hugo -- gohugo.io</generator><language>zh-cn</language><managingEditor>yangzun@treesir.pub (Zayn)</managingEditor><webMaster>yangzun@treesir.pub (Zayn)</webMaster><copyright>2021-2026 Zayn</copyright><lastBuildDate>Mon, 24 Jul 2023 16:14:42 +0800</lastBuildDate><atom:link href="https://blog.treesir.pub/tags/cronjob/index.xml" rel="self" type="application/rss+xml"/><item><title>使用 Cronjob 定时清理 ElasticSearch 中的日志索引</title><link>https://blog.treesir.pub/posts/cronjob-clean-es-index/</link><pubDate>Tue, 13 Jul 2021 11:45:00 +0800</pubDate><author>yangzun@treesir.pub (Zayn)</author><guid>https://blog.treesir.pub/posts/cronjob-clean-es-index/</guid><description>&lt;h2 class="relative group">背景
&lt;div id="背景" class="anchor">&lt;/div>
&lt;span
class="absolute top-0 w-6 transition-opacity opacity-0 ltr:-left-6 rtl:-right-6 not-prose group-hover:opacity-100">
&lt;a class="group-hover:text-primary-300 dark:group-hover:text-neutral-700 !no-underline" href="#%e8%83%8c%e6%99%af" aria-label="锚点">#&lt;/a>
&lt;/span>
&lt;/h2>
&lt;blockquote>
&lt;p>在日常使用 &lt;code>efk&lt;/code> 日志系统的过程中，每天造成的日志索引量是巨大的，需要进行对相关的索引进行定期清理，来缓解后端储存的占用。如何实现在 kubernetes 中进行优雅的定期清理？本文将介绍使用 kubernetes 中的 &lt;code>Cronjob&lt;/code> 资源对象进行其需求的实现。&lt;/p>&lt;/blockquote>
&lt;p>&lt;strong>常规情况下清理 &lt;code>n&lt;/code> day 以前的数据&lt;/strong>&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">curl -XDELETE http://elasticsearch-logging.kube-system:9200/logstash-&lt;span class="sb">`&lt;/span>date -d&lt;span class="s2">&amp;#34;n days ago&amp;#34;&lt;/span> +&lt;span class="s2">&amp;#34;%Y.%m.%d&amp;#34;&lt;/span>&lt;span class="sb">`&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;blockquote>
&lt;p>这种方法，是网上比较常规的方式，如某一天忘记执行了，就会出现数据未清理干净的情况，有点不优雅，如何解决？&lt;/p></description></item><item><title>使用 Kubernetes CronJob 自动备份 MySQL 数据到 MinIO</title><link>https://blog.treesir.pub/posts/k8s-cronjob-backup-mysql/</link><pubDate>Mon, 24 Jul 2023 16:14:42 +0800</pubDate><author>yangzun@treesir.pub (Zayn)</author><guid>https://blog.treesir.pub/posts/k8s-cronjob-backup-mysql/</guid><description>&lt;p>随着 Kubernetes 在生产环境中的广泛应用，越来越多的有状态应用（如 MySQL 数据库）也开始部署在 K8s 集群中。数据安全是生产环境的重中之重，本文将详细介绍如何使用 Kubernetes CronJob 实现 MySQL 数据库的自动化备份，并将备份文件安全地存储到 MinIO 对象存储中。&lt;/p>
&lt;h2 class="relative group">方案概述
&lt;div id="方案概述" class="anchor">&lt;/div>
&lt;span
class="absolute top-0 w-6 transition-opacity opacity-0 ltr:-left-6 rtl:-right-6 not-prose group-hover:opacity-100">
&lt;a class="group-hover:text-primary-300 dark:group-hover:text-neutral-700 !no-underline" href="#%e6%96%b9%e6%a1%88%e6%a6%82%e8%bf%b0" aria-label="锚点">#&lt;/a>
&lt;/span>
&lt;/h2>
&lt;h3 class="relative group">备份架构
&lt;div id="备份架构" class="anchor">&lt;/div>
&lt;span
class="absolute top-0 w-6 transition-opacity opacity-0 ltr:-left-6 rtl:-right-6 not-prose group-hover:opacity-100">
&lt;a class="group-hover:text-primary-300 dark:group-hover:text-neutral-700 !no-underline" href="#%e5%a4%87%e4%bb%bd%e6%9e%b6%e6%9e%84" aria-label="锚点">#&lt;/a>
&lt;/span>
&lt;/h3>
&lt;div class="mermaid" align="center">
&lt;pre>
flowchart LR
MySQL[MySQL Pod&lt;br/>Database] -->|导出数据| CronJob[CronJob Pod&lt;br/>mysqldump]
CronJob -->|上传备份| MinIO[MinIO Server&lt;br/>Bucket]
&lt;/pre>
&lt;/div>
&lt;h3 class="relative group">方案优势
&lt;div id="方案优势" class="anchor">&lt;/div>
&lt;span
class="absolute top-0 w-6 transition-opacity opacity-0 ltr:-left-6 rtl:-right-6 not-prose group-hover:opacity-100">
&lt;a class="group-hover:text-primary-300 dark:group-hover:text-neutral-700 !no-underline" href="#%e6%96%b9%e6%a1%88%e4%bc%98%e5%8a%bf" aria-label="锚点">#&lt;/a>
&lt;/span>
&lt;/h3>
&lt;ul>
&lt;li>&lt;strong>自动化&lt;/strong>: 使用 CronJob 实现定时自动备份，无需人工干预&lt;/li>
&lt;li>&lt;strong>可靠性&lt;/strong>: 备份文件存储在独立的对象存储中，提高数据安全性&lt;/li>
&lt;li>&lt;strong>可扩展&lt;/strong>: 支持多数据库实例的备份，易于扩展&lt;/li>
&lt;li>&lt;strong>监控友好&lt;/strong>: 可以通过 Kubernetes 原生监控查看备份任务状态&lt;/li>
&lt;li>&lt;strong>成本效益&lt;/strong>: 使用开源的 MinIO 作为存储后端，降低成本&lt;/li>
&lt;/ul>
&lt;h2 class="relative group">环境准备
&lt;div id="环境准备" class="anchor">&lt;/div>
&lt;span
class="absolute top-0 w-6 transition-opacity opacity-0 ltr:-left-6 rtl:-right-6 not-prose group-hover:opacity-100">
&lt;a class="group-hover:text-primary-300 dark:group-hover:text-neutral-700 !no-underline" href="#%e7%8e%af%e5%a2%83%e5%87%86%e5%a4%87" aria-label="锚点">#&lt;/a>
&lt;/span>
&lt;/h2>
&lt;h3 class="relative group">前置条件
&lt;div id="前置条件" class="anchor">&lt;/div>
&lt;span
class="absolute top-0 w-6 transition-opacity opacity-0 ltr:-left-6 rtl:-right-6 not-prose group-hover:opacity-100">
&lt;a class="group-hover:text-primary-300 dark:group-hover:text-neutral-700 !no-underline" href="#%e5%89%8d%e7%bd%ae%e6%9d%a1%e4%bb%b6" aria-label="锚点">#&lt;/a>
&lt;/span>
&lt;/h3>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>组件&lt;/th>
&lt;th>版本要求&lt;/th>
&lt;th>说明&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>Kubernetes&lt;/td>
&lt;td>v1.18+&lt;/td>
&lt;td>支持 CronJob v1 API&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>MySQL&lt;/td>
&lt;td>5.7+ / 8.0+&lt;/td>
&lt;td>运行在 K8s 集群中&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>MinIO&lt;/td>
&lt;td>任意版本&lt;/td>
&lt;td>对象存储服务&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>网络连通性&lt;/td>
&lt;td>-&lt;/td>
&lt;td>CronJob Pod 能访问 MySQL 和 MinIO&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;h3 class="relative group">自定义镜像
&lt;div id="自定义镜像" class="anchor">&lt;/div>
&lt;span
class="absolute top-0 w-6 transition-opacity opacity-0 ltr:-left-6 rtl:-right-6 not-prose group-hover:opacity-100">
&lt;a class="group-hover:text-primary-300 dark:group-hover:text-neutral-700 !no-underline" href="#%e8%87%aa%e5%ae%9a%e4%b9%89%e9%95%9c%e5%83%8f" aria-label="锚点">#&lt;/a>
&lt;/span>
&lt;/h3>
&lt;p>本方案使用了专门构建的备份镜像，包含了 mysqldump 和 MinIO 客户端工具。&lt;/p></description></item></channel></rss>