docker 项目案例

标签(空格分隔): docker的部分


  • 一:docker的Compose 打包部署工具
  • 二:CI/CD 构建持续集成环境
  • 三:实现CI/CD发布项目
  • 四:容器服务注册与发现

一:docker的Compose 打包部署工具

1.1:compose的介绍

1 、介绍
Compose是一个定义和管理多容器的工具,使用Python语言编写。使用Compose配置文件描述多个容器应用的架构,比如使用什么镜像、数据卷、网络、映射端口等;然后
一条命令管理所有服务,比如启动、停止、重启等。
2 2 、安装
curl -L https://github.com/docker/compose/releases/download/1.15.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
或者
pip install docker-compose
3 3 、 YAML 文件格式及编写注意事项
YAML是一种标记语言很直观的数据序列化格式,可读性高。类似于XML数据描述语言,语法比XML简单的很多。
YAML数据结构通过缩进来表示,连续的项目通过减号来表示,键值对用冒号分隔,数组用中括号括起来,hash用花括号括起来。
YAML文件格式注意事项:
1.不支持制表符tab键缩进,需要使用空格缩进
2.通常开头缩进2个空格
3.字符后缩进1个空格,如冒号、逗号、横杆
4.用井号注释
5.如果包含特殊字符用单引号引起来
6.布尔值(true、false、yes、no、on、off)必须用引号括起来,这样分析器会将他们解释为字符串。

1.2 compose的常用参数

image_1ccfvlglm1b2c1rcr1s3d1951iu19.png-305.7kB

image_1ccfvm2pmn0nh6qd91ot2vorm.png-192.7kB

1.3 一键部署lnmp

tree compose_lnmp 

image_1ccfvru7haeiudl1la6bvt1r32j.png-212kB

docker-compose -f docker-compose.yml up  一直到构建接收

image_1ccg025t5198n142t18r01ob4192n30.png-228.4kB

image_1ccg0357ica18ta1n4o1acc19a63d.png-316.6kB

image_1ccg0549oug01a0dasjnujc8l5q.png-297.3kB

docker-compose -f docker-compose.yml up -d  ### 后台查看

docker-compose -f docker-compose.yml ps     ### 查看运行的容器

image_1ccg07d0s7f1m0h1gvt18161c5c67.png-134.9kB

image_1ccg07r2djti6m053d171h1joq6k.png-197.8kB

image_1ccg09ji46i8ckl10a0neo11iv71.png-318.9kB

1.4 wordpress 测试

mv wordpress-4.7.4-zh_CN.tar.gz /root/compose_lnmp/wwwroot/
cd /root/compose_lnmp/wwwroot/
tar -zxvf /root/compose_lnmp/wwwroot/

测试: http://172.17.100.12:81/wordpress/

image_1ccg0j765fg86eh15acf1g9dj7e.png-221.5kB

此处用户名密码在docker-compose.yml里面已经定义好了
数据库主机填写:mysql 

image_1ccg0splk19qgh6e4kbkom1f4d8r.png-244.5kB
image_1ccg11urb1ao56331esn1qsm1e3sao.png-2785.4kB

1.5 一键部署 Nginx 反向代理 Tomcat

tree compose_nginx_tomcat/

image_1ccg169311ursnfpks1o721d20c5.png-225.6kB

部署jdk 

cp -p jdk-8u45-linux-x64.tar.gz /root/compose_nginx_tomcat/
cd /root/compose_nginx_tomcat/
tar -zxvf jdk-8u45-linux-x64.tar.gz
mv jdk1.8.0_45/ /usr/local/

1.6 执行compose构建

docker-compose -f docker-compose.yml up  ### 一直到执行结束

image_1ccg25d0g1gro95b1ea1qg41m5ef2.png-396kB

docker-compose -f docker-compose.yml up -d 
docker-compose -f docker-compose.yml ps 

image_1ccg280imvt96401agu16geupufs.png-162.5kB

image_1ccg293nh198e1sg1ja8bbi1qrlg9.png-215.1kB

访问测试:
http://172.17.100.12:82/

image_1ccg2ou1mca01dff1mlbbvl1lbegm.png-88.8kB

二:构建持续集成环境

2.1 CI(持续集成)/CD(持续交付/持续部署)

image_1ccg2tpdqnmblf5183qq0drtsh3.png-653.6kB

2.2 发布流程设计

image_1ccg37jifie91p7e1j7t1i0u1btni0.png-240.4kB

2.3 构建docker的harbor的私用仓库

