如何有效地為 2 個遠程節點使用 Kubernetes (How to use Kubernetes effectively for 2 distant nodes)


問題描述

如何有效地為 2 個遠程節點使用 Kubernetes (How to use Kubernetes effectively for 2 distant nodes)

我想把我所有的操作都轉移到 K8S 上這麼久了,但我仍然猶豫不決。這個問題可能很廣泛,但請耐心等待。讓我先描述一下現有系統。

我託管了很多不同的網站 (>30)。其中很多是為了我自己的實驗,但有些是針對實際客戶的。我在紐約有 1 台虛擬機(我使用的是 DigitalOcean),帶有多個 Docker 容器,經常使用 docker‑compose 進行管理。每個站點有 1 個容器。請求首先進入運行 HAProxy 的 front 容器。這會剝離 SSL,然後將請求轉發到 2 個運行 Nginx 的 proxy 容器。然後這 2 個容器將請求轉發給所有其他容器以提供服務。我所有的證書都來自 LetsEncrypt,並且必須每 3 個月更新一次。為此,我停止 front,運行 certbot ‑‑apache 使其綁定到端口 80。它獲取證書,然後我停止 apache,然後重新創建 front 容器。

我這樣做有幾個原因:

  • 我經常更改站點配置,以及它們是如何連接在一起的. 所以 front 預計會永遠運行,除非我獲得證書,並且 proxy 預計會發生很大變化。我更改了 proxy 映像,然後停止並重新創建第一個容器,然後停止並重新創建第二個容器,這樣就不會有停機時間了。
  • 當有多個節點時,我真的不知道如何獲得證書。事實上,我在整個證書方面完全是個菜鳥,而 LetsEncrypt 幾乎是我所知道的唯一方法。
  • 我想直接在遠程服務器。我直接編輯生產代碼的做法很糟糕,主要是因為我對設置開發、登台和生產環境感到不耐煩。這需要太多時間,而且收益感覺很小。對於客戶來說,他們通常是小型企業,員工少於 10 人,並且他們經常希望對網站進行一些美學上的改變。我可以與他們進行視頻通話,他們確切地告訴我他們想要什麼,我將其編碼,它會立即上傳到服務器,他們會立即看到變化。然後他們可以評論設計,我們可以來回迭代。如果我要設置不同的環境,他們無法立即看到它,並且必須有提交到 git、部署到暫存、然後是生產的漫長過程。這需要很長時間,而且我認為不合理。

我意識到我的系統沒有得到很好的維護。圖像沒有得到安全更新,我不知道它們是否仍在運行,除非我手動檢查它們,這很乏味,所以我根本不做。此外,我有亞洲背景,這意味著我有來自美國和亞洲的客戶,這幾乎是彼此相距最遠的地方,這大大增加了延遲。這意味著亞洲的客戶必須等待大約 1‑2 秒才能真正加載頁面,這是永恆的。過去一周我也搬到了亞洲,所以現在通過 ssh 訪問紐約服務器的速度非常慢,而且我的工作效率直線下降。所以現在可能是改造一切並永遠遷移到 K8S 的最佳時機。但是,在規劃過程中存在重大問題,目前,K8S 似乎缺少很多對我來說只是破壞交易的東西。所以請批評我的計劃,並在你認為合適的情況下改進它們。

我現在計劃做的是:將有 2 台服務器,1 台在紐約,1 台在新加坡。這 2 個服務器將有 2 個不同的 IP 地址。這 2 個將運行 K8S Pod。最好,它們應該具有完全相同的配置、網站容器、數據庫容器等。然後對於每個網站 DNS 記錄,我將修改 A 和 AAAA 記錄,以便它們包含 2 個服務器的 2 個 IP 地址。

我的問題是:

  1. 如果用戶在中國,DNS是否總是路由到新加坡,如果用戶在英國,DNS是否總是路由到紐約?
  2. 如何實際獲得 2 個節點的證書?我的理解是,certbot在頒發證書的時候,是把域名和節點ip地址關聯起來的。這意味著 2 個節點不能擁有相同域名的相同證書。這個對嗎?如果您可以獲得 2 個節點的證書,那麼該怎麼做?
  3. 如何在服務器之間保持文件同步?假設我在新加坡服務器中編輯文件樹,我希望幾秒鐘後在紐約也修改該文件。對於數據庫,我可以在新加坡或紐約擁有一個主數據庫,然後在兩個位置都有從屬數據庫,當主數據庫更新時,從屬數據庫可以作為每個服務器的低延遲數據庫。
  4. 如何將請求從服務器實際路由到內部容器。我最初打算使用 NodePort,將請求定向到 front Pod,然後可以將請求分發到其他 Pod,但是當 NodePort 無法連接到低於 30000 的端口時,我很傷心。唯一的另一個選擇我知道的是有一個外部負載平衡服務,將流量引導到兩台服務器。但這要花 15 美元/站點/月,而且因為我有超過 30 個站點,這樣做會讓我破產。我也可以總共有 4 台服務器,其中 2 台用於 K8S 集群,2 台用作將轉發到 NodePort 的負載均衡器。這個計劃會奏效嗎?證書的自動更新甚至在這裡如何工作?

