Docker + keepalived 部署 Nginx 主從

實驗環境html

dockerhost01 192.168.10.150linux

dockerhost02 192.168.10.151nginx

vip 192.168.10.200docker

全新鏡像,神馬都沒有,連sshd都沒開,容我安裝下ubuntu

開啓SSH架構

sudo apt-get install openssh-client openssh-serverssh

sudo service ssh restartcurl

安裝dockeride

先卸載舊版本url

sudo apt-get remove docker docker-engine docker.io

5fda1811c52d4a76940e35bc667e091a

設置repo

sudo apt-get update

769e9c158a8a4067b02b212d2d6d5547

容許package 使用repo over HTTPS

sudo apt-get install \

apt-transport-https \

ca-certificates \

curl \

software-properties-common

c9b6536be3754d0d8a41fe71d3b714c3

添加GPG key

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

d823001f5bef42239e8800a69e5b8182

驗證下指紋

sudo apt-key fingerprint 0EBFCD88

ea823357aa2041dc8416effb7401ea21

在repo中只使用stable版本

sudo add-apt-repository \

"deb [arch=amd64] https://download.docker.com/linux/ubuntu \

$(lsb_release -cs) \

stable"

開始安裝最新版docker ce

sudo apt-get update

sudo apt-get install docker-ce

sudo docker run hello-world

ba5fb6a1669045b3861003c4fa08acaf

安裝指定版本docker ce

apt-cache madison docker-ce

sudo apt-get install docker-ce=<VERSION>

20149c07274c4062923e8b15fe34172b

下載nginx 鏡像

sudo docker pull nginx:1.7.6

mkdir -p /tmp/docker

sudo echo "<h2 >This is nginx container running on 192.168.10.150 </h2><br /> static files:/tmp/docker/index.html" > /tmp/docker/index.html

sudo docker run --name nginx_m --restart=always -v /tmp/docker:/usr/share/nginx/html:ro -p 80:80 -d nginx:1.7.6

c015cd38a1c1423a950476c06a3b68a7

sudo docker ps

c3f4a29a58f74c48b535c5d8c75a5f4f

宿主機器訪問下,2臺主機都正常啓動nginx了

289bfdf9f72745af8dc59836ac8f9112

76c6d4057151401cbc9648cbdf875f7b

安裝keepalived

sudo apt-get update

sudo apt-get install -y libssl-dev openssl libpopt-dev

sudo apt-get install -y keepalived

sudo cp /usr/share/doc/keepalived/samples/keepalived.conf.sample /etc/keepalived/keepalived.conf

sudo vi /etc/keepalived/keepalived.conf

改這3個地方就行了

clipboard

host01 中出現了10.200的vip

a67e2a4b12c145069e332d8283ed99ed

vip指向150,訪問正常

b16c56fedf3c4b038c8b312c2cbade7a

91187b7e51ab4475a43b3f449e558805

sudo service keepalived status -l

host02 從backup 變成了master

45d178283e774758b8899242336b9389

host01網卡停掉以後,vip跑到了host02上

35999518d288462d8a91fa499e8a089f

訪問192.168.10.200的主機,顯示nginx跑在了10.151上

d9ccfd9a36054b67974025d2e5a734eb

固然主從能夠設置weight 和nopreempt來設置是否奪權從新選舉

當兩個節點上的業務服務都處於啓動狀態,如httpd,那麼優先級變化會以下:


784dfd9eb88f4e6ab8fe3e9109b72b12

當master節點業務啓動,backup節點業務爲中止狀態,如,haproxy(由於haproxy沒有監聽的地址是沒法啓動的,其實不少業務都是兩個節點一啓一停的)

d10847a091e94168959e38566c46176b


從架構來看,docker+nginx主從並不可以提供負載冗餘功能,由於始終後臺只有一臺,只可以提供高可用,這個與我當時想法有所出入,因此目測整個解決方案只適合輕量化訪問,可是輕量化的訪問又不值得搞這兩臺主機作主備,作單機麼又怕單點故障,因此整個解決方案比較尷尬


至於實驗,整個實驗和以前keepalived+nginx的實驗並無太大區別,只是nginx跑在了docker上而已,docker的優點並無發揮出來,新的架構容我三思

相關文章
相關標籤/搜索