下载harbor 软件:harbor-offline-installer-v1.1.2.tgz
     wget https://github.com/vmware/harbor/releases/download/v1.1.2/harbor-offline-installer-v1.1.2.tgz
     tar xvf harbor-offline-installer-v1.1.2.tgz

   mv harbor /usr/local/
   cd /usr/local/harbor 

   ----
   vim harbor.cfg 
  ---
   更改harbor 的hostname 
   hostname = 172.17.100.13
  ---

image_1ccilspca8cq1e0eedg72311l9p.png-111kB

执行harbor脚本开始构建
cd /usr/local/harbor/
./install.sh   #### 一直到构建完毕

····
····
····

---

docker ps -a 

image_1ccim0jisjvn1s6d1ok2q4817pe16.png-155.4kB
image_1ccim3bh91097125i1kjh1fkmq671j.png-469.8kB

打关浏览器:
http://172.17.100.13 

用户名: admin  密码: Harbor12345

增加一个项目flyfish

image_1ccima8u41g8d561o3gllq190o4s.png-214.7kB

image_1ccimappf83n1cn91pbq9t47sp59.png-164.6kB

image_1ccimd3561qj11ce51hse2iqhcj5m.png-221.2kB

更改增加一个registries
vim /etc/docker/daemon.json 
----
{
"registry-mirrors": [ "https://registry.docker-cn.com"],
"insecure-registries":["172.17.100.13"]      ### 增加一行 
----

然后重启docker 

service docker restart

image_1ccimjem6135k6df2f81e61at773.png-113.5kB

登录docker 仓库

docker login 172.17.100.13 

输入用户名 admin  密码: Harbor12345

image_1ccimr30o1vm22rf1k1n11661c6a80.png-121.1kB

给上传仓库的镜像 打一个tag
docker tag nginx:latest 172.17.100.13/flyfish/nginx:latest

上传push 到镜像仓库
 docker push 172.17.100.13/flyfish/nginx

image_1ccio0psf1o7916rf19vvmm210018d.png-187.1kB

三:实现CI/CD发布项目

3.1: 环境信息

172.17.100.11    jenkins 
172.17.100.12    docker (tomcat)
172.17.100.13    git/harbor 

3.2.1 安装git 服务

172.17.100.13

yum install -y git 

useradd git && echo git |passwd  git --stdin 
su - git 

mkdir solo.git 
cd solo.git 
git --bare init

image_1ccveui2i1p7j1s6v1vprlgc11r09.png-359.9kB

image_1ccvf06ik3t81edgd2s1mktcibm.png-316.4kB


172.17.100.11:

cd /root/

git clone https://github.com/b3log/solo.git 

cd solo/src/main/resources

vim latke.properties
---
serverHost=172.17.100.12

serverPort=8888
---

模拟提交solo 到git 

mkdir test 
cd test 
git clone root@172.17.100.13:/home/git/solo.git

cp -ap /root/solo/* /root/test/solo/

cd /root/test/solo/

git add .
git commit -m "all"
git push oriagin master 

image_1ccvi80b11dsnre01mo11h8ctem23.png-261.3kB

3.2.2 安装 jenkins

配置jenkins 

mkdir jenkins 
cd jenkins 
vim Dockerfile
----
FROM jenkins
USER root
RUN echo '' > /etc/apt/sources.list.d/jessie-backports.list && \
wget http://mirrors.163.com/.help/sources.list.jessie -O /etc/apt/sources.list
RUN apt-get update && apt-get install -y git libltdl-dev
----

docker build -t jenkins:v1 .

image_1ccvj5ug6dh513ii1q4ebkp6tpp.png-539kB


生成jenkins 容器:

docker run -d \
--name jenkins \
-p 8080:8080 \
-v /var/jenkins_home/:/var/jenkins_home \
-v /usr/local/apache-maven-3.5.0:/usr/local/maven \
-v /usr/local/jdk1.8.0_45:/usr/local/jdk \
-v /var/run/docker.sock:/var/run/docker.sock \
-v $(which docker):/usr/bin/docker \
-v ~/.ssh:/root/.ssh \
jenkins:v1

----

image_1ccvj71vd11631otnjcm168p1uk316.png-681.6kB

打开jenkins 的web 页面:

http://172.17.100.11:8080

image_1cd1pfmp1uad12n911rs12vc4os19.png-235kB

image_1cd1pjvmv1a9adno178p1b2a1dbi1m.png-240.4kB

image_1cd1pm28246q1n4nnfi1hlbvtv2j.png-185.4kB

image_1cd1q9c1jgk2gpm1drv1qrt141h30.png-175.9kB

image_1cd1qdc8g18ma12rk1k501kcbh8v6a.png-230.6kB

3.2.3 配置tomcat镜像并提交harbor仓库

172.17.100.12 : (tomcat)
引用构建一个tomcat的基础镜像
---
FROM centos:7
MAINTAINER www.aliangedu.com

RUN yum install unzip iproute -y

ENV JAVA_HOME /usr/local/jdk

ADD apache-tomcat-8.0.46.tar.gz /usr/local
RUN mv /usr/local/apache-tomcat-8.0.46 /usr/local/tomcat

WORKDIR /usr/local/tomcat
EXPOSE 8080
ENTRYPOINT ["./bin/catalina.sh", "run"]

---

docker build -t 172.17.100.13/tomcat:v1 .

提交镜像到仓库:harbor 

docker login 172.17.100.13 

image_1cd1rg1sd7it17g61mar1e801q6n.png-417.4kB
image_1cd1rh3pe2irqcpvo43pj176s74.png-568.6kB

将镜像上传打一个tag 

docker tag 172.17.100.13/tomcat:v1 172.17.100.13/flyfish/tomcat:v1

docker push 172.17.100.13/flyfish/tomcat

image_1cd1segp01iihs3i1d541muk1p9nah.png-203.4kB

打开harbor 仓库查看是否提交

image_1cd77eau538n7v9rtnmen10ou9.png-210.6kB

image_1cd77gl6rgkq1v06hqk1ord1o7213.png-171.4kB

3.3.4 配置jenkins 构建

image_1cd77sp351ej411tlhrqvhfr291t.png-391.6kB

image_1cd77tvce9kt12u41c6n1r6kchc2a.png-51.1kB

image_1cd77v8h18141nk87dlvnvktt37.png-63.9kB

image_1cd7804qi9aeoaebe7c27d8q44.png-85.6kB

image_1cd780uuq1e916k31bt7aodff94h.png-153.1kB

image_1cd7833h1npvs0o1l4311sdjbr4u.png-311.3kB

image_1cd7858ti1hqu1g1hu2k9r61sne5b.png-150.2kB

image_1cd786bemp9j11k5hnt1ktnn4n5o.png-183.9kB

image_1cd787kn0qit16g8v5j1caa130p6l.png-376kB

image_1cd78a8i51qbh1b1p7951kq7107j8i.png-395.9kB

image_1cd78b5o81d6anl2re61bsa13fc8v.png-103.1kB

新建一个工程

image_1cd78m36vfkd16htiup5td13pk9s.png-222.4kB

image_1cd7a66fvk24h9n1ndmodbf7639.png-98.1kB

image_1cd79t0t1vq6pu21ab21msh1l.png-123.8kB

image_1cd7a3ldd18k61dg11rchcmeqf022.png-63.3kB

image_1cd7a47aq1o6g1lf9935v201rc22f.png-166.8kB

image_1cd7a4lv4k9v128516nd1ivl4ij2s.png-126.2kB

image_1cd7a7fh710of1mqg11mslmub3u3m.png-161.1kB

image_1cd7aaq2v1pcjotn8eb8l98va43.png-165.9kB

image_1cd7abhpe1q5v1euu1sup15qrbhg4g.png-77.8kB

image_1cd7ace1mlsh1dmlb0a8879p04t.png-536.3kB

image_1cd7adk4413r5189r146dsjgej35q.png-181.8kB

image_1cd7asfvc32je2vv54tfqbtl67.png-285.5kB

image_1cd9rlq4e8uk14g3169450hrrf9.png-195.3kB

image_1cd9rmrtrsch1p4obnslpc12ek16.png-230.3kB

image_1cd9rnjrmraj6go1p3310h5i5r1j.png-224.1kB

image_1cd9rodof1toaqf5oit1jon19qv20.png-738.5kB

四:容器服务注册与发现

4.1 :Consul的介绍与安装

1 、介绍
Consul是一个分布式、高可用性,在基础设施中发现和配置服务的工具。
2 、安装
下载二进制Consul包:https://www.consul.io/downloads.html
# unzip consul_0.9.2_linux_amd64.zip
# mv consul /usr/bin
3 、部署
# consul agent \
-server \
-bootstrap \
-ui \
-data-dir=/var/lib/consul-data \
-bind=172.17.100.11 \
-client=0.0.0.0 \
-node=server01

image_1cda0bnfcja5bj61q8le1nse12d.png-242.5kB
image_1cda0erec54s1djl1c2v1t83141t37.png-315.2kB
image_1cda0eb2o1bt6g613tf1a82tcq2q.png-192.5kB

放在后台运行:

nohup consul agent -server -bootstrap -ui -data-dir=/var/lib/consul-data -bind=172.17.100.11 -client=0.0.0.0 -node=server01 & > /var/log/consul.log &

4.2 consul 的相关命令

查看集群信息:
consul members
consul info |grep leader
consul catalog services
通过 HTTP API 获取集群信息 :
curl 127.0.0.1:8500/v1/status/peers # 集群server成员
curl 127.0.0.1:8500/v1/status/leader # 集群Raft leader
curl 127.0.0.1:8500/v1/catalog/services  # 注册的所有服务
curl 127.0.0.1:8500/v1/catalog/services/nginx # 服务信息
curl 127.0.0.1:8500/v1/catalog/nodes # 集群节点详细信息

image_1cda0muc6chpni31o4q1vk71j1p3k.png-113kB
image_1cda0ncgorlf1rel1l361btf1m0e41.png-143kB


###4.3手动注册一个服务到consul中

curl -X PUT -d \
'{"id": "jetty","name": "service_name","address": "172.17.100.12","port": 8081,"tags": ["test"],"checks": [{"http":
"http://172.17.100.11:8081/","interval": "5s"}]}' \
http://172.17.100.11:8500/v1/agent/service/register

image_1cda0s4ao1vdqr73pu61m891l3a4e.png-206kB

image_1cda0snde1d7teq31ukkuij11kq4r.png-244.1kB

4.3 Docker+Registrator+Consul实现容器服务自动加入Nginx集群

consul- template :
一个守护程序,用于实时查询consul集群数据,并更新文件系统上的任意数量的指定模板,生成配置文件,更新完成后可以选择运行任何Shell命令。

gliderlabs/registrator: 
检查容器运行状态自动注册和注销Docker容器的服务到服务配置中心。目前支持Consu
l、etcd和SkyDNS2。

https://github.com/hashicorp/consul-template
https://releases.hashicorp.com/consul-template/0.19.3/consul-template_0.19.3_linux_amd64.zip

image_1cda1d8l2g901grd7pebs8198458.png-268.9kB

4.4 Docker 主机启动注册器

172.17.100.12 上面:

docker run -d \
--name=registrator \
--net=host \
-v /var/run/docker.sock:/tmp/docker.sock \
--restart=always \
gliderlabs/registrator:latest \
-ip=172.17.100.12 \
consul://172.17.100.11:8500

image_1cda1gig2su3c24t8h4l7hr5l.png-351.7kB

image_1cda1h8t31dki1iv51cod1v0rbaa62.png-522.1kB

4.5 安装nginx 服务器

172.17.100.11

yum install -y gcc gcc-c++ make openssl-devel pcre-devel
tar -zxvf nginx-1.12.1.tar.gz 
cd nginx-1.12.1/
./configure --prefix=/usr/local/nginx && make -j 2 && make install

cd /usr/local/nginx 

sbin/nginx

ps -ef |grep nginx 

image_1cda1v1na5au9h4gaot5s18m17c.png-176.9kB

image_1cda1vi0d1rug1rimtaq145ckhh7p.png-153.8kB

更改nginx 的配置文件增加注册时候生成的配置文件目录

cd /usr/local/nginx/conf

mkdir vhost 

vim nginx.conf
----
在最后增加一个目录

include vhost/*.conf
----

image_1cda593mcs5erorfkq85t1k3e86.png-45.8kB


4.6 配置consul-template

172.17.100.11 上面配置

unzip consul-template_0.19.3_linux_amd64.zip
mv consul-template /usr/bin/

mkdir consul
cd consul
vim nginx.ctmpl
----
upstream http_backend {
     #  ip_hash;
      {{range service "nginx"}}
      server {{ .Address }}:{{ .Port }};
      {{ end }}
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://http_backend;
}
}
----

image_1cda5go631lakbfa1s9somm115v8j.png-223.2kB

nohup consul-template -consul-addr 172.17.100.11:8500 -template "./nginx.ctmpl:/usr/local/nginx/conf/vhost/site.conf:/usr/local/nginx/sbin/nginx -s reload" -log-level=info & > /var/log/site-nginx.log &

image_1cda5mi1s1i8vbci1e0h1iem1do49v.png-186.1kB

image_1cda5nc0p1j4mq87gqa128utulac.png-145.4kB

image_1cda5sadq1cdpu2r1ifv19ldqimb6.png-79kB