零停機遷移 Postgres的正確方式

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在這篇博文中,我們會介紹如何在零停機時間的前提下,使用Bucardo將Postgres數據庫遷移到一個新實例上。我們將介紹如何避免常見的陷阱,比如數據丟失、性能下降和數據完整性故障等。我們已成功使用這一流程將我們的Postgres數據庫從9.5版遷移到Amazon RDS上的12.5版,但該流程不只適用於RDS,也不依賴AWS獨有的任何內容。這種遷移策略應該能適用於任何自託管或託管的Postgres。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"分析"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在本文中,我們將討論將多個Web應用程序(如微服務)從一個數據庫遷移到另一個的過程。現代軟件架構由多個應用程序(或微服務)組成,而每個應用程序都有多個運行實例以增強擴展性。爲了將你的應用程序移動到新的數據庫,你必須首先確保兩個數據庫中的數據是同步的,並在任何給定時間點保持同步,否則你的客戶端遲早會丟失數據,甚至陷入無效狀態。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一個簡單的解決方案是停止舊數據庫的寫入操作,獲取快照,將其恢復到新的數據庫,然後在新數據庫中恢復操作。這種方案需要的停機時間太久,不適合生產環境。我們提到這一點只是爲了做參考,因爲這是確保你不會丟失任何數據的最簡單方法,但用它的話,你可能會失去一些客戶。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"更現實的方法是在兩個數據庫之間設置一個近乎實時的雙向複製,這樣在理想情況下,應用程序可以同時向兩者讀取和寫入,而不會注意到任何差異。你可以用這種方法一次一個實例地逐步移動你的應用程序,過程中不會停機,且不會影響用戶。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"由於我們希望應用程序能寫入兩個數據庫,我們需要進行多主複製(multi-master replication)。在谷歌上搜索“Postgres中的多主複製”可以找到大量"},{"type":"link","attrs":{"href":"https:\/\/www.percona.com\/blog\/2020\/06\/09\/multi-master-replication-solutions-for-postgresql\/","title":"","type":null},"content":[{"type":"text","text":"解決方案"}]},{"type":"text","text":",每種方案都有自己需要注意的優缺點。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們決定繼續使用"},{"type":"link","attrs":{"href":"https:\/\/bucardo.org\/Bucardo\/","title":"","type":null},"content":[{"type":"text","text":"Bucardo"}]},{"type":"text","text":",因爲它開源、速度快,並且提供了簡單的監控和衝突解決機制。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Bucardo的工作機制"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Bucardo充當兩個Postgres實例之間的中間人。你可以讓Bucardo在你喜歡的任何機器上運行,只要它可以訪問源數據庫和目標數據庫即可。安裝並設置多主複製後,Bucardo將爲你選擇複製的所有表添加一些"},{"type":"link","attrs":{"href":"https:\/\/bucardo.org\/slides\/b5_multi_master\/","title":"","type":null},"content":[{"type":"text","text":"額外的觸發器"}]},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"你運行Bucardo的實例在本地使用一個單獨的Postgresql數據庫以保存同步狀態,這樣你就可以隨意暫停和重啓同步過程。當發生更改時,觸發器會將所有受影響的主鍵添加到Bucardo實例的Postgres中的“delta”表,另一個觸發器將“啓動(kick)”同步。每次同步被啓動時,Bucardo將對比所有主表中每個表的受影響行並選擇一個獲勝者,然後將更改同步到其餘數據庫。選擇獲勝者並不簡單,此時可能會發生衝突。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/5d\/5d6a8bf2b6711bb5c8b54833715eff5e.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"小心漂移"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一些在線指南建議,使用Bucardo的正確方法是獲取源數據庫的快照,將其恢復到新的數據庫,然後啓動一個多主Bucardo同步。不要那樣做!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果這樣做,你將丟失與當前數據庫大小和寫入流量成正比的數據。這是因爲獲取快照並恢復它需要大量時間。在這段時間裏,源數據庫將因爲數據寫入而開始漂移,並且這種漂移也必須同步以確保兩個主數據庫包含相同的數據。這裏的問題是人們相信Bucardo會做某種回填,但事實證明它在這項任務上不可靠,並且可能無法同步大的漂移。你自然可以使用跨數據庫對比數據的工具,確保消除偏差;但如果數據集很大,這樣做會浪費大量時間,而恰恰我們追求的就是零停機時間。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"此外,如果複製延遲足夠大,正在進行的同步可能會被誤報爲漂移。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/9f\/9f7f465e779b542886f44ebb52b4d23e.gif","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"如何同步漂移"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"你可以啓動Bucardo同步,並使用"},{"type":"codeinline","content":[{"type":"text","text":"autokick=0"}]},{"type":"text","text":"標誌告訴它在本地數據庫中緩存所有漂移。不幸的是,雖然這個選項很關鍵,但它沒有文檔支持!這一步很關鍵,據我們所知唯一明確的參考資料出現在David E. Wheeler的這篇優秀"},{"type":"link","attrs":{"href":"https:\/\/justatheory.com\/2013\/02\/bootstrap-bucardo-mulitmaster\/","title":"","type":null},"content":[{"type":"text","text":"博文"}]},{"type":"text","text":"中。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"注意autokick=0。這個標誌確保了在記錄增量時,它們不會被複制到任何地方,直到我們讓Bucardo這樣做爲止。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用這個標誌,你就可以在本地緩存Bucardo實例中的增量,爲你騰出了足夠的時間來準備新數據庫。這是非常關鍵的,尤其是對於大漂移更是如此。"}]},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/cc\/ccd9f5f52e721115d55bb2433d369064.gif","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"如何引導新數據庫"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這裏有兩個選項。你可以從第一個數據庫中獲取全包快照並將其恢復到新實例,或者你可以從一個新的空數據庫開始,然後分別傳輸用戶、模式和數據(按這個順序)。我們推薦後一種方法。原因是在對兩個解決方案進行基準測試對比後,第二個的結果更乾淨。我們可以從頭開始關閉舊用戶帳戶和臨時表並細化用戶權限。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你使用的是AWS RDS,推薦的這個方案也會更快。獲取快照可能需要幾分鐘時間,具體取決於你的數據庫大小。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"此外,如果你像我們一樣從未加密的服務器遷移到使用靜態加密的服務器,你需要獲取快照、加密快照,然後將其還原到新的RDS實例。這樣做用的時間更久,而最小化遷移時間是我們的一個關鍵目標。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"選擇性同步"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在開始Bucardo同步前,你需要正確配置它。你需要指定兩個數據庫、它們的類型(主\/副本),還有指定數據庫的哪些部分應包含在同步中。你可以從一個模式(schema)中批量添加所有表,數據庫有很多表的時候這個辦法非常有用。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Bucardo無法在沒有主鍵(PK)的情況下同步表,這很正常,因爲那種情況下它無法區分唯一條目。我們不得不在流程中排除一些表,這些表充當各種表遷移的緩存並且不包含PK。一些未使用的表也被排除在外,因此我們沒有將未使用的數據傳輸到新數據庫。在Bucardo中很容易完成上述操作:添加所有表後,你可以移除要排除的表。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"遷移用戶"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Bucardo不會遷移Postgres用戶,你需要手動轉移你的用戶帳戶。我們爲此編寫了一個"},{"type":"link","attrs":{"href":"https:\/\/github.com\/bluegroundltd\/postgresql-migration-guide\/blob\/master\/setup_new_database.template","title":"","type":null},"content":[{"type":"text","text":"腳本"}]},{"type":"text","text":"。這個腳本會到新數據庫,使用從配置服務器檢索到的密碼創建新用戶,然後設置他們的權限。儘管你可能不會將數據存儲爲代碼,但將用戶保存爲代碼是一種很好的做法,這樣在發生災難時就能夠恢復它們了。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"遷移模式和數據"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"你可以使用Postgres及其"},{"type":"codeinline","content":[{"type":"text","text":"pg_dump\/pg_restore"}]},{"type":"text","text":"工具來傳輸你的模式和數據。這個步驟很簡單,但有一個要點。請記住,此時我們已經啓動並運行了Bucardo來記錄漂移,因此在目標服務器上恢復數據將被解釋爲同步回源數據庫的更改。這就是爲什麼我們需要啓用session_replication_role=replica"},{"type":"link","attrs":{"href":"https:\/\/www.endpoint.com\/blog\/2015\/01\/28\/postgres-sessionreplication-role","title":"","type":null},"content":[{"type":"text","text":"標誌"}]},{"type":"text","text":",使用一個副本會話將數據恢復到目標Postgres數據庫。在我們啓動你的持續同步之前,我們需要禁用它。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"衝突"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"高可用性是零停機遷移的先決條件,它通常要求每個應用程序有多個正在運行的實例。一般來說,每個實例都應該在重新啓動之前排空,因此無法在完全相同的時間點將所有實例切換到新數據庫。所以總會有一個關鍵的——或短或長的——時間窗口,在這個窗口中同一個應用程序將同時寫入兩個數據庫,並且在這段時間內可能會發生衝突。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"衝突很少見,因爲它們需要在兩個數據庫中進行兩次寫入,然後Bucardo才能複製這兩個記錄。複製時間接近於零,你可能根本不會遇到任何衝突,但這種遷移發生在關鍵的生產環境中,因此不能忽略它們。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"想象一下,兩個客戶試圖在同一天預訂同一所房子。如果他們同時嘗試這樣做並且每個用戶都指向不同的數據庫,則可能會發生衝突。Bucardo有一個"},{"type":"link","attrs":{"href":"https:\/\/bucardo.org\/Bucardo\/operations\/conflict_handling","title":"","type":null},"content":[{"type":"text","text":"衝突解決機制"}]},{"type":"text","text":",提供了兩個基本選項:要麼讓Bucardo自動處理衝突(默認選項),要麼中止同步並手動解決它們。這是遷移過程中最關鍵的部分,我們進一步分析一下。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你的表有一個自動遞增的ID作爲主鍵,Postgres會自動從相應的序列中選擇下一個ID。Bucardo也會同步序列。假設在上面的示例中,你有一個帶有自動遞增ID作爲PK的bookings表,並且最新的記錄ID是42。這裏會發生併發插入,並且在兩個數據庫中創建兩條不同的記錄,它們都以43作爲PK,但數據不同。如果你讓Bucardo處理衝突,它會只保留最新的一個並刪除另一個。最後你會丟失一個對你的客戶來說似乎是成功的預訂。你的數據庫仍處於有效狀態,但你會丟失數據,還沒法恢復。這是一個死衚衕!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在討論解決方案之前,讓我們考慮另一種情況。假設你的表使用UUID作爲PK。回放上面的場景,併發預訂將在兩個數據庫中創建兩個不同的記錄,並具有兩個不同的PK。這次沒有發生衝突。Bucardo將成功同步兩個數據庫中的兩條記錄,但從業務角度來看你的數據仍然無效,因爲你不能兩次預訂同一所房子。因此這裏很明顯,從業務角度來看數據庫有效性並不能保證你的數據有效。你需要小心對待衝突的處理方式,以免你的客戶遇到問題。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Bucardo支持自定義解析策略。你可以根據業務需求制定自己的策略,但這很快就會變得過於複雜和耗時。另一種方法是創建你自己的工具來檢測和解決遷移期間的數據違規問題。這並非易事:它必須根據數據的複雜程度來做設計,並且可能需要大量開發工作。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們的解決方案是在開始遷移之前滿足兩個條件,來徹底避免衝突。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先,我們努力最小化數據庫之間的轉換時間,以最小化衝突概率。爲了做到這一點,我們會修改應用的重配置腳本以指向新的數據庫,一次一個實例,但所有的不同應用會並行操作。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"第二步最關鍵,就在我們開始將應用切換到新數據庫之前,我們撤銷了舊數據庫中應用用戶的寫入權限。通過這種方式,我們可以徹底避免衝突,但代價是一定比例的數據庫寫入失敗時間。這當然需要你的應用程序能夠優雅地處理失敗的數據庫寫入。你的應用程序執行此操作時應該能獨立於任何數據庫遷移活動,因爲這對於生產環境來說至關重要。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面就是最終的遷移計劃:"}]},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/6c\/6c2a29bf9e6dabbe5f5826e8b01dc3a7.gif","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"實現"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本節將展示我們遵循的步驟,以及每個步驟對應的腳本。我們已將代碼上傳到這個"},{"type":"link","attrs":{"href":"https:\/\/github.com\/bluegroundltd\/postgresql-migration-guide","title":"","type":null},"content":[{"type":"text","text":"GitHub存儲庫"}]},{"type":"text","text":",下文會對代碼做具體拆解分析。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"準備"}]},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"啓動一個新實例(在我們的例子中是EC2)。該指令假設你運行的是Debian操作系統。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"運行"},{"type":"link","attrs":{"href":"https:\/\/github.com\/bluegroundltd\/postgresql-migration-guide\/blob\/master\/install.sh","title":"","type":null},"content":[{"type":"text","text":"install.sh"}]},{"type":"text","text":"來安裝Bucardo"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"編輯"},{"type":"link","attrs":{"href":"https:\/\/github.com\/bluegroundltd\/postgresql-migration-guide\/blob\/master\/vars.sh","title":"","type":null},"content":[{"type":"text","text":"vars.sh"}]},{"type":"text","text":"以設置你的數據庫和postgres角色密碼"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"在shell中導出上述變量:"},{"type":"codeinline","content":[{"type":"text","text":"$sourcevars.sh"}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null},"content":[{"type":"text","text":"(可選)如果你之前在源數據庫中使用過Bucardo,你可能需要運行"},{"type":"link","attrs":{"href":"https:\/\/github.com\/bluegroundltd\/postgresql-migration-guide\/blob\/master\/uninstall_bucardo.sh","title":"","type":null},"content":[{"type":"text","text":"uninstall_bucardo.sh"}]},{"type":"text","text":"來清除舊觸發器。在運行之前,請查看我們根據我們的數據庫生成的"},{"type":"link","attrs":{"href":"https:\/\/github.com\/bluegroundltd\/postgresql-migration-guide\/blob\/master\/uninstall.template","title":"","type":null},"content":[{"type":"text","text":"uninstall.template"}]},{"type":"text","text":"。你需要在那裏列出你所有的表。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":6,"align":null,"origin":null},"content":[{"type":"text","text":"你需要手動運行"},{"type":"codeinline","content":[{"type":"text","text":"$ bucardo install"}]},{"type":"text","text":"才能完成本地Bucardo安裝。"}]}]}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"遷移"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"仔細看看"},{"type":"link","attrs":{"href":"https:\/\/github.com\/bluegroundltd\/postgresql-migration-guide\/blob\/master\/configure.sh","title":"","type":null},"content":[{"type":"text","text":"configure.sh"}]},{"type":"text","text":"腳本。在這裏,你需要編輯腳本以匹配你的遷移方案。你需要爲Bucardo對象定義描述性名稱並指定排除的表或略過此選項。在你瞭解腳本的作用後可以繼續運行它。該腳本執行以下操作:"}]},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"設置"},{"type":"codeinline","content":[{"type":"text","text":".pgpass"}]},{"type":"text","text":"文件和一條Bucardo別名命令,以避免在此過程中要求你輸入密碼的交互式提示中斷流程"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"配置Bucardo數據庫、"},{"type":"link","attrs":{"href":"https:\/\/bucardo.org\/Bucardo\/object_types\/herd","title":"","type":null},"content":[{"type":"text","text":"herds"}]},{"type":"text","text":"、"},{"type":"link","attrs":{"href":"https:\/\/bucardo.org\/Bucardo\/object_types\/database_group","title":"","type":null},"content":[{"type":"text","text":"數據庫組"}]},{"type":"text","text":"和"},{"type":"link","attrs":{"href":"https:\/\/bucardo.org\/Bucardo\/object_types\/sync","title":"","type":null},"content":[{"type":"text","text":"同步"}]},{"type":"text","text":"。如果你需要進一步瞭解Bucardo對象類型,他們的文檔頁面中有一個"},{"type":"link","attrs":{"href":"https:\/\/bucardo.org\/Bucardo\/object_types\/","title":"","type":null},"content":[{"type":"text","text":"列表"}]},{"type":"text","text":"。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"在新的Postgresql主機中初始化一個空數據庫並運行此"},{"type":"link","attrs":{"href":"https:\/\/github.com\/bluegroundltd\/postgresql-migration-guide\/blob\/master\/setup_new_database.template","title":"","type":null},"content":[{"type":"text","text":"腳本"}]},{"type":"text","text":"創建用戶。你需要編輯這個腳本來指定你的角色。密碼由我們之前獲取的"},{"type":"codeinline","content":[{"type":"text","text":"vars.sh"}]},{"type":"text","text":"文件檢索。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"這一步只傳輸數據庫模式,使用"},{"type":"codeinline","content":[{"type":"text","text":"pg_dump"}]},{"type":"text","text":"並將其傳輸到新主機"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":5,"align":null,"origin":null},"content":[{"type":"text","text":"使用本地緩存啓動Bucardo同步"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":6,"align":null,"origin":null},"content":[{"type":"text","text":"以壓縮格式傳輸數據庫數據。當數據傳輸和漂移開始堆積時,Bucardo會將其保存在本地並在autokick標誌更改值後重播"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":7,"align":null,"origin":null},"content":[{"type":"text","text":"重置autokick標誌的值以停止本地緩存,然後重新加載配置以讓同步遵守新值"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":8,"align":null,"origin":null},"content":[{"type":"text","text":"啓動多主同步"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"現在持續同步已就位,是時候開始在新數據庫中移動應用了。對我們來說,我們是更改配置服務器中的應用程序參數然後一一重新部署來完成這一步的。在這一步中,我們需要將舊數據庫中的用戶權限設置爲只讀。一旦我們應用的第一個實例連接到新數據庫,我們就運行"},{"type":"link","attrs":{"href":"https:\/\/github.com\/bluegroundltd\/postgresql-migration-guide\/blob\/master\/revoke_write_access_from_old_db.sql","title":"","type":null},"content":[{"type":"text","text":"revoke_write_access_from_old_db.sql"}]},{"type":"text","text":"腳本更改舊數據庫中的權限。這一步的時機非常重要。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"遷移後檢查"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當你的同步運行時,你應該驗證數據複製。我們使用分叉的pgdatadiff"},{"type":"link","attrs":{"href":"https:\/\/github.com\/andrikoz\/pgdatadiff\/","title":"","type":null},"content":[{"type":"text","text":"工具"}]},{"type":"text","text":"來做到這一點。我們還進一步擴展了它,允許數據diff來"},{"type":"link","attrs":{"href":"https:\/\/github.com\/andrikoz\/pgdatadiff\/commit\/225de1f1700a011e225fc42b830b6e82cd9792a2","title":"","type":null},"content":[{"type":"text","text":"排除表"}]},{"type":"text","text":"。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"將所有應用切換到新數據庫後,你可以停止Bucardo同步並下線它的機器。你應該再次運行"},{"type":"link","attrs":{"href":"https:\/\/github.com\/bluegroundltd\/postgresql-migration-guide\/blob\/master\/uninstall_bucardo.sh","title":"","type":null},"content":[{"type":"text","text":"uninstall_bucardo.sh"}]},{"type":"text","text":"以便從觸發器清理你的新數據庫。"}]}]}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"總結"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"將你的postgresql數據庫遷移到一個新實例會面臨巨大挑戰。無論你選擇哪種工具來實施,你要面對的挑戰都是一樣的:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"傳輸數據"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在兩個數據庫之間設置多主複製"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"從業務角度處理衝突,確保數據一致性"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"驗證同步過程"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"消除停機時間以避免干擾你的客戶"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在本文中,我們介紹了自己是如何解決這些問題的。我們遇到的一大困難是沒有這方面的在線教程,因此我們不得不隨機應變,並多次迭代我們的解決方案,直到我們正確地完成任務。我們也想聽聽你的反饋意見,這樣可以幫助我們改進流程,並幫助可能面臨相同問題的其他讀者。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"PS:背景故事"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"2020年初,我們發現我們使用了兩個Postgres9.5實例,我們從Blueground的早期就一直在使用它們。2020年1月,我們不得不關閉舊實例並使用新實例,因爲亞馬遜即將遷移到新的SSL\/TLS證書。這次遷移中,我們丟失了不少數據,花費了幾天的時間來恢復它們。問題出在我們信任Bucardo的自動同步機制,讓它處理我們的漂移;正如前面提到的那樣,它有問題並且失敗了。今年我們不得不再做一次,因爲Postgres 9.5即將EOL了,否則它們會被AWS強行升級。這次我們下定決心要注意每一個小細節。我們相信我們可以快速、可靠且無故障地達成目標,我們做到了。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"爲什麼要升級到新實例"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先,我們需要解釋爲什麼我們不讓亞馬遜在沒有我們干預的情況下在線升級我們的數據庫。亞馬遜提供了"},{"type":"link","attrs":{"href":"https:\/\/docs.aws.amazon.com\/AmazonRDS\/latest\/UserGuide\/USER_UpgradeDBInstance.Maintenance.html","title":"","type":null},"content":[{"type":"text","text":"升級流程"}]},{"type":"text","text":",但與遷移到新數據庫實例的方案相比,它有一些嚴重的缺點:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"AWSRDS不爲你提供即時回滾選項。在遷移過程中有兩個實例,回滾是對我們應用的一個簡單重配置,指向舊數據庫。在整個過程中,這是一個非常重要的故障預防措施。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"透明度。如果RDS升級數據庫失敗、出現延遲或性能問題,我們根本無法採取任何措施。在生產環境中,你需要有一個可靠的回滾計劃,以防萬一。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們想要的某些功能在當前實例中不可用,例如靜態加密和RDS見解。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在某些情況下,我們需要更改實例類型。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們選擇Bucardo是因爲我們想要一個在我們的VPC中沙盒化的解決方案,這樣生產數據永遠不會泄露到互聯網上。最後遷移很成功,也沒有丟失數據。遷移過程的總耗時不到2小時,算是比較成功的!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"原文鏈接:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"https:\/\/engineering.theblueground.com\/blog\/zero-downtime-postgres-migration-done-right\/"}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章