一、前言
Kubernetes 是Google开源的容器集群管理系统,基于Docker构建一个容器的调度服务,提供资源调度、均衡容灾、服务注册、动态扩缩容等功能套件,目前最新版本为0.6.2。本文介绍如何基于Centos7.0构建Kubernetes平台,在正式介绍之前,大家有必要先理解Kubernetes几个核心概念及其承担的功能。以下为Kubernetes的架构设计图:
1. Pods
在Kubernetes系统中,调度的最小颗粒不是单纯的容器,而是抽象成一个Pod,Pod是一个可以被创建、销毁、调度、管理的最小的部署单元。比如一个或一组容器。
2. Replication Controllers
Replication Controller是Kubernetes系统中最有用的功能,实现复制多个Pod副本,往往一个应用需要多个Pod来支撑,并且可以保证其复制的副本数,即使副本所调度分配的主宿机出现异常,通过Replication Controller可以保证在其它主宿机启用同等数量的Pod。Replication Controller可以通过repcon模板来创建多个Pod副本,同样也可以直接复制已存在Pod,需要通过Label selector来关联。
3、Services
Services是Kubernetes最外围的单元,通过虚拟一个访问IP及服务端口,可以访问我们定义好的Pod资源,目前的版本是通过iptables的nat转发来实现,转发的目标端口为Kube_proxy生成的随机端口,目前只提供GOOGLE云上的访问调度,如GCE。如果与我们自建的平台进行整合?请关注下篇《kubernetes与HECD架构的整合》文章。
4、Labels
Labels是用于区分Pod、Service、Replication Controller的key/value键值对,仅使用在Pod、Service、 Replication Controller之间的关系识别,但对这些单元本身进行操作时得使用name标签。
5、Proxy
Proxy不但解决了同一主宿机相同服务端口冲突的问题,还提供了Service转发服务端口对外提供服务的能力,Proxy后端使用了随机、轮循负载均衡算法。
说说个人一点看法,目前Kubernetes 保持一周一小版本、一个月一大版本的节奏,迭代速度极快,同时也带来了不同版本操作方法的差异,另外官网文档更新速度相对滞后及欠缺,给初学者带来一定挑战。在上游接入层官方侧重点还放在GCE(Google Compute Engine)的对接优化,针对个人私有云还未推出一套可行的接入解决方案。在v0.5版本中才引用service代理转发的机制,且是通过iptables来实现,在高并发下性能令人担忧。但作者依然看好Kubernetes未来的发展,至少目前还未看到另外一个成体系、具备良好生态圈的平台,相信在V1.0时就会具备生产环境的服务支撑能力。
一、环境部署
1、平台版本说明
1)Centos7.0 OS
2)Kubernetes V0.6.2
3)etcd version 0.4.6
4)Docker version 1.3.2
2、平台环境说明
3、环境安装
1)系统初始化工作(所有主机)
系统安装-选择[最小化安装]
# yum -y install wget ntpdate bind-utils
# wget http://mirror.centos.org/centos/7/extras/x86_64/Packages/epel-release-7-2.noarch.rpm
# yum update
CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙(熟悉度更高,非必须)。
1.1、关闭firewall:
# systemctl stop firewalld.service #停止firewall
# systemctl disable firewalld.service #禁止firewall开机启动
1.2、安装iptables防火墙
# yum install iptables-services #安装
# systemctl start iptables.service #最后重启防火墙使配置生效
# systemctl enable iptables.service #设置防火墙开机启动
2)安装Etcd(192.168.1.10主机)
# mkdir -p /home/install && cd /home/install
# wget https://github.com/coreos/etcd/releases/download/v0.4.6/etcd-v0.4.6-linux-amd64.tar.gz
# tar -zxvf etcd-v0.4.6-linux-amd64.tar.gz
# cd etcd-v0.4.6-linux-amd64
# cp etcd* /bin/
# /bin/etcd -version
etcd version 0.4.6
启动服务etcd服务,如有提供第三方管理需求,另需在启动参数中添加“-cors='*'”参数。
# mkdir /data/etcd
# /bin/etcd -name etcdserver -peer-addr 192.168.1.10:7001 -addr 192.168.1.10:4001 -data-dir /data/etcd -peer-bind-addr 0.0.0.0:7001 -bind-addr 0.0.0.0:4001 &
配置etcd服务防火墙,其中4001为服务端口,7001为集群数据交互端口。
# iptables -I INPUT -s 192.168.1.0/24 -p tcp --dport 4001 -j ACCEPT
# iptables -I INPUT -s 192.168.1.0/24 -p tcp --dport 7001 -j ACCEPT
3)安装Kubernetes(涉及所有Master、Minion主机)
通过yum源方式安装,默认将安装etcd, docker, and cadvisor相关包。
# curl https://copr.fedoraproject.org/coprs/eparis/kubernetes-epel-7/repo/epel-7/eparis-kubernetes-epel-7-epel-7.repo -o /etc/yum.repos.d/eparis-kubernetes-epel-7-epel-7.repo
#yum -y install kubernetes
升级至v0.6.2,覆盖bin文件即可,方法如下:
# mkdir -p /home/install && cd /home/install
# wget https://github.com/GoogleCloudPlatform/kubernetes/releases/download/v0.6.2/kubernetes.tar.gz
# tar -zxvf kubernetes.tar.gz
# tar -zxvf kubernetes/server/kubernetes-server-linux-amd64.tar.gz
# cp kubernetes/server/bin/kube* /usr/bin
校验安装结果,出版以下信息说明安装正常。
[root@SN2014-12-200 bin]# /usr/bin/kubectl version
Client Version: version.Info{Major:"0", Minor:"6+", GitVersion:"v0.6.2", GitCommit:"729fde276613eedcd99ecf5b93f095b8deb64eb4", GitTreeState:"clean"}
Server Version: &version.Info{Major:"0", Minor:"6+", GitVersion:"v0.6.2", GitCommit:"729fde276613eedcd99ecf5b93f095b8deb64eb4", GitTreeState:"clean"}
4)Kubernetes配置(仅Master主机)
master运行三个组件,包括apiserver、scheduler、controller-manager,相关配置项也只涉及这三块。
4.1、【/etc/kubernetes/config】
- # Comma seperated list of nodes in the etcd cluster
- KUBE_ETCD_SERVERS="--etcd_servers=http://192.168.1.10:4001"
- # logging to stderr means we get it in the systemd journal
- KUBE_LOGTOSTDERR="--logtostderr=true"
- # journal message level, 0 is debug
- KUBE_LOG_LEVEL="--v=0"
- # Should this cluster be allowed to run privleged docker containers
- KUBE_ALLOW_PRIV="--allow_privileged=false"
4.2、【/etc/kubernetes/apiserver】
- # The address on the local server to listen to.
- KUBE_API_ADDRESS="--address=0.0.0.0"
- # The port on the local server to listen on.
- KUBE_API_PORT="--port=8080"
- # How the replication controller and scheduler find the kube-apiserver
- KUBE_MASTER="--master=192.168.1.200:8080"
- # Port minions listen on
- KUBELET_PORT="--kubelet_port=10250"
- # Address range to use for services
- KUBE_SERVICE_ADDRESSES="--portal_net=10.254.0.0/16"
- # Add you own!
- KUBE_API_ARGS=""
4.3、【/etc/kubernetes/controller-manager】
- # Comma seperated list of minions
- KUBELET_ADDRESSES="--machines= 192.168.1.201,192.168.1.202"
- # Add you own!
- KUBE_CONTROLLER_MANAGER_ARGS=""
4.4、【/etc/kubernetes/scheduler】
- # Add your own!
- KUBE_SCHEDULER_ARGS=""
启动master侧相关服务
# systemctl daemon-reload
# systemctl start kube-apiserver.service kube-controller-manager.service kube-scheduler.service
# systemctl enable kube-apiserver.service kube-controller-manager.service kube-scheduler.service
5)Kubernetes配置(仅minion主机)
minion运行两个组件,包括kubelet、proxy,相关配置项也只涉及这两块。
Docker启动脚本更新
# vi /etc/sysconfig/docker
添加:-H tcp://0.0.0.0:2375,最终配置如下,以便以后提供远程API维护。
OPTIONS=--selinux-enabled -H tcp://0.0.0.0:2375 -H fd://
修改minion防火墙配置,通常master找不到minion主机多半是由于端口没有连通。
iptables -I INPUT -s 192.168.1.200 -p tcp --dport 10250 -j ACCEPT
修改kubernetes minion端配置,以192.168.1.201主机为例,其它minion主机同理。
5.1、【/etc/kubernetes/config】
- # Comma seperated list of nodes in the etcd cluster
- KUBE_ETCD_SERVERS="--etcd_servers=http://192.168.1.10:4001"
- # logging to stderr means we get it in the systemd journal
- KUBE_LOGTOSTDERR="--logtostderr=true"
- # journal message level, 0 is debug
- KUBE_LOG_LEVEL="--v=0"
- # Should this cluster be allowed to run privleged docker containers
- KUBE_ALLOW_PRIV="--allow_privileged=false"
5.2、【/etc/kubernetes/kubelet】
- ###
- # kubernetes kubelet (minion) config
- # The address for the info server to serve on (set to 0.0.0.0 or "" for all interfaces)
- KUBELET_ADDRESS="--address=0.0.0.0"
- # The port for the info server to serve on
- KUBELET_PORT="--port=10250"
- # You may leave this blank to use the actual hostname
- KUBELET_HOSTNAME="--hostname_override=192.168.1.201"
- # Add your own!
- KUBELET_ARGS=""
5.3、【/etc/kubernetes/proxy】
- KUBE_PROXY_ARGS=""
启动kubernetes服务
# systemctl daemon-reload
# systemctl enable docker.service kubelet.service kube-proxy.service
# systemctl start docker.service kubelet.service kube-proxy.service
3、校验安装(在master主机操作,或可访问master主机8080端口的client api主机)
1) kubernetes常用命令
# kubectl get minions #查查看minion主机
# kubectl get pods #查看pods清单
# kubectl get services 或 kubectl get services -o json #查看service清单
# kubectl get replicationControllers #查看replicationControllers清单
# for i in `kubectl get pod|tail -n +2|awk '{print $1}'`; do kubectl delete pod $i; done #删除所有pods
或者通过Server api for REST方式(推荐,及时性更高):
# curl -s -L http://192.168.1.200:8080/api/v1beta1/version | python -mjson.tool #查看kubernetes版本
# curl -s -L http://192.168.1.200:8080/api/v1beta1/pods | python -mjson.tool #查看pods清单
# curl -s -L http://192.168.1.200:8080/api/v1beta1/replicationControllers | python -mjson.tool #查看replicationControllers清单
# curl -s -L http://192.168.1.200:8080/api/v1beta1/minions | python -m json.tool #查查看minion主机
# curl -s -L http://192.168.1.200:8080/api/v1beta1/services | python -m json.tool #查看service清单
注:在新版kubernetes中,所有的操作命令都整合至kubectl,包括kubecfg、kubectl.sh、kubecfg.sh等
2)创建测试pod单元
# /home/kubermange/pods && cd /home/kubermange/pods
# vi apache-pod.json
- {
- "id": "fedoraapache",
- "kind": "Pod",
- "apiVersion": "v1beta1",
- "desiredState": {
- "manifest": {
- "version": "v1beta1",
- "id": "fedoraapache",
- "containers": [{
- "name": "fedoraapache",
- "image": "fedora/apache",
- "ports": [{
- "containerPort": 80,
- "hostPort": 8080
- }]
- }]
- }
- },
- "labels": {
- "name": "fedoraapache"
- }
- }
# kubectl create -f apache-pod.json
# kubectl get pod
NAME IMAGE(S) HOST LABELS STATUS
fedoraapache fedora/apache 192.168.1.202/ name=fedoraapache Running
启动浏览器访问http://192.168.1.202:8080/,对应的服务端口切记在iptables中已添加。效果图如下:
观察kubernetes在etcd中的数据存储结构
观察单个pods的数据存储结构,以json的格式存储。
二、实战操作
任务:通过Kubernetes创建一个LNMP架构的服务集群,以及观察其负载均衡,涉及镜像“yorko/webserver”已经push至registry.hub.docker.com,大家可以通过“docker pull yorko/webserver”下载。
# mkdir -p /home/kubermange/replication && mkdir -p /home/kubermange/service
# cd /home/kubermange/replication
1、 创建一个replication ,本例直接在replication模板中创建pod并复制,也可独立创建pod再通过replication来复制。
【replication/lnmp-replication.json】
- {
- "id": "webserverController",
- "kind": "ReplicationController",
- "apiVersion": "v1beta1",
- "labels": {"name": "webserver"},
- "desiredState": {
- "replicas": 2,
- "replicaSelector": {"name": "webserver_pod"},
- "podTemplate": {
- "desiredState": {
- "manifest": {
- "version": "v1beta1",
- "id": "webserver",
- "volumes": [
- {"name":"httpconf", "source":{"hostDir":{"path":"/etc/httpd/conf"}}},
- {"name":"httpconfd", "source":{"hostDir":{"path":"/etc/httpd/conf.d"}}},
- {"name":"httproot", "source":{"hostDir":{"path":"/data"}}}
- ],
- "containers": [{
- "name": "webserver",
- "image": "yorko/webserver",
- "command": ["/bin/sh", "-c", "/usr/bin/supervisord -c /etc/supervisord.conf"],
- "volumeMounts": [
- {"name":"httpconf", "mountPath":"/etc/httpd/conf"},
- {"name":"httpconfd", "mountPath":"/etc/httpd/conf.d"},
- {"name":"httproot", "mountPath":"/data"}
- ],
- "cpu": 100,
- "memory": 50000000,
- "ports": [{
- "containerPort": 80,
- },{
- "containerPort": 22,
- }]
- }]
- }
- },
- "labels": {"name": "webserver_pod"},
- },
- }
- }
执行创建命令
#kubectl create -f lnmp-replication.json
观察生成的pod副本清单:
[root@SN2014-12-200 replication]# kubectl get pod
NAME IMAGE(S) HOST LABELS STATUS
84150ab7-89f8-11e4-970d-000c292f1620 yorko/webserver 192.168.1.202/ name=webserver_pod Running
84154ed5-89f8-11e4-970d-000c292f1620 yorko/webserver 192.168.1.201/ name=webserver_pod Running
840beb1b-89f8-11e4-970d-000c292f1620 yorko/webserver 192.168.1.202/ name=webserver_pod Running
84152d93-89f8-11e4-970d-000c292f1620 yorko/webserver 192.168.1.202/ name=webserver_pod Running
840db120-89f8-11e4-970d-000c292f1620 yorko/webserver 192.168.1.201/ name=webserver_pod Running
8413b4f3-89f8-11e4-970d-000c292f1620 yorko/webserver 192.168.1.201/ name=webserver_pod Running
2、创建一个service,通过selector指定 "name": "webserver_pod"与pods关联。
【service/lnmp-service.json】
- {
- "id": "webserver",
- "kind": "Service",
- "apiVersion": "v1beta1",
- "selector": {
- "name": "webserver_pod",
- },
- "protocol": "TCP",
- "containerPort": 80,
- "port": 8080
- }
执行创建命令:
# kubectl create -f lnmp-service.json
登录minion主机(192.168.1.201),查询主宿机生成的iptables转发规则(最后一行)
# iptables -nvL -t nat
Chain KUBE-PROXY (2 references)
pkts bytes target prot opt in out source destination
2 120 REDIRECT tcp -- * * 0.0.0.0/0 10.254.102.162 /* kubernetes */ tcp dpt:443 redir ports 47700
1 60 REDIRECT tcp -- * * 0.0.0.0/0 10.254.28.74 /* kubernetes-ro */ tcp dpt:80 redir ports 60099
0 0 REDIRECT tcp -- * * 0.0.0.0/0 10.254.216.51 /* webserver */ tcp dpt:8080 redir ports 40689
访问测试,http://192.168.1.201:40689/info.php,刷新浏览器发现proxy后端的变化,默认为随机轮循算法。
三、测试过程
1、pods自动复制、销毁测试,观察kubernetes自动保持副本数(6份)
删除replicationcontrollers中一个副本fedoraapache
[root@SN2014-12-200 pods]# kubectl delete pods fedoraapache
I1219 23:59:39.305730 9516 restclient.go:133] Waiting for completion of operation 142530
fedoraapache
[root@SN2014-12-200 pods]# kubectl get pods
NAME IMAGE(S) HOST LABELS STATUS
5d70892e-8794-11e4-970d-000c292f1620 fedora/apache 192.168.1.201/ name=fedoraapache Running
5d715e56-8794-11e4-970d-000c292f1620 fedora/apache 192.168.1.202/ name=fedoraapache Running
5d717f8d-8794-11e4-970d-000c292f1620 fedora/apache 192.168.1.202/ name=fedoraapache Running
5d71c584-8794-11e4-970d-000c292f1620 fedora/apache 192.168.1.201/ name=fedoraapache Running
5d71a494-8794-11e4-970d-000c292f1620 fedora/apache 192.168.1.202/ name=fedoraapache Running
#自动生成出一个副本,保持6份的效果
[root@SN2014-12-200 pods]# kubectl get pods
NAME IMAGE(S) HOST LABELS STATUS
5d717f8d-8794-11e4-970d-000c292f1620 fedora/apache 192.168.1.202/ name=fedoraapache Running
5d71c584-8794-11e4-970d-000c292f1620 fedora/apache 192.168.1.201/ name=fedoraapache Running
5d71a494-8794-11e4-970d-000c292f1620 fedora/apache 192.168.1.202/ name=fedoraapache Running
2a8fb993-8798-11e4-970d-000c292f1620 fedora/apache 192.168.1.201/ name=fedoraapache Running
5d70892e-8794-11e4-970d-000c292f1620 fedora/apache 192.168.1.201/ name=fedoraapache Running
5d715e56-8794-11e4-970d-000c292f1620 fedora/apache 192.168.1.202/ name=fedoraapache Running
2、测试不同角色模块中的hostPort
1)pod中hostPort为空,而replicationcontrollers为指定端口,则异常;两侧都指定端口,相同或不同时都异常;pod的hostport为指定,另replicationcon为空,则正常;pod的hostport为空,另replicationcon为空,则正常;结论是在replicationcontrollers场景不能指定hostport,否则异常,待持续测试。
2)结论:在replicationcontronllers.json中,"replicaSelector": {"name": "webserver_pod"}要与"labels": {"name": "webserver_pod"}以及service中的"selector": {"name": "webserver_pod"}保持一致;
请关注下篇《kubernetes与HECD架构的整合》,近期推出。
参考文献:
https://github.com/GoogleCloudPlatform/kubernetes/blob/master/docs/getting-started-guides/fedora/fedora_manual_config.md
https://github.com/GoogleCloudPlatform/kubernetes/blob/master/DESIGN.md
http://www.infoq.com/cn/articles/Kubernetes-system-architecture-introduction