订阅云计算RSS CSDN首页> 云计算

Kubernetes初探:原理及实践应用

发表于2014-10-31 09:46| 次阅读| 来源http://blog.csdn.net/zhangjun2915| 0 条评论| 作者张俊

摘要:Kubernetes是Google开源的Docker容器集群管理系统,为容器化的应用提供资源调度、部署运行、服务发现、扩容缩容等整一套功能,本文旨在梳理其架构、概念及基本工作流,并通过一个示例应用介绍如何使用Kubernetes。

Kubernetes是Google开源的容器集群管理系统。它构建Ddocker技术之上,为容器化的应用提供资源调度、部署运行、服务发现、扩容缩容等整一套功能,本质上可看作是基于容器技术的mini-PaaS平台。本文旨在梳理Kubernetes的架构、概念及基本工作流,并且通过运行一个简单的示例应用来介绍如何使用Kubernetes。本文转载自张俊的博客,以下伟原文:

总体概览

如下图所示是我初步阅读文档和源代码之后整理的总体概览,基本上可以从如下三个维度来认识Kubernetes。


操作对象

Kubernetes以RESTFul形式开放接口,用户可操作的REST对象有三个:

  • pod:是Kubernetes最基本的部署调度单元,可以包含container,逻辑上表示某种应用的一个实例。比如一个web站点应用由前端、后端及数据库构建而成,这三个组件将运行在各自的容器中,那么我们可以创建包含三个container的pod。
  • service:是pod的路由代理抽象,用于解决pod之间的服务发现问题。因为pod的运行状态可动态变化(比如切换机器了、缩容过程中被终止了等),所以访问端不能以写死IP的方式去访问该pod提供的服务。service的引入旨在保证pod的动态变化对访问端透明,访问端只需要知道service的地址,由service来提供代理。
  • replicationController:是pod的复制抽象,用于解决pod的扩容缩容问题。通常,分布式应用为了性能或高可用性的考虑,需要复制多份资源,并且根据负载情况动态伸缩。通过replicationController,我们可以指定一个应用需要几份复制,Kubernetes将为每份复制创建一个pod,并且保证实际运行pod数量总是与该复制数量相等(例如,当前某个pod宕机时,自动创建新的pod来替换)。

可以看到,service和replicationController只是建立在pod之上的抽象,最终是要作用于pod的,那么它们如何跟pod联系起来呢?这就要引入label的概念:label其实很好理解,就是为pod加上可用于搜索或关联的一组key/value标签,而service和replicationController正是通过label来与pod关联的。如下图所示,有三个pod都有label为"app=backend",创建service和replicationController时可以指定同样的label:"app=backend",再通过label selector机制,就将它们与这三个pod关联起来了。例如,当有其他frontend pod访问该service时,自动会转发到其中的一个backend pod。


功能组件

如下图所示是官方文档里的集群架构图,一个典型的master/slave模型。

alt

master运行三个组件:

  • apiserver:作为kubernetes系统的入口,封装了核心对象的增删改查操作,以RESTFul接口方式提供给外部客户和内部组件调用。它维护的REST对象将持久化到etcd(一个分布式强一致性的key/value存储)。
  • scheduler:负责集群的资源调度,为新建的pod分配机器。这部分工作分出来变成一个组件,意味着可以很方便地替换成其他的调度器。
  • controller-manager:负责执行各种控制器,目前有两类:
    • endpoint-controller:定期关联service和pod(关联信息由endpoint对象维护),保证service到pod的映射总是最新的。
    • replication-controller:定期关联replicationController和pod,保证replicationController定义的复制数量与实际运行pod的数量总是一致的。

slave(称作minion)运行两个组件:

  • kubelet:负责管控docker容器,如启动/停止、监控运行状态等。它会定期从etcd获取分配到本机的pod,并根据pod信息启动或停止相应的容器。同时,它也会接收apiserver的HTTP请求,汇报pod的运行状态。
  • proxy:负责为pod提供代理。它会定期从etcd获取所有的service,并根据service信息创建代理。当某个客户pod要访问其他pod时,访问请求会经过本机proxy做转发。

