---
title: "利用Racher轻松构建PASS平台"
author: "yangactive"
date: "2017-09-29T09:45:26Z"
tags: []
categories: ["互联网"]
slug: "racher-pass"
draft: false
lastmod: "2017-09-29T09:45:26Z"
id: 2394936
url: "https://yangactive.iteye.com/blog/2394936"
---

# 一，Rancher是什么

1.1 Rancher背景

❶Rancher Labs成立于2014年。总部设立在美国加州的Cupertino。在美国的亚利桑那州的菲尼克斯和中国的深圳设立有研发中心。

❷核心团队自2008年就在一起工作,曾创立Cloud.com并推出了CloudStack,

经历了从VM到容器的整个虚拟化技术演进过程。

❸Rancher的两个产品,Rancher平台和Rancher操作系统,已经有了超过4000

万次的下载量,被大量的运用在了云计算的各个领域。

❹作为容器领域的先行者和重要参与者,Rancher Labs是OCI、CNCF的成员;也是Docker生态核心组织-Docker治理委员会(DGAB)的成员。

1.2 Rancher创始人

![](http://upload-images.jianshu.io/upload_images/7927573-fa65a5e1163f1f6b.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

❶Rancher创始人是梁胜博士。

❷梁胜博士是美国Rancher Labs Inc.公司联合创始人及公司CEO。

❸在此之前,从2011年至2014年间,梁博士担任Citrix System Inc.公司云平台首席技术官,也是Citrix公司首位华人CTO。

❹在加入Citrix公司之前,梁胜创立了cloud.com公司并担任首席执行官,直至2011年7月被Citrix以2.3亿美金购入旗下。

❺之前他作为Sun Microsystems公司核心主任工程师,先后领导设计和开发了Java 2平台的JNI(Java Native Interface)和JVM(Java虚拟机)。

❻梁胜毕业于中国科技大学少年班,并拥有耶鲁大学计算机博士学位。

1.3 Rancher简介

![](http://upload-images.jianshu.io/upload_images/7927573-17ebedce834772d7.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

1.4 容器生态圈

❶什么是容器?

与宿主机系统共享内核但与系统中的其他进程资源隔离的运行环境。

❷容器相关技术的历史(部分)

1982 chroot in Unix

2000 FreeBSD Jail

2001 Linux VServer

2004 Solaris Zone o 2005 OpenVZ

2008 LXC

2013 Docker

2014 Rocket(Rkt) o ......

![](http://upload-images.jianshu.io/upload_images/7927573-1bae5118f1cf5aa3.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

❸容器生态圈知名产品体系

![](http://upload-images.jianshu.io/upload_images/7927573-c9ed3db62f65813c.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

1.5 Rancher容器管理平台系统架构图

![](http://upload-images.jianshu.io/upload_images/7927573-1bba0fb3768260fb.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

1.6 Rancher容器管理UI界面

![](http://upload-images.jianshu.io/upload_images/7927573-9f9bd00081dc177b.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

# 二，Rancher如何搭建

提示：为了降低理解难度，以下过程全部采用图形化界面操作以及汉语进行演示，如果想有更进一步的认识或者你是一个极客，请手动敲命令进行操作

![](http://upload-images.jianshu.io/upload_images/7927573-89d567b220f239e9.png?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

2.1 安装Docker环境

❶Windows用户或者Mac用户通过下载  DockerToolbox 来安装 docker, docker-machine 和其他辅助工具，可以到页面[https://www.docker.com/products/docker-toolbox](https://www.docker.com/products/docker-toolbox)中下载

![](http://upload-images.jianshu.io/upload_images/7927573-8d8222f4a4590e79.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

❷安装过程中会检测是否已经安装Virtual Box，如果未安装，则会提示安装Virtual Box

❷安装成功后，桌面会出现Docker Quickstart Terminal 和 Kitematic (Beta)图标

![](http://upload-images.jianshu.io/upload_images/7927573-74c52214162d453b.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

❸点击Kitematic(Docker图形化管理工具)，第一次启动的时候自动创建 Virtual Box 的  default 虚拟机.可以打开Virtual Box进行查看，如下图：

![](http://upload-images.jianshu.io/upload_images/7927573-9b5410d7c0381c14.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

❹Kitematic启动成功之后如下图，至此，Docker环境创建成功

![](http://upload-images.jianshu.io/upload_images/7927573-44b764917cd3612e.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

2.2 安装Rancher

❶Rancher的各种容器管理理念均架构在由Rancher server和rancher agent构建的Infrastructure之上。Rancher server是Rancher的核心，提供核心容器管理服务以及API服务。

❷Rancher的一个设计理念是所有组件都Containerized（容器化）

❸搭建一个单节点的Rancher Server为例，首先安装一个Rancher server，直接在Kitematic搜索Rancher，如下图，点击CREATE：

![](http://upload-images.jianshu.io/upload_images/7927573-69e7cb50e877ca45.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

❹然后选中Rancher server，点击Start，加载完毕后，点击EXEC，输入命令：PS AUX，可以看到内部，/usr/bin/s6-svscan是容器的第一个启动进程，/service这个 路径作为其命令行参数，又分别内部启动了mysql,graphite\_exporter,cattle等服务，注：单节点rancher server的数据都保存在其内部的MySQL中，而多节点rancher server则采用一个外部的MySQL存储数据。

![](http://upload-images.jianshu.io/upload_images/7927573-008d5f1fcc0a65e4.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

❺等待一定时间后，WEB PREVIEW出现了预览界面，点击小箭头，直接在浏览器打开。

![](http://upload-images.jianshu.io/upload_images/7927573-3b7f5eb4c34b1d17.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

❻打开Rancher server界面，如下，至此，Rancher server已经初步搭建成功！

![](http://upload-images.jianshu.io/upload_images/7927573-93a6ad55a96cb95e.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

2.3 管理Rancher，添加用户(Account)

❶第一次启动Rancher后，Rancher的UI是没有访问控制的,点击系统管理-&gt;访问控制，如下图，可以看到有7种方式，选择LOCAL，输入用户名和密码

![](http://upload-images.jianshu.io/upload_images/7927573-556d2ab7b5330a5f.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

❷然后点击退出登录，可以看到如下界面，访问控制已经添加上

![](http://upload-images.jianshu.io/upload_images/7927573-c6f1c1b87729c6e1.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

2.4 管理Rancher，设置环境(Environment)

❶登录之后，点击左上角环境管理，Rancher 支持将资源分组归属到多个环境。 每个环境具有自己独立的基础架构资源及服务，并由一个或多个用户、团队或组织所管理。

例如，您可以创建独立的“开发”、“测试”及“生产”环境以确保环境之间的安全隔离，将“开发”环境的访问权限赋予全部人员，但限制“生产”环境的访问权限给一个小的团队。

如下图，建立Pass云测试和Pass云管理，两套环境，分别给测试和开发人员使用，可以修改它的Name、Container Orchestration引擎（cattle、k8s和swarm，默认cattle）以及Access Control，我们使用Cattle进行编排引擎。

![](http://upload-images.jianshu.io/upload_images/7927573-9420b4a416fb653e.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

下图可以体现出环境的隔离性：

![](http://upload-images.jianshu.io/upload_images/7927573-b880bdc0a242a65f.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

环境管理，也可以基于环境模板，如下图，比如Cattle，上面已经安装好了ipsec,healthcheck等服务：

![](http://upload-images.jianshu.io/upload_images/7927573-df156ac81485c413.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

❷创建好环境后，环境下面还没有设置没有任何Resources（主机、存储 等）可供使用（比如创建Containers）。

选择基础架构，添加主机(hosts)，如下图，可以自己定义物理机或者虚拟机，也可以选择亚马逊云等，如果没有发现你想使用的云，比如阿里云，可以选择管理docker-machine驱动进行添加：

 

![](http://upload-images.jianshu.io/upload_images/7927573-c05449d18b31e45a.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

2.5 管理Rancher，设置主机(Hosts)

❶以添加本地环境作为主机资源为例，将下图中的命令在命令行进行执行

![](http://upload-images.jianshu.io/upload_images/7927573-90d45957076357a8.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

❷执行成功后，可以看到基础设施里面多了一个主机，并且这个主机上，已经默认在跑了一些应用，比如healthcheck，至此一套基础的架构已经具备了

![](http://upload-images.jianshu.io/upload_images/7927573-69f90e922d2c2081.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

2.6 管理Rancher，设置应用(Stacks)

❶应用(stacks)就是由一组服务组成，比如上图的应用：ipsec由cni-driver-1和ipsec-1服务组成。

❷而服务是一组由相同docker镜像创建的容器，服务扩展了Docker的“link”概念以利用Rancher的轻量级分布式DNS服务用于服务发现。服务可以单独添加或通过应用商店部署。

❸服务也能够利用其他Rancher内置服务，如负载均衡、健康监控、升级支持以及高可用。

下面这幅图直观描述了用户, 环境与应用之间的关系：

![](http://upload-images.jianshu.io/upload_images/7927573-0446494ee84d8592.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

❹在Pass云管理环境，创建一个example应用，如下图，点击创建：

![](http://upload-images.jianshu.io/upload_images/7927573-dc4776c371a8e13b.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

❺创建成功后如下图：

![](http://upload-images.jianshu.io/upload_images/7927573-88e43ce7f60885ac.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

2.7 管理Rancher，设置服务(Services)

❶如上图，点击添加服务，输入名称，选择镜像，镜像就可以理解成为用户提供服务的运行程序。这里选择healthcheck为例子。下面可以进行一些端口映射，服务连接，网络，安全，调度等配置

![](http://upload-images.jianshu.io/upload_images/7927573-2b45832f538c650d.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

❷成功后，可以看到下图，可以对这个服务进行升级，停止，删除等操作

![](http://upload-images.jianshu.io/upload_images/7927573-bcaffc9a23b14e6d.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

❸如果用户访问量过大，还可以进行针对这个服务，增加运行节点的数量或者说集群，如下图，可以增加节点数量，点击后，会自动寻找hosts列表，资源比较空闲的主机，或者其他策略。可以快速的将服务部署到任意多个主机节点上。

![](http://upload-images.jianshu.io/upload_images/7927573-150e36eada672070.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

❹点击查看日志，可以看到服务运行的实时日志：

![](http://upload-images.jianshu.io/upload_images/7927573-9a2ee00b7951b3bf.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

# 三，Rancher使用体验

3.1 Rancher自带了一套网络方案

❶rancher 自带了一套网络方案，可以实现跨机器的docker容器互联。

❷其原理大致是：在每个机器上通过docker启动一个路由容器，将docker容器启动时的ip定义为XX.XX网段，并在iptables中将XX.XX网段的请求转发到路由进程的监听端口，进行udp的封装和解封。

❸其原理是隧道技术，都是通过一个程序进行封包解包，并引导docker启动容器时指定相应的ip。

3.2 Rancher使用性

❶自带了一套UI，并集成了日志，shell等

❷支持服务发现，在rancher-agen容器中运行了dns服务

❸支持容器伸缩，一键扩容

❹支持多套环境管理（一套方案，支持开发、测试，生成等多个环境，环境彼此隔离，也即多租户隔离）

总体而言，比较稳定，易于使用

![](http://upload-images.jianshu.io/upload_images/7927573-ac899e22ab583f41.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

3.3 Rancher解决的典型场景

❶新一代的私有云、混合云

![](http://upload-images.jianshu.io/upload_images/7927573-532f7185707d2dc0.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

混合云下的容器服务：

![](http://upload-images.jianshu.io/upload_images/7927573-1f2ec003d0902c0d.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

应用在不同云间动态扩展和迁移：

![](http://upload-images.jianshu.io/upload_images/7927573-792ced9c7e149f24.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

容器改变了应用管理的模式：

![](http://upload-images.jianshu.io/upload_images/7927573-3219d649e5a5ecfc.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

❷企业应用商店和一键部署应用

![](http://upload-images.jianshu.io/upload_images/7927573-ab9cb067212e5d3d.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

❸构建轻量级Pass服务

![](http://upload-images.jianshu.io/upload_images/7927573-441281c83b0b76d1.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

![](http://upload-images.jianshu.io/upload_images/7927573-86f883e02957e94b.jpg?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1240)

![](http://dl2.iteye.com/upload/attachment/0127/0254/adc97e04-f0ff-3627-93dc-966fd48612b4.jpg)