1. 程式人生 > >Kubernetes入門-叢集安裝

Kubernetes入門-叢集安裝

  Kubernetes是谷歌開源的容器叢集編排平臺,是一個完備的分散式系統支撐平臺,為容器化應用提供部署執行、資源排程、服務發現和動態伸縮等一系列完整功能,具有強大的故障發現和自我修復機制、服務滾動升級和線上擴容能力,可擴充套件資源自動排程機制以及多粒度的資源配額管理能力。
  Kubernetes目前以77%的市場份額獨佔容器編排平臺市場鰲頭。目前所有的主要雲服務提供商(亞馬遜、微軟、Google、IBM等)都將其作為部署雲原生應用的解決方案。Kubernetes已經在大量企業中落地,並廣泛的運用在生產環境中。
作為容器化的主流執行和編排管理平臺,Kubernetes的重要性毋庸置疑,以下相關內容只是本人的相關筆記,僅作參考,歡迎業內各路大神指點。

下面是K8S架構簡圖:

可以看出,K8S架構分Master+Node兩部分,其中Master節點作為管理節點,主要提供管理使用者相關操作,Node節點作為應用部署節點,實現業務功能,被Master節點管理控制和排程。通過Master和Node節點的各大元件,不難看出,要安裝K8S叢集,需要分別安裝如下元件:

Master節點:

元件名稱 元件說明
etcd 非關係型資料庫,用於儲存相關資訊
kube-apiserver 核心通訊元件,其他元件通過它實現通訊
kube-controller-manager 叢集內部管理中心,通過各種manager實現叢集管理
kube-scheduler 負責相關排程,比如node上pod的排程

 

 

 

 

Node節點:

元件名稱 元件說明
kube-proxy 負責網路通訊,相當與代理,實現請求到pod的轉發
kubelet node節點核心元件,作為Master節點任務的執行者

 

 

 

安裝環境如下:

作業系統 節點角色 節點IP
centOS7.2 X64 Master 120.27.15.116
centOS7.2 X64 node 39.108.85.46

 

 

  

安裝過程如下:

step1、安裝etcd,這個不屬於k8s的東東,直接通過yum install etcd 安裝即可

 1 [[email protected] kubernetes]# yum install etcd 
 2 Loaded plugins: fastestmirror
 3 Loading mirror speeds from cached hostfile
 4 Resolving Dependencies
 5 --> Running transaction check
 6 ---> Package etcd.x86_64 0:3.2.22-1.el7 will be installed
 7 --> Finished Dependency Resolution
 8 
 9 Dependencies Resolved
10 
11 ========================================================================================================================================================================
12  Package                              Arch                                   Version                                       Repository                              Size
13 ========================================================================================================================================================================
14 Installing:
15  etcd                                 x86_64                                 3.2.22-1.el7                                  extras                                 9.3 M
16 
17 Transaction Summary
18 ========================================================================================================================================================================
19 Install  1 Package
20 
21 Total download size: 9.3 M
22 Installed size: 42 M
23 Is this ok [y/d/N]: y
24 Downloading packages:
25 etcd-3.2.22-1.el7.x86_64.rpm                                                                                                                     | 9.3 MB  00:00:01     
26 Running transaction check
27 Running transaction test
28 Transaction test succeeded
29 Running transaction
30   Installing : etcd-3.2.22-1.el7.x86_64                                                                                                                             1/1 
31   Verifying  : etcd-3.2.22-1.el7.x86_64                                                                                                                             1/1 
32 
33 Installed:
34   etcd.x86_64 0:3.2.22-1.el7                                                                                                                                            
35 
36 Complete!
View Code

安裝完進行配置:

vi /etc/etcd/etcd.conf

 1 [[email protected] etcd]# vi /etc/etcd/etcd.conf 
 2 
 3 #[Member]
 4 #ETCD_CORS=""
 5 ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
 6 #ETCD_WAL_DIR=""
 7 #ETCD_LISTEN_PEER_URLS="http://localhost:2380"
 8 ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379,http://0.0.0.0:4001"
 9 #ETCD_MAX_SNAPSHOTS="5"
