<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Chawalit Sengsang on Medium]]></title>
        <description><![CDATA[Stories by Chawalit Sengsang on Medium]]></description>
        <link>https://medium.com/@chawalit.s?source=rss-aab8035b6971------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*uEKUlCa4qQTP99KAP4f3fQ.jpeg</url>
            <title>Stories by Chawalit Sengsang on Medium</title>
            <link>https://medium.com/@chawalit.s?source=rss-aab8035b6971------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 23:32:39 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@chawalit.s/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[ตั้งค่า PostgrSQL สำหรับรองรับการทำงาน]]></title>
            <link>https://medium.com/@chawalit.s/%E0%B8%95%E0%B8%B1%E0%B9%89%E0%B8%87%E0%B8%84%E0%B9%88%E0%B8%B2-postgrsql-%E0%B8%AA%E0%B8%B3%E0%B8%AB%E0%B8%A3%E0%B8%B1%E0%B8%9A%E0%B8%A3%E0%B8%AD%E0%B8%87%E0%B8%A3%E0%B8%B1%E0%B8%9A%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B8%97%E0%B8%B3%E0%B8%87%E0%B8%B2%E0%B8%99-cc51c803aaf8?source=rss-aab8035b6971------2</link>
            <guid isPermaLink="false">https://medium.com/p/cc51c803aaf8</guid>
            <dc:creator><![CDATA[Chawalit Sengsang]]></dc:creator>
            <pubDate>Sun, 20 Sep 2026 14:25:54 GMT</pubDate>
            <atom:updated>2026-09-20T14:25:54.670Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><em>นิยามว่าหากเรามี Server spec</em></blockquote><ul><li>4 vCPU</li><li>RAM 8GB</li><li>Storage 200GB ต้องรองรับกับ 500 Concurrent Connections</li></ul><p>การที่จะต้องให้ PostgreSQL รองรับ 500 concurrent แบบตรงๆ เข้า Database Node อาจจะทำให้ RAM หมดทันที (Out of Memory — OOM) และ CPU ติดคอขวดที่ Context Switching ดังนั้น การใช้ Connection Pooler (PgBouncer) จึงกลายเป็น ข้อบังคับ (Mandatory) ไม่ใช่แค่ออปชันเสริมสำหรับสเปกนี้</p><h3>1. ปรับสถาปัตยกรรม Connection (PgBouncer + PostgreSQL)</h3><p><em>ห้ามตั้ง max_connections บน PostgreSQL เกิน 60–80</em></p><ul><li>ด้วย 4 vCPU จำนวน active connections ที่ CPU สลับการทำงานได้ดีที่สุดโดยไม่ drop throughput อยู่ที่ประมาณ (4 x 2) ถึง 40–60 connections</li><li>ให้ PgBouncer รับ 500 connections จากฝั่ง Client แล้วส่งต่อเข้า DB เพียง 40–60 pool connections เท่านั้น (โหมด pool_mode = transaction)</li></ul><h3>2. การปรับแต่ง postgresql.conf สำหรับ RAM 8GB</h3><p>สูตรคำนวณถูกบีบให้กระชับ เพื่อเหลือพื้นที่ให้ OS, Background Workers และ Query Sort/Hash:</p><ul><li>max_connections 80,จำกัดไม่ให้แย่งกันใช้ CPU และกัน Memory บวม</li><li>shared_buffers 2GB,25% ของ RAM ทั้งหมดสำหรับบัฟเฟอร์หลัก</li><li>effective_cache_size 6GB,75% ของ RAM ให้ Query Planner คำนวณ Index scan แม่นยำ</li><li>work_mem 8MB – 16MB,คำนวณจาก: (8GB - 2GB) / (80 x 2-3 queries) กัน OOM ยามมี Sort ซับซ้อน</li><li>maintenance_work_mem 512MB,เพียงพอสำหรับ Index creation และ Autovacuum โดยไม่กิน RAM เครื่องจนค้าง</li><li>checkpoint_completion_target 0.9,เกลี่ย I/O ให้เรียบ ป้องกัน Disk spike บน Storage 200GB</li><li>max_wal_size 4GB – 8GB,ห้ามตั้งสูงเกินไป เพื่อไม่ให้กินพื้นที่ Storage 200GB เร็วเกินไป</li><li>min_wal_size 1GB,ขนาดพื้นฐานของ WAL</li><li>random_page_cost 1.1,เหมาะสำหรับ SSD/NVMe Cloud Volume</li><li>wal_compression on (หรือ lz4),ประหยัดทั้ง I/O และพื้นที่ Storage 200GB</li></ul><h3>3. แผนรับมือข้อจำกัด Storage 200GB (สำหรับเป้าหมายใช้งานยาว)</h3><p>พื้นที่ 200GB ถือว่าจำกัดมากสำหรับการใช้งานระยะยาวหลายปี หากไม่มีการจัดการ:</p><ul><li>แยกที่เก็บ WAL Archive / Backup ออกจากเครื่องทันที:</li><li>ห้ามเก็บไฟล์ Backup (pg_dump หรือ pgBackRest stanza) ไว้บน Disk เดียวกันกับ Data Directory</li><li>ตั้งค่าส่ง WAL files และ Snapshot ไปยัง Cloud Object Storage (เช่น S3/MinIO) ทันที</li><li>ตั้ง Autovacuum ให้ดักเก็บพื้นที่บ่อยขึ้น:</li></ul><pre>autovacuum_vacuum_scale_factor = 0.05<br>autovacuum_analyze_scale_factor = 0.02<br>autovacuum_vacuum_cost_limit = 1000<br>autovacuum_vacuum_cost_delay = 2ms</pre><ul><li>ควบคุม Data Growth:</li><li>ตาราง Logs/Audit ต้องมี Retention Policy (เช่น ลบหรือ Move ออกทุก 3–6 เดือน)</li><li>หลีกเลี่ยงการเก็บภาพหรือไฟล์ Binary (BLOB/bytea) ลงในตารางโดยตรง ให้เก็บเป็น Object Storage แล้วอ้างอิงด้วย URL/Path</li></ul><h3>4. ตรวจสอบการใช้งานจริง (Verification)</h3><p>หลัง Deploy และปล่อยโหลด สามารถเช็กสถานะการใช้ Resource ได้จากคำสั่ง:</p><pre># ดู Memory และ CPU บน OS<br>free -m<br>top -b -n 1 | head -n 15</pre><pre>-- ตรวจสอบ Memory work_mem ต่อ process ว่าไม่มี Query ที่ทำ Temp File ลง Disk บ่อยเกินไป<br>SELECT query, temp_bytes, calls <br>FROM pg_stat_statements <br>WHERE temp_bytes &gt; 0 <br>ORDER BY temp_bytes DESC LIMIT 5;</pre><p>(ถ้าพบ temp_bytes สูงมาก แสดงว่าค่า work_mem = 8MB อาจเล็กไปสำหรับบาง Complex Query ให้พิจารณาจูน Query นั้นๆ หรือเพิ่มเป็น 16MB)</p><h3>5. ตั้งค่า pgBouncer</h3><p>ตั้งค่า pgbouncer.ini และ userlist.txt ที่ปรับแต่งสำหรับการรับ 500 Concurrent Clients โดยส่งต่อไปยัง PostgreSQL บนเครื่อง 4 vCPU / RAM 8GB หลักการสำคัญคือการใช้ pool_mode = transaction เพื่อให้แอปพลิเคชัน 500 session สลับกันใช้ physical connection สู่ Database จริงเพียง 40–60 connections (ช่วยประหยัด RAM และ CPU ไม่ติด Wait/Context Switch)</p><ol><li>ไฟล์ /etc/pgbouncer/pgbouncer.ini</li></ol><pre>[databases]<br>; กำหนดชื่อฐานข้อมูลและปลายทางไปยัง PostgreSQL เครื่อง Local<br>; รูปแบบ: &lt;dbname&gt; = host=&lt;pg_host&gt; port=&lt;pg_port&gt; dbname=&lt;dbname&gt;<br>* = host=127.0.0.1 port=5432 auth_user=postgres<br><br>[pgbouncer]<br>;; -------------------------------------------------------------<br>;; Administrative settings<br>;; -------------------------------------------------------------<br>logfile = /var/log/postgresql/pgbouncer.log<br>pidfile = /var/run/postgresql/pgbouncer.pid<br>listen_addr = 0.0.0.0<br>listen_port = 6432<br>auth_type = scram-sha-256<br>auth_file = /etc/pgbouncer/userlist.txt<br>admin_users = postgres<br>stats_users = postgres, monitoring<br><br>;; -------------------------------------------------------------<br>;; Pool Configuration (สำหรับ 500 Clients บน RAM 8GB / 4 vCPU)<br>;; -------------------------------------------------------------<br>; โหมด Transaction: คืน Connection เข้า Pool ทันทีที่คำสั่ง Transaction จบ<br>pool_mode = transaction<br>; จำนวน Client Connection สูงสุดที่ PgBouncer ยอมให้ต่อเข้ามาได้<br>max_client_conn = 1000<br>; จำนวน Connection จริงสู่ PostgreSQL ต่อ 1 User/Database pair<br>; บน 4 vCPU ค่า 40-50 เหมาะสมที่สุดในการรีด Throughput สูงสุด<br>default_pool_size = 40<br>; จำนวน Connection ขั้นต่ำที่จะเปิดทิ้งไว้สู่ PostgreSQL<br>min_pool_size = 10<br>; Connection สำรองกรณีเกิด Spike โหลดสั้นๆ (รวมกับ default_pool_size = 60)<br>reserve_pool_size = 20<br>reserve_pool_timeout = 5.0<br>; เพดานรวมของ Connection จริงสู่ PostgreSQL จากทุก Database รวมกัน<br>; ต้องตั้งให้น้อยกว่า max_connections ใน postgresql.conf (ตั้งไว้ 80)<br>max_db_connections = 65<br>;; -------------------------------------------------------------<br>;; Connection Timeouts &amp; Lifecycle<br>;; -------------------------------------------------------------<br>; หาก Pool เต็ม Client จะรอคิวนานสุดกี่วินาทีก่อน Error (ไม่ให้ค้างข้ามนาที)<br>query_wait_timeout = 30.0<br>; ตัด Connection ฝั่ง Client ที่ค้างว่างเกิน 10 นาที<br>client_idle_timeout = 600.0<br>; ปิด Connection ฝั่ง Server (PostgreSQL) ที่ไม่ได้ใช้งานเกิน 5 นาที<br>server_idle_timeout = 300.0<br>; รีเฟรช Connection สู่ Database ทุกๆ 1 ชั่วโมง เพื่อป้องกัน Connection Stale<br>server_lifetime = 3600.0<br>; ป้องกัน Client ที่เปิด Transaction ทิ้งไว้แล้วหายไป (หน่วยวินาที)<br>server_idle_transaction_timeout = 60.0<br>;; -------------------------------------------------------------<br>;; Low-level &amp; Memory tuning<br>;; -------------------------------------------------------------<br>; ขนาด Buffer ต่อ Connection (ค่ามาตรฐาน 4KB ช่วยประหยัด RAM บนเครื่อง 8GB)<br>pkt_buf = 4096<br>listen_backlog = 512</pre><ol><li>ไฟล์ /etc/pgbouncer/userlist.txt ไฟล์นี้เก็บ Username และ Hash รหัสผ่าน (ใช้ SCRAM-SHA-256):</li></ol><pre>&quot;postgres&quot; &quot;SCRAM-SHA-256$4096:...&quot;<br>&quot;app_user&quot; &quot;SCRAM-SHA-256$4096:...&quot;</pre><blockquote><em>คำแนะนำในการดึง Hash รหัสผ่าน: ไม่จำเป็นต้องพิมพ์เอง ให้รัน Query นี้บน PostgreSQL แล้วคัดลอกผลลัพธ์มาใส่ใน userlist.txt:</em></blockquote><pre>SELECT concat(&#39;&quot;&#39;, usename, &#39;&quot; &quot;&#39;, passwd, &#39;&quot;&#39;) <br>FROM pg_shadow <br>WHERE usename IN (&#39;postgres&#39;, &#39;app_user&#39;);</pre><ol><li>การประสานงานกับ postgresql.conf เพื่อให้ PgBouncer ทำงานสอดคล้องกัน ให้ตรวจสอบค่าใน postgresql.conf ของ Database หลัก:</li></ol><pre># ต้องมีค่ามากกว่า max_db_connections ของ PgBouncer เสมอ (เผื่อ admin/monitoring)<br>max_connections = 80<br><br># แนะนำให้เปิดใช้งานเพื่อรองรับ Prepared Statements ข้าม Session ใน Transaction Mode<br>track_commit_timestamp = off</pre><ol><li>ข้อจำกัดและข้อควรระวังในโหมด transaction เนื่องจากเราใช้ pool_mode = transaction (ซึ่งจำเป็นมากสำหรับเครื่อง RAM 8GB):</li><li>Prepared Statements ฝั่ง Client: หากใช้งานผ่าน Driver เช่น Go pgx, Node.js pg, หรือ Java JDBC ต้องตั้งค่าโหมด Connection ใน Library ให้เป็น prefer_simple_protocol = true หรือปิด Client-side prepared statement caching (หรือใช้ PgBouncer เวอร์ชัน 1.21+ ขึ้นไปที่มีฟีเจอร์ max_prepared_statements ในตัว)</li><li>Session-level Features ที่ห้ามใช้: คำสั่งอย่าง SET TIME ZONE …, LISTEN / NOTIFY, หรือ Temporary Tables จะไม่คงสถานะข้าม Transaction ตรวจสอบการทำงาน (Verification) เชื่อมต่อเข้าคอนโซลแอดมินของ PgBouncer เพื่อเช็กปริมาณ Queue และ Pool:</li></ol><pre>psql -h 127.0.0.1 -p 6432 -U postgres -d pgbouncer</pre><pre>-- 1. ดูสถานะของแต่ละ Pool (ตรวจสอบ cl_waiting ว่ามีคิวค้างนานหรือไม่)<br>SHOW POOLS;<br><br>-- 2. ดูจำนวน Connection ฝั่ง Client และ Server จริง<br>SHOW CLIENTS;<br>SHOW SERVERS;<br><br>-- 3. ดูสถิติ Throughput และ Latency<br>SHOW STATS;</pre><p>(ถ้าคอลัมน์ cl_waiting มีค่ามากกว่า 0 ต่อเนื่องเป็นเวลานาน แสดงว่า Query ช้าหรือขนาด default_pool_size เริ่มตึงมือ)</p><h3>6. วิธีตั้งค่า PgBouncer 1.21+ ให้รองรับ Prepared Statements ใน Transaction Mode พร้อมตัวอย่างโค้ดแอป</h3><p>ตั้งแต่ PgBouncer 1.21 เป็นต้นมา มีการเพิ่มฟีเจอร์ Built-in Prepared Statement Tracking ทำให้ Client สามารถใช้ Named/Server-side Prepared Statements ร่วมกับ pool_mode = transaction ได้ทันที โดย PgBouncer จะคอย map statement ข้าม connection ให้เองโดยที่ Transaction ไม่ชนกัน</p><ol><li>การตั้งค่าใน pgbouncer.ini เพิ่มพารามิเตอร์ max_prepared_statements ภายใต้เซกชัน [pgbouncer]:</li></ol><pre>[pgbouncer]<br>pool_mode = transaction<br><br>; กำหนดจำนวน Prepared Statements สูงสุดที่ PgBouncer จะจำไว้ต่อ connection<br>; แนะนำ 100 - 200 สำหรับเครื่อง RAM 8GB (ใช้ Memory น้อยมาก แต่ช่วยให้ Cache hit สูง)<br>max_prepared_statements = 100<br>; เปิดใช้งาน protocol-level statement cache<br>; (ปกติ default เป็น 0 ในเวอร์ชันเก่า แต่ 1.21+ ต้องระบุค่าตัวเลขเพื่อเปิดใช้)</pre><blockquote><em>คำเตือนเวอร์ชัน: ตรวจสอบว่า PgBouncer เป็นเวอร์ชัน 1.21 หรือใหม่กว่าโดยรันคำสั่ง pgbouncer — version</em></blockquote><ol><li>ตัวอย่างการคอนฟิกฝั่ง Application Drivers แม้ PgBouncer 1.21+ จะรองรับ Prepared Statement แล้ว แต่ Driver บางตัวอาจมี Behavior การตั้งชื่อ Statement ชนกัน หรือมี Session Cache เฉพาะตัว จึงควรคอนฟิกตามตัวอย่างด้านล่าง Go (pgx/v5) pgx รองรับฟีเจอร์นี้ได้ดีมาก โดยสามารถตั้งค่าใน Connection String หรือ pgxpool.Config:</li></ol><pre>package main<br><br>import (<br> &quot;context&quot;<br> &quot;log&quot;<br> &quot;github.com/jackc/pgx/v5/pgxpool&quot;<br>)<br>func main() {<br> // เชื่อมต่อไปที่ Port 6432 ของ PgBouncer<br> connStr := &quot;postgres://app_user:secret@127.0.0.1:6432/mydb?sslmode=disable&quot;<br> <br> config, err := pgxpool.ParseConfig(connStr)<br> if err != nil {<br>  log.Fatalf(&quot;Config parse error: %v&quot;, err)<br> }<br> // แนะนำสำหรับ Transaction Mode: ใช้ Named Statement Cache ปกติได้เลยบน v1.21+<br> // หากพบปัญหาชื่อชนกัน ให้ fallback มาใช้ Describe Cache:<br> // config.ConnConfig.DefaultQueryExecMode = pgx.QueryExecModeDescribeExec<br> pool, err := pgxpool.NewWithConfig(context.Background(), config)<br> if err != nil {<br>  log.Fatalf(&quot;Unable to connect to PgBouncer: %v&quot;, err)<br> }<br> defer pool.Close()<br> // Query ด้วย Prepared Statement อัตโนมัติ<br> var id int<br> var username string<br> err = pool.QueryRow(context.Background(), <br>  &quot;SELECT id, username FROM users WHERE id = $1&quot;, 1).Scan(&amp;id, &amp;username)<br> if err != nil {<br>  log.Fatalf(&quot;Query failed: %v&quot;, err)<br> }<br>}</pre><p><em>Node.js / TypeScript (pg / TypeORM / Prisma)</em> สำหรับไลบรารี pg (node-postgres):</p><pre>import { Pool } from &#39;pg&#39;;<br><br>const pool = new Pool({<br>  host: &#39;127.0.0.1&#39;,<br>  port: 6432, // PgBouncer port<br>  user: &#39;app_user&#39;,<br>  password: &#39;secret_password&#39;,<br>  database: &#39;mydb&#39;,<br>  max: 20, // client-side pool size<br>});<br>async function getUser(userId: number) {<br>  // ส่งคำสั่งแบบ Parameterized Query<br>  // บน PgBouncer 1.21+ สามารถใส่ name เพื่อทำ Server-side Prepared Statement ได้<br>  const query = {<br>    name: &#39;fetch-user-by-id&#39;,<br>    text: &#39;SELECT id, username, email FROM users WHERE id = $1&#39;,<br>    values: [userId],<br>  };<br>  const res = await pool.query(query);<br>  return res.rows[0];<br>}</pre><blockquote><em>กรณีใช้ Prisma: Prisma engine มี Client-side pooler handling ในตัว หากต่อผ่าน PgBouncer แนะนำให้ต่อด้วย Query Parameter ?pgbouncer=true ใน DATABASE_URL </em>Java (Spring Boot + HikariCP)<em> ใน application.yml หรือ application.properties ให้ปรับ Driver property:</em></blockquote><pre>spring:<br>  datasource:<br>    url: jdbc:postgresql://127.0.0.1:6432/mydb?prepareThreshold=5&amp;preparedStatementCacheQueries=100<br>    username: app_user<br>    password: secret_password<br>    driver-class-name: org.postgresql.Driver<br>    hikari:<br>      maximum-pool-size: 20<br>      minimum-idle: 5<br>      # ปิด auto-commit เพื่อให้รอบ Transaction ชัดเจน<br>      auto-commit: false</pre><blockquote><em>prepareThreshold=5: ไดรเวอร์จะสร้าง Server-side Prepared Statement เมื่อ Query ตัวเดิมถูกเรียกซ้ำตั้งแต่ 5 ครั้งขึ้นไป ช่วยลดภาระการเตรียม Statement ที่ไม่จำเป็น</em></blockquote><ol><li>การตรวจสอบบน PgBouncer (Verification) หลังรันแอปพลิเคชันและมีการยิง Prepared Queries สามารถตรวจสอบว่า PgBouncer ทำการแคช Statement สำเร็จหรือไม่ผ่าน Administrative Console:</li></ol><pre>psql -h 127.0.0.1 -p 6432 -U postgres -d pgbouncer</pre><pre>-- 1. ดูรายการและจำนวน Prepared Statements ที่ถูกแคชไว้<br>SHOW PREPARED_STATEMENTS;<br>-- 2. สังเกตคอลัมน์ prepared_statements ในคำสั่ง SHOW POOLS<br>SHOW POOLS;</pre><p>หากตารางแสดงรายการ Query String และ Hash ชัดเจน แสดงว่า PgBouncer จัดการ Prepared Statement ข้าม Client Connection ได้อย่างสมบูรณ์โดยไม่มี Error prepared statement “…” does not exist อีกต่อไป</p><h3>7. ขั้นตอนการตั้งค่า Systemd Service และสิทธิ์ของไฟล์ (File Permissions) สำหรับ PgBouncer ให้ทำงานเป็น Background Service บน Linux (รองรับทั้ง Ubuntu/Debian และ RHEL/Rocky/AlmaLinux)</h3><ol><li>สร้าง User และไดเรกทอรีที่จำเป็น หากติดตั้งผ่าน Package Manager ตัวระบบมักสร้าง user postgres หรือ pgbouncer มาให้อัตโนมัติ หากยังไม่มี ให้สร้าง user และไดเรกทอรีสำหรับ Log, PID, และ Runtime Configuration:</li></ol><pre># สร้าง Directory สำหรับ Config, Log และ PID<br>sudo mkdir -p /etc/pgbouncer<br>sudo mkdir -p /var/log/postgresql<br>sudo mkdir -p /var/run/postgresql<br><br># กำหนด Owner เป็น postgres (หรือ pgbouncer ตามระบบที่ใช้)<br>sudo chown -R postgres:postgres /var/log/postgresql<br>sudo chown -R postgres:postgres /var/run/postgresql</pre><p>วิธีตรวจสอบ: รัน ls -ld /var/run/postgresql จะต้องเห็น Owner และ Group เป็น postgres:postgres 2. กำหนด File Permissions อย่างปลอดภัย ไฟล์ userlist.txt บรรจุ Hash รหัสผ่าน จึงต้องจำกัดสิทธิ์อ่าน/เขียนเฉพาะ Service User เท่านั้น:</p><pre># กำหนด Owner ทั้งหมดใน /etc/pgbouncer<br>sudo chown -R postgres:postgres /etc/pgbouncer<br><br># pgbouncer.ini ให้อ่านได้เฉพาะ owner และ group<br>sudo chmod 640 /etc/pgbouncer/pgbouncer.ini<br><br># userlist.txt ล็อกสิทธิ์ให้อ่าน/เขียนได้เฉพาะ owner เท่านั้น<br>sudo chmod 600 /etc/pgbouncer/userlist.txt</pre><blockquote><em>วิธีตรวจสอบ: รัน ls -la /etc/pgbouncer ตรวจสอบว่า userlist.txt มีสิทธิ์เป็น -rw — — — -</em></blockquote><ol><li>สร้าง Systemd Unit File สร้างไฟล์ Unit ที่ /etc/systemd/system/pgbouncer.service:</li></ol><pre>sudo nano /etc/systemd/system/pgbouncer.service</pre><p>ใส่เนื้อหา Configuration ด้านล่าง:</p><pre>[Unit]<br>Description=PgBouncer connection pooler for PostgreSQL<br>Documentation=man:pgbouncer(1) https://www.pgbouncer.org/<br>After=network.target local-fs.target<br># หากรัน PostgreSQL บนเครื่องเดียวกัน ให้รอ DB สตาร์ทก่อน<br>After=postgresql.service<br>[Service]<br>Type=notify<br>User=postgres<br>Group=postgres<br># รัน PgBouncer โดยชี้ไปที่ไฟล์คอนฟิกหลัก<br>ExecStart=/usr/bin/pgbouncer /etc/pgbouncer/pgbouncer.ini<br># Reload ค่าคอนฟิกโดยไม่ต้อง Restart process (SIGHUP)<br>ExecReload=/bin/kill -HUP $MAINPID<br># การจัดการ Lifecycle และ Auto-restart<br>KillSignal=SIGINT<br>Restart=on-failure<br>RestartSec=5s<br># ปรับเพิ่มขีดจำกัด File Descriptors เพื่อรองรับ 500+ Connections<br>LimitNOFILE=65536<br># สร้าง Directory ชั่วคราวอัตโนมัติหาก Reboot (systemd tmpfiles handler)<br>RuntimeDirectory=postgresql<br>RuntimeDirectoryMode=0755<br>[Install]<br>WantedBy=multi-user.target</pre><blockquote><em>(หมายเหตุ: บน RHEL บางเวอร์ชัน binary อาจอยู่ที่ /usr/sbin/pgbouncer ให้ตรวจสอบด้วยคำสั่ง which pgbouncer ก่อนบันทึก)</em></blockquote><ol><li>เปิดใช้งานและ Start Service Reload systemd daemon เพื่ออ่าน Unit file ใหม่ และสั่งเปิด Service:</li></ol><pre># โหลดคอนฟิก systemd ใหม่<br>sudo systemctl daemon-reload<br># เปิดใช้งานให้ Start อัตโนมัติตอน Boot เครื่อง<br>sudo systemctl enable pgbouncer<br># สั่ง Start Service ทันที<br>sudo systemctl start pgbouncer</pre><p>วิธีตรวจสอบ: รัน sudo systemctl status pgbouncer สถานะต้องขึ้นเป็น active (running) สีเขียว</p><ol><li>ตรวจสอบการเชื่อมต่อและการ Reload Config</li></ol><ul><li>ทดสอบการ Reload คอนฟิก (Zero-downtime): เมื่อมีการแก้ไข userlist.txt หรือปรับขนาด Pool ใน pgbouncer.ini สามารถสั่ง Reload ได้ทันทีโดย Connection ไม่หลุด:</li></ul><pre>sudo systemctl reload pgbouncer</pre><p>ตรวจสอบ Port ที่เปิดรับ:</p><pre>ss -tlpn | grep 6432</pre><ul><li>ต้องพบ process pgbouncer ฟังอยู่ที่พอร์ต 0.0.0.0:6432 หรือ 127.0.0.1:6432 ตามที่ตั้งไว้</li></ul><h3>8. ขั้นตอนการตั้งค่า Logrotate สำหรับตัดและบีบอัดไฟล์ Log ของ PgBouncer แบบอัตโนมัติ เพื่อป้องกันไม่ให้ Log โตจนกินพื้นที่ Storage 200GB จนเต็ม</h3><ol><li>สร้างไฟล์คอนฟิก Logrotate สร้างไฟล์คอนฟิกใหม่ในไดเรกทอรี /etc/logrotate.d/:</li></ol><pre>sudo nano /etc/logrotate.d/pgbouncer</pre><p>ใส่เนื้อหาด้านล่างลงในไฟล์:</p><pre>/var/log/postgresql/pgbouncer.log {<br>    # ตัดรอบไฟล์ Log ทุกวัน<br>    daily<br>    # เก็บ Log ย้อนหลัง 14 ไฟล์ (ประมาณ 2 สัปดาห์) เก่ากว่านั้นจะถูกลบอัตโนมัติ<br>    rotate 14<br>    # บีบอัด Log เก่าเป็น .gz เพื่อประหยัดเนื้อที่ Disk<br>    compress<br>    # ชะลอการบีบอัดไฟล์ล่าสุดไว้ 1 รอบ ป้องกันปัญหาไฟล์กำลังถูกเขียน<br>    delaycompress<br>    # ข้ามการทำงานเงียบๆ หากไม่พบไฟล์ Log (ไม่แจ้ง Error กวนระบบ)<br>    missingok<br>    # ไม่ตัดไฟล์หากไฟล์มีขนาดเป็น 0 Byte<br>    notifempty<br>    # กำหนดสิทธิ์และ Owner ให้ไฟล์ Log ที่สร้างขึ้นใหม่<br>    create 0640 postgres postgres<br>    # รวมคำสั่งหลังหมุนไฟล์ให้ยิง signal เพียงครั้งเดียว<br>    sharedscripts<br>    # สั่งให้ PgBouncer ปิดไฟล์เดิมแล้วเปิดเขียนไฟล์ใหม่ (Reopen Log)<br>    postrotate<br>        if [ -f /var/run/postgresql/pgbouncer.pid ]; then<br>            kill -HUP $(cat /var/run/postgresql/pgbouncer.pid) 2&gt;/dev/null || true<br>        fi<br>    endscript<br>}</pre><blockquote><em>จุดสำคัญ: PgBouncer เมื่อได้รับสัญญาณ SIGHUP จะทำการ reload config และ Reopen logfile ใหม่ทันที ทำให้สามารถเขียนลงไฟล์ใหม่ได้ต่อเนื่องโดยไม่ต้อง restart service</em></blockquote><ol><li>ตรวจสอบสิทธิ์ของไฟล์คอนฟิก Logrotate กำหนดความปลอดภัยเข้มงวด ไฟล์ใน /etc/logrotate.d/ ต้องเป็นของ root และห้ามมีสิทธิ์เขียนสำหรับกลุ่มอื่น:</li></ol><pre>sudo chown root:root /etc/logrotate.d/pgbouncer<br>sudo chmod 0644 /etc/logrotate.d/pgbouncer</pre><blockquote><em>วิธีตรวจสอบ: รัน ls -l /etc/logrotate.d/pgbouncer ต้องได้สิทธิ์ -rw-r — r — root root</em></blockquote><ol><li>ทดสอบคำสั่งการหมุน Log (Dry Run) ทดสอบประเมินเงื่อนไขโดยไม่ตัดไฟล์จริง (Debug mode):</li></ol><pre>sudo logrotate -d /etc/logrotate.d/pgbouncer</pre><blockquote><em>วิธีตรวจสอบ: บรรทัดล่างสุดของ output ต้องแสดงแผนการทำงาน เช่น rotating pattern: /var/log/postgresql/pgbouncer.log after 1 days… โดยไม่มีข้อผิดพลาดแจ้งเตือนเรื่อง syntax หรือ permission</em></blockquote><ol><li>ทดสอบตัดไฟล์จริง (Force Run) สั่ง Force rotation เพื่อดูว่าไฟล์ถูกแยกและสร้างใหม่ถูกต้อง:</li></ol><pre>sudo logrotate -f /etc/logrotate.d/pgbouncer</pre><p><em>วิธีตรวจสอบ:</em> รัน ls -la /var/log/postgresql/ จะต้องเห็นไฟล์ใหม่และไฟล์เก่าที่ถูก rotate:</p><pre>-rw-r----- 1 postgres postgres     0 Sep 17 12:00 pgbouncer.log<br>-rw-r----- 1 postgres postgres 15234 Sep 17 11:59 pgbouncer.log.1</pre><p>และตรวจสอบว่า PgBouncer ยังคงทำงานปกติ:</p><pre>-rw-r----- 1 postgres postgres     0 Sep 17 12:00 pgbouncer.log<br>-rw-r----- 1 postgres postgres 15234 Sep 17 11:59 pgbouncer.log.1</pre><p>และตรวจสอบว่า PgBouncer ยังคงทำงานปกติ:</p><pre>systemctl status pgbouncer</pre><p>ข้อแนะนำเพิ่มเติมสำหรับ Storage 200GB หากในอนาคตพบว่าปริมาณ Traffic สูงจน Log โตเร็วเกินไปในแต่ละวัน (ก่อนจะถึงรอบ daily):</p><ul><li>สามารถเพิ่มบรรทัด maxsize 500M ไว้ในบล็อกคอนฟิก เพื่อสั่งให้ตัดไฟล์ทันทีที่ขนาดแตะ 500MB โดยไม่ต้องรอหมดวัน</li><li>ไฟล์ที่ถูกบีบอัด (.gz) จะมีขนาดเล็กลงจากเดิมประมาณ 80–90% ช่วยประหยัดพื้นที่บน Disk 200GB ได้อย่างมีประสิทธิภาพ</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cc51c803aaf8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ใช้ Python ทำการตรวจสอบ Commit ล่าสุดใน Github เพื่อสั่งให้ Gitea ทำการ Sync ข้อมูลล่าสุดมา…]]></title>
            <link>https://medium.com/@chawalit.s/%E0%B9%83%E0%B8%8A%E0%B9%89-python-%E0%B8%97%E0%B8%B3%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B8%95%E0%B8%A3%E0%B8%A7%E0%B8%88%E0%B8%AA%E0%B8%AD%E0%B8%9A-commit-%E0%B8%A5%E0%B9%88%E0%B8%B2%E0%B8%AA%E0%B8%B8%E0%B8%94%E0%B9%83%E0%B8%99-github-%E0%B9%80%E0%B8%9E%E0%B8%B7%E0%B9%88%E0%B8%AD%E0%B8%AA%E0%B8%B1%E0%B9%88%E0%B8%87%E0%B9%83%E0%B8%AB%E0%B9%89-gitea-%E0%B8%97%E0%B8%B3%E0%B8%81%E0%B8%B2%E0%B8%A3-sync-%E0%B8%82%E0%B9%89%E0%B8%AD%E0%B8%A1%E0%B8%B9%E0%B8%A5%E0%B8%A5%E0%B9%88%E0%B8%B2%E0%B8%AA%E0%B8%B8%E0%B8%94%E0%B8%A1%E0%B8%B2-06a70239e6ed?source=rss-aab8035b6971------2</link>
            <guid isPermaLink="false">https://medium.com/p/06a70239e6ed</guid>
            <dc:creator><![CDATA[Chawalit Sengsang]]></dc:creator>
            <pubDate>Mon, 07 Sep 2026 15:03:50 GMT</pubDate>
            <atom:updated>2026-09-08T14:34:23.298Z</atom:updated>
            <content:encoded><![CDATA[<h3>ใช้ Python ทำการตรวจสอบ Commit ล่าสุดใน Github เพื่อสั่งให้ Gitea ทำการ Sync ข้อมูลล่าสุดมา เพื่อสั่ง Jenkins ทำงาน</h3><p>เวลาที่ต้องเจอ Server สำหรับ Production ที่ไม่สามารถ trigger ด้วย webhook หรือ runner อื่นๆจากภายนอกได้ จริงๆปัจจุบันก็มี Tool หลายตัวที่มาทำเรื่องแต่ด้วยนิสัยอยากทำเองเลยลองทำดู โดยมีไอเดียใช้ Python ทำการวิ่งไปเช็ค commit hash ระหว่าง GitHub และ Gitea ผ่าน Git CLI หรือ REST API จากนั้นสั่ง Gitea API ให้ทำการ Sync Mirror ทันที และส่ง webhook ไป Trigger Jenkins Pipeline</p><h3>หลักการทำงาน</h3><ol><li>ใช้ git ls-remote เช็ค remote hash ของ GitHub และ Gitea โดยตรงโดยไม่ต้อง clone repo ลงเครื่อง</li><li>ตรวจสอบ 4 branches: prod-storage, prod-common, prod-api, prod-back-offfice</li><li>หากมี branch ใด branch หนึ่ง hash ไม่ตรงกัน (GitHub มี commit ใหม่):</li></ol><ul><li>เรียก API ของ Gitea: POST /api/v1/repos/{owner}/{repo}/mirror-sync เพื่อสั่ง Sync ทันที</li><li>ส่ง HTTP Request ไปยัง Jenkins Remote Build Trigger URL</li></ul><pre>import subprocess<br>import time<br>import requests<br>import sys<br>from datetime import datetime<br># --- Configuration ---<br>CHECK_INTERVAL_SECONDS = 300  # ทุก 5 นาที<br><br># Repositories<br>GITHUB_REMOTE_URL = &quot;https://github.com/[Git Owner]/[Repo Name].git&quot;<br>GITEA_REMOTE_URL = &quot;https://gitea.example.com/[Git Owner]/[Repo Name].git&quot;<br># Target Branches<br>TARGET_BRANCHES = [<br>    &quot;prod-api&quot;,<br>    &quot;prod-front-office&quot;,<br>    &quot;prod-back-offfice&quot;<br>]<br># Gitea Config<br>GITEA_BASE_URL = &quot;[http://10.3.23.139:3000](https://gitea.example.com)&quot;<br>GITEA_OWNER = &quot;[Git Owner]&quot;<br>GITEA_REPO = &quot;[Repo Name]&quot;<br># สร้างได้ที่: Gitea Settings -&gt; Applications -&gt; Generate New Token<br>GITEA_TOKEN = &quot;[Your Gitea API Token]&quot;<br># Jenkins Config<br>JENKINS_URL = &quot;[Jenkins URL]&quot;<br>JENKINS_JOB_NAME = &quot;[Job name build]&quot;<br>JENKINS_USER = &quot;[Jenkins Username]&quot;<br>JENKINS_API_TOKEN = &quot;[Jenkins User API Token]&quot;<br>JENKINS_JOB_TOKEN = &quot;[Configured Job Auth Token]&quot;<br><br>def get_remote_hashes(remote_url: str) -&gt; dict[str, str]:<br>    &quot;&quot;&quot;ดึง HEAD commit hash ของแต่ละ branch โดยไม่ต้อง clone repo ลงเครื่อง&quot;&quot;&quot;<br>    cmd = [&quot;git&quot;, &quot;ls-remote&quot;, &quot;--heads&quot;, remote_url]<br>    res = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True)<br>    <br>    branch_map = {}<br>    for line in res.stdout.strip().splitlines():<br>        if not line:<br>            continue<br>        commit_hash, ref = line.split()<br>        branch_name = ref.replace(&quot;refs/heads/&quot;, &quot;&quot;)<br>        branch_map[branch_name] = commit_hash<br>    return branch_map<br><br>def trigger_gitea_sync() -&gt; bool:<br>    &quot;&quot;&quot;สั่ง Gitea Migration Mirror ให้ทำการ Sync ทันทีผ่าน REST API&quot;&quot;&quot;<br>    url = f&quot;{GITEA_BASE_URL}/api/v1/repos/{GITEA_OWNER}/{GITEA_REPO}/mirror-sync&quot;<br>    headers = {<br>        &quot;Authorization&quot;: f&quot;token {GITEA_TOKEN}&quot;,<br>        &quot;Accept&quot;: &quot;application/json&quot;<br>    }<br>    <br>    response = requests.post(url, headers=headers, timeout=30)<br>    if response.status_code in [200, 202]:<br>        return True<br>    print(f&quot;Failed to sync Gitea: {response.status_code} - {response.text}&quot;, file=sys.stderr)<br>    return False<br><br>def trigger_jenkins_build(updated_branches: list[str]) -&gt; bool:<br>    &quot;&quot;&quot;Trigger Jenkins pipeline พร้อมส่ง parameter branch ที่อัปเดต&quot;&quot;&quot;<br>    # กรณี Parameterized Build:<br>    url = f&quot;{JENKINS_URL}/job/{JENKINS_JOB_NAME}/buildWithParameters&quot;<br>    params = {<br>        &quot;token&quot;: JENKINS_JOB_TOKEN,<br>        &quot;UPDATED_BRANCHES&quot;: &quot;,&quot;.join(updated_branches)<br>    }<br>    <br>    # กรณี Non-parameterized build ให้เปลี่ยน URL เป็น:<br>    # url = f&quot;{JENKINS_URL}/job/{JENKINS_JOB_NAME}/build?token={JENKINS_JOB_TOKEN}&quot;<br>    # params = {}<br>    response = requests.post(<br>        url,<br>        params=params,<br>        auth=(JENKINS_USER, JENKINS_API_TOKEN),<br>        timeout=30<br>    )<br>    if response.status_code in [200, 201]:<br>        return True<br>    print(f&quot;Failed to trigger Jenkins: {response.status_code} - {response.text}&quot;, file=sys.stderr)<br>    return False<br><br>def check_and_process():<br>    now = datetime.now().strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)<br>    try:<br>        github_hashes = get_remote_hashes(GITHUB_REMOTE_URL)<br>        gitea_hashes = get_remote_hashes(GITEA_REMOTE_URL)<br>        updated_branches = []<br>        for branch in TARGET_BRANCHES:<br>            gh_hash = github_hashes.get(branch)<br>            gt_hash = gitea_hashes.get(branch)<br>            if gh_hash and gh_hash != gt_hash:<br>                print(f&quot;[{now}] Branch &#39;{branch}&#39; updated on GitHub ({gt_hash[:7] if gt_hash else &#39;None&#39;} -&gt; {gh_hash[:7]})&quot;)<br>                updated_branches.append(branch)<br>        if updated_branches:<br>            print(f&quot;[{now}] Updates detected on: {updated_branches}&quot;)<br>            <br>            # 1. สั่ง Gitea sync<br>            print(f&quot;[{now}] Triggering Gitea mirror-sync...&quot;)<br>            if trigger_gitea_sync():<br>                print(f&quot;[{now}] Gitea mirror-sync triggered successfully.&quot;)<br>                <br>                # รอให้ Gitea ดึง repo เสร็จสิ้นชั่วขณะก่อนบอก Jenkins<br>                time.sleep(15)<br>                # 2. ยิง webhook ไปยัง Jenkins<br>                print(f&quot;[{now}] Triggering Jenkins build...&quot;)<br>                if trigger_jenkins_build(updated_branches):<br>                    print(f&quot;[{now}] Jenkins build triggered successfully.&quot;)<br>        else:<br>            print(f&quot;[{now}] No changes detected on watched branches.&quot;)<br>    except Exception as e:<br>        print(f&quot;[{now}] Error: {str(e)}&quot;, file=sys.stderr)<br><br>def main():<br>    print(&quot;Starting GitHub-Gitea-Jenkins Sync Watcher...&quot;)<br>    while True:<br>        check_and_process()<br>        time.sleep(CHECK_INTERVAL_SECONDS)<br><br>if __name__ == &quot;__main__&quot;:<br>    main()</pre><h3>การตั้งค่าระบบที่เกี่ยวข้อง</h3><ol><li>Gitea API Token</li></ol><ul><li>เข้า Gitea ด้วย user ที่มีสิทธิ์ admin หรือสิทธิ์ write ใน repo ที่ต้องการ</li><li>ไปที่ Settings -&gt; Applications -&gt; Generate New Token ติ๊กสิทธิ์ repo แล้วนำ token มาใส่ใน</li></ul><p>2. GitHub Credentials (กรณี repo เป็น Private) GITEA_TOKEN</p><ul><li>หาก repo https://github.com/[Git Owner]/[Repo Name].git บน GitHub เป็น private ให้เพิ่ม Personal Access Token (PAT) ไว้ใน URL เช่น:</li></ul><pre>GITHUB_REMOTE_URL = &quot;https://&lt;Personal Access Token (PAT)&gt;@github.com/[Git Owner]/[Repo Name].git&quot;</pre><p>หรือคอนฟิก SSH key ไว้ในเครื่องที่รันสคริปต์ แล้วใช้ URL รูปแบบ <a href="/service/mailto:git@github.com">git@github.com</a>:…</p><p>3. Jenkins Remote Trigger</p><ul><li>ไปที่ Job ของ Jenkins -&gt; ติ๊ก Trigger builds remotely (e.g., from scripts)</li><li>ตั้งค่า Authentication Token แล้วนำค่านั้นมาใส่ใน JENKINS_JOB_TOKEN</li><li>ไปที่ User Profile ใน Jenkins -&gt; Configure -&gt; API Token -&gt; Add new Token นำมาใส่ใน JENKINS_API_TOKEN</li></ul><h3>รันเป็น Systemd Service</h3><p>ช่วยให้ระบบ auto-restart เองหาก script crash หรือเซิร์ฟเวอร์ reboot</p><ol><li>สร้างไฟล์ service definition:</li></ol><pre>sudo nano /etc/systemd/system/git-sync-watcher.service</pre><p>2. วางเนื้อหาคอนฟิก:</p><pre>[Unit]<br>Description=GitHub to Gitea Sync and Jenkins Trigger Service<br>After=network.target<br><br>[Service]<br>Type=simple<br>User=root<br>WorkingDirectory=/path/to/script_directory<br>ExecStart=/path/to/script_directory/venv/bin/python3 /path/to/script_directory/sync_mirror_and_trigger_jenkins.py<br>Restart=always<br>RestartSec=10<br><br>[Install]<br>WantedBy=multi-user.target</pre><p>3. สั่งเปิดและเริ่มทำงาน:</p><pre>sudo systemctl daemon-reload<br>sudo systemctl enable git-sync-watcher<br>sudo systemctl start git-sync-watcher<br><br># ดูสถานะและ log<br>sudo systemctl status git-sync-watcher<br>journalctl -u git-sync-watcher -f</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=06a70239e6ed" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[การติดตั้ง HA-NFS Cluster ด้วย DRBD บน RHEL 9]]></title>
            <link>https://medium.com/@chawalit.s/%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B8%95%E0%B8%B4%E0%B8%94%E0%B8%95%E0%B8%B1%E0%B9%89%E0%B8%87-ha-nfs-cluster-%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2-drbd-%E0%B8%9A%E0%B8%99-rhel-9-94c8d2345df0?source=rss-aab8035b6971------2</link>
            <guid isPermaLink="false">https://medium.com/p/94c8d2345df0</guid>
            <dc:creator><![CDATA[Chawalit Sengsang]]></dc:creator>
            <pubDate>Fri, 27 Feb 2026 14:34:50 GMT</pubDate>
            <atom:updated>2026-02-27T14:43:48.966Z</atom:updated>
            <content:encoded><![CDATA[<p>จะเป็นอธิบายขั้นตอนการตั้งค่า High Availability NFS ระหว่างโหนด nfs01 และ nfs02 โดยใช้ DRBD ในการ Sync ข้อมูล และ Pacemaker ในการควบคุม Failover</p><h3>1. การเตรียมระบบ (ทำทั้ง 2 เครื่อง)</h3><h4>1.1. ตั้งค่า Hostname และ /etc/hosts</h4><pre># บน nfs01<br>hostnamectl set-hostname nfs01<br># บน nfs02<br>hostnamectl set-hostname nfs02<br><br># แก้ไข /etc/hosts ทั้งสองเครื่อง<br>cat &gt;&gt; /etc/hosts &lt;&lt;EOF<br>192.168.1.20 nfs01<br>192.168.1.21 nfs02<br>EOF</pre><h4>1.2. ปิด SELinux และ Firewall</h4><pre>setenforce 0<br>sed -i &#39;s/SELINUX=enforcing/SELINUX=disabled/g&#39; /etc/selinux/config<br>firewall-cmd --permanent --add-service=high-availability<br>firewall-cmd --permanent --add-service=nfs<br>firewall-cmd --permanent --add-port=7788/tcp # DRBD<br>firewall-cmd --reload</pre><h3>2. ติดตั้งและตั้งค่า DRBD (ทำทั้ง 2 เครื่อง)</h3><h4>2.1 ติดตั้ง Packages</h4><pre>rpm --import [https://www.elrepo.org/RPM-GPG-KEY-elrepo.org](https://www.elrepo.org/RPM-GPG-KEY-elrepo.org)<br>dnf install -y [https://www.elrepo.org/elrepo-release-9.el9.elrepo.noarch.rpm](https://www.elrepo.org/elrepo-release-9.el9.elrepo.noarch.rpm)<br>dnf config-manager --set-enabled elrepo-extras<br>dnf config-manager --set-enabled elrepo-kernel<br>dnf install -y drbd9x-utils kmod-drbd9x</pre><h4>2.2 ตรวจสอบ Module และไฟล์คอนฟิก</h4><pre>modprobe drbd<br>lsmod | grep drbd<br>cat /etc/drbd.conf</pre><h4>2.3 กำหนดค่า Resource</h4><pre>cat &gt; /etc/drbd.d/nfsdata.res &lt;&lt;EOF<br>resource nfsdata {<br>    protocol C;<br>    device /dev/drbd0;<br>    meta-disk internal;<br>    on nfs01 {<br>        address 192.168.1.20:7788;<br>        disk /dev/mapper/rhel-nfs_data;<br>    }<br>    on nfs02 {<br>        address 192.168.1.21:7788;<br>        disk /dev/mapper/rhel-nfs_data;<br>    }<br>}<br>EOF</pre><h4>2.4 เริ่มการทำงาน (Initial Sync) ทำทั้ง 2 เครื่อง:</h4><pre>drbdadm down nfsdata<br>wipefs -a /dev/mapper/rhel-nfs_data<br>drbdadm create-md nfsdata (พิมพ์ yes)<br>drbdadm up nfsdata</pre><p>เฉพาะบน nfs01 (บังคับ Primary ครั้งแรก):</p><pre>drbdadm primary --force nfsdata<br>mkfs.xfs /dev/drbd0</pre><h3>3. ติดตั้ง Cluster Stack (ทำทั้ง 2 เครื่อง)</h3><pre>subscription-manager repos --enable=rhel-9-for-x86_64-highavailability-rpms<br>dnf install -y pacemaker pcs resource-agents<br>systemctl enable --now pcsd<br>echo &quot;Cluster@2024!&quot; | passwd --stdin hacluster</pre><h3>4. กำหนดค่า Cluster (ทำจาก nfs01)</h3><pre>pcs host auth nfs01 nfs02 -u hacluster -p Cluster@2024!<br>pcs cluster setup nfscluster nfs01 nfs02 --force<br>pcs cluster start --all<br>pcs cluster enable --all<br>pcs property set stonith-enabled=false<br>pcs property set no-quorum-policy=ignore</pre><h3>5. การสร้าง Resource (Final Fix)</h3><h4>5.1 ล้างสถานะ Error และสร้าง DRBD Promotable</h4><pre>pcs resource cleanup<br>pcs resource delete nfs_drbd --force<br>pcs resource create nfs_drbd ocf:linbit:drbd drbd_resource=nfsdata op monitor interval=20s<br>pcs resource promotable nfs_drbd promoted-max=1 promoted-node-max=1 clone-max=2 clone-node-max=1 meta notify=true</pre><h4>5.2 สร้าง NFS Group หมายเหตุ: ใช้ IP 192.168.1.100 เพื่อให้อยู่ในวงเดียวกับ Host</h4><pre>pcs resource delete g_nfs --force<br>mkdir -p /mnt/nfs_share<br>pcs resource create nfs_fs ocf:heartbeat:Filesystem device=&quot;/dev/drbd0&quot; directory=&quot;/mnt/nfs_share&quot; fstype=&quot;xfs&quot;<br>pcs resource create nfs_ip ocf:heartbeat:IPaddr2 ip=192.168.1.100 cidr_netmask=24<br>pcs resource create nfs_server ocf:heartbeat:nfsserver nfs_shared_infodir=&quot;/mnt/nfs_share/nfsinfo&quot; nfs_no_notify=true<br>pcs resource group add g_nfs nfs_fs nfs_ip nfs_server</pre><h4>5.3 ตั้งค่า Constraints</h4><pre>pcs constraint colocation add g_nfs with promoted nfs_drbd-clone INFINITY<br>pcs constraint order promote nfs_drbd-clone then start g_nfs</pre><h3>6. การตรวจสอบสถานะที่ถูกต้อง ก่อนใช้งานจริง ตรวจสอบให้แน่ใจว่า:</h3><ul><li>DRBD Sync เสร็จสิ้น: drbdadm status ต้องแสดง disk:UpToDate ทั้งสองฝั่ง และ replication:Established</li><li>สถานะ Resource: pcs status ต้องไม่มี Failed Actions (หากมีให้รัน pcs resource cleanup)</li><li>Mount Point: เครื่องที่เป็น Primary ต้องมองเห็น /mnt/nfs_share ผ่านคำสั่ง df -h</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=94c8d2345df0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[การติดตั้ง Kubernetes + Docker บน Ubuntu 24.04 LTS]]></title>
            <link>https://medium.com/@chawalit.s/%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B8%95%E0%B8%B4%E0%B8%94%E0%B8%95%E0%B8%B1%E0%B9%89%E0%B8%87-kubernetes-docker-%E0%B8%9A%E0%B8%99-ubuntu-24-04-lts-e63fc27f9dae?source=rss-aab8035b6971------2</link>
            <guid isPermaLink="false">https://medium.com/p/e63fc27f9dae</guid>
            <dc:creator><![CDATA[Chawalit Sengsang]]></dc:creator>
            <pubDate>Tue, 24 Feb 2026 10:26:26 GMT</pubDate>
            <atom:updated>2026-02-24T10:26:26.111Z</atom:updated>
            <content:encoded><![CDATA[<p>จะติดตั้ง Kubernetes Cluster โดยใช้ Docker Engine เป็น Container Runtime ผ่าน cri-dockerd โดยเน้นความยืดหยุ่นในการเลือกบทบาทของโหนด (Master หรือ Worker)</p><h3>1. การเตรียมระบบ (ทำทุก Node)ตั้งค่า Hostnameตรวจสอบให้แน่ใจว่าแต่ละ Node มีชื่อไม่ซ้ำกัน</h3><p>ตัวอย่างสำหรับ Master Node หลัก</p><pre>sudo hostnamectl set-hostname k8s-master-1</pre><p>ตัวอย่างสำหรับ Master Node เพิ่มเติม</p><pre>sudo hostnamectl set-hostname k8s-master-2</pre><p>ตัวอย่างสำหรับ Worker Node</p><pre>sudo hostnamectl set-hostname k8s-worker-1</pre><p>ตัวอย่างสำหรับ Worker Node เพิ่มเติม</p><pre>sudo hostnamectl set-hostname k8s-worker-2</pre><p>ปิด Swap เนื่องจาก Kubernetes จำเป็นต้องปิด Swap เพื่อการจัดการหน่วยความจำที่แม่นยำ</p><pre>sudo swapoff -a<br>sudo sed -i &#39;/ swap / s/^\(.*\)$/#\1/g&#39; /etc/fstab<br>ตั้งค่า Network สำหรับ Bridgingcat &lt;&lt;EOF | sudo tee /etc/modules-load.d/k8s.conf<br>overlay<br>br_netfilter<br>EOF<br><br>sudo modprobe overlay<br>sudo modprobe br_netfilter<br><br>cat &lt;&lt;EOF | sudo tee /etc/sysctl.d/k8s.conf<br>net.bridge.bridge-nf-call-iptables  = 1<br>net.bridge.bridge-nf-call-ip6tables = 1<br>net.ipv4.ip_forward                 = 1<br>EOF<br><br>sudo sysctl --system</pre><h3>2. ติดตั้ง Docker Engine (ทำทุก Node)</h3><pre>sudo rm -f /etc/apt/sources.list.d/docker.list<br><br>sudo apt update<br>sudo apt install -y ca-certificates curl gnupg<br>sudo install -m 0755 -d /etc/apt/keyrings<br><br>curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg<br>sudo chmod a+r /etc/apt/keyrings/docker.gpg<br><br>echo &quot;deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(. /etc/os-release &amp;&amp; echo &quot;$VERSION_CODENAME&quot;) stable&quot; | sudo tee /etc/apt/sources.list.d/docker.list &gt; /dev/null<br><br>sudo apt update<br>sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin<br>sudo systemctl enable --now docker</pre><h3>3. ติดตั้ง cri-dockerd (ทำทุก Node)</h3><pre>VER=$(curl -s https://api.github.com/repos/Mirantis/cri-dockerd/releases/latest | grep tag_name | cut -d &#39;&quot;&#39; -f 4 | sed &#39;s/v//g&#39;)<br>wget https://github.com/Mirantis/cri-dockerd/releases/download/v${VER}/cri-dockerd-${VER}.amd64.tgz<br>tar xvf cri-dockerd-${VER}.amd64.tgz<br>sudo mv cri-dockerd/cri-dockerd /usr/local/bin/</pre><pre>wget <a href="https://raw.githubusercontent.com/Mirantis/cri-dockerd/master/packaging/systemd/cri-docker.service">https://raw.githubusercontent.com/Mirantis/cri-dockerd/master/packaging/systemd/cri-docker.service</a><br>wget [https://raw.githubusercontent.com/Mirantis/cri-dockerd/master/packaging/systemd/cri-docker.socket](https://raw.githubusercontent.com/Mirantis/cri-dockerd/master/packaging/systemd/cri-docker.socket)<br>sudo mv cri-docker.service cri-docker.socket /etc/systemd/system/<br>sudo sed -i -e &#39;s,/usr/bin/cri-dockerd,/usr/local/bin/cri-dockerd,&#39; /etc/systemd/system/cri-docker.service</pre><pre>sudo systemctl daemon-reload<br>sudo systemctl enable --now cri-docker.socket<br>sudo systemctl enable --now cri-docker.service</pre><h3>4. ติดตั้ง Kubeadm, Kubelet และ Kubectl (ทำทุก Node)</h3><pre>sudo rm -f /etc/apt/sources.list.d/kubernetes.list<br>sudo apt update<br>sudo apt install -y apt-transport-https ca-certificates curl gpg<br>curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.30/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg<br>echo &quot;deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.30/deb/ /&quot; | sudo tee /etc/apt/sources.list.d/kubernetes.list<br><br>sudo apt update<br>sudo apt install -y kubelet kubeadm kubectl<br>sudo apt-mark hold kubelet kubeadm kubectl</pre><h3>5. เริ่มต้น Cluster (เฉพาะ Master Node ตัวแรก)</h3><p>สำคัญ: หากต้องการเพิ่ม Master ภายหลัง ต้องระบุ IP ของ Master</p><ol><li>ในช่อง control-plane-endpoint</li></ol><pre>sudo kubeadm init \<br>  --control-plane-endpoint &quot;IP_MASTER_1:6443&quot; \<br>  --upload-certs \<br>  --pod-network-cidr=10.244.0.0/16 \<br>  --cri-socket unix:///var/run/cri-dockerd.sock</pre><p>การตั้งค่า kubectl และแก้ปัญหา TLS Certificateหากพบ Error “tls: failed to verify certificate” ให้รันคำสั่งลบของเก่าแล้วคัดลอกใหม่ดังนี้:</p><ol><li>ลบไฟล์คอนฟิกเดิมที่ค้างอยู่</li></ol><pre>rm -rf $HOME/.kube</pre><p>2. สร้างโฟลเดอร์และคัดลอกไฟล์ใหม่</p><pre>mkdir -p $HOME/.kube<br>sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config<br>sudo chown $(id -u):$(id -g) $HOME/.kube/config</pre><p>3. เคลียร์ตัวแปรสภาพแวดล้อมที่อาจค้าง</p><pre>unset KUBECONFIG</pre><p>4. การ Join โหนดเข้าสู่ Clusterกรณีที่ 1: Join เป็น Worker Node</p><pre>sudo kubeadm join IP_MASTER_1:6443 --token &lt;token&gt; \<br>    --discovery-token-ca-cert-hash sha256:&lt;hash&gt; \<br>    --cri-socket unix:///var/run/cri-dockerd.sock</pre><p>กรณีที่ 2: Join เป็น Master Node (Control Plane)</p><pre>sudo kubeadm join IP_MASTER_1:6443 --token &lt;token&gt; \<br>    --discovery-token-ca-cert-hash sha256:&lt;hash&gt; \<br>    --control-plane --certificate-key &lt;key&gt; \<br>    --cri-socket unix:///var/run/cri-dockerd.sock</pre><ol><li>การยกเลิกและเริ่มใหม่ (Reset Node) ล้างค่าการตั้งค่าเดิม (บังคับ)</li></ol><pre>sudo kubeadm reset -f --cri-socket unix:///var/run/cri-dockerd.sock</pre><p>ล้างข้อมูลเน็ตเวิร์ก</p><pre>sudo rm -rf /etc/cni/net.d<br>sudo rm -rf $HOME/.kube/config</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e63fc27f9dae" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ตัวอย่างการแบ่ง Partition สำหรับ Ubuntu อ้างอิงขนาด HDD 100GB]]></title>
            <link>https://medium.com/@chawalit.s/%E0%B8%95%E0%B8%B1%E0%B8%A7%E0%B8%AD%E0%B8%A2%E0%B9%88%E0%B8%B2%E0%B8%87%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B9%81%E0%B8%9A%E0%B9%88%E0%B8%87-partition-%E0%B8%AA%E0%B8%B3%E0%B8%AB%E0%B8%A3%E0%B8%B1%E0%B8%9A-ubuntu-%E0%B8%AD%E0%B9%89%E0%B8%B2%E0%B8%87%E0%B8%AD%E0%B8%B4%E0%B8%87%E0%B8%82%E0%B8%99%E0%B8%B2%E0%B8%94-hdd-100gb-6c7019229c99?source=rss-aab8035b6971------2</link>
            <guid isPermaLink="false">https://medium.com/p/6c7019229c99</guid>
            <dc:creator><![CDATA[Chawalit Sengsang]]></dc:creator>
            <pubDate>Tue, 17 Feb 2026 12:08:06 GMT</pubDate>
            <atom:updated>2026-02-17T13:40:59.026Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/896/1*oYiAlcgXEshxd90tOpdPnQ.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6c7019229c99" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[วิธีการติดตั้ง Kubernetes บน Redhat Enterprise 9.7]]></title>
            <link>https://medium.com/@chawalit.s/%E0%B8%A7%E0%B8%B4%E0%B8%98%E0%B8%B5%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B8%95%E0%B8%B4%E0%B8%94%E0%B8%95%E0%B8%B1%E0%B9%89%E0%B8%87-kubernetes-%E0%B8%9A%E0%B8%99-redhat-enterprise-9-7-bc723f76a410?source=rss-aab8035b6971------2</link>
            <guid isPermaLink="false">https://medium.com/p/bc723f76a410</guid>
            <dc:creator><![CDATA[Chawalit Sengsang]]></dc:creator>
            <pubDate>Fri, 09 Jan 2026 03:22:59 GMT</pubDate>
            <atom:updated>2026-01-09T03:22:59.829Z</atom:updated>
            <content:encoded><![CDATA[<ol><li>เตรียมระบบพื้นฐาน (ทุก Node) 1.1. ปิด Swap และตั้งค่า SELinux: Kubernetes จะไม่ทำงานหากเปิด Swap และ SELinux อาจบล็อกการทำงานของ Container (ในขั้นตอนเริ่มต้นแนะนำให้เป็น Permissive)</li></ol><pre># ปิด Swap<br>sudo swapoff -a<br>sudo sed -i &#39;/ swap / s/^/#/&#39; /etc/fstab<br><br># ตั้งค่า SELinux เป็น Permissive<br>sudo setenforce 0<br>sudo sed -i &#39;s/^SELINUX=enforcing$/SELINUX=permissive/&#39; /etc/selinux/config</pre><p>1.2. โหลด Kernel Modules และตั้งค่า Network:</p><pre>cat &lt;&lt;EOF | sudo tee /etc/modules-load.d/k8s.conf<br>overlay<br>br_netfilter<br>EOF<br>sudo modprobe overlay<br>sudo modprobe br_netfilter<br><br># ตั้งค่า sysctl<br>cat &lt;&lt;EOF | sudo tee /etc/sysctl.d/k8s.conf<br>net.bridge.bridge-nf-call-iptables  = 1<br>net.bridge.bridge-nf-call-ip6tables = 1<br>net.ipv4.ip_forward                 = 1<br>EOF<br><br>sudo sysctl --system</pre><ol><li>ติดตั้ง Docker Engine และ cri-dockerd (ทุก Node) 2.1. เพิ่ม Docker Repository และติดตั้ง:</li></ol><pre>sudo yum install -y yum-utils<br>sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.reposudo yum install -y docker-ce docker-ce-cli containerd.io<br>sudo systemctl enable --now docker</pre><p>2.2. ติดตั้ง cri-dockerd (เนื่องจาก K8s v1.24+ ไม่รองรับ Docker โดยตรง): สำหรับ RHEL 9 ให้ใช้แพ็กเกจของ el9</p><pre># ดึงเวอร์ชันล่าสุด<br>VER=$(curl -s https://api.github.com/repos/Mirantis/cri-dockerd/releases/latest | grep tag_name | cut -d &#39;&quot;&#39; -f 4 | sed &#39;s/v//g&#39;)<br>wget https://github.com/Mirantis/cri-dockerd/releases/download/v${VER}/cri-dockerd-${VER}.amd64.tgz<br><br># แตกไฟล์และย้ายไปที่ตำแหน่งระบบ<br>tar xzwf cri-dockerd-${VER}.amd64.tgz<br>sudo mv cri-dockerd/cri-dockerd /usr/local/bin/<br>sudo chmod +x /usr/local/bin/cri-dockerd</pre><p>2.3. สร้าง Systemd Unit Files (Socket และ Service)</p><pre>cat &lt;&lt;EOF | sudo tee /etc/systemd/system/cri-docker.socket<br>[Unit]<br>Description=cri-docker: Docker Application Container Engine API [Socket]<br><br>[Socket]<br>ListenStream=/var/run/cri-dockerd.sock<br><br>[Install]<br>WantedBy=sockets.target<br>EOF</pre><p>2.4. สร้างไฟล์ Service:</p><pre>cat &lt;&lt;EOF | sudo tee /etc/systemd/system/cri-docker.service<br>[Unit]<br>Description=cri-docker: Docker Application Container Engine API<br>Documentation=https://docs.mirantis.com<br>After=network-online.target firewalld.service docker.service<br>Wants=network-online.target<br>Requires=cri-docker.socket<br><br>[Service]<br>Type=notify<br>ExecStart=/usr/local/bin/cri-dockerd --container-runtime-endpoint fd://<br>ExecReload=/bin/kill -s HUP \$MAINPID<br>TimeoutSec=0<br>RestartSec=2<br>Restart=always<br><br>[Install]<br>WantedBy=multi-user.target<br>EOF</pre><ol><li>ติดตั้ง Kubernetes Tools (ทุก Node) เพิ่ม Repository ของ Kubernetes (เวอร์ชัน 1.31 เป็นต้นไปใช้ URL ใหม่):</li></ol><pre>cat &lt;&lt;EOF | sudo tee /etc/yum.repos.d/kubernetes.repo<br>[kubernetes]<br>name=Kubernetes<br>baseurl=https://pkgs.k8s.io/core:/stable:/v1.31/rpm/<br>enabled=1<br>gpgcheck=1<br>gpgkey=https://pkgs.k8s.io/core:/stable:/v1.31/rpm/repodata/repomd.xml.key<br>EOF<br><br>sudo yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes<br>sudo systemctl enable --now kubelet</pre><p>ติดตั้งแพ็กเกจเสริมที่จำเป็น (ป้องกัน Error เหมือนที่เจอใน Ubuntu):</p><pre>sudo yum install -y conntrack-tools socat iproute-tc</pre><ol><li>เปิดพอร์ตบน Firewall (ทุก Node) RHEL 9 เปิด Firewall เป็นค่าเริ่มต้น หากไม่เปิดพอร์ต Cluster จะคุยกันไม่ได้:</li></ol><ul><li>Master Node:</li></ul><pre>sudo firewall-cmd --permanent --add-port={6443,2379-2380,10250,10251,10252}/tcp<br>sudo firewall-cmd --reload</pre><ul><li>Worker Node:</li></ul><pre>sudo firewall-cmd --permanent --add-port={10250,30000-32767}/tcp<br>sudo firewall-cmd --reload</pre><ol><li>เริ่มต้น Cluster (เฉพาะ Master Node) รันคำสั่ง Init โดยระบุ cri-socket ของ Docker:</li></ol><pre>sudo kubeadm init \<br>  --pod-network-cidr=10.244.0.0/16 \<br>  --cri-socket unix:///var/run/cri-dockerd.sock</pre><p>หลังรันเสร็จ ให้ตั้งค่าสิทธิ์ Access:</p><pre>mkdir -p $HOME/.kube<br>sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config<br>sudo chown $(id -u):$(id -g) $HOME/.kube/config</pre><ol><li>ติดตั้ง Network Plugin และ Join Worker 6.1 ติดตั้ง Flannel (ที่ Master):</li></ol><pre>kubectl apply -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml</pre><p>6.2 Join Worker Node: นำคำสั่งที่ได้จาก Master มาเติม — cri-socket แล้วรันที่เครื่อง Worker:</p><pre>sudo kubeadm join &lt;MASTER_IP&gt;:6443 --token &lt;TOKEN&gt; \<br>  --discovery-token-ca-cert-hash sha256:&lt;HASH&gt; \<br>  --cri-socket unix:///var/run/cri-dockerd.sock</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bc723f76a410" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ติดตั้ง PostgreSQL High Availability (HA) แบบ Active-Standby บน Redhat 9]]></title>
            <link>https://medium.com/@chawalit.s/%E0%B8%95%E0%B8%B4%E0%B8%94%E0%B8%95%E0%B8%B1%E0%B9%89%E0%B8%87-postgresql-high-availability-ha-%E0%B9%81%E0%B8%9A%E0%B8%9A-active-standby-%E0%B8%9A%E0%B8%99-redhat-9-ac5068c28e9d?source=rss-aab8035b6971------2</link>
            <guid isPermaLink="false">https://medium.com/p/ac5068c28e9d</guid>
            <dc:creator><![CDATA[Chawalit Sengsang]]></dc:creator>
            <pubDate>Thu, 08 Jan 2026 02:51:45 GMT</pubDate>
            <atom:updated>2026-01-08T02:51:45.950Z</atom:updated>
            <content:encoded><![CDATA[<h3>ทำทั้ง 2 เครื่อง</h3><ol><li>ขั้นตอนการเตรียมระบบ (ทำทั้ง 2 เครื่อง)</li></ol><pre>sudo su<br>dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-$(rpm -E %rhel)-x86_64/pgdg-redhat-repo-latest.noarch.rpm<br>dnf -qy module disable postgresql<br>dnf install -y postgresql17-server</pre><ol><li>ตั้งค่า Firewall: เปิดพอร์ต 5432 ให้ทั้งสองเครื่องคุยกันได้</li></ol><pre>firewall-cmd --permanent --add-port=5432/tcp<br>firewall-cmd --reload</pre><h3>การตั้งค่าเครื่องหลัก (Active/Primary)</h3><ol><li>Initialize Database:</li></ol><pre>/usr/pgsql-17/bin/postgresql-17-setup initdb</pre><ol><li>แก้ไข postgresql.conf: เพื่อให้เครื่องอื่นเชื่อมต่อและส่งข้อมูล Replication ได้</li></ol><pre>nano /var/lib/pgsql/17/data/postgresql.conf</pre><ul><li>แก้ไขบรรทัดเหล่านี้:</li></ul><pre>listen_addresses = &#39;*&#39;<br>...<br>wal_level = replica<br>...<br>max_wal_senders = 10<br>...<br>max_replication_slots = 10</pre><ol><li>แก้ไข pg_hba.conf: อนุญาตให้เครื่อง Standby เข้ามาขอข้อมูล (สมมติ IP Standby คือ 192.178.1.100)</li></ol><pre>nano /var/lib/pgsql/17/data/pg_hba.conf</pre><ul><li>เพิ่มบรรทัดนี้ที่ท้ายไฟล์: host replication repuser 192.178.1.100/32 scram-sha-256</li></ul><ol><li>เริ่มการทำงานและสร้าง User สำหรับ Replication:</li></ol><pre>systemctl enable --now postgresql-17<br>sudo -i -u postgres psql -c &quot;CREATE USER repuser REPLICATION LOGIN CONNECTION LIMIT 10 ENCRYPTED PASSWORD &#39;YourPassword&#39;;&quot;</pre><h3>การตั้งค่าเครื่องสำรอง (Standby)</h3><ul><li>สำคัญ: ห้ามทำการ initdb ในเครื่อง Standby แต่เราจะทำการ &quot;Clone&quot; ข้อมูลมาจากเครื่องหลัก</li></ul><ol><li>หยุดการทำงานของ Postgres (ถ้ามี):</li></ol><pre>systemctl stop postgresql-17</pre><ol><li>ล้างข้อมูลเก่าใน Data Directory:</li></ol><pre>rm -rf /var/lib/pgsql/17/data/*</pre><ol><li>ทำการดึงข้อมูลจากเครื่อง Active (Base Backup): (สมมติ IP เครื่อง Active คือ 192.178.1.99)</li></ol><pre>sudo -i -u postgres pg_basebackup -h 192.178.1.99 -D /var/lib/pgsql/17/data/ -U repuser -P -v -R --wal-method=stream</pre><ul><li>-R: จะสร้างไฟล์ standby.signal และตั้งค่าการเชื่อมต่อให้โดยอัตโนมัติ</li></ul><ol><li>เริ่มการทำงานของ Standby:</li></ol><pre>systemctl enable --now postgresql-17</pre><h3>วิธีการตรวจสอบสถานะ (Check Replication)</h3><ol><li>ที่เครื่อง Active (Primary): รันคำสั่งนี้เพื่อดูว่ามี Standby เชื่อมต่ออยู่หรือไม่:</li></ol><pre>sudo -i -u postgres psql -c &quot;select * from pg_stat_replication;&quot;</pre><ol><li>ที่เครื่อง Standby: รันคำสั่งนี้เพื่อดูว่าเป็นโหมด Read-only หรือไม่:</li></ol><pre>sudo -i -u postgres psql -c &quot;select pg_is_in_recovery();&quot;</pre><ul><li>(ถ้าได้ค่า t แสดงว่าเป็น Standby สำเร็จ)</li></ul><h3>(Optional) ตั้งค่าให้เชื่อมต่อระยะไกล</h3><ol><li>แก้ไขไฟล์</li></ol><pre>nano /var/lib/pgsql/17/data/pg_hba.conf</pre><ol><li>ใส่คำสั่งต่อไปนี้ๆว้ในบรรทัดสุดท้าย</li></ol><pre>host    all             all             0.0.0.0/0               md5</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ac5068c28e9d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[วิธีตั้งตั้ง และตั้งค่า NFS แบบ Active/Passive บน Redhat Enterprise 9.7]]></title>
            <link>https://medium.com/@chawalit.s/%E0%B8%A7%E0%B8%B4%E0%B8%98%E0%B8%B5%E0%B8%95%E0%B8%B1%E0%B9%89%E0%B8%87%E0%B8%95%E0%B8%B1%E0%B9%89%E0%B8%87-%E0%B9%81%E0%B8%A5%E0%B8%B0%E0%B8%95%E0%B8%B1%E0%B9%89%E0%B8%87%E0%B8%84%E0%B9%88%E0%B8%B2-nfs-%E0%B9%81%E0%B8%9A%E0%B8%9A-active-passive-%E0%B8%9A%E0%B8%99-redhat-enterprise-9-7-0bfbb8d20d70?source=rss-aab8035b6971------2</link>
            <guid isPermaLink="false">https://medium.com/p/0bfbb8d20d70</guid>
            <dc:creator><![CDATA[Chawalit Sengsang]]></dc:creator>
            <pubDate>Thu, 08 Jan 2026 02:50:30 GMT</pubDate>
            <atom:updated>2026-01-08T02:50:30.524Z</atom:updated>
            <content:encoded><![CDATA[<ol><li>เตรียม Disk บน Node ใด Node หนึ่ง (เช่น nfs-01)</li><li>ปิดการใช้งาน NFS Service ปกติ (ทำทั้ง 2 Nodes)</li><li>สร้าง Resource Group บน Pacemaker (ทำที่ nfs-01)</li><li>ตรวจสอบสถานะ</li></ol><h3>1. เตรียม Disk บน Node ใด Node หนึ่ง (เช่น nfs-01)</h3><p>สมมติว่า Shared Disk ของคุณคือ /dev/sdb (ตรวจสอบด้วยคำสั่ง lsblk) 1.1. สร้าง Partition และ Format (ทำที่ nfs-01 เท่านั้น):</p><pre>mkfs.xfs /dev/sdb1</pre><p>1.2. สร้างจุด Mount Point (ทำทั้ง 2 Nodes):</p><pre>mkdir -p /data/nfs</pre><p>1.3. สร้างโฟลเดอร์สำหรับเก็บ Metadata ของ NFS (ทำที่ nfs-01): เพื่อให้สถานะการ Lock ไฟล์ของ NFS ต่อเนื่องเวลาเกิด Failover เราต้องมีที่เก็บข้อมูลส่วนนี้บน Shared Disk</p><pre>mount /dev/sdb1 /data/nfs<br>mkdir /data/nfs/nfsinfo<br>umount /data/nfs</pre><h3>2. ปิดการใช้งาน NFS Service ปกติ (ทำทั้ง 2 Nodes)</h3><p>เราต้องป้องกันไม่ให้ OS เรียก NFS ขึ้นมาเองตอน Boot แต่จะให้ Pacemaker เป็นคนสั่งรัน:</p><pre>systemctl disable --now nfs-server</pre><h3>3. ปิดการใช้งาน NFS Service ปกติ (ทำทั้ง 2 Nodes)</h3><pre>systemctl disable --now nfs-server</pre><h3>4. สร้าง Resource Group บน Pacemaker (ทำที่ nfs-01)</h3><pre># 1. ปิด STONITH ชั่วคราว (หากยังไม่มี Fencing Hardware)<br>pcs property set stonith-enabled=false</pre><pre># 2. สร้าง Virtual IP (เปลี่ยน IP เป็นเลขที่ว่างในวงของคุณ)<br>pcs resource create VirtualIP ocf:heartbeat:IPaddr2 ip=192.168.1.100 cidr_netmask=24 op monitor interval=30s</pre><pre># 3. สร้าง Resource สำหรับ Mount Disk<br>pcs resource create NFS_FS ocf:heartbeat:Filesystem device=&quot;/dev/sdb1&quot; directory=&quot;/data/nfs&quot; fstype=&quot;xfs&quot;</pre><pre># 4. สร้าง Resource สำหรับ NFS Server<br>pcs resource create NFS_Server ocf:heartbeat:nfsserver nfs_shared_infodir=&quot;/data/nfs/nfsinfo&quot; nfs_no_notify=true</pre><pre># 5. รวมเข้ากลุ่มเดียวกันชื่อ &quot;nfsgroup&quot;<br>pcs resource group add nfsgroup VirtualIP NFS_FS NFS_Server</pre><h3>4. ตรวจสอบสถานะ</h3><pre>pcs status</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0bfbb8d20d70" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[วิธีติดตั้ง Kubernetes บน Unbutu server 22.04LTS]]></title>
            <link>https://medium.com/@chawalit.s/%E0%B8%A7%E0%B8%B4%E0%B8%98%E0%B8%B5%E0%B8%95%E0%B8%B4%E0%B8%94%E0%B8%95%E0%B8%B1%E0%B9%89%E0%B8%87-kubernetes-%E0%B8%9A%E0%B8%99-unbutu-server-24-04lts-fd7024527b7f?source=rss-aab8035b6971------2</link>
            <guid isPermaLink="false">https://medium.com/p/fd7024527b7f</guid>
            <dc:creator><![CDATA[Chawalit Sengsang]]></dc:creator>
            <pubDate>Wed, 24 Dec 2025 06:29:27 GMT</pubDate>
            <atom:updated>2025-12-24T06:34:31.338Z</atom:updated>
            <content:encoded><![CDATA[<ol><li>เตรียมระบบพื้นฐาน (ทุก Node) Kubernetes ต้องการการตั้งค่าระบบที่เฉพาะเจาะจงเพื่อให้เน็ตเวิร์กและหน่วยความจำทำงานได้ถูกต้อง</li></ol><ul><li>ปิด Swap (จำเป็นมาก):</li></ul><pre>sudo swapoff -a<br>sudo sed -i &#39;/ swap / s/^/#/&#39; /etc/fstab</pre><ul><li>โหลด Kernel Modules ที่จำเป็น:</li></ul><pre>cat &lt;&lt;EOF | sudo tee /etc/modules-load.d/k8s.conf<br>overlay<br>br_netfilter<br>EOF<br>sudo modprobe overlay<br>sudo modprobe br_netfilter</pre><ul><li>ตั้งค่า Network Forwarding:</li></ul><pre>cat &lt;&lt;EOF | sudo tee /etc/sysctl.d/k8s.conf<br>net.bridge.bridge-nf-call-iptables  = 1<br>net.bridge.bridge-nf-call-ip6tables = 1<br>net.ipv4.ip_forward                 = 1<br>EOF<br>sudo sysctl --system</pre><p>2. ติดตั้ง Docker Engine (ทุก Node) เราจะติดตั้ง Docker เพื่อเป็น Container Runtime หลัก</p><pre>sudo apt update<br>sudo apt install -y docker.io<br>sudo systemctl enable --now docker</pre><p>3. ติดตั้ง cri-dockerd (ทุก Node)</p><ul><li>ดาวน์โหลด Binary</li></ul><pre>wget https://github.com/Mirantis/cri-dockerd/releases/download/v0.3.16/cri-dockerd-0.3.16.amd64.tgz<br>tar -xvf cri-dockerd-0.3.16.amd64.tgz</pre><ul><li>ย้ายไปที่ /usr/local/bin</li></ul><pre>sudo mv cri-dockerd/cri-dockerd /usr/local/bin/</pre><ul><li>ดาวน์โหลดไฟล์ Service จากต้นฉบับ</li></ul><pre>wget https://raw.githubusercontent.com/Mirantis/cri-dockerd/master/packaging/systemd/cri-docker.service<br>wget https://raw.githubusercontent.com/Mirantis/cri-dockerd/master/packaging/systemd/cri-docker.socket<br>sudo mv cri-docker.service cri-docker.socket /etc/systemd/system/<br>sudo sed -i -e &#39;s,/usr/bin/cri-dockerd,/usr/local/bin/cri-dockerd,&#39; /etc/systemd/system/cri-docker.service</pre><ul><li>เริ่มระบบ</li></ul><pre>sudo systemctl daemon-reload<br>sudo systemctl enable --now cri-docker.socket</pre><p>4. ติดตั้ง Kubeadm, Kubelet และ Kubectl (ทุก Node)</p><pre>sudo apt update &amp;&amp; sudo apt install -y apt-transport-https ca-certificates curl gpg</pre><ul><li>เพิ่ม Kubernetes GPG Key และ Repository</li></ul><pre>sudo mkdir -p /etc/apt/keyrings<br>curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.31/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg<br>echo &#39;deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.31/deb/ /&#39; | sudo tee /etc/apt/sources.list.d/kubernetes.list</pre><ul><li>ติดตั้งเครื่องมือ</li></ul><pre>sudo apt update<br>sudo apt install -y kubelet kubeadm kubectl<br>sudo apt-mark hold kubelet kubeadm kubectl</pre><p>5. เริ่มต้น Cluster (เฉพาะ Master Node)</p><ul><li>ติดตั้ง conntrack</li></ul><pre>sudo apt update<br>sudo apt install -y conntrack</pre><ul><li>(Optional) ตรวจสอบแพ็กเกจที่จำเป็นอื่นๆ</li></ul><pre>sudo apt install -y socat ebtables ipset</pre><ul><li>ติดตั้ง Kubeadm, Kubelet และ Kubectl (ทุก Node) คำสั่งนี้จะทำการสร้าง Control Plane โดยเราต้องระบุ Socket ของ cri-dockerd ลงไปเพื่อให้มันรู้ว่าต้องคุยกับ Docker</li></ul><pre>sudo kubeadm init \<br>  --pod-network-cidr=10.244.0.0/16 \<br>  --cri-socket unix:///var/run/cri-dockerd.sock</pre><ul><li>ระบบจะให้คำสั่งสำหรับสร้างโฟลเดอร์ .kube (รันใน user ปกติ):</li></ul><pre>mkdir -p $HOME/.kube<br>sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config<br>sudo chown $(id -u):$(id -g) $HOME/.kube/config</pre><p>6. ติดตั้ง Network Plugin (CNI)</p><pre>kubectl apply -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml</pre><p>7. เพิ่ม Worker Node (เฉพาะ Worker) นำคำสั่ง kubeadm join ที่ได้จากหน้าจอตอนรัน init ในขั้นตอนที่ 5 มาวางในเครื่อง Worker แต่ ต้องเพิ่ม --cri-socket ต่อท้ายด้วย:</p><ul><li>ติดตั้งสิ่งจำเป็นบน Worker Node</li></ul><pre>sudo apt update<br>sudo apt install -y conntrack socat ebtables ipset</pre><ul><li>Joint Worker Node</li></ul><pre>sudo kubeadm join &lt;MASTER_IP&gt;:6443 --token &lt;TOKEN&gt; \<br>    --discovery-token-ca-cert-hash sha256:&lt;HASH&gt; \<br>    --cri-socket unix:///var/run/cri-dockerd.sock</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fd7024527b7f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ติดตั้ง PostgreSQL High Availability (HA) แบบ Active-Standby บน Redhat 9]]></title>
            <link>https://medium.com/@chawalit.s/%E0%B8%95%E0%B8%B4%E0%B8%94%E0%B8%95%E0%B8%B1%E0%B9%89%E0%B8%87-postgresql-high-availability-ha-%E0%B9%81%E0%B8%9A%E0%B8%9A-active-standby-%E0%B8%9A%E0%B8%99-redhat-9-8d9003b7ece1?source=rss-aab8035b6971------2</link>
            <guid isPermaLink="false">https://medium.com/p/8d9003b7ece1</guid>
            <dc:creator><![CDATA[Chawalit Sengsang]]></dc:creator>
            <pubDate>Tue, 23 Dec 2025 10:09:51 GMT</pubDate>
            <atom:updated>2025-12-23T10:09:51.767Z</atom:updated>
            <content:encoded><![CDATA[<h3>ทำทั้ง 2 เครื่อง</h3><ol><li>ขั้นตอนการเตรียมระบบ (ทำทั้ง 2 เครื่อง)</li></ol><pre>sudo su<br>dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-$(rpm -E %rhel)-x86_64/pgdg-redhat-repo-latest.noarch.rpm<br>dnf -qy module disable postgresql<br>dnf install -y postgresql16-server</pre><p>2. ตั้งค่า Firewall: เปิดพอร์ต 5432 ให้ทั้งสองเครื่องคุยกันได้</p><pre>firewall-cmd --permanent --add-port=5432/tcp<br>firewall-cmd --reload</pre><h3>การตั้งค่าเครื่องหลัก (Active/Primary)</h3><ol><li>Initialize Database:</li></ol><pre>/usr/pgsql-16/bin/postgresql-16-setup initdb</pre><p>2. แก้ไข postgresql.conf: เพื่อให้เครื่องอื่นเชื่อมต่อและส่งข้อมูล Replication ได้</p><pre>nano /var/lib/pgsql/16/data/postgresql.conf</pre><ul><li>แก้ไขบรรทัดเหล่านี้:</li></ul><pre>listen_addresses = &#39;*&#39;<br>...<br>wal_level = replica<br>...<br>max_wal_senders = 10<br>...<br>max_replication_slots = 10</pre><p>3. แก้ไข pg_hba.conf: อนุญาตให้เครื่อง Standby เข้ามาขอข้อมูล (สมมติ IP Standby คือ 192.168.1.100)</p><pre>nano /var/lib/pgsql/16/data/pg_hba.conf</pre><ul><li>เพิ่มบรรทัดนี้ที่ท้ายไฟล์: host replication repuser 192.168.1.100/32 scram-sha-256</li></ul><p>4. เริ่มการทำงานและสร้าง User สำหรับ Replication:</p><pre>systemctl enable --now postgresql-16<br>sudo -i -u postgres psql -c &quot;CREATE USER repuser REPLICATION LOGIN CONNECTION LIMIT 10 ENCRYPTED PASSWORD &#39;YourPassword&#39;;&quot;</pre><h3>การตั้งค่าเครื่องสำรอง (Standby)</h3><ul><li>สำคัญ: ห้ามทำการ initdb ในเครื่อง Standby แต่เราจะทำการ &quot;Clone&quot; ข้อมูลมาจากเครื่องหลัก</li></ul><ol><li>หยุดการทำงานของ Postgres (ถ้ามี):</li></ol><pre>systemctl stop postgresql-16</pre><p>2. ล้างข้อมูลเก่าใน Data Directory:</p><pre>rm -rf /var/lib/pgsql/16/data/*</pre><p>3. ทำการดึงข้อมูลจากเครื่อง Active (Base Backup): (สมมติ IP เครื่อง Active คือ 192.168.1.99)</p><pre>sudo -i -u postgres pg_basebackup -h 192.168.1.99 -D /var/lib/pgsql/16/data/ -U repuser -P -v -R --wal-method=stream</pre><ul><li>-R: จะสร้างไฟล์ standby.signal และตั้งค่าการเชื่อมต่อให้โดยอัตโนมัติ</li></ul><p>4. เริ่มการทำงานของ Standby:</p><pre>systemctl enable --now postgresql-16</pre><h3>วิธีการตรวจสอบสถานะ (Check Replication)</h3><ol><li>ที่เครื่อง Active (Primary): รันคำสั่งนี้เพื่อดูว่ามี Standby เชื่อมต่ออยู่หรือไม่:</li></ol><pre>sudo -i -u postgres psql -c &quot;select * from pg_stat_replication;&quot;</pre><p>2. ที่เครื่อง Standby: รันคำสั่งนี้เพื่อดูว่าเป็นโหมด Read-only หรือไม่:</p><pre>sudo -i -u postgres psql -c &quot;select pg_is_in_recovery();&quot;</pre><ul><li>(ถ้าได้ค่า t แสดงว่าเป็น Standby สำเร็จ)</li></ul><h3>(Optional) ตั้งค่าให้เชื่อมต่อระยะไกล</h3><ol><li>แก้ไขไฟล์</li></ol><pre>nano /var/lib/pgsql/16/data/pg_hba.conf</pre><p>2. ใส่คำสั่งต่อไปนี้ๆว้ในบรรทัดสุดท้าย</p><pre>host    all             all             0.0.0.0/0               md5</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8d9003b7ece1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>