我試圖建立一個Kubernetes集羣內的以下服務:如何創建Kubernetes集羣只顧自己的容器SSL和NGINX
- 泊塢的註冊表(它將包含我的Django泊塢窗圖像)
- Nginx的一邊收聽兩個端口80和443
- PostgreSQL的
- 幾個Django應用程序與服務gunicorn
- letsencrypt集裝箱生成和自動更新簽名的S SL證書
我的問題是創建集羣的過程中發生了雞和蛋的問題:
我的SSL證書存儲在一個由letsencrypt集裝箱生成量的祕密。爲了能夠生成證書,我們需要顯示我們是域名的所有者,並且通過驗證可以從服務器名稱訪問的文件來完成(基本上,這包括Nginx能夠通過端口80提供靜態文件)
所以這裏出現我的第一個問題:爲了提供letsencrypt所需的靜態文件,我需要啓動nginx。 nginx的SSL部分無法啓動,如果祕密尚未安裝,並且只有當我們的加密成功時才生成祕密...
所以,一個簡單的解決方案可能是有2個Nginx容器:一個監聽只有在端口80將會先開始,然後letsencrypt然後我們開始第二個Nginx容器監聽端口443
- >這種看起來像在我看來浪費資源,但爲什麼不。
現在假設我有2個nginx容器,我希望我的Docker Registry可以通過https訪問。
所以在我的nginx的配置,我將有一個碼頭工人,registry.conf文件看起來像:
upstream docker-registry {
server registry:5000;
}
server {
listen 443;
server_name docker.thedivernetwork.net;
# SSL
ssl on;
ssl_certificate /etc/nginx/conf.d/cacert.pem;
ssl_certificate_key /etc/nginx/conf.d/privkey.pem;
# disable any limits to avoid HTTP 413 for large image uploads
client_max_body_size 0;
# required to avoid HTTP 411: see Issue #1486 (https://github.com/docker/docker/issues/1486)
chunked_transfer_encoding on;
location /v2/ {
# Do not allow connections from docker 1.5 and earlier
# docker pre-1.6.0 did not properly set the user agent on ping, catch "Go *" user agents
if ($http_user_agent ~ "^(docker\/1\.(3|4|5(?!\.[0-9]-dev))|Go).*$") {
return 404;
}
# To add basic authentication to v2 use auth_basic setting plus add_header
auth_basic "registry.localhost";
auth_basic_user_file /etc/nginx/conf.d/registry.password;
add_header 'Docker-Distribution-Api-Version' 'registry/2.0' always;
proxy_pass http://docker-registry;
proxy_set_header Host $http_host; # required for docker client's sake
proxy_set_header X-Real-IP $remote_addr; # pass on real client's IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 900;
}
}
的重要組成部分,是對註冊表容器重定向proxy_pass。
我現在面臨的問題是,我的Django的Gunicorn服務器也有在同一文件夾django.conf它的配置文件:
upstream django {
server django:5000;
}
server {
listen 443 ssl;
server_name example.com;
charset utf-8;
ssl on;
ssl_certificate /etc/nginx/conf.d/cacert.pem;
ssl_certificate_key /etc/nginx/conf.d/privkey.pem;
ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
client_max_body_size 20M;
location/{
# checks for static file, if not found proxy to app
try_files $uri @proxy_to_django;
}
location @proxy_to_django {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_redirect off;
#proxy_pass_header Server;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 65;
proxy_read_timeout 65;
proxy_pass http://django;
}
}
所以nginx的將成功啓動僅在3個條件:
- 祕密安裝(這可以通過拆分Nginx的解決成2個獨立的容器)
- 註冊表服務啓動
- 本身的Django服務啓動
問題是,Django映像從註冊表服務拉它的形象,所以我們再次處於死鎖狀態。
我沒有提到它,但是這兩個註冊表和Django的有不同的服務器名稱,以便nginx的是能夠既爲他們服務
的解決方案,我雖然它(但它很骯髒!)將重新加載nginx的幾個時間隨着越來越多的配置:
- 我開始搬運工註冊表服務
- 我開始Nginx的只用registry.conf
- 創建我的Django的RC和服務
- 我既registry.conf和django.conf
如果有一種方法,使nginx的開始忽略失敗的配置,這可能會解決我的問題,以及nginx的重裝。
我該如何幹淨地實現這個設置?
感謝您的幫助
蒂博
您可以在一個容器中只容納3個容器,並讓另一個容器崩潰直到letsencrypt容器將證書寫入共享容量?如果你想避免crashlooping,你可以使用--cmd =「your-script」將實際的容器封裝到不同的入口點,並讓腳本例如:繼續輪詢註冊表端點,然後啓動django。這當然需要3個nginx,但我認爲它比重新加載單個配置更清潔。它也是可組合的,例如,如果你想換出一個去webserver的letsencrypt nginx或其他東西。 –