工作流

上文已经提到了Kubernetes中最基本的三个操作对象:pod, replicationController及service。 下面分别从它们的对象创建出发,通过时序图来描述Kubernetes各个组件之间的交互及其工作流。

alt

使用示例

最后,让我们进入实战模式,这里跑一个最简单的单机示例(所有组件运行在一台机器上),旨在打通基本流程。

搭建环境

第一步,我们需要Kuberntes各组件的二进制可执行文件。有以下两种方式获取: 

  • 下载源代码自己编译:

git clone <a href="https://github.com/GoogleCloudPlatform/kubernetes.git">https://github.com/GoogleCloudPlatform/kubernetes.git</a> &nbsp;<br>cd kubernetes/build &nbsp;<br>./release.sh &nbsp;

  • 直接下载人家已经编译打包好的tar文件:
wget <a href="https://storage.googleapis.com/kubernetes/binaries.tar.gz">https://storage.googleapis.com/kubernetes/binaries.tar.gz</a><br>

自己编译源码需要先安装好golang,编译完之后在kubernetes/_output/release-tars文件夹下可以得到打包文件。直接下载的方式不需要安装其他软件,但可能得不到最新的版本。

第二步,我们还需要etcd的二进制可执行文件,通过如下方式获取:

wget <a href="https://github.com/coreos/etcd/releases/download/v0.4.6/etcd-v0.4.6-linux-amd64.tar.gz">https://github.com/coreos/etcd/releases/download/v0.4.6/etcd-v0.4.6-linux-amd64.tar.gz</a>&nbsp;<br><span style="background-color: initial;">tar xvf etcd-v0.4.6-linux-amd64.tar.gz &nbsp;</span>

第三步,就可以启动各个组件了:

etcd

cd etcd-v0.4.6-linux-amd64 &nbsp;<br>./etcd &nbsp;

apiserver

./apiserver \ &nbsp;<br>-address=127.0.0.1 \ &nbsp;<br>-port=8080 \ &nbsp;<br>-portal_net="172.0.0.0/16" \ &nbsp;<br>-etcd_servers=http://127.0.0.1:4001 \ &nbsp;<br>-machines=127.0.0.1 \ &nbsp;<br>-v=3 \ &nbsp;<br>-logtostderr=false \ &nbsp;<br>-log_dir=./log &nbsp;

scheduler

./scheduler -master 127.0.0.1:8080 \ &nbsp;<br>-v=3 \ &nbsp;<br>-logtostderr=false \ &nbsp;<br>-log_dir=./log &nbsp;<br>

controller-manager

./controller-manager -master 127.0.0.1:8080 \ &nbsp;<br>-v=3 \ &nbsp;<br>-logtostderr=false \ &nbsp;<br>-log_dir=./log &nbsp;

kubelet

./kubelet \ &nbsp;<br>-address=127.0.0.1 \ &nbsp;<br>-port=10250 \ &nbsp;<br>-hostname_override=127.0.0.1 \ &nbsp;<br>-etcd_servers=http://127.0.0.1:4001 \ &nbsp;<br>-v=3 \ &nbsp;<br>-logtostderr=false \ &nbsp;<br>-log_dir=./log &nbsp;

创建pod

搭好了运行环境后,就可以提交pod了。首先编写pod描述文件,保存为redis.json:

