1.下載ˇ官方httpd image
#docker pull httpd
root@k8s02:/container/www1# docker pull httpd
root@k8s02:/container/www1# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd latest 11426a19f1a2 4 weeks ago 178 MB
2.建立並啟動容器
root@k8s02:/container/www1/conf# docker run -p 80:80 -d httpd
3.檢查容器內相關檔案路徑
root@k8s02:/container/www1/conf# docker exec -ti b0659d224641 pwd
/usr/local/apache2
root@k8s02:/container/www1/conf# docker exec -ti b0659d224641 ls
bin build cgi-bin conf error htdocs icons include logs modules
root@k8s02:/container/www1/conf# docker exec -ti b0659d224641 ls /usr/local/apache2/htdocs
index.html
容器中網頁存放在/usr/local/apache2/htdoc中
root@k8s02:/container/www1/conf# docker exec -ti b0659d224641 ls /usr/local/apache2/conf
extra httpd.conf magic mime.types original
apache設定檔httpd.conf存放在/usr/local/apache2/conf中
4.將相關檔案放在HOST上維護,在做移轉時較方便
在HOST上建立相關目錄
mkdir /container/www1/
mkdir /container/www1/web1
mkdir /container/www1/web2
mkdir /container/www1/web3
mkdir /container/www1/conf
mkdir /container/www1/logs
分別修改3個web的index.html
cd mkdir /container/www1/web1
vi index.html
Hello,web1
(web2 以及web3)
cd /container/www1/conf
將https container的httpd.conf 轉出到HOST上
root@k8s02:/container/www1/conf# docker exec -ti b0659d224641 cat /usr/local/apache2/conf/httpd.conf > httpd.conf
5.建立容器
建立並啟動第一個httpd容器,由HOST的8001port進入
docker run -p 8001:80 -d -v /container/www1/web1/:/usr/local/apache2/htdocs/ -v /container/www1/conf/httpd.conf:/usr/local/apache2/conf/httpd.conf -v /container/www1/logs/:/usr/local/apache2/logs/ --name web1 httpd
瀏覽器網址輸入http://10.88.1.134:8001,即會導到容器1的80 port
瀏覽器顯示Hello,web1
建立並啟動第二個httpd容器,由HOST的8002port進入
docker run -p 8002:80 -d -v /container/www1/web2/:/usr/local/apache2/htdocs/ -v /container/www1/conf/httpd.conf:/usr/local/apache2/conf/httpd.conf -v /container/www1/logs/:/usr/local/apache2/logs/ --name web2 httpd
瀏覽器網址輸入http://10.88.1.134:8002,即會導到容器2的80 port
瀏覽器顯示Hello,web2
建立並啟動第三個httpd容器,由HOST的8003port進入
docker run -p 8003:80 -d -v /container/www1/web3/:/usr/local/apache2/htdocs/ -v /container/www1/conf/httpd.conf:/usr/local/apache2/conf/httpd.conf -v /container/www1/logs/:/usr/local/apache2/logs/ --name web3 httpd
瀏覽器網址輸入http://10.88.1.134:8003,即會導到容器3的80 port
瀏覽器顯示Hello,web3
6.建立HAproxy
查出3個https容器的內部IP
root@k8s02:/container/haproxy17/conf# docker exec -it web1 ip a
ip為 172.17.0.2
root@k8s02:/container/haproxy17/conf# docker exec -it web2 ip a
ip為 172.17.0.3
root@k8s02:/container/haproxy17/conf# docker exec -it web3 ip a
ip為 172.17.0.4
haproxy.cfg設定檔會用到
mkdir /container/haproxy17
cd /container/haproxy17
vi Dockerfile
FROM haproxy:1.7
# 為了要把haproxy.cfg設定檔放在HOST方便修改,所以在容器中刪除原先設定檔再建一個設定檔連結到從HOST掛載的目錄上,建立容器時再將HOST存放haproxy.cfg的路徑掛載到容器中使用
RUN rm -f /usr/local/etc/haproxy/haproxy.cfg
RUN ln -s /usr/local/etc/haproxy/conf/haproxy.cfg /usr/local/etc/haproxy/haproxy.cfg
mkdir conf
cd conf
vi haproxy.cfg
global
daemon
maxconn 4096
defaults
mode http
timeout connect 5000
timeout client 5000
timeout server 5000
frontend main
bind *:80
stats uri /haproxy?stats
mode http
balance roundrobin
option httpclose
option forwardfor
server web1 172.17.0.2:80 check weight 1 maxconn 50
server web2 172.17.0.3:80 check weight 1 maxconn 30
server web1 172.17.0.4:80 check weight 1 maxconn 50
option redispatch
retries 3
建立haproxy image
root@k8s02:/container/haproxy17# docker build -t my-haproxy .
建立並啟用haproxy容器
root@k8s02:/container/haproxy17# docker run -d --name my-haproxy -p 80:80 my-haproxy
開啟瀏覽器輸入10.88.1.134,就會輪流顯示web1、web2、web3
查看haproxy status
http://10.88.1.134/haproxy?stats