搭建Docker私有云环境

准备工作

全平台都支持(windows,osx,linux/unix),操作简单,但是至少你要对kubernetes,docker有所了解。

  1. 需要的工具
    1. Vagrant
    2. VirtualBox
  2. 下载Coreos
  3. 一键搭建集群
    1. 检查环境
    2. 优雅配置启用服务
    3. calico网络方式
    4. flannel网络方式

Tools

本来我是想用Windows平台来安装做说明,但是苦于身边没有Windows机器,刚好身边有台Centos7,所以下面的说明都是基于它的,这些工具都是全平台通用的。

  • Vagrant

    Vagrant是一个构建完整开发环境的工具,使用非常简单,有详细说明。

  • 下载安装Centos版本:
 #wget https://releases.hashicorp.com/vagrant/1.8.1/vagrant_1.8.1_x86_64.rpm
 #rpm -ivh vagrant_1.8.1_x86_64.rpm
 
  • VirtualBox

    VirtualBox是oracle开源的虚拟机,不要钱的,放心玩。

  • 下载安装最新版: Windows,Osx,Ubuntu的版本都比较好安装基本是傻瓜式的,下面介绍Centos 7的安装方式
 #cd /etc/yum.repos.d/
 #wget http://download.virtualbox.org/virtualbox/rpm/rhel/virtualbox.repo
 #yum update
 #rpm -Uvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm
 #yum install VirtualBox-5.0
 #/usr/lib/virtualbox/vboxdrv.sh setup
 #reboot
 
  • 注意:VirtualBox5.0需要使用cpu虚拟化技术,请在bois里面开启

Coreos

Coreos是个经过裁剪,非常轻量级,占用资源极少,支持平滑升级回滚的集群操作系统,可以使用fleet轻松管理Coreos集群。

  • 下载coreos
 #git clone https://github.com/Aleishus/easy-docker-cloud.git
 #cd easy-docker-cloud/coreos-vagrant/
 #vagrant up

  • 完成下载之后查看状态
  #vagrant status
   Current machine states:
     core-01                   running (virtualbox)    

  • 停止虚拟机
#vagrant destroy  -f  

Install

万事都具备,下面我们就快要将私有云搭建起来了 ^ ^

  • Check

    检查系统是否安装nfs服务

 #systemctl cat nfs-server
 # /usr/lib/systemd/system/nfs-server.service
[Unit]
Description=NFS server and services
DefaultDependencies=no
Requires= network.target proc-fs-nfsd.mount rpcbind.target
Requires= nfs-mountd.service
Wants=rpc-statd.service nfs-idmapd.service
Wants=rpc-statd-notify.service

After= local-fs.target       

...

如果没有安装请安装 yum install nfs-utils , Windows 启用NFS

  • Cloud-config

在启动之前, 我们先来看看coreos是如何优雅来控制系统服务的启动和顺序。 Coreos 整了一个叫Cloud-config的东西来自定启动的服务,使得你可以配置一个cloud-config.yaml的文件,然后在系统启动的时候通过coreos-cloudinit, 从而定义服务的依赖和启动顺序,其服务的组织模板是规范于systemd标准的。我们查看一下 easy-docker-cloud/calico-kubernetes/cloud-config/master-config.yaml.tmpl

 - name: kube-scheduler.service
      command: start
      content: |
        [Unit]
        Description=Kubernetes Scheduler
        Documentation=https://github.com/GoogleCloudPlatform/kubernetes
        Requires=kube-apiserver.service
        After=kube-apiserver.service
        [Service]
        ExecStart=/opt/bin/kube-scheduler --master=$private_ipv4:8080 --logtostderr=true
        Restart=always
        RestartSec=10
        [Install]
        WantedBy=multi-user.target

    - name: docker.service
      drop-ins:
        - name: "50-registry.conf"
          content: |
            [Unit]
            Before=calico-node.service
            [Service]
            Environment=DOCKER_OPTS='--insecure-registry="192.168.46.21:5000"'   

注意为了方便下载docker镜像,使用了私有仓库192.168.46.21:5000,你需要改成自己的(需要修改以下文件中的对应项 */cloud-config/master-config.yaml.tmpl, */cloud-config/node-config.yaml.tmpl),或者不用(GFW大家懂得)

  • Calico

私有云集群要求docker容器之间能相互访问,有独立ip且ip对其他容器是可见的。Calico通过给主机添加路由表,各主机之间通过BPG相互学习路由规则达到网络互通的目的。

ip-hops1

 

image

 

  • 一键安装
 # cd easy-docker-cloud/calico-kubernetes/
 # vagrant up
Bringing machine 'calico-01' up with 'virtualbox' provider...
Bringing machine 'calico-02' up with 'virtualbox' provider...
Bringing machine 'calico-03' up with 'virtualbox' provider...
==> calico-01: Importing base box 'coreos-stable'...
==> calico-01: Matching MAC address for NAT networking...
==> calico-01: Checking if box 'coreos-stable' is up to date...
==> calico-01: Setting the name of the VM: calico-kubernetes_calico-01_1456484431502_68953
==> calico-01: Fixed port collision for 22 => 2222. Now on port 2202.
==> calico-01: Clearing any previously set network interfaces...
==> calico-01: Preparing network interfaces based on configuration...
    calico-01: Adapter 1: nat
    calico-01: Adapter 2: hostonly
