Kubernetes入门-集群安装

时间:2021-01-08 21:32:23

  Kubernetes是谷歌开源的容器集群编排平台,是一个完备的分布式系统支撑平台,为容器化应用提供部署运行、资源调度、服务发现和动态伸缩等一系列完整功能,具有强大的故障发现和自我修复机制、服务滚动升级和在线扩容能力,可扩展资源自动调度机制以及多粒度的资源配额管理能力。
  Kubernetes目前以77%的市场份额独占容器编排平台市场鳌头。目前所有的主要云服务提供商(亚马逊、微软、Google、IBM等)都将其作为部署云原生应用的解决方案。Kubernetes已经在大量企业中落地,并广泛的运用在生产环境中。
作为容器化的主流运行和编排管理平台,Kubernetes的重要性毋庸置疑,以下相关内容只是本人的相关笔记,仅作参考,欢迎业内各路大神指点。

下面是K8S架构简图:

Kubernetes入门-集群安装

可以看出,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 安装即可

 [root@alta-sz kubernetes]# yum install etcd
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package etcd.x86_64 :3.2.-.el7 will be installed
--> Finished Dependency Resolution Dependencies Resolved ========================================================================================================================================================================
Package Arch Version Repository Size
========================================================================================================================================================================
Installing:
etcd x86_64 3.2.-.el7 extras 9.3 M Transaction Summary
========================================================================================================================================================================
Install Package Total download size: 9.3 M
Installed size: M
Is this ok [y/d/N]: y
Downloading packages:
etcd-3.2.-.el7.x86_64.rpm | 9.3 MB ::
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : etcd-3.2.-.el7.x86_64 /
Verifying : etcd-3.2.-.el7.x86_64 / Installed:
etcd.x86_64 :3.2.-.el7 Complete!

安装完进行配置:

vi /etc/etcd/etcd.conf

 [root@alta-sz etcd]# vi /etc/etcd/etcd.conf 

 #[Member]
#ETCD_CORS=""
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
#ETCD_WAL_DIR=""
#ETCD_LISTEN_PEER_URLS="http://localhost:2380"
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379,http://0.0.0.0:4001"
#ETCD_MAX_SNAPSHOTS=""
#ETCD_MAX_WALS=""
ETCD_NAME="master"
#ETCD_SNAPSHOT_COUNT=""
#ETCD_HEARTBEAT_INTERVAL=""
#ETCD_ELECTION_TIMEOUT=""
#ETCD_QUOTA_BACKEND_BYTES=""
#ETCD_MAX_REQUEST_BYTES=""
#ETCD_GRPC_KEEPALIVE_MIN_TIME="5s"
#ETCD_GRPC_KEEPALIVE_INTERVAL="2h0m0s"
#ETCD_GRPC_KEEPALIVE_TIMEOUT="20s" #
#[Clustering]
#ETCD_INITIAL_ADVERTISE_PEER_URLS="http://localhost:2380"
ETCD_ADVERTISE_CLIENT_URLS="http://120.27.15.116:2379,http://120.27.15.116:4001"
#ETCD_DISCOVERY=""
#ETCD_DISCOVERY_FALLBACK="proxy"
#ETCD_DISCOVERY_PROXY=""
#ETCD_DISCOVERY_SRV=""
#ETCD_INITIAL_CLUSTER="default=http://localhost:2380"
#ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
#ETCD_INITIAL_CLUSTER_STATE="new"
#ETCD_STRICT_RECONFIG_CHECK="true"
#ETCD_ENABLE_V2="true"
#
#[Proxy]
#ETCD_PROXY="off"
#ETCD_PROXY_FAILURE_WAIT=""
#ETCD_PROXY_REFRESH_INTERVAL=""
#ETCD_PROXY_DIAL_TIMEOUT=""
#ETCD_PROXY_WRITE_TIMEOUT=""
#ETCD_PROXY_READ_TIMEOUT=""
#
#[Security]
#ETCD_CERT_FILE=""
#ETCD_KEY_FILE=""
#ETCD_CLIENT_CERT_AUTH="false"
#ETCD_TRUSTED_CA_FILE=""
#ETCD_AUTO_TLS="false"
#ETCD_PEER_CERT_FILE=""
#ETCD_PEER_KEY_FILE=""
#ETCD_PEER_CLIENT_CERT_AUTH="false"
#ETCD_PEER_TRUSTED_CA_FILE=""
#ETCD_PEER_AUTO_TLS="false"
#
#[Logging]
#ETCD_DEBUG="false"
#ETCD_LOG_PACKAGE_LEVELS=""
#ETCD_LOG_OUTPUT="default"
#
#[Unsafe]
#ETCD_FORCE_NEW_CLUSTER="false"
#
#[Version]
#ETCD_VERSION="false"
#ETCD_AUTO_COMPACTION_RETENTION=""
#
#[Profiling]
#ETCD_ENABLE_PPROF="false"
#ETCD_METRICS="basic"
#
#[Auth]
#ETCD_AUTH_TOKEN="simple"

