docker映象管理倉庫搭建加密認證及負載均衡倉庫的搭建
阿新 • • 發佈:2018-12-26
1.映象管理:
Docker 倉庫:
Docker 官方已經把倉庫封裝為映象,直接通過啟動容器就可以部署完成倉庫:
# docker run -d --name registry -p 5000:5000 -v /opt/registry:/var/lib/registry registry:2.3.1
目錄 /var/lib/registry 是倉庫存放映象的位置。除了使用資料卷做映象儲存之外,Registry
還支援將映象儲存到 亞馬遜的 S3,OpenStack 的 Swift/Glance 等儲存後端。
# docker tag nginx localhost:5000/nginx:latestDocker 映象的命名規則 localhost:5000/nginx:latest 中,localhost:5000 表示 Registry 的地
址和埠。
# docker push localhost:5000/nginx:latest
推送映象到 localhost:5000 倉庫
# docker rmi localhost:5000/nginx:latest 刪除本地 nginx 映象的 TAG
# docker pull localhost:5000/nginx:latest 拉取映象到本地
1.1>製作安裝httpd的映象
登陸測試:在另一個bash測試安裝命令是否正確
docker run -it --name vm1 rhel7 bash
cd /etc/yum.repos.d/
vi dev.repo
yum repolist
yum install -y httpd
在docker目錄下編輯安裝指令碼進行編譯
vim Dockerfile
FROM rhel7
MAINTAINER [email protected].org
ENV HOSTNAME server1
EXPOSE 80
COPY dvd.repo /etc/yum.repos.d/dvd.repo
RUN rpmdb --rebuilddb && yum install -y httpd && yum clean all
VOLUME ["/var/www/html"]
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND" ]
[dvd]
name=rhel7.3
baseurl=http://172.25.30.250/rhel7.3
gpgcheck=0
docker build -t rhel7:v1 . #在本地編譯構建映象
1.2>製作安裝ssh的映象:
製作映象之前我們需要將操作測試一下看看是否正確
cd /etc/docker
mkdir ssh
cd ssh
cp ../dvd.repo .
vim Dockerfile
FROM rhel7
MAINTAINER [email protected].org
ENV HOSTNAME server2
EXPOSE 22
COPY dvd.repo /etc/yum.repos.d/dvd.repo
RUN rpmdb --rebuilddb && yum install openssh-server openssh-client -y && ssh-keygen -q -t rsa -f /etc/ssh/ssh_host_rsa_key -N "" && ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N "" && ssh-keygen -q -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N "" && echo root:westos | chpasswd
CMD ["/usr/sbin/sshd", "-D"]
docker build -t rhel7:v2 .
docker run -d –name vm4 rhel7:v2
檢視映象:
刪除映象:
docker rmi rhtl7:v2
2. 在映象內部封裝靜態資訊
構建一個nginx映象:
cd /tmp/docker/test
mkdir nginx
mkdir /tmp/docker/test/nginx/html
vim /tmp/docker/test/nginx/html/index.html
<h1>www.westos.org</h1>
<h1>www.westos.org</h1>
<h1>www.westos.org</h1>
<h1>www.westos.org</h1>
<h1>www.westos.org</h1>
<h1>www.westos.org</h1>
tar cf html.tar nginx #打包靜態檔案到html
tar tf html.tar nginx #檢視
nginx/
nginx/html/
nginx/html/index.html
vim /tmp/docker/test/Dockerfile
FROM rhel7
ADD html.tar /usr/share/
VOLUME ["/usr/share/nginx/html"]
docker build -t rhel7:v4 . #構建一個v4的映象內部封裝了靜態頁面
docker create --name vol rhel7:v4 bash
docker run -d --name vm1 --volumes-from vol nginx
curl 172.17.0.2 #訪問容器內部
搭建nginx倉庫
docker push westos.org:5000/nginx
docker tag nginx localhost:5000/nginx
docker push localhost:5000/nginx
3. 遠端加密
搭建nginx倉庫5000埠做法只能實現本地使用者登陸,遠端登陸根本無法登陸;我們希望可以通過加密也可以實現遠端從倉庫下載映象;因此我們做了443的埠對映和加密證書
cd /tmp/docker/
mkdir certs
openssl req -newkey rsa:4096 -nodes -sha256 -keyout certs/domain.key -x509 -days 365 -out certs/domain.crt
#生成製作證書在certs目錄下面
docker run -d --restart=always --name registry -v `pwd`/certs:/certs -e REGISTRY_HTTP_ADDR=0.0.0.0:443 -e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt -e REGISTRY_HTTP_TLS_KEY=/certs/domain.key -p 443:443 registry:2 #生成443加密埠以便遠端登陸
docker tag nginx westos.org/nginx #建立nginx倉庫
cd /etc/docker/
mkdir -p certs.d/westos.org
cd certs.d/westos.org/
cp /tmp/docker/certs/domain.crt ./ca.crt #copy證書
docker push westos.org/nginx #推送nginx映象生成443---
>5000的埠對映
5. 新增認證
新增認證:建立認證使用者並新增使用者密碼
為了保證環境的乾淨我們把之前的volume刪除
docker volume rm d81c6c2a8a60ba5d997da08e4c555101ca1eb1ce9217db115e9824cdfe9d7025
d81c6c2a8a60ba5d997da08e4c555101ca1eb1ce9217db115e9824cdfe9d7025
docker volume ls
DRIVER VOLUME NAME
cd .. #注意`pwd`表當前路徑所以加密認證要在/tmp/docker執行
新增認證使用者及密碼wei;westos
docker run --entrypoint htpasswd registry:2 -Bbn wei westos > auth/htpasswd
cat auth/htpasswd #生成資訊記錄在當前目錄下的auth/htpasswd檔案中
新增兩個認證使用者
docker run --entrypoint htpasswd registry:2 -Bbn admin admin >> auth/htpasswd
cat auth/htpasswd
docker ps -a #檢視使用者程序
[[email protected] docker]# htpasswd -cm htpaswd wei #建立使用者新建使用者c;再次建立使用者不需要加c否則會被覆蓋
htpasswd -m htpaswd admin
docker container prune #暫停容器服務
docker run -d --restart=always --name registry -v `pwd`/certs:/certs -e REGISTRY_HTTP_ADDR=0.0.0.0:443 -e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt -e REGISTRY_HTTP_TLS_KEY=/certs/domain.key -v `pwd`/auth:/auth -e "REGISTRY_AUTH=htpasswd" -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" -e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd -p 443:443 registry:2 #新增加密認證,證書位置金鑰路徑443埠對映
docker push westos.org/rhel7 #推送
docker tag nginx westos.org/nginx #上傳映象
docker login -u wei -p westos westos.org #登陸認證登陸,登陸成功之後下次登陸不用認證
生成認證資訊記錄在~.docker/config.json檔案中
6 .Docker-compose整合實現負載均衡
安裝docker-compose即下即使用
cd /tmp/docker/
mkdir compose
vim docker-compose.yml
cp -r web/ compose/
ls compose/
mv docker-compose.yml compose/
cd compose/
mkdir haproxy
cd haproxy/
vim haproxy.cfg #haproxy實現負載均衡
global #全域性變數
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
stats uri /status
frontend balancer
bind 0.0.0.0:80
default_backend web_backends
backend web_backends
balance roundrobin
server web1 apache:80 check
server web2 nginx:80 check
[[email protected] haproxy]# vim /tmp/docker/compose/docker-compose.yml #構建整個均衡負載映象
訪問8080代理埠測試是否實現: