手把手搭建一个容器化+代理网关+可视化管理环境
作者 | togettoyou
來源 | SuperGopher
前言
本文主要分享個人服務(wù)器的應(yīng)用部署方案現(xiàn)狀,容器化+代理網(wǎng)關(guān)+可視化管理。
準(zhǔn)備階段
我購買的是騰訊云服務(wù)器(2 核 4GB 3Mbps)
域名也是在騰訊云備案過的,提前準(zhǔn)備域名解析
配置環(huán)境
安裝 Docker
curl?-sSL?https://get.daocloud.io/docker?|?sh安裝 Docker Compose
curl?-L?https://get.daocloud.io/docker/compose/releases/download/1.29.2/docker-compose-`uname?-s`-`uname?-m`?>?/usr/local/bin/docker-compose chmod?+x?/usr/local/bin/docker-compose設(shè)置鏡像加速和開機自啟
mkdir?/etc/docker echo?'{"registry-mirrors":["https://reg-mirror.qiniu.com/"]}'?>?/etc/docker/daemon.json sudo?systemctl?daemon-reload sudo?systemctl?restart?docker sudo?systemctl?enable?docker部署 Traefik
創(chuàng)建 Traefik 目錄,并在該目錄下進行系列操作
cd?~ mkdir?traefik cd?traefik/創(chuàng)建 Traefik 的啟動配置
vi?traefik.yml#?定義監(jiān)聽端口:web(80)?和 websecure(443) entryPoints:web:address:?":80"websecure:address:?":443" #?配置發(fā)現(xiàn)提供者,這里為?docker providers:docker:?{} #?開啟?Traefik?面板訪問?(8080端口) api:dashboard:?trueinsecure:?true #?創(chuàng)建名稱為?open-https?的??tls?配置(會使用?Let's?Encrypt?自動生成?https?證書) certificatesResolvers:open-https:acme:email:?"youremail@qq.com"storage:?"acme.json"httpChallenge:entryPoint:?web創(chuàng)建名稱為 traefik 的 Docker 網(wǎng)絡(luò)環(huán)境,后續(xù) Traefik 和需要代理的應(yīng)用容器都需要部署在該網(wǎng)絡(luò)環(huán)境下才可以被訪問到
docker?network?create?traefik創(chuàng)建 docker-compose 配置
vi?docker-compose.ymlversion:?'3' services:traefik:image:?traefik:v2.4#?需要將本地的?/var/run/docker.sock?掛載到?Traefik?容器內(nèi),才可以使用到?docker?配置發(fā)現(xiàn)volumes:-?/var/run/docker.sock:/var/run/docker.sock-?$PWD/traefik.yml:/etc/traefik/traefik.ymlrestart:?always#?把?Traefik?容器監(jiān)聽的?80?和?443?端口映射到宿主機(以后宿主機只要暴露?80?和?443?就可以了,所有流量通過?Traefik?代理)ports:-?"80:80"-?"443:443"#?使用自定義的?traefik?網(wǎng)絡(luò)networks:-?traefik#?標(biāo)簽配置,Traefik?的配置發(fā)現(xiàn)是通過標(biāo)簽抓取的labels:#?開啟?redirectscheme?中間件,中間件名稱為?redirect-https?,該中間件可以重定向?http?到?https?,達到強制?https?的目的-?"traefik.http.middlewares.redirect-https.redirectscheme.scheme=https"#?配置一個名稱為?traefik-service?的服務(wù),容器內(nèi)端口為?8080-?"traefik.http.services.traefik-service.loadbalancer.server.port=8080"#?配置一個名稱為?https-traefik?的路由,代理服務(wù)為?traefik-service#?監(jiān)聽域名為?traefik.togettoyou.com?,端口為?websecure?(443)?的流量請求#?開啟?tls?,使用?open-https?,自動簽發(fā)證書-?"traefik.http.routers.https-traefik.service=traefik-service"-?"traefik.http.routers.https-traefik.rule=Host(`traefik.togettoyou.com`)"-?"traefik.http.routers.https-traefik.entrypoints=websecure"-?"traefik.http.routers.https-traefik.tls=true"-?"traefik.http.routers.https-traefik.tls.certresolver=open-https"#?配置一個名稱為?http-traefik?的路由,代理服務(wù)為?traefik-service#?監(jiān)聽域名為?traefik.togettoyou.com?,端口為?web?(80)?的流量請求#?使用?redirect-https?中間件,將?http?請求重定向到?https,即重定向到了上面配置的?https-traefik?路由-?"traefik.http.routers.http-traefik.service=traefik-service"-?"traefik.http.routers.http-traefik.rule=Host(`traefik.togettoyou.com`)"-?"traefik.http.routers.http-traefik.entrypoints=web"-?"traefik.http.routers.http-traefik.middlewares=redirect-https"networks:traefik:external:?true啟動 Traefik
docker-compose?up?-d訪問 http://traefik.togettoyou.com 時會發(fā)現(xiàn)被 302 重定向到了 https://traefik.togettoyou.com ,并且自動配置了證書
查看面板也可以發(fā)現(xiàn)和我們預(yù)期的一致
部署 Docker 可視化工具
Portainer 是一個 Docker 的可視化圖形工具。
同理,我們創(chuàng)建 Portainer 目錄,并在該目錄進行系列操作
cd?~ mkdir?portainer cd?portainer/創(chuàng)建 docker-compose 配置
vi?docker-compose.ymlversion:?'3' services:portainer:image:?portainer/portainer-ce#?掛載?/var/run/docker.sock?,并持久化?portainer?數(shù)據(jù)volumes:-?/var/run/docker.sock:/var/run/docker.sock-?$PWD/data:/datarestart:?always#?使用自定義的?traefik?網(wǎng)絡(luò)networks:-?traefiklabels:#?配置一個名稱為?portainer-service?的服務(wù),容器內(nèi)端口為?9000-?"traefik.http.services.portainer-service.loadbalancer.server.port=9000"#?配置一個名稱為?https-portainer?的路由,代理服務(wù)為?portainer-service#?監(jiān)聽域名為?docker.togettoyou.com?,端口為?websecure?(443)?的流量請求#?開啟?tls?,使用?open-https?,自動簽發(fā)證書-?"traefik.http.routers.https-portainer.service=portainer-service"-?"traefik.http.routers.https-portainer.rule=Host(`docker.togettoyou.com`)"-?"traefik.http.routers.https-portainer.entrypoints=websecure"-?"traefik.http.routers.https-portainer.tls=true"-?"traefik.http.routers.https-portainer.tls.certresolver=open-https"#?配置一個名稱為?http-portainer?的路由,代理服務(wù)為?portainer-service#?監(jiān)聽域名為?docker.togettoyou.com?,端口為?web?(80)?的流量請求#?使用?redirect-https?中間件,將?http?請求重定向到?https,即重定向到了上面配置的??https-portainer?路由-?"traefik.http.routers.http-portainer.service=portainer-service"-?"traefik.http.routers.http-portainer.rule=Host(`docker.togettoyou.com`)"-?"traefik.http.routers.http-portainer.entrypoints=web"-?"traefik.http.routers.http-portainer.middlewares=redirect-https"networks:traefik:external:?true啟動 Portainer
docker-compose?up?-d查看面板
訪問 docker.togettoyou.com
總結(jié)
利用 Docker + Traefik + Portainer ,極大方便了我們個人服務(wù)器應(yīng)用部署。Traefik 監(jiān)聽著 80 和 443 端口,因此服務(wù)器只需要暴露出這兩個端口,其他的流量請求都交由 Traefik 來代理,基于 Traefik 的配置發(fā)現(xiàn)機制,在部署我們的容器應(yīng)用時只要在 Label 加上 Traefik 的規(guī)則即可;對于容器應(yīng)用的啟動停止、日志查看等使用 Portainer 綽綽有余,幾乎不需要登陸到服務(wù)器上操作查看。
往期推薦
Spring 完美導(dǎo)入 IDEA
k8s集群居然可以圖形化安裝了?
使用這個庫,讓你的服務(wù)操作 Redis 速度飛起
將 k8s 制作成 3D 射擊游戲,好玩到停不下來
點分享
點收藏
點點贊
點在看
總結(jié)
以上是生活随笔為你收集整理的手把手搭建一个容器化+代理网关+可视化管理环境的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 高密自智,体小量大,希捷Exos Cor
- 下一篇: 从 40% 跌至 4%,“糊”了的 Fi