天天看點

17.Docker(八)-----Docker三劍客之compose一、compose簡介二、Docker Compose實踐

compose簡介、安裝、負載均衡

  • 一、compose簡介
    • Docker Compose 中有兩個重要的概念:
  • 二、Docker Compose實踐
    • 1.docker compose安裝
    • 2.導入鏡像
    • 3.docker-compose.yml 屬性
    • 4.建立haproxy配置檔案:
    • 5.流程
    • 6.運作docker-compose
    • 7.真機測試,實作負載均衡
    • 8.Docker Compose 常用指令

一、compose簡介

  • 微服務架構的應用系統一般包含若幹個微服務,每個微服務一般都會部署多個執行個體,如果每個微服務都要手動啟停,那麼效率之低,維護量之大可想而知。
  • Docker Compose是一種編排服務,基于pyhton語言實作,是一個用于在 Docker 上定義并運作複雜應用的工具,可以讓使用者在叢集中部署分布式應用。
  • 使用者可以很容易地用一個配置檔案定義一個多容器的應用,然後使用一條指令安裝這個應用的所有依賴,完成建構。
  • 解決了容器與容器之間如何管理編排的問題。

Docker Compose 中有兩個重要的概念:

  • 服務 (service) :一個應用的容器,實際上可以包括若幹運作相同鏡像的容器執行個體。
  • 項目 (project) :由一組關聯的應用容器組成的一個完整業務單元,在 docker-compose.yml 檔案中定義。
    17.Docker(八)-----Docker三劍客之compose一、compose簡介二、Docker Compose實踐

二、Docker Compose實踐

1.docker compose安裝

方式1:(官方推薦,外網但是慢)

# curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
# chmod +x /usr/local/bin/docker-compose
           

方式2:(阿裡雲 國内推薦,速度快)

https://mirrors.aliyun.com/docker-toolbox/linux/compose/1.21.2/
下載下傳後放在: /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
           

2.導入鏡像

[[email protected] ~]# lftp 172.25.15.250
lftp 172.25.15.250:~> cd pub/images/
lftp 172.25.15.250:/pub/images> get haproxy.tar  #下載下傳鏡像
145218560 bytes transferred                     
lftp 172.25.15.250:/pub/images> exit
[[email protected] ~]# docker load -i haproxy.tar 	#導入鏡像
917c0fc99b35: Loading layer  130.9MB/130.9MB
5f70bf18a086: Loading layer  1.024kB/1.024kB
c205bb11f213: Loading layer  4.684MB/4.684MB
ffef890bdf7b: Loading layer  9.549MB/9.549MB
3ec368642ee3: Loading layer  2.048kB/2.048kB
The image haproxy:latest already exists, renaming the old one with ID sha256:271c3cfa9d6bd4ac64f3204fee4b75a3a275378fcfc9ad3f314e4e4cfecaa3c3 to empty string
Loaded image: haproxy:latest
           
17.Docker(八)-----Docker三劍客之compose一、compose簡介二、Docker Compose實踐

3.docker-compose.yml 屬性

Image:指定為鏡像名稱或鏡像 ID,如果鏡像在本地不存在,Compose 将會嘗試拉取這個鏡像。

Build:指定 Dockerfile 所在檔案夾的路徑。 Compose 将會利用它自動建構這個鏡像,然後使用這個鏡像。

Command:覆寫容器啟動後預設執行的指令。

Links:連結到其它服務中的容器。

Ports:端口映射。

Expose::暴露端口資訊

Volumes:卷挂載路徑設定

##建立docker-compose.yml檔案:

vim docker-compose.yml
version: "3.8"
services:
  web1:
    image: nginx
    networks:
      - webnet
    volumes:
      - ./web1:/usr/share/nginx/html
    
  web2:
    image: nginx
    networks:
      - webnet
    volumes:
      - ./web2:/usr/share/nginx/html

  haproxy:
    image: haproxy
    networks:
      - webnet
    volumes:
      - ./haproxy:/usr/local/etc/haproxy
    ports:
      - "80:80"

networks:
  webnet:
           
17.Docker(八)-----Docker三劍客之compose一、compose簡介二、Docker Compose實踐

4.建立haproxy配置檔案:

[[email protected] compose]# cd haproxy/
[[email protected] haproxy]# vim haroxy.cfg
#
# This is a sample configuration. It illustrates how to separate static objects
# traffic from dynamic traffic, and how to dynamically regulate the server load.
#
# It listens on 192.168.1.10:80, and directs all requests for Host 'img' or
# URIs starting with /img or /css to a dedicated group of servers. URIs
# starting with /admin/stats deliver the stats page.
#

global
        maxconn         65535
        stats socket    /var/run/haproxy.stat mode 600 level admin
        log             127.0.0.1 local0
        uid             200
        gid             200
        daemon

defaults
        mode            http
        log             global
        option          httplog
        option          dontlognull
        monitor-uri     /monitoruri
        maxconn         8000
        timeout client  30s
        retries         2
        option redispatch
        timeout connect 5s
        timeout server  5s
        stats uri       /admin/stats




# The public 'www' address in the DMZ
frontend public
        bind            *:80 name clear
        #bind            192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem

        #use_backend     static if { hdr_beg(host) -i img }
        #use_backend     static if { path_beg /img /css   }
        default_backend dynamic

# The static backend backend for 'Host: img', /img and /css.
backend dynamic
        balance         roundrobin
        server          a web1:80 check inter 1000
        server          b web2:80 check inter 1000

           
17.Docker(八)-----Docker三劍客之compose一、compose簡介二、Docker Compose實踐

5.流程

[[email protected] ~]# mkdir compose
[[email protected] ~]# cd compose/
[[email protected] compose]# ls
[[email protected] compose]# mkdir haproxy
[[email protected] compose]# cd haproxy/
[[email protected] haproxy]# vim haroxy.cfg
[[email protected] haproxy]# cd ..
[[email protected] compose]# vim docker-compose.yml 
[[email protected] compose]# mkdir web1
[[email protected] compose]# mkdir web2
[[email protected] compose]# echo web1 > web1/index.html	
[[email protected] compose]# echo web2 > web2/index.html
[ro[email protected] compose]# ls
docker-compose.yml  haproxy  web1  web2
           
17.Docker(八)-----Docker三劍客之compose一、compose簡介二、Docker Compose實踐

6.運作docker-compose

docker-compose指令必須在項目下運作。

以下是項目目錄結構:

17.Docker(八)-----Docker三劍客之compose一、compose簡介二、Docker Compose實踐
[[email protected] compose]# docker-compose up -d	#建立并啟動容器
Starting compose_web2_1    ... done
Starting compose_web1_1    ... done
Starting compose_haproxy_1 ... done
[[email protected] compose]# docker-compose ps
           
17.Docker(八)-----Docker三劍客之compose一、compose簡介二、Docker Compose實踐

7.真機測試,實作負載均衡

[[email protected] pub]# curl  172.25.15.1
web2
[[email protected] pub]# curl  172.25.15.1
web1
[[email protected] pub]# curl  172.25.15.1
web2
[[email protected] pub]# curl  172.25.15.1
web1
[[email protected] pub
           
17.Docker(八)-----Docker三劍客之compose一、compose簡介二、Docker Compose實踐

** 關閉一個,另一個自動接管**

8.Docker Compose 常用指令

Build: 建構或重新建構服務。

kill:強制停止服務容器。

logs:檢視服務的輸出。

port:列印綁定的公共端口。

ps:列出所有容器。

pull:拉取服務所需鏡像。

rm:删除停止的服務容器。

up:建構并啟動容器。