基于Kubernetes构建Docker集群管理详解
from: 基于Kubernetes構(gòu)建Docker集群管理詳解
Kubernetes是Google開源的容器集群管理系統(tǒng),基于Docker構(gòu)建一個容器的調(diào)度服務(wù),提供資源調(diào)度、均衡容災(zāi)、服務(wù)注冊、動態(tài)擴(kuò)縮容等功能套件,目前最新版本為0.6.2。本文介紹如何基于CentOS7.0構(gòu)建Kubernetes平臺。
在正式介紹之前,大家有必要先理解Kubernetes幾個核心概念及其承擔(dān)的功能。以下為Kubernetes的架構(gòu)設(shè)計圖:
1. Pods
在Kubernetes系統(tǒng)中,調(diào)度的最小顆粒不是單純的容器,而是抽象成一個Pod,Pod是一個可以被創(chuàng)建、銷毀、調(diào)度、管理的最小的部署單元。比如一個或一組容器。
2. Replication Controllers
Replication Controller是Kubernetes系統(tǒng)中最有用的功能,實現(xiàn)復(fù)制多個Pod副本,往往一個應(yīng)用需要多個Pod來支撐,并且可以保證其復(fù)制的副本數(shù),即使副本所調(diào)度分配的宿主機(jī)出現(xiàn)異常,通過Replication Controller可以保證在其它主宿機(jī)啟用同等數(shù)量的Pod。Replication Controller可以通過repcon模板來創(chuàng)建多個Pod副本,同樣也可以直接復(fù)制已存在Pod,需要通過Label selector來關(guān)聯(lián)。
3. Services
Services是Kubernetes最外圍的單元,通過虛擬一個訪問IP及服務(wù)端口,可以訪問我們定義好的Pod資源,目前的版本是通過iptables的nat轉(zhuǎn)發(fā)來實現(xiàn),轉(zhuǎn)發(fā)的目標(biāo)端口為Kube_proxy生成的隨機(jī)端口,目前只提供GOOGLE云上的訪問調(diào)度,如GCE。如果與我們自建的平臺進(jìn)行整合?請關(guān)注下篇《kubernetes與HECD架構(gòu)的整合》文章。
4. Labels
Labels是用于區(qū)分Pod、Service、Replication Controller的key/value鍵值對,僅使用在Pod、Service、Replication Controller之間的關(guān)系識別,但對這些單元本身進(jìn)行操作時得使用name標(biāo)簽。
5. Proxy
Proxy不但解決了同一主宿機(jī)相同服務(wù)端口沖突的問題,還提供了Service轉(zhuǎn)發(fā)服務(wù)端口對外提供服務(wù)的能力,Proxy后端使用了隨機(jī)、輪循負(fù)載均衡算法。
說說個人一點看法,目前Kubernetes 保持一周一小版本、一個月一大版本的節(jié)奏,迭代速度極快,同時也帶來了不同版本操作方法的差異,另外官網(wǎng)文檔更新速度相對滯后及欠缺,給初學(xué)者帶來一定挑戰(zhàn)。在上游接入層官方側(cè)重點還放在GCE(Google Compute Engine)的對接優(yōu)化,針對個人私有云還未推出一套可行的接入解決方案。在v0.5版本中才引用service代理轉(zhuǎn)發(fā)的機(jī)制,且是通過iptables來實現(xiàn),在高并發(fā)下性能令人擔(dān)憂。但作者依然看好Kubernetes未來的發(fā)展,至少目前還未看到另外一個成體系、具備良好生態(tài)圈的平臺,相信在V1.0時就會具備生產(chǎn)環(huán)境的服務(wù)支撐能力。
一、環(huán)境部署
1. 平臺版本說明
2. 平臺環(huán)境說明
3. 環(huán)境安裝
1)系統(tǒng)初始化工作(所有主機(jī))
系統(tǒng)安裝-選擇[最小化安裝]
# wget?http://mirror.centos.org/centos/7/extras/x86_64/Packages/epel-release-7-2.noarch.rpm
# yum update
CentOS 7.0默認(rèn)使用的是firewall作為防火墻,這里改為iptables防火墻(熟悉度更高,非必須)。
1.1 關(guān)閉firewall:1.1、關(guān)閉firewall:
# systemctl stop firewalld.service #停止firewall# systemctl disable firewalld.service #禁止firewall開機(jī)啟動
1.2 安裝iptables防火墻
# yum install iptables-services #安裝# systemctl start iptables.service #最后重啟防火墻使配置生效
# systemctl enable iptables.service #設(shè)置防火墻開機(jī)啟動
2)安裝Etcd(192.168.1.10主機(jī))
# 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?
啟動服務(wù)etcd服務(wù),如有提供第三方管理需求,另需在啟動參數(shù)中添加“-cors='*'”參數(shù)。
# 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服務(wù)防火墻,其中4001為服務(wù)端口,7001為集群數(shù)據(jù)交互端口。
# 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主機(jī))
通過yum源方式安裝,默認(rèn)將安裝etcd, docker, and cadvisor相關(guān)包。
# 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# wgethttps://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
校驗安裝結(jié)果,出版以下信息說明安裝正常。
[root@SN2014-12-200 bin]# /usr/bin/kubectl versionClient 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主機(jī))
master運行三個組件,包括apiserver、scheduler、controller-manager,相關(guān)配置項也只涉及這三塊。
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側(cè)相關(guān)服務(wù)
# 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主機(jī))
minion運行兩個組件,包括kubelet、proxy,相關(guān)配置項也只涉及這兩塊。
Docker啟動腳本更新
# vi /etc/sysconfig/docker添加:-H tcp://0.0.0.0:2375,最終配置如下,以便以后提供遠(yuǎn)程API維護(hù)。
OPTIONS=--selinux-enabled -H tcp://0.0.0.0:2375 -H fd://修改minion防火墻配置,通常master找不到minion主機(jī)多半是由于端口沒有連通。
iptables -I INPUT -s 192.168.1.200 -p tcp --dport 10250 -j ACCEPT修改kubernetes minion端配置,以192.168.1.201主機(jī)為例,其它minion主機(jī)同理。
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服務(wù)
# systemctl daemon-reload# systemctl enable docker.service kubelet.service kube-proxy.service
# systemctl start docker.service kubelet.service kube-proxy.service
3.校驗安裝(在master主機(jī)操作,或可訪問master主機(jī)8080端口的client api主機(jī))
1) kubernetes常用命令
# kubectl get minions ? ?#查查看minion主機(jī)# 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主機(jī)
# 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)創(chuàng)建測試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 ? ? ? ? ? ? ?STATUSfedoraapache ? ? ? ?fedora/apache ? ? ? 192.168.1.202/ ? ? ?name=fedoraapache ? Running
啟動瀏覽器訪問http://192.168.1.202:8080/,對應(yīng)的服務(wù)端口切記在iptables中已添加。效果圖如下:
觀察kubernetes在etcd中的數(shù)據(jù)存儲結(jié)構(gòu)
觀察單個pods的數(shù)據(jù)存儲結(jié)構(gòu),以json的格式存儲。
二、實戰(zhàn)操作
任務(wù):通過Kubernetes創(chuàng)建一個LNMP架構(gòu)的服務(wù)集群,以及觀察其負(fù)載均衡,涉及鏡像“yorko/webserver”已經(jīng)push至registry.hub.docker.com,大家可以通過“docker pull yorko/webserver”下載。
# mkdir -p /home/kubermange/replication && mkdir -p /home/kubermange/service# cd /home/kubermange/replication?
1. 創(chuàng)建一個replication,本例直接在replication模板中創(chuàng)建pod并復(fù)制,也可獨立創(chuàng)建pod再通過replication來復(fù)制。
【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"}, ?
? ? ? }, ?
? } ?
} ?
執(zhí)行創(chuàng)建命令
#kubectl create -f lnmp-replication.json
觀察生成的pod副本清單:
[root@SN2014-12-200 replication]# kubectl get pod
NAME ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? IMAGE(S) ? ? ? ? ? ?HOST ? ? ? ? ? ? ? ?LABELS ? ? ? ? ? ? ? STATUS84150ab7-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.創(chuàng)建一個service,通過selector指定 "name": "webserver_pod"與pods關(guān)聯(lián)。
【service/lnmp-service.json】
{ ?? "id": "webserver", ?
? "kind": "Service", ?
? "apiVersion": "v1beta1", ?
? "selector": { ?
? ? "name": "webserver_pod", ?
? }, ?
? "protocol": "TCP", ?
? "containerPort": 80, ?
? "port": 8080 ?
} ?
執(zhí)行創(chuàng)建命令:
# kubectl create -f lnmp-service.json
登錄minion主機(jī)(192.168.1.201),查詢主宿機(jī)生成的iptables轉(zhuǎn)發(fā)規(guī)則(最后一行)
# 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,刷新瀏覽器發(fā)現(xiàn)proxy后端的變化,默認(rèn)為隨機(jī)輪循算法。
三、測試過程
1.pods自動復(fù)制、銷毀測試,觀察Kubernetes自動保持副本數(shù)(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 ? ? ? ? ? ? ?STATUS5d70892e-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 ? ? ? ? ? ? ?STATUS5d717f8d-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為指定端口,則異常;兩側(cè)都指定端口,相同或不同時都異常;pod的hostport為指定,另replicationcon為空,則正常;pod的hostport為空,另replicationcon為空,則正常;結(jié)論是在replicationcontrollers場景不能指定hostport,否則異常,待持續(xù)測試。
2)結(jié)論:在replicationcontronllers.json中,"replicaSelector": {"name": "webserver_pod"}要與"labels": {"name": "webserver_pod"}以及service中的"selector": {"name": "webserver_pod"}保持一致;
下篇文章中,劉天斯計劃介紹《Kubernetes與HECD架構(gòu)的整合》,敬請期待!
參考文獻(xiàn):
Kubernetes?fedora_manual_config.md
kubernetes/DESIGN.md
Kubernetes系統(tǒng)架構(gòu)簡介
總結(jié)
以上是生活随笔為你收集整理的基于Kubernetes构建Docker集群管理详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 图像拼接和图像融合技术
- 下一篇: latex 公式转图片