10 #ETCD_MAX_WALS="5"
11 ETCD_NAME="master"
12 #ETCD_SNAPSHOT_COUNT="100000"
13 #ETCD_HEARTBEAT_INTERVAL="100"
14 #ETCD_ELECTION_TIMEOUT="1000"
15 #ETCD_QUOTA_BACKEND_BYTES="0"
16 #ETCD_MAX_REQUEST_BYTES="1572864"
17 #ETCD_GRPC_KEEPALIVE_MIN_TIME="5s"
18 #ETCD_GRPC_KEEPALIVE_INTERVAL="2h0m0s"
19 #ETCD_GRPC_KEEPALIVE_TIMEOUT="20s"
20 
21 #
22 #[Clustering]
23 #ETCD_INITIAL_ADVERTISE_PEER_URLS="http://localhost:2380"
24 ETCD_ADVERTISE_CLIENT_URLS="http://120.27.15.116:2379,http://120.27.15.116:4001"
25 #ETCD_DISCOVERY=""
26 #ETCD_DISCOVERY_FALLBACK="proxy"
27 #ETCD_DISCOVERY_PROXY=""
28 #ETCD_DISCOVERY_SRV=""
29 #ETCD_INITIAL_CLUSTER="default=http://localhost:2380"
30 #ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
31 #ETCD_INITIAL_CLUSTER_STATE="new"
32 #ETCD_STRICT_RECONFIG_CHECK="true"
33 #ETCD_ENABLE_V2="true"
34 #
35 #[Proxy]
36 #ETCD_PROXY="off"
37 #ETCD_PROXY_FAILURE_WAIT="5000"
38 #ETCD_PROXY_REFRESH_INTERVAL="30000"
39 #ETCD_PROXY_DIAL_TIMEOUT="1000"
40 #ETCD_PROXY_WRITE_TIMEOUT="5000"
41 #ETCD_PROXY_READ_TIMEOUT="0"
42 #
43 #[Security]
44 #ETCD_CERT_FILE=""
45 #ETCD_KEY_FILE=""
46 #ETCD_CLIENT_CERT_AUTH="false"
47 #ETCD_TRUSTED_CA_FILE=""
48 #ETCD_AUTO_TLS="false"
49 #ETCD_PEER_CERT_FILE=""
50 #ETCD_PEER_KEY_FILE=""
51 #ETCD_PEER_CLIENT_CERT_AUTH="false"
52 #ETCD_PEER_TRUSTED_CA_FILE=""
53 #ETCD_PEER_AUTO_TLS="false"
54 #
55 #[Logging]
56 #ETCD_DEBUG="false"
57 #ETCD_LOG_PACKAGE_LEVELS=""
58 #ETCD_LOG_OUTPUT="default"
59 #
60 #[Unsafe]
61 #ETCD_FORCE_NEW_CLUSTER="false"
62 #
63 #[Version]
64 #ETCD_VERSION="false"
65 #ETCD_AUTO_COMPACTION_RETENTION="0"
66 #
67 #[Profiling]
68 #ETCD_ENABLE_PPROF="false"
69 #ETCD_METRICS="basic"
70 #
71 #[Auth]
72 #ETCD_AUTH_TOKEN="simple"
View Code

啟動etcd,然後測試:

systemctl enable etcd

systemctl start etcd

分別存入一個值{test/key1:123}進行測試

etcdctl set test/key1 123

etcd get test/key1

 1 [[email protected] etcd]# etcdctl set test/key1 123
 2 123
 3 [[email protected] etcd]# etcdctl get test/key1
 4 123
 5 [[email protected]sz etcd]# 
 6 [[email protected]sz etcd]# 
 7 [[email protected]sz etcd]# 
 8 [[email protected] etcd]# etcdctl -C http://localhost:2379 cluster-health
 9 member 8e9e05c52164694d is healthy: got healthy result from http://localhost:2379
10 cluster is healthy
View Code

以上說明etcd安裝成功了!

step2、Master節點安裝Kubernetes

執行:yum install kubernetes -y

 1 [[email protected] k8s]# yum install kubernetes -y
 2 Loaded plugins: fastestmirror
 3 Loading mirror speeds from cached hostfile
 4 Resolving Dependencies
 5 --> Running transaction check
 6 ---> Package kubernetes.x86_64 0:1.5.2-0.7.git269f928.el7 will be installed
 7 --> Processing Dependency: kubernetes-node = 1.5.2-0.7.git269f928.el7 for package: kubernetes-1.5.2-0.7.git269f928.el7.x86_64
 8 --> Processing Dependency: kubernetes-master = 1.5.2-0.7.git269f928.el7 for package: kubernetes-1.5.2-0.7.git269f928.el7.x86_64
 9 --> Running transaction check