==> calico-01: Forwarding ports...
    calico-01: 22 (guest) => 2202 (host) (adapter 1)
==> calico-01: Running 'pre-boot' VM customizations...
==> calico-01: Booting VM...
==> calico-01: Waiting for machine to boot. This may take a few minutes...
    calico-01: SSH address: 127.0.0.1:2202
    calico-01: SSH username: core
    calico-01: SSH auth method: private key
    calico-01: Warning: Remote connection disconnect. Retrying...
                                                                                                                                          
  • 安装完成后
 //查看节点运行状态
 # vagrant status
 Current machine states:

calico-01                 running (virtualbox)
calico-02                 running (virtualbox)
calico-03                 running (virtualbox) 
 
 //进入kubernetes master节点
 # vagrant ssh calico-01
CoreOS stable (835.13.0)
Update Strategy: No Reboots
[email protected] ~ $ kubectl get node  //查看k8s状态
NAME            LABELS                                 STATUS    AGE
172.18.18.101   kubernetes.io/hostname=172.18.18.101   Ready     1m
172.18.18.102   kubernetes.io/hostname=172.18.18.102   Ready     47s
172.18.18.103   kubernetes.io/hostname=172.18.18.103   Ready     2s
 
[email protected] ~ $ calicoctl status   //查看calico状态
calico-node container is running. Status: Up 2 minutes
Running felix version 1.3.0rc6

IPv4 BGP status
IP: 172.18.18.101    AS Number: 64511 (inherited)
+---------------+-------------------+-------+----------+-------------+
|  Peer address |     Peer type     | State |  Since   |     Info    |
+---------------+-------------------+-------+----------+-------------+
| 172.18.18.102 | node-to-node mesh |   up  | 11:17:18 | Established |
| 172.18.18.103 | node-to-node mesh |   up  | 11:18:03 | Established |
+---------------+-------------------+-------+----------+-------------+

IPv6 BGP status
No IPv6 address configured.                                           
 
 //创建一个pod看看
[email protected] ~ $ kubectl create -f busybox.yaml
pod "busybox" created
[email protected] ~ $ kubectl get po -o wide
NAME      READY     STATUS    RESTARTS   AGE       NODE
busybox   1/1       Running   0          6s        172.18.18.103
 
 //可以看到kubernetes和calico都正常工作了,恭喜你3节点私有云已经搭建完成
  • Flannel

Flannel 通过vxlan ,udp … overlay的方式行解包封包来实现容器间的ip访问,关于flannel和calico系能对比我会在以后说明

packet-01[1]

  • 一键安装
 #cd easy-docker-cloud/flannel-kubernetes
 #vagrant up
 Bringing machine 'flannel-01' up with 'virtualbox' provider...
Bringing machine 'flannel-02' up with 'virtualbox' provider...
Bringing machine 'flannel-03' up with 'virtualbox' provider...
==> flannel-01: Importing base box 'coreos-stable'...
==> flannel-01: Matching MAC address for NAT networking...
==> flannel-01: Checking if box 'coreos-stable' is up to date...
==> flannel-01: Setting the name of the VM: flannel-kubernetes_flannel-01_1456486316604_71560
==> flannel-01: Clearing any previously set network interfaces...
==> flannel-01: Preparing network interfaces based on configuration...
    flannel-01: Adapter 1: nat
    flannel-01: Adapter 2: hostonly
==> flannel-01: Forwarding ports...
    flannel-01: 22 (guest) => 2222 (host) (adapter 1)
==> flannel-01: Running 'pre-boot' VM customizations...
==> flannel-01: Booting VM... 
  • 安装完成后
//查看节点运行状态
 # vagrant status
 Current machine states:

flannel-01                running (virtualbox)
flannel-02                running (virtualbox)
flannel-03                running (virtualbox)   

//进入kubernetes master节点
 #vagrant ssh flannel-01
 CoreOS stable (835.13.0)
 Update Strategy: No Reboots
 [email protected] ~ $ 
 [email protected] ~ $ kubectl get node      //查看k8s状态
NAME            LABELS                                 STATUS    AGE
172.18.18.101   kubernetes.io/hostname=172.18.18.101   Ready     4m
172.18.18.102   kubernetes.io/hostname=172.18.18.102   Ready     3m
172.18.18.103   kubernetes.io/hostname=172.18.18.103   Ready     3m  

[email protected] ~ $ etcdctl ls /coreos.com/network/subnets     //查看flannel状态
/coreos.com/network/subnets/10.1.44.0-24
/coreos.com/network/subnets/10.1.21.0-24
/coreos.com/network/subnets/10.1.23.0-24 

//创建一个pod看看
[email protected] ~ $ kubectl create -f busybox.yaml
pod "busybox" created
[email protected] ~ $ kubectl get po -o wide
NAME      READY     STATUS    RESTARTS   AGE       NODE
busybox   1/1       Running   0          6s        172.18.18.103 

//可以看到kubernetes和flannel都正常工作了,恭喜你3节点私有云已经搭建完成
                             
来源:http://zhouxi.me/2016/02/26/coreos-docker-k8s/

About 智足者富

http://chenpeng.info

发表评论

电子邮件地址不会被公开。 必填项已用*标注

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>