Docker 云搭建

原创
2019/07/24 10:33
阅读数 27

Docker 安装

yum -y install docker-io

搜索镜像 docker search

获取镜像 docker pull

查看镜像 docker images

删除镜像 docker rmi

docker registry 搭建

docker run -d -p 5000:5000 --restart=always --name  registry docker.io/registry


上传镜像
docker tag 8140d0c64310 10.10.0.201:5000/analysys-centos
docker push 10.10.0.201:5000/analysys-centos

使用私服镜像
docker run -ti 10.10.0.201:5000/analysys-centos /bin/bash

docker registry ui : Portus 安装 被墙没安装成功

1) 安装docker-compse
2) git Clone  https://github.com/SUSE/Portus.git
3) 修改Gemfile  的第一行  "https://rubygems.org" 改为 “http://rubygems.org”
4)  运行 compose-setup.sh -e server IP   进行构建

企业级 docker registry : Harbor

1)下载 离线安装包
2) 解压  unzip harbor-release-1.1.0.zip
3)修改  harbor.cfg  文件,修改 hostname , admin , password 及邮箱相关信息
4)# ./prepare   
5) # ./install.sh
6) # docker-compose stop   停止
7) # docker-compose start  启动
8) 修改配置文件 
    #  vi /etc/sysconfig/docker
    
    OPTIONS  后加入  --insecure-registry 10.10.0.210
9)重启 docker
10) 登录私服
    #  docker login 10.10.0.210
    
    [root[@manager](https://my.oschina.net/u/184929) harbor]# docker login 10.10.0.201
    Username: admin
    Password: 
    Login Succeeded
 
11)提交公共镜像    
    #  docker pull centos
    #  docker  tag  centos   10.10.0.201/library/centos
    #  docker push  10.10.0.201/library/centos

image

12)登录harbor 新增 pro1 私有项目 ,新增测试用户  demo1 

13) admin 用户 提交至私库

13) 退出登录 
    # docker  logout 10.10.0.201
    
