Docker之~集群配置

一、前言

Kubernetes 是Google開源的容器集群管理系統,基于Docker構建一個容器的調度服務,提供資源調度、均衡容災、服務注冊、動態擴縮容等功能套件,目前最新版本為0.6.2。

本文介紹如何基于Centos7.0構建Kubernetes平臺,在正式介紹之前,大家有必要先理解Kubernetes幾個核心概念及其承擔的功能。以下為Kubernetes的架構設計圖:


  
Docker之~集群配置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、平臺環境說明
Docker之~集群配置

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】

view plainprint?
# 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=""  
view plainprint?
# 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】

 

view plainprint?
# 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】

view plainprint?
# 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】

view plainprint?
###  
# 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】

view plainprint?
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清單

<

p style=”text-indent:2em;”>
注:在新版kubernetes中,所有的操作命令都整合至kubectl,包括kubecfg、kubectl.sh、kubecfg.sh等

2)創建測試pod單元

# /home/kubermange/pods && cd /home/kubermange/pods
   # vi apache-pod.json

view plainprint?
{  
  "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
<p>
    NAME                IMAGE(S)            HOST                LABELS              STATUS<br />

fedoraapache fedora/apache 192.168.1.202/ name=fedoraapache Running
 啟動瀏覽器訪問http://192.168.1.202:8080/,對應的服務端口切記在iptables中已添加。效果圖如下:
 觀察kubernetes在etcd中的數據存儲結構
Docker之~集群配置 
觀察單個pods的數據存儲結構,以json的格式存儲。
  Docker之~集群配置
  

二、實戰操作

任務:通過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 
    
    </div>
</div>
<blockquote>
    <p style="text-indent:2em;">
        1、 創建一個replication ,本例直接在replication模板中創建pod并復制,也可獨立創建pod再通過replication來復制。<br />

【replication/lnmp-replication.json】

view plainprint?
{  
  "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"},  
      },  
  }  
}  
</div>
<p style="text-indent:2em;">
    執行創建命令<br />

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
    </div>
</div>
<p style="text-indent:2em;">
    2、創建一個service,通過selector指定 "name": "webserver_pod"與pods關聯。
【service/lnmp-service.json】

view plainprint?
{  
  "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
    <p>
        &nbsp;
    </p>
    <div style="margin:10px;" class="quote">
        <div class="quote-content">
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


        </div>
    </div>
    <h3 style="text-indent:2em;">
        訪問測試,http://192.168.1.201:40689/info.php,刷新瀏覽器發現proxy后端的變化,默認為隨機輪循算法。<br />

Docker之~集群配置
Docker之~集群配置
 
  

三、測試過程

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
    <p>
        &nbsp;
    </p>
    <div style="margin:10px;" class="quote">
        <div class="quote-content">
[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
        </div>
    </div>
    <p style="text-indent:2em;">
        #自動生成出一個副本,保持6份的效果
    </p>
    <div style="margin:10px;" class="quote">
        <div class="quote-content">
[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

  
        </div>
    </div>
    <div style="text-align:left;" class="textbox-bottom">
        &nbsp;1)pod中hostPort為空,而replicationcontrollers為指定端口,則異常;兩側都指定端口,相同或不同時都異常;pod的hostport為指定,另replicationcon為空,則正常;pod的hostport為空,另replicationcon為空,則正常;結論是在replicationcontrollers場景不能指定hostport,否則異常,待持續測試。<br />

 2)結論:在replicationcontronllers.json中,”replicaSelector”: {“name”: “webserver_pod”}要與”labels”: {“name”: “webserver_pod”}以及service中的”selector”: {“name”: “webserver_pod”}保持一致;

原創文章,作者:renjin,如若轉載,請注明出處:http://www.www58058.com/78816

(0)
renjinrenjin
上一篇 2015-03-21
下一篇 2015-03-23

相關推薦

  • CentOS6.8啟動卡死在開機進度條

    不知道什么原因CentOS6.8開機的時候卡在進度條一直進不去。就是下面的畫面 在這個畫面下面也看不到什么原因,果斷F5切換至有顯示開機進程的界面 看到了上述的錯誤提示:invalid user :'root' root是無效的root這是什么鬼? 接下來重新開機,進入到救援模式 開機的時候快速按一下ESC,進入到CD啟動,然后選擇救援模…

    Linux干貨 2016-12-08
  • 簡單shell腳本習題

    習題 作業 簡單shell腳本習題 習題1 答案 習題2 答案 習題3 答案 習題4 答案 習題5 答案 習題6 答案 習題1 編寫腳本/root/bin/systeminfo.sh,顯示當前主機系統信息,包括主機名,IPv4地址,操作系統版本,內核版本, CPU型號,內存大小,硬盤大小。 答案 #!/bin/bash IPADDR=$(…

    Linux干貨 2017-04-10
  • haproxy 簡單實現80轉后端8000

    一,安裝 yum -y install gcc automake autoconf libtool make tar -xzf haproxy-1.6.8 cd haproxy-1.6.8 make TARGET=linux2628 make install 二,編輯配置文件 Haproxy配置中分成五部分內容,當然這些組件不是必選的,可以根據需要選擇作為配…

    Linux干貨 2016-09-19
  • CentOS7下重置root密碼

    CentOS7下重置root密碼          Linux系統、UNIX系統和其他類UNIX系統中,存在唯一的超級用戶root。普通用戶密碼忘掉可以用root用戶重置,但是一旦root密碼忘掉,事情就復雜起來了。本文主要介紹root密碼忘掉之后,重置密碼的過程。   &nbs…

    Linux干貨 2017-03-30
  • Linux運維學習歷程-第三天-初識Linux

    初識Linux 本章內容    初安裝Linus的網絡配置    防火墻的關閉    用戶    終端    shell    命令紀要 安裝linux之后,linux默認網卡開機不是自動激活,并且防火墻開啟的,這對初期我們學習linux會…

    Linux干貨 2016-08-03
  • Btrfs文件系統

    一:概述     1.1簡介         Btrfs被稱為是下一代Linux文件系統。通常念成 Butter FS,Better FS 或B-tree FS。它采用了很多先進的文件系統設計,不僅解決了 ext2/3的擴展性問題,支持寫時…

    Linux干貨 2016-04-19
欧美性久久久久