启动etcd,然后测试:

systemctl enable etcd

systemctl start etcd

分别存入一个值{test/key1:123}进行测试

etcdctl set test/key1 123

etcd get test/key1

 [root@alta-sz etcd]# etcdctl set test/key1 

 [root@alta-sz etcd]# etcdctl get test/key1

 [root@alta-sz etcd]#
[root@alta-sz etcd]#
[root@alta-sz etcd]#
[root@alta-sz etcd]# etcdctl -C http://localhost:2379 cluster-health
member 8e9e05c52164694d is healthy: got healthy result from http://localhost:2379
cluster is healthy

以上说明etcd安装成功了!

step2、Master节点安装Kubernetes

执行:yum install kubernetes -y

 [root@alta-sz k8s]# yum install kubernetes -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package kubernetes.x86_64 :1.5.-0.7.git269f928.el7 will be installed
--> Processing Dependency: kubernetes-node = 1.5.-0.7.git269f928.el7 for package: kubernetes-1.5.-0.7.git269f928.el7.x86_64
--> Processing Dependency: kubernetes-master = 1.5.-0.7.git269f928.el7 for package: kubernetes-1.5.-0.7.git269f928.el7.x86_64
--> Running transaction check
---> Package kubernetes-master.x86_64 :1.5.-0.7.git269f928.el7 will be installed
--> Processing Dependency: kubernetes-client = 1.5.-0.7.git269f928.el7 for package: kubernetes-master-1.5.-0.7.git269f928.el7.x86_64
---> Package kubernetes-node.x86_64 :1.5.-0.7.git269f928.el7 will be installed
--> Processing Dependency: socat for package: kubernetes-node-1.5.-0.7.git269f928.el7.x86_64
--> Processing Dependency: conntrack-tools for package: kubernetes-node-1.5.-0.7.git269f928.el7.x86_64
--> Running transaction check
---> Package conntrack-tools.x86_64 :1.4.-.el7_3 will be installed
--> Processing Dependency: libnetfilter_cttimeout.so.(LIBNETFILTER_CTTIMEOUT_1.)(64bit) for package: conntrack-tools-1.4.-.el7_3.x86_64
--> Processing Dependency: libnetfilter_cttimeout.so.(LIBNETFILTER_CTTIMEOUT_1.)(64bit) for package: conntrack-tools-1.4.-.el7_3.x86_64
--> Processing Dependency: libnetfilter_cthelper.so.(LIBNETFILTER_CTHELPER_1.)(64bit) for package: conntrack-tools-1.4.-.el7_3.x86_64
--> Processing Dependency: libnetfilter_queue.so.()(64bit) for package: conntrack-tools-1.4.-.el7_3.x86_64
--> Processing Dependency: libnetfilter_cttimeout.so.()(64bit) for package: conntrack-tools-1.4.-.el7_3.x86_64
--> Processing Dependency: libnetfilter_cthelper.so.()(64bit) for package: conntrack-tools-1.4.-.el7_3.x86_64
---> Package kubernetes-client.x86_64 :1.5.-0.7.git269f928.el7 will be installed
---> Package socat.x86_64 :1.7.3.2-.el7 will be installed
--> Running transaction check
---> Package libnetfilter_cthelper.x86_64 :1.0.-.el7 will be installed
---> Package libnetfilter_cttimeout.x86_64 :1.0.-.el7 will be installed
---> Package libnetfilter_queue.x86_64 :1.0.-.el7_2 will be installed
--> Finished Dependency Resolution Dependencies Resolved ========================================================================================================================================================================
Package Arch Version Repository Size
========================================================================================================================================================================
Installing:
kubernetes x86_64 1.5.-0.7.git269f928.el7 extras k
Installing for dependencies:
conntrack-tools x86_64 1.4.-.el7_3 base k
kubernetes-client x86_64 1.5.-0.7.git269f928.el7 extras M
kubernetes-master x86_64 1.5.-0.7.git269f928.el7 extras M
kubernetes-node x86_64 1.5.-0.7.git269f928.el7 extras M
libnetfilter_cthelper x86_64 1.0.-.el7 base k
libnetfilter_cttimeout x86_64 1.0.-.el7 base k
libnetfilter_queue x86_64 1.0.-.el7_2 base k
socat x86_64 1.7.3.2-.el7 base k Transaction Summary
========================================================================================================================================================================
Install Package (+ Dependent packages) Total download size: M
Installed size: M
Downloading packages:
(/): kubernetes-1.5.-0.7.git269f928.el7.x86_64.rpm | kB ::
(/): conntrack-tools-1.4.-.el7_3.x86_64.rpm | kB ::
(/): kubernetes-client-1.5.-0.7.git269f928.el7.x86_64.rpm | MB ::
(/): libnetfilter_cttimeout-1.0.-.el7.x86_64.rpm | kB ::
(/): libnetfilter_cthelper-1.0.-.el7.x86_64.rpm | kB ::
(/): libnetfilter_queue-1.0.-.el7_2.x86_64.rpm | kB ::
(/): kubernetes-node-1.5.-0.7.git269f928.el7.x86_64.rpm | MB ::
(/): socat-1.7.3.2-.el7.x86_64.rpm | kB ::
(/): kubernetes-master-1.5.-0.7.git269f928.el7.x86_64.rpm | MB ::
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total MB/s | MB ::
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : kubernetes-client-1.5.-0.7.git269f928.el7.x86_64 /
Installing : kubernetes-master-1.5.-0.7.git269f928.el7.x86_64 /
Installing : socat-1.7.3.2-.el7.x86_64 /
Installing : libnetfilter_cthelper-1.0.-.el7.x86_64 /
Installing : libnetfilter_queue-1.0.-.el7_2.x86_64 /
Installing : libnetfilter_cttimeout-1.0.-.el7.x86_64 /
Installing : conntrack-tools-1.4.-.el7_3.x86_64 /
Installing : kubernetes-node-1.5.-0.7.git269f928.el7.x86_64 /
Installing : kubernetes-1.5.-0.7.git269f928.el7.x86_64 /
Verifying : libnetfilter_cttimeout-1.0.-.el7.x86_64 /
Verifying : libnetfilter_queue-1.0.-.el7_2.x86_64 /
Verifying : kubernetes-node-1.5.-0.7.git269f928.el7.x86_64 /
Verifying : kubernetes-client-1.5.-0.7.git269f928.el7.x86_64 /
Verifying : kubernetes-master-1.5.-0.7.git269f928.el7.x86_64 /
Verifying : libnetfilter_cthelper-1.0.-.el7.x86_64 /
Verifying : conntrack-tools-1.4.-.el7_3.x86_64 /
Verifying : socat-1.7.3.2-.el7.x86_64 /
Verifying : kubernetes-1.5.-0.7.git269f928.el7.x86_64 / Installed:
kubernetes.x86_64 :1.5.-0.7.git269f928.el7 Dependency Installed:
conntrack-tools.x86_64 :1.4.-.el7_3 kubernetes-client.x86_64 :1.5.-0.7.git269f928.el7 kubernetes-master.x86_64 :1.5.-0.7.git269f928.el7
kubernetes-node.x86_64 :1.5.-0.7.git269f928.el7 libnetfilter_cthelper.x86_64 :1.0.-.el7 libnetfilter_cttimeout.x86_64 :1.0.-.el7
libnetfilter_queue.x86_64 :1.0.-.el7_2 socat.x86_64 :1.7.3.2-.el7 Complete!

安装完成可以分别查看如下目录文件:

Kubernetes入门-集群安装

step3、配置Master节点,Master节点需要配置config、apiserver、controller-manager、schduler

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

Kubernetes入门-集群安装

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

Kubernetes入门-集群安装

3、controller-manager配置:保持默认即可

Kubernetes入门-集群安装

4、schduler配置:保持默认即可

Kubernetes入门-集群安装

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

Kubernetes入门-集群安装

至此,Master节点就安装完成了,下面继续Node节点的安装。

step5、安装Node节点

yum install kubernetes -y    安装和Master的步骤相同

step6、配置Node节点、Node节点需要配置config、kubelet、proxy

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

Kubernetes入门-集群安装

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

Kubernetes入门-集群安装

3、配置proxy:保持默认即可

Kubernetes入门-集群安装

step7、启动Node节点

启动kubelet:

systemctl enable kubelet.service

systemctl startkubelet.service

启动proxy:

systemctl enable kube-proxy.service

systemctl startkube-proxy.service

Kubernetes入门-集群安装

step8、测试验证

到Master节点执行:kubectl get nodes  如下,node节点成功加入到Master。

Kubernetes入门-集群安装

一个简单的Kubernetes集群就搭建起来了。