10 ---> Package kubernetes-master.x86_64 0:1.5.2-0.7.git269f928.el7 will be installed
11 --> Processing Dependency: kubernetes-client = 1.5.2-0.7.git269f928.el7 for package: kubernetes-master-1.5.2-0.7.git269f928.el7.x86_64
12 ---> Package kubernetes-node.x86_64 0:1.5.2-0.7.git269f928.el7 will be installed
13 --> Processing Dependency: socat for package: kubernetes-node-1.5.2-0.7.git269f928.el7.x86_64
14 --> Processing Dependency: conntrack-tools for package: kubernetes-node-1.5.2-0.7.git269f928.el7.x86_64
15 --> Running transaction check
16 ---> Package conntrack-tools.x86_64 0:1.4.4-3.el7_3 will be installed
17 --> Processing Dependency: libnetfilter_cttimeout.so.1(LIBNETFILTER_CTTIMEOUT_1.1)(64bit) for package: conntrack-tools-1.4.4-3.el7_3.x86_64
18 --> Processing Dependency: libnetfilter_cttimeout.so.1(LIBNETFILTER_CTTIMEOUT_1.0)(64bit) for package: conntrack-tools-1.4.4-3.el7_3.x86_64
19 --> Processing Dependency: libnetfilter_cthelper.so.0(LIBNETFILTER_CTHELPER_1.0)(64bit) for package: conntrack-tools-1.4.4-3.el7_3.x86_64
20 --> Processing Dependency: libnetfilter_queue.so.1()(64bit) for package: conntrack-tools-1.4.4-3.el7_3.x86_64
21 --> Processing Dependency: libnetfilter_cttimeout.so.1()(64bit) for package: conntrack-tools-1.4.4-3.el7_3.x86_64
22 --> Processing Dependency: libnetfilter_cthelper.so.0()(64bit) for package: conntrack-tools-1.4.4-3.el7_3.x86_64
23 ---> Package kubernetes-client.x86_64 0:1.5.2-0.7.git269f928.el7 will be installed
24 ---> Package socat.x86_64 0:1.7.3.2-2.el7 will be installed
25 --> Running transaction check
26 ---> Package libnetfilter_cthelper.x86_64 0:1.0.0-9.el7 will be installed
27 ---> Package libnetfilter_cttimeout.x86_64 0:1.0.0-6.el7 will be installed
28 ---> Package libnetfilter_queue.x86_64 0:1.0.2-2.el7_2 will be installed
29 --> Finished Dependency Resolution
30 
31 Dependencies Resolved
32 
33 ========================================================================================================================================================================
34  Package                                        Arch                           Version                                             Repository                      Size
35 ========================================================================================================================================================================
36 Installing:
37  kubernetes                                     x86_64                         1.5.2-0.7.git269f928.el7                            extras                          36 k
38 Installing for dependencies:
39  conntrack-tools                                x86_64                         1.4.4-3.el7_3                                       base                           186 k
40  kubernetes-client                              x86_64                         1.5.2-0.7.git269f928.el7                            extras                          14 M
41  kubernetes-master                              x86_64                         1.5.2-0.7.git269f928.el7                            extras                          25 M
42  kubernetes-node                                x86_64                         1.5.2-0.7.git269f928.el7                            extras                          14 M
43  libnetfilter_cthelper                          x86_64                         1.0.0-9.el7                                         base                            18 k
44  libnetfilter_cttimeout                         x86_64                         1.0.0-6.el7                                         base                            18 k
45  libnetfilter_queue                             x86_64                         1.0.2-2.el7_2                                       base                            23 k
46  socat                                          x86_64                         1.7.3.2-2.el7                                       base                           290 k
47 
48 Transaction Summary
49 ========================================================================================================================================================================
50 Install  1 Package (+8 Dependent packages)
51 
52 Total download size: 54 M
53 Installed size: 304 M
54 Downloading packages:
55 (1/9): kubernetes-1.5.2-0.7.git269f928.el7.x86_64.rpm                                                                                            |  36 kB  00:00:01     
56 (2/9): conntrack-tools-1.4.4-3.el7_3.x86_64.rpm                                                                                                  | 186 kB  00:00:01     
57 (3/9): kubernetes-client-1.5.2-0.7.git269f928.el7.x86_64.rpm                                                                                     |  14 MB  00:00:02     
58 (4/9): libnetfilter_cttimeout-1.0.0-6.el7.x86_64.rpm                                                                                             |  18 kB  00:00:00     
59 (5/9): libnetfilter_cthelper-1.0.0-9.el7.x86_64.rpm                                                                                              |  18 kB  00:00:00     
60 (6/9): libnetfilter_queue-1.0.2-2.el7_2.x86_64.rpm                                                                                               |  23 kB  00:00:00     
61 (7/9): kubernetes-node-1.5.2-0.7.git269f928.el7.x86_64.rpm                                                                                       |  14 MB  00:00:00     
62 (8/9): socat-1.7.3.2-2.el7.x86_64.rpm                                                                                                            | 290 kB  00:00:00     
63 (9/9): kubernetes-master-1.5.2-0.7.git269f928.el7.x86_64.rpm                                                                                     |  25 MB  00:00:01     
64 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------
65 Total                                                                                                                                    17 MB/s |  54 MB  00:00:03     
66 Running transaction check
67 Running transaction test
68 Transaction test succeeded
69 Running transaction
70   Installing : kubernetes-client-1.5.2-0.7.git269f928.el7.x86_64                                                                                                    1/9 
71   Installing : kubernetes-master-1.5.2-0.7.git269f928.el7.x86_64                                                                                                    2/9 
72   Installing : socat-1.7.3.2-2.el7.x86_64                                                                                                                           3/9 
73   Installing : libnetfilter_cthelper-1.0.0-9.el7.x86_64                                                                                                             4/9 
74   Installing : libnetfilter_queue-1.0.2-2.el7_2.x86_64                                                                                                              5/9 
75   Installing : libnetfilter_cttimeout-1.0.0-6.el7.x86_64                                                                                                            6/9 
76   Installing : conntrack-tools-1.4.4-3.el7_3.x86_64                                                                                                                 7/9 
77   Installing : kubernetes-node-1.5.2-0.7.git269f928.el7.x86_64                                                                                                      8/9 
78   Installing : kubernetes-1.5.2-0.7.git269f928.el7.x86_64                                                                                                           9/9 
79   Verifying  : libnetfilter_cttimeout-1.0.0-6.el7.x86_64                                                                                                            1/9 
80   Verifying  : libnetfilter_queue-1.0.2-2.el7_2.x86_64                                                                                                              2/9 
81   Verifying  : kubernetes-node-1.5.2-0.7.git269f928.el7.x86_64                                                                                                      3/9 
82   Verifying  : kubernetes-client-1.5.2-0.7.git269f928.el7.x86_64                                                                                                    4/9 
83   Verifying  : kubernetes-master-1.5.2-0.7.git269f928.el7.x86_64                                                                                                    5/9 
84   Verifying  : libnetfilter_cthelper-1.0.0-9.el7.x86_64                                                                                                             6/9 
85   Verifying  : conntrack-tools-1.4.4-3.el7_3.x86_64                                                                                                                 7/9 
86   Verifying  : socat-1.7.3.2-2.el7.x86_64                                                                                                                           8/9 
87   Verifying  : kubernetes-1.5.2-0.7.git269f928.el7.x86_64                                                                                                           9/9 
88 
89 Installed:
90   kubernetes.x86_64 0:1.5.2-0.7.git269f928.el7                                                                                                                          
91 
92 Dependency Installed:
93   conntrack-tools.x86_64 0:1.4.4-3.el7_3                kubernetes-client.x86_64 0:1.5.2-0.7.git269f928.el7     kubernetes-master.x86_64 0:1.5.2-0.7.git269f928.el7    
94   kubernetes-node.x86_64 0:1.5.2-0.7.git269f928.el7     libnetfilter_cthelper.x86_64 0:1.0.0-9.el7              libnetfilter_cttimeout.x86_64 0:1.0.0-6.el7            
95   libnetfilter_queue.x86_64 0:1.0.2-2.el7_2             socat.x86_64 0:1.7.3.2-2.el7                           
96 
97 Complete!
View Code