14) demo 用户拉取私库镜像
    [root[@manager](https://my.oschina.net/u/184929) harbor]# docker pull 10.10.0.201/pro1/centos:latest
    Trying to pull repository 10.10.0.201/pro1/centos ... 
    Pulling repository 10.10.0.201/pro1/centos
    Error: image pro1/centos:latest not found
    [root[@manager](https://my.oschina.net/u/184929) harbor]# 
    
15)harbor 私库

image

Harbor 默认管理员用户为 admin ,密码在 harbor.cfg 中设置过,默认的是Harbor12345

docker 1.12 内置 swarm 搭建 (swarm 模式 )

manger  10.10.0.201
node1   10.10.0.210
node2   10.10.0.211

manger
# docker swarm init --advertise-addr 10.10.0.201

保存token  SWMTKN-1-1m2y8zflq4yp6kre9dg5xpr7kfrf5svc6d70o6ouk7656vk9kw-bdaeh3q9kn79fhmgm9hj8n5fo

# docker swarm join-token manager 

查看集群
# docker node ls 


node1 
# docker swarm join  --token SWMTKN-1-1m2y8zflq4yp6kre9dg5xpr7kfrf5svc6d70o6ouk7656vk9kw-bdaeh3q9kn79fhmgm9hj8n5fo   10.10.0.201:2377 

node2
# docker swarm join  --token SWMTKN-1-1m2y8zflq4yp6kre9dg5xpr7kfrf5svc6d70o6ouk7656vk9kw-bdaeh3q9kn79fhmgm9hj8n5fo   10.10.0.201:2377




忘记token,可使用如下命令查询怎么加入:
docker swarm join-token worker

创建服务

    在管理节点上创建一个服务,每隔三秒输出一个 “hello world”
    #  docker service create --replicas 1 --name helloworld busybox:1.25.1-musl /bin/sh -c "while true; do echo hello world; sleep 3; done"

查看服务

    在管理节点上
    
    # docker service ls 

image

    查看服务详细信息
        # docker service inspect --pretty helloworld 
        
        ID:		2slcckk31k1qh14x6bybb3b7q
        Name:		helloworld
        Mode:		Replicated
         Replicas:	1
        Placement:
        UpdateConfig:
         Parallelism:	1
         On failure:	pause
        ContainerSpec:
         Image:		busybox:1.25.1-musl
         Args:		/bin/sh -c while true; do echo hello world; sleep 3; done
        Resources:
        
        
查看运行情况
    # docker service ps helloworld
    
    ID                         NAME          IMAGE                NODE               DESIRED STATE  CURRENT STATE          ERROR
    16pafuhjq6ksfr2lg4vcoobzs  helloworld.1  busybox:1.25.1-musl  manager  Running        Running 5 minutes ago 

动态伸缩服务

 #  docker service scale helloworld=5
 
 我们将服务的数量伸缩到5

删除服务

# docker service rm helloworld

docker服务滚动更新

部署一个基于Redis 3.07镜像的服务。然后使用滚动更新将服务更新到使用Redis3.2.5 镜像

在swarm中部署 Redis3.0.7,并配置 10 秒 更新延迟

#  docker service create --replicas 3 --name redis --update-delay 10s redis:3.0.7-alpine

更新 redis 服务至 3.2.5 版本

# docker service update --image redis:3.2.5-alpine redis 

ID                         NAME         IMAGE               NODE     DESIRED     STATE  CURRENT STATE                   ERROR
bc1uicc97h68tdv26jlxiyuqz  redis.1      redis:3.2.5-alpine  manager  Running        Running 33 seconds ago          
1i8pltd1fs0fx50e00vpe25ow   \_ redis.1  redis:3.0.7-alpine  node2    Shutdown       Shutdown 37 seconds ago         
1hwbiy3ilfo2xfd9m87lqwvih  redis.2      redis:3.2.5-alpine  node2    Running        Running 17 seconds ago          
494pyp6t53szw6fvzquxgah4u   \_ redis.2  redis:3.0.7-alpine  node1    Shutdown       Shutdown 21 seconds ago         
dvji4l8k9pj7wguu8xjc3diju  redis.3      redis:3.2.5-alpine  node1    Running        Running less than a second ago  
7ps32ortyujpigibaxoss8k2y   \_ redis.3  redis:3.0.7-alpine  manager  Shutdown       Shutdown 5 seconds ago 

自动调度

下线某个节点 ( 维护机器等 )

# docker node ls

ID                           HOSTNAME  STATUS  AVAILABILITY  MANAGER STATUS
7m29632ceez5vamqf2e9epj90 *  manager   Ready   Active        Leader
chzgekn7ls17h20sa4irm5e9a    node1     Ready   Active        
eh9xldypvnlnfx55ycuib3d7o    node2     Ready   Active 


查看运行状态的 redis
#  docker service ps -f desired-state=running redis

ID                         NAME     IMAGE               NODE     DESIRED     STATE  CURRENT STATE          ERROR
bc1uicc97h68tdv26jlxiyuqz  redis.1  redis:3.2.5-alpine  manager  Running        Running 4 minutes ago  
1hwbiy3ilfo2xfd9m87lqwvih  redis.2  redis:3.2.5-alpine  node2    Running        Running 4 minutes ago  
dvji4l8k9pj7wguu8xjc3diju  redis.3  redis:3.2.5-alpine  node1    Running        Running 3 minutes ago 


下线node2 节点

#  docker node update --availability drain node2

#  docker node ls 

ID                           HOSTNAME  STATUS  AVAILABILITY  MANAGER STATUS
7m29632ceez5vamqf2e9epj90 *  manager   Ready   Active        Leader
chzgekn7ls17h20sa4irm5e9a    node1     Ready   Active        
eh9xldypvnlnfx55ycuib3d7o    node2     Ready   Drain   

#  docker service ps -f desired-state=running redis

ID                         NAME     IMAGE               NODE     DESIRED STATE  CURRENT STATE           ERROR
bc1uicc97h68tdv26jlxiyuqz  redis.1  redis:3.2.5-alpine  manager  Running        Running 6 minutes ago   
c36sp9bl4bhf4krvbh4sv2q2r  redis.2  redis:3.2.5-alpine  node1    Running        Running 19 seconds ago  
dvji4l8k9pj7wguu8xjc3diju  redis.3  redis:3.2.5-alpine  node1    Running        Running 5 minutes ago 

将下线节点重置为活动状态

# docker node update --availability active node2

ID                           HOSTNAME  STATUS  AVAILABILITY  MANAGER STATUS
7m29632ceez5vamqf2e9epj90 *  manager   Ready   Active        Leader
chzgekn7ls17h20sa4irm5e9a    node1     Ready   Active        
eh9xldypvnlnfx55ycuib3d7o    node2     Ready   Active 

重新分配任务给激活节点

#  docker service scale redis=5

#  docker service ps -f desired-state=running  redis

ID                         NAME     IMAGE               NODE     DESIRED STATE  CURRENT STATE           ERROR
bc1uicc97h68tdv26jlxiyuqz  redis.1  redis:3.2.5-alpine  manager  Running        Running 12 minutes ago  
c36sp9bl4bhf4krvbh4sv2q2r  redis.2  redis:3.2.5-alpine  node1    Running        Running 6 minutes ago   
dvji4l8k9pj7wguu8xjc3diju  redis.3  redis:3.2.5-alpine  node1    Running        Running 11 minutes ago  
2xuyik97m7tt02s9p1w8b10xs  redis.4  redis:3.2.5-alpine  node2    Running        Running 31 seconds ago  
00y5tt10x8jp0vuu9stsd5ed8  redis.5  redis:3.2.5-alpine  node2    Running        Running 32 seconds ago 

DockerFly Web 管理

# docker pull helyho/dockerfly
# docker run \
    --name dockerfly -d \
    -v /var/run/docker.sock:/var/run/docker.sock \
    --restart always \
    -p 28083:28083 \
    helyho/dockerfly
    
    
访问 http://ip:28083 ,使用 admin / 1234 就可以登录dockerfly


支持多主机,需要监听node机器上的docker 通讯端口
方案: 
    1) 安装 socat 
    2) socat /var/run/docker.sock  映射端口
    3) 避免程序关闭,编写系统service脚本
    
    #  cd /etc/init.d/
    #   vi socat_2375.sh
        
        #!/bin/bash

        #description: socat_2375.sh
        #chkconfig: 2345 20 81 

        nohup socat tcp4-listen:2735,reuseaddr,fork unix-connect:/var/run/docker.sock &
    
    说明:后台运行  socat 命令, 把 unix socket 转换成 tcp 端口,2735 可自定义指定
    
    # chmod 700 socat_2375.sh
    # chkconfig --add socat_2375.sh
    # service socat_2375.sh start
    # chkconfig socat_2375.sh on
    
    查看是否启动成功
    # ps -ef | grep socat
    
    root      5439     1  0 14:31 pts/1    00:00:00 socat tcp4-listen:2735,reuseaddr,fork unix-connect:/var/run/docker.sock
    root      5474  2804  0 14:31 pts/1    00:00:00 grep --color=auto socat


    如果想要正常输出和错误信息都不显示,则要把标准输出和标准错误都重定向到/dev/null, 例如:
    # ls 1>/dev/null 2>/dev/null

Zabbix 安装

https://hub.docker.com/r/monitoringartist/zabbix-xxl/

docker run -d -v /var/lib/mysql --name zabbix-db-storage busybox:latest


docker run \
-d \
--name zabbix-db \
-v /backups:/backups \
-v /etc/localtime:/etc/localtime:ro \
--volumes-from zabbix-db-storage \
--env="MARIADB_USER=zabbix" \
--env="MARIADB_PASS=my_password" \
monitoringartist/zabbix-db-mariadb


docker run \
-d \
--name zabbix \
-p 80:80 \
-p 10051:10051 \
-v /etc/localtime:/etc/localtime:ro \
--link zabbix-db:zabbix.db \
--env="ZS_DBHost=zabbix.db" \
--env="ZS_DBUser=zabbix" \
--env="ZS_DBPassword=my_password" \
monitoringartist/zabbix-xxl:latest


访问: 
http://10.10.0.211
Admin
zabbix

image

监控VM节点


监控docker节点
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部