請注意,我的問題可能是錯誤的問題(例如,我可能不應該使用 A 和 AAAA 記錄來指導流量),並且有完全不同的方法可以做到這一點,所以請隨時提出正確的問題。


參考解法

方法 1:

read your question hats off to write down the whole stuff but half of the stuff is useless.

Answers of your question :

  1. Can we add the same or multiple entries in DNS? example.com with A record multiple times possible?

  2. You might require to set up a regional K8s cluster with regional ingress support. you can use certmanager with letsencrypt which will manage your cert at LB level and terminate it at the front.

    If you are looking forward to use two VMs put one LB in front of both and set SSL over there.

  3. if you are using K8s with stateless PODs editing direct file inside container is not a option. better you manage the Github update inside and container get deployed on to both cluster at a same time for that you can setup CI/CD. You are right in case of database server setup with master slave concept you can use read replicas.

  4. To route the traffic from server to internal application of K8s you can an internal LB or exposing services with node ports(above 30000 but change target port in SVC) and route the port if you want to redirect requests on a specific port using the target port.

still, i am not getting "I can also have 4 servers in total, 2 for the K8S cluster, and 2 serves as a load balancer that will forward to NodePort. Will this plan works? How will automatic renewing of certificates even work here?" which server will be in front and which one in the backend.

方法 2:

If all your services are websites (run over http) you could use k8s ingress to route traffic to pods based on Host header (domain name) and use only one LB with one IP address. The most popular ingress controller seems to be the Nginx Ingress Controller

If you don't want to use LB you can use hostPort to expose nginx ingress but as soon as you have k8s cluster with more than one node, use LB because hostPort is generally not advised to use unless you have a very good reason to do so.

Speaking of DNS, you can use sth like AWS route53 routing policies for location routing. You don't necessarily need to use AWS. I just want to show you that there are solutions to this problem, but use whatever you like.

For certificates use cetrmanager with DNS‑01 challenge. From letsencrypt docs about DNS‑01 challenge:

  • It works well even if you have multiple web servers.

cetrmanager will also handle certificate renewal for you.

About keeping files in sync between servers; It depends on files, but for static content it might be best to use CDN that will replicate content from one source to other locations.

For simultanous deploys to 2 separate clusters you can use some CI/CD pipeline like e.g. github actions.

(by 157 239nHarsh ManvarMatt)

參考文件

  1. How to use Kubernetes effectively for 2 distant nodes (CC BY‑SA 2.5/3.0/4.0)

#Certbot #Kubernetes #docker #SSL #networking






相關問題

certbot-auto /letsencrypt 為指向同一服務器的多個域設置一個密鑰 (certbot-auto / letsencrypt setting up one key for multiple domains pointing to the same server)

certbot 工作時 Phpseclib 早期執行 (Phpseclib early execution while certbot working)

“httpd.service 的作業失敗,因為控制進程以錯誤代碼退出。” 我該如何解決? ("Job for httpd.service failed because the control process exited with error code." How do I fix this?)

無法使用 certbot renew/Letsencrypt 續訂證書 (Cannot renew certificates with certbot renew/Letsencrypt)

為 AWS 中託管的網站設置 HTTPS,但指向局域網中的 webAPI (Setting up HTTPS for a website hosted in AWS, but points to a webAPI in the local area network)

docker 發送內部 cerbot 日誌文件到主機 journald (docker send internal cerbot log file to host journald)

Nginx 允許來自任何域的流量 (Nginx allow traffic from any domain)

如何有效地為 2 個遠程節點使用 Kubernetes (How to use Kubernetes effectively for 2 distant nodes)

如何在 docker 映像中創建目錄? (How can I make dir inside docker image?)

更新 Certbot 時出現“連接超時(可能是防火牆問題)” ("Timeout during connect (likely firewall problem)" while renewing Certbot)

安裝certbot後找不到nginx頁面錯誤 (nginx page not found error after certbot installation)

Nginx 和 certbot 的 ERR_SSL_PROTOCOL_ERROR (ERR_SSL_PROTOCOL_ERROR with Nginx & certbot)







留言討論