云上的天涯

导航

docker swarm集群搭建以及使用滚动更新

基础环境,三台虚拟机
172.17.3.70
172.17.3.71
172.17.3.72
系统配置:centos 7,关闭selinux
需要优化的基础配置:
[root@sw1 ~]# vim /etc/rc.local
ulimit -SHn 65535
modprobe br_netfilter
sleep 60 && /sbin/iptables -P FORWARD ACCEPT
sleep 15 && docker container prune -f
[root@sw1 ~]# chmod a+x /etc/rc.d/rc.local
[root@sw1 ~]# vim /etc/sysctl.conf
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 10000 65000
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_max_tw_buckets = 5000
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
vm.swappiness = 0
net.ipv4.ip_forward = 1
更改下主机名,基本就没别的了。
接下来安装docker,为了使用高版本的docker-compose.yml语法,安装了最新的docker-ce
yum install -y vim-enhanced.x86_64 wget net-tools epel-release
以上是安装基础工具
然后正式开始安装docker
yum install -y yum-utils
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install  -y --setopt=obsoletes=0 docker-ce-18.06.1.ce-3.el7.x86_64
安装完毕后,编辑下docker.service启动文件增加如下配置
[root@sw1 ~]# vim /usr/lib/systemd/system/docker.service
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd \
          --storage-driver=overlay2 \
          --storage-opt overlay2.override_kernel_check=1 \
          --exec-opt native.cgroupdriver=cgroupfs \
          --insecure-registry 172.17.3.14:5000
ExecReload=/bin/kill -s HUP $MAINPID
红色部分为增加部分,更改存储驱动为overlay2,提升性能,增加了一个私有仓库,方便测试
重载一下服务
[root@sw1 ~]# systemctl daemon-reload
三台物理机均如此。
开始建立swarm集群
我们在172.17.3.70执行如下命令
docker swarm init ###初始化集群
然后在其他两台机器上按照提示命令加入集群,至此集群建立完毕
部署一个service服务,我们使用stack命令来部署,需要新建一个yml文件
[root@sw1 ~]# vim nginx.yml
version: "3"
services:
  webs:  ####新建服务的名字
    image: 172.17.3.14:5000/k8s/nginx:1.0  ####使用的镜像
    deploy:
      replicas: 2  ####初始数量为2
      resources:
        limits:  ###最大资源使用量
          cpus: "0.1"
          memory: 50M
      restart_policy:  ###失败后重启策略
        condition: on-failure
    ports:  ###映射到主机那个端口,前面是主机端口后面是容器开放端口
      - "80:80"
    networks:  ####使用那个网络
      - ifeng
networks: ###同上,使用那个网络
  ifeng:
在创建服务前我们先看下swarm集群的网络
其中Ingress是给服务使用的集群共享网络,其他的自行查询
我们创建服务后会再创建一个集群共享网络
[root@sw1 ~]# docker stack deploy -c nginx.yml house
可以看到服务已经创建,网络已经创建,命令最后的house,是指哪个组,可以把一些服务都放到这个组里,这个命令的组名是house
我们再来看看集群的网络
可以看到多出来一个house_ifeng的网络,在其他节点上也可以看到,新网络是组名+网络名的方式组成的,网络名是在上面的yml文件定义的,如果有好多个服务需要使用同一个网络的话,就需要在定义yml文件时候写网络名写一致,比如都写ifeng,然后创建服务时候记得放到同一组里,比如上面的house就是house组,这样创建出来的服务都在一个网络里了
我们看下创建的服务
我们可以看到,集群显示的服务名是以为组+自定义的服务名方式组成的,我们yml文件里定义了服务名叫webs那么创建后显示的服务名就是house_webs,显示有2个副本,以及端口映射
至此我们在任意节点访问节点IP+端口就可以访问到服务了
滚动更新
[root@sw1 ~]# docker service update --image httpd:latest  --update-parallelism 1 --update-delay 2s house_webs ##最后跟服务名
--image: 使用那个镜像
 --update-parallelism:每次最大更新副本数
--update-delay:延迟几秒
执行后,可以进行滚动更新,不会影响服务
更新完毕
可以看到镜像已改变
完成。
 

posted on 2019-01-11 16:19  云上的天涯  阅读(480)  评论(0编辑  收藏  举报