安裝完成可以分別檢視如下目錄檔案:

 step3、配置Master節點,Master節點需要配置config、apiserver、controller-manager、schduler

1、config配置:vi /etc/kubernetes/config  主要配置其中的引數KUBE_MASTER

 

2、apiserver配置:vi /etc/kubernetes/apiserver  

引數說明:

KUBE_API_ADDRESS:ApiServer監聽IP地址,配置為0.0.0.0,監聽所有

KUBE_API_PORT:ApIServer監聽埠

KUBE_ETCD_SERVERS:ETCD儲存伺服器IP+PORT

 

3、controller-manager配置:保持預設即可

4、schduler配置:保持預設即可

 

step4、啟動Master節點

啟動apiserver:

systemctl enable kube-apiserver.service
systemctl start kube-apiserver.service

啟動controller-manager:
systemctl enable kube-controller-manager.service
systemctl start kube-controller-manager.service

啟動scheduler:

systemctl enable kube-scheduler.service

systemctl start kube-scheduler.service

至此,Master節點就安裝完成了,下面繼續Node節點的安裝。

 

step5、安裝Node節點

yum install kubernetes -y    安裝和Master的步驟相同

step6、配置Node節點、Node節點需要配置config、kubelet、proxy

1、配置config:vi /etc/kubernetes/config 

2、配置kubelet:vi /etc/kubernetes/kublet

 

3、配置proxy:保持預設即可

step7、啟動Node節點

啟動kubelet:

systemctl enable kubelet.service

systemctl startkubelet.service

啟動proxy:

systemctl enable kube-proxy.service

systemctl startkube-proxy.service

step8、測試驗證

 到Master節點執行:kubectl get nodes  如下,node節點成功加入到Master。

 

一個簡單的Kubernetes叢集就搭建起來了。