Kubernetes
作者介绍王渊命,团队协作IM服务Grouk联合创始人及CTO,技术极客,曾任新浪微博架构师、微米技术总监。2015年作为联合创始人创立团队协作IM服务Grouk,长期关注团队协作基础工具和研发环境建设,Docker深度实践者。正文最近研究了一段时间的Kubernetes,将我们服务的测试环境服务部署到了Kubernetes上,上周末在团队中分享了下,顺便整理成文章。阅读对象:对Kubernetes
Docker的流行激活了一直不温不火的PaaS,随着而来的是各类Micro-PaaS的出现,Kubernetes是其中最具代表性的一员,它是Google多年大规模容器管理技术的开源版本。本系列文章将逐一分析Kubernetes,本文通过一个例子进行入门,介绍Kubernetes的基本概念和功能。
1. Kubernetes介绍
基本概念
- Pod
Pod是Kubernetes的基本操作单元,把相关的一个或多个容器构成一个Pod,通常Pod里的容器运行相同的应用。Pod包含的容器运行在同一个Node(Host)上,看作一个统一管理单元,共享相同的volumes和network namespace/IP和Port空间。 - Replication Controller
Replication Controller确保任何时候Kubernetes集群中有指定数量的pod副本(replicas)在运行, 如果少于指定数量的pod副本(replicas),Replication Controller会启动新的Container,反之会杀死多余的以保证数量不变。Replication Controller使用预先定义的pod模板创建pods,一旦创建成功,pod 模板和创建的pods没有任何关联,可以修改pod 模板而不会对已创建pods有任何影响,也可以直接更新通过Replication Controller创建的pods。对于利用pod 模板创建的pods,Replication Controller根据label selector来关联,通过修改pods的label可以删除对应的pods。 - Service
Service也是Kubernetes的基本操作单元,是真实应用服务的抽象,每一个服务后面都有很多对应的容器来支持,通过Proxy的port和服务selector决定服务请求传递给后端提供服务的容器,对外表现为一个单一访问接口,外部不需要了解后端如何运行,这给扩展或维护后端带来很大的好处。 - Label
Label是用于区分Pod、Service、Replication Controller的key/value键值对,Pod、Service、 Replication Controller可以有多个label,但是每个label的key只能对应一个value。Labels是Service和Replication Controller运行的基础,为了将访问Service的请求转发给后端提供服务的多个容器,正是通过标识容器的labels来选择正确的容器。同样,Replication Controller也使用labels来管理通过pod 模板创建的一组容器,这样Replication Controller可以更加容易,方便地管理多个容器,无论有多少容器。
架构
Kubernets属于主从的分布式集群架构,包含Master和Node:
Master作为控制节点,调度管理整个系统,包含以下组件:
- API Server作为kubernetes系统的入口,封装了核心对象的增删改查操作,以RESTFul接口方式提供给外部客户和内部组件调用。它维护的REST对象将持久化到etcd(一个分布式强一致性的key/value存储)。
- Scheduler:负责集群的资源调度,为新建的pod分配机器。这部分工作分出来变成一个组件,意味着可以很方便地替换成其他的调度器。
- Controller Manager:负责执行各种控制器,目前有两类:(1)Endpoint Controller:定期关联service和pod(关联信息由endpoint对象维护),保证service到pod的映射总是最新的。(2)Replication Controller:定期关联replicationController和pod,保证replicationController定义的复制数量与实际运行pod的数量总是一致的。
Node是运行节点,运行业务容器,包含以下组件:
- Kubelet:责管控docker容器,如启动/停止、监控运行状态等。它会定期从etcd获取分配到本机的pod,并根据pod信息启动或停止相应的容器。同时,它也会接收apiserver的HTTP请求,汇报pod的运行状态。
- Kube Proxy:负责为pod提供代理。它会定期从etcd获取所有的service,并根据service信息创建代理。当某个客户pod要访问其他pod时,访问请求会经过本机proxy做转发。
Kubernets使用Etcd作为存储和通信中间件,实现Master和Node的一致性,这是目前比较常见的做法,典型的SOA架构,解耦Master和Node。
2. Guestbook示例
Guestbook示例将会展示如何运行一个应用到Kubernetes上,应用包含
- Web前端
- Redis集群(一个master,2个slave)
1) 部署Kubernetes
如果你熟悉Bosh/Bosh Lite的话,可以使用kubernetes-release.本文使用Kubernetes环境:
- master:192.168.3.146
- node1:192.168.3.147
- node2:192.168.3.148
- node3:192.168.3.149
2)启动Redis Master
需要准备配置文件redis-master-controller.yaml,用于描述pod如何运行服务容器:
<code class="hljs applescript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">apiVersion: v1 kind: ReplicationController metadata: <span class="hljs-property" style="box-sizing: border-box;">name</span>: redis-master labels: <span class="hljs-property" style="box-sizing: border-box;">name</span>: redis-master spec: replicas: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> selector: <span class="hljs-property" style="box-sizing: border-box;">name</span>: redis-master template: metadata: labels: <span class="hljs-property" style="box-sizing: border-box;">name</span>: redis-master spec: containers: - <span class="hljs-property" style="box-sizing: border-box;">name</span>: master image: redis ports: - containerPort: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6379</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li></ul>
即使只有一个Redis Master Pod实例,这里也使用ReplicationController保证Pod持续运行,否则Node挂掉的话,Pod将停止运行。
创建Pod:
<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">$ kubectl create <span class="hljs-attribute" style="box-sizing: border-box;">-f</span> redis<span class="hljs-attribute" style="box-sizing: border-box;">-master</span><span class="hljs-attribute" style="box-sizing: border-box;">-controller</span><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>yaml </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
查看ReplicationController:
<code class="hljs applescript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">$ kubectl <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">get</span> rc CONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICAS redis-master master redis <span class="hljs-property" style="box-sizing: border-box;">name</span>=redis-master <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
查看Pod:
<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">$ kubectl get pods <span class="hljs-attribute" style="box-sizing: border-box;">-o</span> wide NAME READY STATUS RESTARTS AGE NODE redis<span class="hljs-attribute" style="box-sizing: border-box;">-master</span><span class="hljs-attribute" style="box-sizing: border-box;">-u3fup</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> Running <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>m node1</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
可以看到Pod运行在Node1节点,在Node1查看docker容器:
<code class="hljs r has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">$ docker ps CONTAINER ID IMAGE <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">...</span> feb393fbe42b redis:latestminute ago <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">...</span> d9e934ee55ae gcr.io/google_containers/pause:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.8</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.0</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">...</span> </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
总共有2个容器正在运行,其中一个Redis Master,另外一个是google_containers/pause,它是Netowrk Container, 每启动一个Pod都会附加启动这样一个容器,它的作用就只是简单的等待,设置Pod的网络。
如果docker rm -f feb393fbe42b,删掉Redis Master Container,过一会儿就会有新的容器启动,这说明Kubernetes会保证Pod的容器运行。
<code class="hljs r has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">$ docker ps CONTAINER ID IMAGE <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">...</span> fc3b458d333a redis:latestminute ago <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">...</span> d9e934ee55ae gcr.io/google_containers/pause:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.8</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.0</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">...</span> </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
如果把Node1关掉,Pod会迁移到其他Node上,这是ReplicationController保证Pod运行。
<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">$ kubectl get pods <span class="hljs-attribute" style="box-sizing: border-box;">-o</span> wide NAME READY STATUS RESTARTS AGE NODE redis<span class="hljs-attribute" style="box-sizing: border-box;">-master</span><span class="hljs-attribute" style="box-sizing: border-box;">-x5kjp</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> Running <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span>s node3</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
上一步已经运行起了一个Redis Master Pod, 即使只有一个Pod,也是有必要使用Service。Kubernetes中Service中起到了负载均衡器的作用,通过Proxy和Selector决定服务请求传递给后端提供服务的Pod,对外提供固定的IP,这样的话Redis Master Pod迁移变化也不会影响。
需要redis-master-service.yaml来描述redis master service:
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-label" style="box-sizing: border-box;">apiVersion:</span> v1 <span class="hljs-label" style="box-sizing: border-box;">kind:</span> Service <span class="hljs-label" style="box-sizing: border-box;">metadata:</span> name: redis-master labels: name: redis-master <span class="hljs-label" style="box-sizing: border-box;">spec:</span> ports: <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># the port that this service should serve on</span> - port: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6379</span> targetPort: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6379</span> selector: name: redis-master <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#和redis-master的Label对应</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li></ul>
创建Service:
<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">$ kubectl create <span class="hljs-attribute" style="box-sizing: border-box;">-f</span> redis<span class="hljs-attribute" style="box-sizing: border-box;">-master</span><span class="hljs-attribute" style="box-sizing: border-box;">-service</span><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>yaml</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
查看Service:
$ kubectl get service
<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">NAME LABELS SELECTOR IP(S) PORT(S) redis<span class="hljs-attribute" style="box-sizing: border-box;">-master</span> name<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span>redis<span class="hljs-attribute" style="box-sizing: border-box;">-master</span> name<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span>redis<span class="hljs-attribute" style="box-sizing: border-box;">-master</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10.254</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.189</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.63</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6379</span>/TCP</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
Kubernetes会分配IP(10.254.189.63)给Redis Master Service,这个就是Redis Master Service对外暴露的IP,可以通过redis-cli访问:
<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">$ redis<span class="hljs-attribute" style="box-sizing: border-box;">-cli</span> <span class="hljs-attribute" style="box-sizing: border-box;">-h</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10.254</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.189</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.63</span> info</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
Kubernetes同时提供2种了发现Service的方法:
- 环境变量
当Pod运行的时候,Kubernetes会将之前存在的Service的信息通过环境变量写到Pod里面,以Redis Master Service为例,它的信息会被写到新的Pod里面:
<code class="hljs bash has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"REDIS_MASTER_PORT_6379_TCP=tcp://10.254.189.63:6379"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"REDIS_MASTER_PORT_6379_TCP_PROTO=tcp"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"REDIS_MASTER_PORT_6379_TCP_ADDR=10.254.189.63"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"REDIS_MASTER_SERVICE_PORT=6379"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"REDIS_MASTER_SERVICE_HOST=10.254.189.63"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"REDIS_MASTER_PORT=tcp://10.254.189.63:6379"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"REDIS_MASTER_PORT_6379_TCP_PORT=6379"</span>,</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>
这种方法有个比较明显的缺陷,Pod必须在Service之后启动,之前启动的Pod将没有这些环境变量。那么下一种方法就没有这个限制。
- DNS
当有新的Service创建,就会自动生成一条DNS记录,比如在Kubernetes的Namespace “my-ns”中有个Service叫”my-service”,那么就有条DNS记录”my-service.my-ns”对应Service的IP。以Redis Master Service为例, 就有条DNS记录:
<code class="hljs fix has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-attribute" style="box-sizing: border-box;">redis-master </span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">> 10.254.189.63</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
3)启动Redis Slave
redis-slave-controller.yaml:
<code class="hljs applescript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">apiVersion: v1 kind: ReplicationController metadata: <span class="hljs-property" style="box-sizing: border-box;">name</span>: redis-slave labels: <span class="hljs-property" style="box-sizing: border-box;">name</span>: redis-slave spec: replicas: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> selector: <span class="hljs-property" style="box-sizing: border-box;">name</span>: redis-slave template: metadata: labels: <span class="hljs-property" style="box-sizing: border-box;">name</span>: redis-slave spec: containers: - <span class="hljs-property" style="box-sizing: border-box;">name</span>: worker image: kubernetes/redis-slave:v2 ports: - containerPort: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6379</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li></ul>
创建Pod:
<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">$ kubectl create <span class="hljs-attribute" style="box-sizing: border-box;">-f</span> redis<span class="hljs-attribute" style="box-sizing: border-box;">-slave</span><span class="hljs-attribute" style="box-sizing: border-box;">-controller</span><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>yaml $ kubectl get rc CONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICAS redis<span class="hljs-attribute" style="box-sizing: border-box;">-master</span> master redis name<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span>redis<span class="hljs-attribute" style="box-sizing: border-box;">-master</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> redis<span class="hljs-attribute" style="box-sizing: border-box;">-slave</span> worker kubernetes/redis<span class="hljs-attribute" style="box-sizing: border-box;">-slave</span>:v2 name<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span>redis<span class="hljs-attribute" style="box-sizing: border-box;">-slave</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> $ kubectl get pods <span class="hljs-attribute" style="box-sizing: border-box;">-o</span> wide NAME READY STATUS RESTARTS AGE NODE redis<span class="hljs-attribute" style="box-sizing: border-box;">-master</span><span class="hljs-attribute" style="box-sizing: border-box;">-x5kjp</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> Running <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>h node3 redis<span class="hljs-attribute" style="box-sizing: border-box;">-slave</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">04</span>o8g <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> Running <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>m node1 redis<span class="hljs-attribute" style="box-sizing: border-box;">-slave</span><span class="hljs-attribute" style="box-sizing: border-box;">-llxpk</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> Running <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>m node1</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li></ul>
redis-slave-service.yaml:
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-label" style="box-sizing: border-box;">apiVersion:</span> v1 <span class="hljs-label" style="box-sizing: border-box;">kind:</span> Service <span class="hljs-label" style="box-sizing: border-box;">metadata:</span> name: redis-slave labels: name: redis-slave <span class="hljs-label" style="box-sizing: border-box;">spec:</span> ports: <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># the port that this service should serve on</span> - port: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6379</span> selector: name: redis-slave</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li></ul>
创建Service:
<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">$ kubectl create <span class="hljs-attribute" style="box-sizing: border-box;">-f</span> redis<span class="hljs-attribute" style="box-sizing: border-box;">-slave</span><span class="hljs-attribute" style="box-sizing: border-box;">-service</span><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>yaml $ kubectl get service NAME LABELS SELECTOR IP(S) PORT(S) redis<span class="hljs-attribute" style="box-sizing: border-box;">-master</span> name<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span>redis<span class="hljs-attribute" style="box-sizing: border-box;">-master</span> name<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span>redis<span class="hljs-attribute" style="box-sizing: border-box;">-master</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10.254</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.189</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.63</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6379</span>/TCP redis<span class="hljs-attribute" style="box-sizing: border-box;">-slave</span> name<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span>redis<span class="hljs-attribute" style="box-sizing: border-box;">-slave</span> name<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span>redis<span class="hljs-attribute" style="box-sizing: border-box;">-slave</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10.254</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.70</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.184</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6379</span>/TCP</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>
4)启动Web Frontend
frontend-controller.yaml:
<code class="hljs applescript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">apiVersion: v1 kind: ReplicationController metadata: <span class="hljs-property" style="box-sizing: border-box;">name</span>: frontend labels: <span class="hljs-property" style="box-sizing: border-box;">name</span>: frontend spec: replicas: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> selector: <span class="hljs-property" style="box-sizing: border-box;">name</span>: frontend template: metadata: labels: <span class="hljs-property" style="box-sizing: border-box;">name</span>: frontend spec: containers: - <span class="hljs-property" style="box-sizing: border-box;">name</span>: php-redis image: kubernetes/example-guestbook-php-redis:v2 ports: - containerPort: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">80</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li></ul>
创建Pod:
<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">$ kubectl create <span class="hljs-attribute" style="box-sizing: border-box;">-f</span> frontend<span class="hljs-attribute" style="box-sizing: border-box;">-controller</span><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>yaml $ kubectl get rc CONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICAS frontend php<span class="hljs-attribute" style="box-sizing: border-box;">-redis</span> kubernetes/example<span class="hljs-attribute" style="box-sizing: border-box;">-guestbook</span><span class="hljs-attribute" style="box-sizing: border-box;">-php</span><span class="hljs-attribute" style="box-sizing: border-box;">-redis</span>:v2 name<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span>frontend <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> redis<span class="hljs-attribute" style="box-sizing: border-box;">-master</span> master redis name<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span>redis<span class="hljs-attribute" style="box-sizing: border-box;">-master</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> redis<span class="hljs-attribute" style="box-sizing: border-box;">-slave</span> worker kubernetes/redis<span class="hljs-attribute" style="box-sizing: border-box;">-slave</span>:v2 name<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span>redis<span class="hljs-attribute" style="box-sizing: border-box;">-slave</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> $ kubectl get pods NAME READY STATUS RESTARTS AGE frontend<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span>ukb6 <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> Running <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">45</span>s frontend<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span>ch4l <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> Running <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">45</span>s frontend<span class="hljs-attribute" style="box-sizing: border-box;">-n8l7w</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> Running <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">45</span>s redis<span class="hljs-attribute" style="box-sizing: border-box;">-master</span><span class="hljs-attribute" style="box-sizing: border-box;">-x5kjp</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> Running <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>h redis<span class="hljs-attribute" style="box-sizing: border-box;">-slave</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">04</span>o8g <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> Running <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>h redis<span class="hljs-attribute" style="box-sizing: border-box;">-slave</span><span class="hljs-attribute" style="box-sizing: border-box;">-llxpk</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> Running <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>h</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li></ul>
frontend-service.yaml:
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-label" style="box-sizing: border-box;">apiVersion:</span> v1 <span class="hljs-label" style="box-sizing: border-box;">kind:</span> Service <span class="hljs-label" style="box-sizing: border-box;">metadata:</span> name: frontend labels: name: frontend <span class="hljs-label" style="box-sizing: border-box;">spec:</span> ports: <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># the port that this service should serve on</span> - port: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">80</span> selector: name: frontend</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li></ul>
创建Service:
<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">$ kubectl create <span class="hljs-attribute" style="box-sizing: border-box;">-f</span> frontend<span class="hljs-attribute" style="box-sizing: border-box;">-service</span><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>yaml $ kubectl get service NAME LABELS SELECTOR IP(S) PORT(S) frontend name<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span>frontend name<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span>frontend <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10.254</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.58</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.118</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">80</span>/TCP redis<span class="hljs-attribute" style="box-sizing: border-box;">-master</span> name<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span>redis<span class="hljs-attribute" style="box-sizing: border-box;">-master</span> name<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span>redis<span class="hljs-attribute" style="box-sizing: border-box;">-master</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10.254</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.189</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.63</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6379</span>/TCP redis<span class="hljs-attribute" style="box-sizing: border-box;">-slave</span> name<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span>redis<span class="hljs-attribute" style="box-sizing: border-box;">-slave</span> name<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span>redis<span class="hljs-attribute" style="box-sizing: border-box;">-slave</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10.254</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.70</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.184</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6379</span>/TCP</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>
Web Frontend是需要对外暴露的,这样外部网络才能真正访问该应用,Kubernetes提供了2种方式暴露Service到外部网络:
- NodePort
Kubernetes将会在每个Node上设置一个Port,访问该Port会被转发到对应的Service。这支持开发者设置自己的LoadBalancer。
- LoadBalancer
Kubernetes会设置LoadBalancer给Service。
本文采用NodePort方式, 更改frontend-service.yaml:
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-label" style="box-sizing: border-box;">apiVersion:</span> v1 <span class="hljs-label" style="box-sizing: border-box;">kind:</span> Service <span class="hljs-label" style="box-sizing: border-box;">metadata:</span> name: frontend labels: name: frontend <span class="hljs-label" style="box-sizing: border-box;">spec:</span> type: NodePort ports: <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># the port that this service should serve on</span> - port: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">80</span> nodePort: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30061</span> selector: name: frontend</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li></ul>
那么就可以通过任意节点访问该应用:
Kubernetes可用来管理Linux容器集群,加速开发和简化运维(即DevOps)。但目前网络上关于Kubernetes的文章介绍性远多于实际使用。本系列文章着眼于实际部署,带您快速掌握Kubernetes。在介绍部署之前需要了解的原理和概念之后,作者在本文中以一个简单的nginx服务来展示了复制器和Service的使用,特别通过对Service的cluster IP和NodePort的分析,使得读者能够了解这个模型中的网络特性。
一个简单的应用
讲了这么多的原理和概念,本章我们就部署一个简单应用来感受一下Kubernetes的部署模型。
部署Kubernetes集群
在 kubernetes github站点 上有数十种针对各种环境的部署文档,本文选择基于ubuntu的集群部署方案。在没有使用本地docker镜像的情况下,在部署过程中需要确保能够访问站点gcr.io。
基于 Ubuntu的集群部署方案文档 写得比较详细,按照它的步骤几乎不会出错。在进行真正的部署之前,一定要确保:
- 所有的节点安装了docker version 1.2+ 和 bridge-utils
- 如果没有本地的docker registry, 要确保节点能访问互联网gcr.io
- 确保管理节点能够ssh 访问所有节点。比如ssh gongysh@192.168.0.201 ls
这里我们集群将采用下图显示的结构。我们将在管理节点上运行集群管理命令。我们将有一个服务和代理混合的节点,还有两个纯的代理节点。
首先我们要下载kubernetes的代码到管理节点上:
$ git clone https://github.com/GoogleCloudPlatform/kubernetes.git
然后进行本地构建:
cd kubernetes ./build/run.sh hack/build-do.sh
修改config-default.sh定义集群,本文使用的几个关键配置如下:
gongysh@fedora20:~/git/kubernetes/cluster/ubuntu$ cat config-default.sh #!/bin/bash # Define all your cluster nodes, MASTER node comes first" # And separated with blank space like <user_1@ip_1> <user_2@ip_2> <user_3@ip_3> export nodes="gongysh@192.168.0.201 gongysh@192.168.0.202 gongysh@192.168.0.203" # Define all your nodes role: a(master) or i(minion) or ai(both master and minion), must be the order same export roles=("ai" "i" "i") # Define minion numbers export NUM_MINIONS=${NUM_MINIONS:-3} # define the IP range used for service portal. # according to rfc 1918 ref: https://tools.ietf.org/html/rfc1918 choose a private ip range here. export SERVICE_CLUSTER_IP_RANGE=192.168.3.0/24 # define the IP range used for flannel overlay network, should not conflict with above SERVICE_CLUSTER_IP_RANGE range export FLANNEL_NET=172.16.0.0/16 ....
最后运行集群构建命令:
$ cd cluster $ KUBERNETES_PROVIDER=ubuntu ./kube-up.sh
当你看到:
Kubernetes cluster is running. The master is running at: http://192.168.0.201 ... calling validate-cluster Found 3 nodes. 1 NAME LABELS STATUS 2 192.168.0.201 <none> Ready 3 192.168.0.202 <none> Ready 4 192.168.0.203 <none> Ready Validate output: Cluster validation succeeded Done, listing cluster services: Kubernetes master is running at http://192.168.0.201:8080表明集群构建成功。
部署nginx应用
我们以下面的图来安装一个简单的静态内容的nginx应用:
首先,我们用复制器启动一个2个备份的nginx Pod。然后在前面挂Service,一个service只能被集群内部访问,一个能被集群外的节点访问。下面所有的命令都是在管理节点上运行的。
部署nginx pod 和复制器
如下表所示:
$ cat nginx-rc.yaml apiVersion: v1 kind: ReplicationController metadata: name: nginx-controller spec: replicas: 2 selector: name: nginx template: metadata: labels: name: nginx spec: containers: - name: nginx image: nginx ports: - containerPort: 80
我们定义了一个nginx pod复制器,复制份数为2,我们使用nginx docker镜像。
执行下面的操作创建nginx pod复制器:
$ kubectl -s http://192.168.0.201:8080 create -f nginx-rc.yaml
由于kubernetes要去gcr.io下载gcr.io/google_containers/pause镜像,然后下载nginx镜像,所以所创建的Pod需要等待一些时间才能处于running状态。
$ kubectl -s http://192.168.0.201:8080 get pods NAME READY REASON RESTARTS AGE nginx-controller-6zr34 1/1 Running 0 48m nginx-controller-njlgt 1/1 Running 0 48m
我们可以使用describe 命令查看pod所分到的节点:
$ $ kubectl -s http://192.168.0.201:8080 describe pod nginx-controller-6zr34 2>/dev/null | grep Node: Node: 192.168.0.203/192.168.0.203 $ kubectl -s http://192.168.0.201:8080 describe pod nginx-controller-njlgt 2>/dev/null | grep Node: Node: 192.168.0.201/192.168.0.201
从上表可以看出,这个复制器启动了两个Pod,分别运行在192.168.0.201和203代理节点主机上。
部署节点内部可访问的nginx service
Service的type有ClusterIP和NodePort之分,缺省是ClusterIP,这种类型的Service只能在集群内部访问。下表是本文用的配置文件:
$ cat nginx-service-clusterip.yaml apiVersion: v1 kind: Service metadata: name: nginx-service-clusterip spec: ports: - port: 8001 targetPort: 80 protocol: TCP selector: name: nginx
执行下面的命令创建service:
$ kubectl -s http://192.168.0.201:8080 create -f ./nginx-service-clusterip.yaml services/nginx-service $ kubectl -s http://192.168.0.201:8080 get service NAME LABELS SELECTOR IP(S) PORT(S) kubernetes component=apiserver,provider=kubernetes <none> 192.168.3.1 443/TCP nginx-service-clusterip <none> name=nginx 192.168.3.91 8001/TCP
验证service的可访问性:
上面的输出告诉我们这个Service的Cluster IP是192.168.3.91,端口是8001。下面我们验证这个PortalNet IP的工作情况:
$ ssh 192.168.0.202 curl -s 192.168.3.91:8001 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
从前面部署复制器的部分我们知道nginx Pod运行在201和203节点上。上面我们特意从202代理节点上访问我们的服务来体现Service Cluster IP在所有集群代理节点的可到达性。
部署外部可访问的nginx service
下面我们创建NodePort类型的Service,这种类型的Service在集群外部是可以访问。下表是本文用的配置文件:
$ cat nginx-service-nodeport.yaml apiVersion: v1 kind: Service metadata: name: nginx-service-nodeport spec: ports: - port: 8000 targetPort: 80 protocol: TCP type: NodePort selector: name: nginx
执行下面的命令创建service:
$ kubectl -s http://192.168.0.201:8080 create -f ./nginx-service-nodeport.yaml services/nginx-service-nodeport $ kubectl -s http://192.168.0.201:8080 get service NAME LABELS SELECTOR IP(S) PORT(S) kubernetes component=apiserver,provider=kubernetes <none> 192.168.3.1 443/TCP nginx-service-clusterip <none> name=nginx 192.168.3.91 8001/TCP nginx-service-nodeport <none> name=nginx 192.168.3.84 8000/TCP
使用下面的命令获得这个service的节点级别的端口:
$ kubectl -s http://192.168.0.201:8080 describe service nginx-service-nodeport 2>/dev/null | grep NodePort Type: NodePort NodePort: <unnamed> 32606/TCP
验证service的可访问性:
上面的输出告诉我们这个Service的节点级别端口是32606。下面我们验证这个Service的工作情况:
$ curl 192.168.0.201:32606 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
代理节点上的IP tables规则解析
下面的图是IPTables中流量经过的table和chain。
可以看出,Kubernetes在nat表中插入了下面四条chain:
1. KUBE-PORTALS-CONTAINER
这个chain主要是处理所有service对象的cluster IP和port到kube-proxy本地端口的映射。比如下面规则:
-A KUBE-PORTALS-CONTAINER -d 192.168.3.84/32 -p tcp -m comment --comment "default/nginx-service-nodeport:" -m tcp --dport 8000 -j REDIRECT --to-ports 43981
就是为nginx-service-nodeport服务的Cluster IP准备的。其中192.168.3.84/32是该服务获得的Cluster IP,端口8000是其在定义文件中指定的spec.ports.port。43981则是kube-proxy为这个service分配的本地端口。规则的意思是到192.168.3.84:8000的流量重定向到43981。
2. KUBE-NODEPORT-CONTAINER
这条chain上则串连着类型为NodePort的service的NodePort规则。比如下面规则:
-A KUBE-NODEPORT-CONTAINER -p tcp -m comment --comment "default/nginx-service-nodeport:" -m tcp --dport 32606 -j REDIRECT --to-ports 43981
就是为nginx-service-nodeport服务的NodePort 32606准备的。意思是访问本地32606端口的流量重新定向到43981,后者是kube-proxy为这个service分配的本地端口。
3. KUBE-PORTALS-HOST
这条chain上也关联着各个service的Cluster IP和Port的规则,比如:
-A KUBE-PORTALS-HOST -d 192.168.3.84/32 -p tcp -m comment --comment "default/nginx-service-nodeport:" -m tcp --dport 8000 -j DNAT --to-destination 192.168.0.201:43981
这条规则是和KUBE-PORTALS-CONTAINER类似的,只不过流量来自于本地进程。
4. KUBE-NODEPORT-HOST
这条chain上则关联着类型为NodePort的service的NodePort规则。比如下面规则:
-A KUBE-NODEPORT-HOST -p tcp -m comment --comment "default/nginx-service-nodeport:" -m tcp --dport 30975 -j DNAT --to-destination 192.168.0.201:43981
这条规则是和KUBE-NODEPORT-CONTAINER类似的,只不过流量来自于本地进程。
总结
笔者认为Docker已经不是仅代表容器本身,而是一组以应用部署为中心的技术,产品和最佳实践生态系统。Kubernetes以其出身,文档的成熟度,社区的支持在这个生态系统中表现得比较突出。在部署Kubernetes时,我们首先要理解Kubernetes的组件结构,它们有哪些角色,各个角色的作用是什么和它们之接的通信。在应用部署时,了解Kubernetes的应用模型是非常重要的。笔者认为复制器和Service的概念是Kubernetes模型的核心,复制器和Service共同完成了应用的高可用性要求。最后本文以一个简单的nginx服务来展示了复制器和Service的使用,特别通过对Service的cluster IP和NodePort的分析,使得读者能够了解这个模型中的网络特性。
最后就是容器技术的选型,本文使用Docker作为容器,其实Kubernetes也支持CoreOS的rkt容器。kubelet的参数--container_runtime用于选择使用的容器技术。(责编/周建丁)
作者简介:龚永生,九州云架构师。多年Linux系统开发,J2EE产品和云计算相关技术研发经验。目前活跃在OpenStack社区的各个项目上,主要技术方向是虚拟网络项目Neutron,是Neutron项目早期的主要贡献者之一。
Kubernetes是Google开源的容器集群管理系统。它构建于docker技术之上,为容器化的应用提供资源调度、部署运行、服务发现、扩容缩容等整一套功能,本质上可看作是基于容器技术的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模型。
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各个组件之间的交互及其工作流。
使用示例
最后,让我们进入实战模式,这里跑一个最简单的单机示例(所有组件运行在一台机器上),旨在打通基本流程。
搭建环境
第一步,我们需要Kuberntes各组件的二进制可执行文件。有以下两种方式获取:
- 下载源代码自己编译:
- git clone https://github.com/GoogleCloudPlatform/kubernetes.git cd kubernetes/build ./release.sh
- 直接下载人家已经编译打包好的tar文件:
- wget https://storage.googleapis.com/kubernetes/binaries.tar.gz
自己编译源码需要先安装好golang,编译完之后在kubernetes/_output/release-tars文件夹下可以得到打包文件。直接下载的方式不需要安装其他软件,但可能得不到最新的版本。
第二步,我们还需要etcd的二进制可执行文件,通过如下方式获取:
- wget https://github.com/coreos/etcd/releases/download/v0.4.6/etcd-v0.4.6-linux-amd64.tar.gz tar xvf etcd-v0.4.6-linux-amd64.tar.gz
第三步,就可以启动各个组件了:
etcd
- cd etcd-v0.4.6-linux-amd64
- ./etcd
apiserver
- ./apiserver \
- -address=127.0.0.1 \
- -port=8080 \
- -portal_net="172.0.0.0/16" \
- -etcd_servers=http://127.0.0.1:4001 \
- -machines=127.0.0.1 \
- -v=3 \
- -logtostderr=false \
- -log_dir=./log
scheduler
- ./scheduler -master 127.0.0.1:8080 \
- -v=3 \
- -logtostderr=false \
- -log_dir=./log
controller-manager
- ./controller-manager -master 127.0.0.1:8080 \
- -v=3 \
- -logtostderr=false \
- -log_dir=./log
kubelet
- ./kubelet \
- -address=127.0.0.1 \
- -port=10250 \
- -hostname_override=127.0.0.1 \
- -etcd_servers=http://127.0.0.1:4001 \
- -v=3 \
- -logtostderr=false \
- -log_dir=./log
创建pod
搭好了运行环境后,就可以提交pod了。首先编写pod描述文件,保存为redis.json:
- {
- "id": "redis",
- "desiredState": {
- "manifest": {
- "version": "v1beta1",
- "id": "redis",
- "containers": [{
- "name": "redis",
- "image": "dockerfile/redis",
- "imagePullPolicy": "PullIfNotPresent",
- "ports": [{
- "containerPort": 6379,
- "hostPort": 6379
- }]
- }]
- }
- },
- "labels": {
- "name": "redis"
- }
- }
然后,通过命令行工具kubecfg提交:
- ./kubecfg -c redis.json create /pods
提交完后,通过kubecfg查看pod状态:
- # ./kubecfg list /pods
- ID Image(s) Host Labels Status
- ---------- ---------- ---------- ---------- ----------
- redis dockerfile/redis 127.0.0.1/ name=redis Running
Status是Running表示pod已经在容器里运行起来了,可以用"docker ps"命令来查看容器信息:
- # docker ps
- CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- ae83d1e4b1ec dockerfile/redis:latest "redis-server /etc/r 19 seconds ago Up 19 seconds k8s_redis.caa18858_redis.default.etcd_1414684622_1b43fe35
创建replicationController
- {
- "id": "redisController",
- "apiVersion": "v1beta1",
- "kind": "ReplicationController",
- "desiredState": {
- "replicas": 1,
- "replicaSelector": {"name": "redis"},
- "podTemplate": {
- "desiredState": {
- "manifest": {
- "version": "v1beta1",
- "id": "redisController",
- "containers": [{
- "name": "redis",
- "image": "dockerfile/redis",
- "imagePullPolicy": "PullIfNotPresent",
- "ports": [{
- "containerPort": 6379,
- "hostPort": 6379
- }]
- }]
- }
- },
- "labels": {"name": "redis"}
- }},
- "labels": {"name": "redis"}
- }
然后,通过命令行工具kubecfg提交:
- ./kubecfg -c redisController.json create /replicationControllers
提交完后,通过kubecfg查看replicationController状态:
- # ./kubecfg list /replicationControllers
- ID Image(s) Selector Replicas
- ---------- ---------- ---------- ----------
- redisController dockerfile/redis name=redis 1
同时,1个pod也将被自动创建出来,即使我们故意删除该pod,replicationController也将保证创建1个新pod。
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)