{ &nbsp;<br>&nbsp; "id": "redis", &nbsp;<br>&nbsp; "desiredState": { &nbsp;<br>&nbsp; &nbsp; "manifest": { &nbsp;<br>&nbsp; &nbsp; &nbsp; "version": "v1beta1", &nbsp;<br>&nbsp; &nbsp; &nbsp; "id": "redis", &nbsp;<br>&nbsp; &nbsp; &nbsp; "containers": [{ &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; "name": "redis", &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; "image": "dockerfile/redis", &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; "imagePullPolicy": "PullIfNotPresent", &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; "ports": [{ &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "containerPort": 6379, &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "hostPort": 6379 &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; }] &nbsp;<br>&nbsp; &nbsp; &nbsp; }] &nbsp;<br>&nbsp; &nbsp; } &nbsp;<br>&nbsp; }, &nbsp;<br>&nbsp; "labels": { &nbsp;<br>&nbsp; &nbsp; "name": "redis" &nbsp;<br>&nbsp; } &nbsp;<br>} &nbsp;

然后,通过命令行工具kubecfg提交:

./kubecfg -c redis.json create /pods

提交完后,通过kubecfg查看pod状态:

# ./kubecfg list /pods &nbsp;<br>ID &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Image(s) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Host &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Labels &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Status &nbsp;<br>---------- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;---------- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;---------- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;---------- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;---------- &nbsp;<br>redis &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dockerfile/redis &nbsp; &nbsp;127.0.0.1/ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;name=redis &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Running &nbsp; &nbsp;

Status是Running表示pod已经在容器里运行起来了,可以用"docker ps"命令来查看容器信息:

# docker ps &nbsp;<br>CONTAINER ID &nbsp; &nbsp; &nbsp; &nbsp;IMAGE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; COMMAND &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CREATED &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; STATUS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PORTS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NAMES &nbsp;<br>ae83d1e4b1ec &nbsp; &nbsp; &nbsp; &nbsp;dockerfile/redis:latest &nbsp; "redis-server /etc/r &nbsp; 19 seconds ago &nbsp; &nbsp; &nbsp;Up 19 seconds &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;k8s_redis.caa18858_redis.default.etcd_1414684622_1b43fe35 &nbsp;

创建replicationController

{ &nbsp;<br>&nbsp; &nbsp; "id": "redisController", &nbsp;<br>&nbsp; &nbsp; "apiVersion": "v1beta1", &nbsp;<br>&nbsp; &nbsp; "kind": "ReplicationController", &nbsp;<br>&nbsp; &nbsp; "desiredState": { &nbsp;<br>&nbsp; &nbsp; &nbsp; "replicas": 1, &nbsp;<br>&nbsp; &nbsp; &nbsp; "replicaSelector": {"name": "redis"}, &nbsp;<br>&nbsp; &nbsp; &nbsp; "podTemplate": { &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; "desiredState": { &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"manifest": { &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"version": "v1beta1", &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"id": "redisController", &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"containers": [{ &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"name": "redis", &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"image": "dockerfile/redis", &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"imagePullPolicy": "PullIfNotPresent", &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"ports": [{ &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"containerPort": 6379, &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"hostPort": 6379 &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}] &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}] &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}, &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"labels": {"name": "redis"} &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; }}, &nbsp;<br>&nbsp; &nbsp; "labels": {"name": "redis"} &nbsp;<br>&nbsp; } &nbsp;

然后,通过命令行工具kubecfg提交:

./kubecfg&nbsp;-c&nbsp;redisController.json&nbsp;create&nbsp;/replicationControllers&nbsp;

提交完后,通过kubecfg查看replicationController状态:

# ./kubecfg list /replicationControllers &nbsp;<br>ID &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Image(s) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Selector &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Replicas &nbsp;<br>---------- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;---------- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;---------- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;---------- &nbsp;<br>redisController &nbsp; &nbsp; dockerfile/redis &nbsp; &nbsp;name=redis &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 &nbsp;

同时,1个pod也将被自动创建出来,即使我们故意删除该pod,replicationController也将保证创建1个新pod。 

原文链接: Kubernetes初探(责编:周小璐)

如需要了解更多Docker相关的资讯或是技术文档可关注CSDN云计算微信


0
0