Getting Kubernetes to practice (a) Introduction and Kubernetes


A, Kubernetes Introduction

    Kubernetes Google in 2014, is an open source container layout tool, using the language go, Google is already running nearly a decade of internal container layout tool (Borg system) by the derived. Because docker, turned out to cause Google was prepared to container technology stillborn as a secret weapon. Plan was disrupted, container-level opportunity has been lost, a step slow, only to start the level layout tool, Google act decisively, to prepare a Kubernetes Brog logic-based systems, open source and donated CNCF (the open-source community of native cloud computing Fund will), because the experience of using Google for nearly a decade, so Kubernetes born to sweep the other layout tools, to date, position remains solid.

    Kubernetes comes from the Greek, meaning "helmsman" or "pilot" and the English governor and cybernetic roots, referred K8s, is formed instead of a 88-character "ubernete" acronym. Google uses this name meaning is this: Since you docker to position itself as laden containers on ocean travel whale, then I will take the helm of the right to speak Kubernetes Age of Discovery, the whale must be set in accordance with my parade route.

    Kubernetes is a major highlight of automation, in Kubernetes solutions, a self-service can be extended, self-diagnostics, and easy to upgrade, expansion of the service after receiving the request, Kubernetes triggers the scheduling process, culminating in the selected destination node the corresponding number of service instances start a copy, these instances will be automatically added after a successful start load balancer and take effect, Kubernetes will regularly inspect the availability of all instances of each service, to ensure consistency with the expected number of service instances, when when it finds an instance is unavailable, or rebuild will automatically restart on other nodes of this example, the entire process without additional manual operation.




Two, Kubernetes Core Features

  • 自愈Not before restarting the failed container, when the current node is not available, will replace and re-scheduling of the container on the other nodes, while the container will undergo a medical examination, the container does not respond will be suspended, and the container is not ready to serve: the client broadcasts

  • 弹性伸缩Cpu load value by the number of monitoring a container, the container elastically stretchable achieved, if the average is higher than the set value, the number of containers is increased, if the average is less than the set value, reducing the container:

  • 滚动升级和一键回滚: Kubernetes have applications or configuration changes made gradually deploy, and monitor the health of the application to ensure that it does not terminate all instances simultaneously. If a problem arises, you can use the one-click rollback feature Kubernetes, restored to its original operating state

  • 服务的自动发现和负载均衡: No need to modify your application, Kubernetes will go to find services based on identity. At the same time, provides a single DNS name Kubernetes own IP address and a set of containers a container, and load balancing can be performed between them



Three, Kubernetes node assembly Introduction

    Kubernetes is a classic-to-many model, there is a major management node Master node and a lot of work Node. Of course, a plurality Kubernetes management node may be arranged with two or more managed nodes is called Master availability. Then we are on the Master component on the management node and worker nodes Node introduced.

    Master components can run on any node in the cluster. But for simplicity, usually start the Master of all components on a virtual machine, and the user does not run on this virtual machine container. At the same time, the cluster of all control commands are passed to the Master component runs on the Master management node, so kubectl command on the other node Node is unable to perform the work, the Master components are as follows:

  • api server: In charge of foreign Restful the Kubernetes API provides services to other components by calling the Master Restful api server provides an interface to achieve their respective functions, such as controller manager is through api server to monitor real-time status of each resource

  • etcd: It is a key database Kubernetes provide high availability for all network resources saved cluster configuration and status information of the object, that is, to save the state information of the entire cluster. Change data are carried out by api server. Kubernetes whole system a total of two services will need etcd for collaboration and storage configurations, namely:

      1. Network plug flannel, also need to use other networks plug storage network configuration information etcd

      2. kubernetes itself, including the status of various resources and objects meta-information configuration

  • scheduler: 监听新建pod副本信息,并通过调度算法为该pod选择一个最合适的Node节点。会检索到所有符合该pod要求的Node节点,执行pod调度逻辑。调度成功之后,会将pod信息绑定到目标节点上,同时将信息写入到etcd中。一旦绑定,就由Node节点上的kubelet接手pod的接下来的生命周期管理。如果把scheduler看成一个黑匣子,那么它的输入是pod和由多个Node节点组成的列表,输出是pod和一个Node节点的绑定,即将这个pod部署到这个Node节点上。Kubernetes目前提供了调度算法,但是同样也保留了接口,用户可以根据自己的需求定义自己的调度算法

  • controller manager: 负责维护集群的状态,比如故障检测、自动扩展、滚动更新等。每个资源一般都对应有一个控制器,这些controller通过api server实时监控各个资源的状态,controller manager就是负责管理这些控制器的。当有资源因为故障导致状态变化,controller就会尝试将系统由“现有状态”恢复到“期待状态”,保证其下每一个controller所对应的资源始终处于期望状态。比如我们通过api server创建一个pod,当这个pod创建成功后,api server的任务就算完成了。而后面保证pod的状态始终和我们预期的一样的重任就由controller manager去保证了

    Node工作节点运行着Master管理节点分配的pod,当一个Node工作节点宕机时,其上的pod会被自动转移到其他Node工作节点上,每一个Node工作节点都安装了Node组件,其Node组件如下所示:

  • kubelet: 会监视已分配给Node节点的pod,负责pod的创建、启动、监控、重启和销毁等工作,同时与Master节点密切协作,维护和管理该Node节点上面的所有容器,实现集群管理的基本功能。kubelet会把自己注册到Master节点上,当Node节点纳入集群管理的范围后,kubelet进程就会定时向Master节点上报自身的资源情况。Master节点就掌握着每个Node节点的资源情况,就可以实施高效的资源调度策略。当某个Node节点超过指定时间未上报信息时,Master节点会将该Node节点标记为Not Ready,同时会进行工作负载转移。即Node工作节点通过kubelet与master组件交互,可以理解为kubelet是Master节点在每个Node工作节点上面的代理人(agent)。本质上,它负责使pod的运行状态与期望的状态保持一致

  • kube-proxy: 是实现Kubernetes Service的通信以及负载均衡的重要组件,将到service的请求转发到后端的pod上

  • Container runtime: 容器运行环境,负责节点容器的创建与管理工作,目前Kubernetes支持docker和rkt两种容器

公共组件:

  • flannel: flannel是一个专门为kubernetes定制的网络解决方案,主要解决pod跨主机通信问题


四、Kubernetes集群的安装与配置

1、Kubernetes集群的五种安装方式对比

安装方式 介绍
源码编译安装 大神级别安装,需要安装golang编译环境,同时对golang版本还有要求,可以安装最新版本
二进制安装 二进制安装又分为 全称手动安装 和 自动安装,自动安装分为 shell、ansible 和 saltstack版,可以安装最新版本,网上安装文档比较多
kubeadm安装 全自动化官方安装工具,安装比较简单,但是对网络有要求,可以安装最新版本
minkube安装 并不适合在生产环境下使用,适合开发者学习使用,可以安装最新版本
yum安装 安装简单,唯一不足的是Kubernetes的版本固定为1.5.2,不能安装最新版本

2、安装前的环境准备

1、需要准备三台Linux虚拟机,其配置如下所示:

主机名称 节点名称 IP地址 防火墙(firewalld) SELinux 系统及内核
k8s-master master 192.168.198.145 关闭 关闭 CentOS7.x,内核版本至少为3.10及以上
k8s-node1 node1 192.168.198.146 关闭 关闭 CentOS7.x,内核版本至少为3.10及以上
k8s-node2 node2 192.168.198.147 关闭 关闭 CentOS7.x,内核版本至少为3.10及以上

2、关闭三个节点(Linux虚拟机) 的firewalld防火墙和SELinux

firewalld会影响Docker的网络功能,SELinux是2.6+版本的Linux内核中提供的强制访问控制系统,在很大程度上加强了Linux的安全性,但是它会影响Kubernetes的某些组件功能,所以我们需要在安装部署前将其禁用掉

//1、关闭Firewalld防火墙
systemctl stop firewalld.service  // 停止firewalld服务
systemctl disable firewalld.service  // 禁止firewalld开机启动

//2、关闭SELinux
vim /etc/sysconfig/selinux       //编辑selinux文件
SELINUX=disabled         //把文件中的SELINUX=enforcing 改成 SELINUX=disabled 即可

//3、重启linux
reboot

3、修改三个节点(Linux虚拟机) 的主机名称

//1、查看当前主机的主机名称
hostname

//2、修改当前主机的主机名称
hostnamectl set-hostname 主机名称

这里以修改master节点的主机名为例 (node1 和 node2节点与修改master节点的主机名类似,只不过需要把k8s-master 替换为k8s-node1 和 k8s-node2即可)


4、配置三个节点(Linux虚拟机) 的ip地址 与 主机名的映射关系

//1、编辑hosts文件
vim /etc/hosts

//2、配置映射关系
192.168.198.145 k8s-master
192.168.198.146 k8s-node1
192.168.198.147 k8s-node2

这里以配置master节点的ip地址 与 主机名的映射关系为例 (node1 和 node2节点与配置master节点的ip地址 与 主机名的映射关系相同)


5、同步三个节点(Linux虚拟机) 的系统时间 (多个节点之间强烈建议同步时间)

//1、安装ntpdate
yum install -y ntpdate

//2、同步系统时间,ntp1.aliyun.com为阿里的时间(NTP)服务器
ntpdate ntp1.aliyun.com

//3、创建定时任务来自动同步阿里的时间(NTP)服务器
crontab -e

//4、设置每十分钟同步一次时间
0 */10 * * * ? /usr/sbin/ntpdate ntp1.aliyun.com



设置每十分钟同步一次时间
在这里插入图片描述

6、更新三个节点(Linux虚拟机) 的yum库

yum -y upgrade //只升级所有包,不升级软件和系统内核

注意:请不要使用yum -y update命令进行更新,该方式不仅会升级所有包,也会升级软件和系统内核,容易导致系统奔溃,同时在生产环境对软件版本和内核版本要求非常精确,所以该方式升级请慎重考虑




3、yum方式安装 Kubernetes集群

3.1、节点安装的相关组件

节点名称 相关组件
master etcd、api-server、scheduler、controller-manager、flannel 和 harbor(harbor安装依赖docker 和 docker-compose)
node1 docker、kubelet、proxy 和 flannel
node2 docker、kubelet、proxy 和 flannel

3.2、Master节点的安装

3.2.1、harbor镜像仓库的安装与配置

由于kubernetes默认使用的docker镜像下载地址为国外地址,导致docker镜像下载速度太慢 或者 根本下载不下来,所以这里我们在master节点上自行搭建一个harbor镜像仓库

harbor镜像仓库的搭建:https://blog.csdn.net/qq_39135287/article/details/104041772


3.2.1、etcd组件的安装与配置

1、安装etcd组件

yum install -y etcd



2、编辑etcd.conf配置文件

//1、编辑etcd.conf配置文件
vim /etc/etcd/etcd.conf

//2、修改etcd对外提供服务的的ip地址
ETCD_LISTEN_CLIENT_URLS="http://localhost:2379"
修改为
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379"

//3、修改etcd对外公告该节点客户端的监听地址
ETCD_ADVERTISE_CLIENT_URLS="http://localhost:2379"
修改为
ETCD_ADVERTISE_CLIENT_URLS="http://master节点的ip地址:2379"



3、启动etcd服务,并加入到开机启动

//1、启动etcd服务
systemctl start etcd

//2、设置etcd服务开机启动
systemctl enable etcd



3.2.2、kubernetes-master组件的安装与配置

1、安装kubernetes-master组件 (其组件包含apiserver、config、controller-manager 和 scheduler)

yum install -y kubernetes-master

可以看到在安装kubernetes-master组件的同时,会将依赖的kubernetes-client组件一同安装


2、编辑apiserver配置文件

//1、编辑apiserver配置文件
vim /etc/kubernetes/apiserver

//2、修改api server的监听地址
KUBE_API_ADDRESS="--insecure-bind-address=127.0.0.1"
修改为
KUBE_API_ADDRESS="--insecure-bind-address=0.0.0.0"

//3、去掉前面的注释
KUBE_API_PORT="--port=8080"  //api server监听的端口
KUBELET_PORT="--kubelet-port=10250"  //kubelet监听的服务端口

//4、修改etcd的服务地址,如果有多个,请以逗号隔开
KUBE_ETCD_SERVERS="--etcd-servers=http://127.0.0.1:2379"
修改为
KUBE_ETCD_SERVERS="--etcd-servers=http://master节点的ip地址:2379"

//5、去掉认证参数ServiceAccount
KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ServiceAccount,ResourceQuota"
修改为
KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ResourceQuota"




3、编辑config配置文件

//1、编辑config配置文件
vim /etc/kubernetes/config

//2、修改kubernetes master统一的管理入口ip地址
KUBE_MASTER="--master=http://127.0.0.1:8080"
修改为
KUBE_MASTER="--master=http://master节点的ip地址:8080"



4、启动apiserver、controller-manager 和 scheduler服务,并加入到开机启动

//1、启动apiserver服务,并设置开机启动
systemctl start kube-apiserver
systemctl enable kube-apiserver

//2、启动controller-manager服务,并设置开机启动
systemctl start kube-controller-manager
systemctl enable kube-controller-manager

//3、启动scheduler服务,并设置开机启动
systemctl start kube-scheduler
systemctl enable kube-scheduler



3.2.3、flannel组件的安装与配置

1、安装flannel组件

yum install -y flannel



2、编辑flanneld配置文件

//1、编辑flanneld配置文件
vim /etc/sysconfig/flanneld

//2、修改flanneld服务读取etcd数据库的ip地址
FLANNEL_ETCD_ENDPOINTS="http://127.0.0.1:2379"
修改为
FLANNEL_ETCD_ENDPOINTS="http://master节点的ip地址:2379"




3、分配docker网段

在etcd中添加一个名称为 /automic.io/network/config 的主键,通过该主键设置提供给docker容器使用的网段

etcdctl mk /atomic.io/network/config '{"Network":"172.17.0.0/16"}'



4、启动flanneld服务,并加入到开机启动

//1、启动flanneld服务
systemctl start flanneld

//2、设置开机启动
systemctl enable flanneld



3.2.4、查看组件的健康状态
kubectl get componentstatus

//查看flanneld启动服务的进程
ps -ef|grep flanneld





3.3、Node节点的安装

3.3.1、kubernetes-node组件的安装与配置

1、安装kubernetes-node组件 (其组件包含config、kubelet 和 proxy )

yum install -y kubernetes-node

提示:如果在使用yum安装kubernetes-node组件之前,没有安装过docker,将不会出现如下安装失败现象

可以看到最后四行显示如下所示:

Error: docker-ce-cli conflicts with 2:docker-1.13.1-103.git7f2769b.el7.centos.x86_64
Error: docker-ce conflicts with 2:docker-1.13.1-103.git7f2769b.el7.centos.x86_64
 You could try using --skip-broken to work around the problem
 You could try running: rpm -Va --nofiles --nodigest

提示我们kubernetes-node组件安装失败,原因:在我们使用yum安装kubernetes-node组件时会自动安装docker,而在安装之前虚拟机上已经安装过docker了,所以导致安装kubernetes-node组件失败,按照提示执行rpm -Va --nofiles --nodigest命令,还是解决不了问题,接下来就来记录一下解决的方法:

(1) 查询已经安装过的docker安装包

yum list installed | grep docker



(2) 删除已经安装过的docker安装包

//1、删除docker-ce-cli.x86_64 (同时还会删除docker-ce.x86_64)
yum remove -y docker-ce-cli.x86_64

//2、删除containerd.io.x86_64
yum remove -y containerd.io.x86_64




(3) 删除已经安装过的docker安装包

//1、删除镜像
rm -rf /var/lib/docker

//2、删除容器
rm -rf /var/run/docker

//3、删除daemon.json配置文件
rm -rf /etc/docker/daemon.json



(4) 再次安装kubernetes-node组件

yum install -y kubernetes-node

可以看到在安装kubernetes-node组件的同时,会将依赖的dcoker和其它组件一同安装



2、编辑kubelet配置文件

//1、编辑kubelet配置文件
vim /etc/kubernetes/kubelet

//2、修改kubelet的监听地址
KUBELET_ADDRESS="--address=127.0.0.1"
修改为
KUBELET_ADDRESS="--address=0.0.0.0"

//3、去掉前面的注释
KUBELET_PORT="--port=10250"  //kubelet监听的服务端口

//4、修改主机名
KUBELET_HOSTNAME="--hostname-override=127.0.0.1"
修改为
KUBELET_HOSTNAME="--hostname-override=node节点的ip地址"

//5、指定api server的ip地址
KUBELET_API_SERVER="--api-servers=http://127.0.0.1:8080"
修改为
KUBELET_API_SERVER="--api-servers=http://master节点的ip地址:8080"




3、编辑config配置文件

//1、编辑config配置文件
vim /etc/kubernetes/config

//2、修改kubernetes master统一的管理入口ip地址
KUBE_MASTER="--master=http://127.0.0.1:8080"
修改为
KUBE_MASTER="--master=http://master节点的ip地址:8080"



4、启动kubelet 和 kube-proxy服务,并加入到开机启动

//1、启动kubelet服务,并设置开机启动
systemctl start kubelet
systemctl enable kubelet

//2、启动kube-proxy服务,并设置开机启动
systemctl start kube-proxy
systemctl enable kube-proxy



3.3.2、flannel组件的安装与配置

1、安装flannel组件

yum install -y flannel



2、编辑flanneld配置文件

//1、编辑flanneld配置文件
vim /etc/sysconfig/flanneld

//2、修改flanneld服务读取etcd数据库的ip地址
FLANNEL_ETCD_ENDPOINTS="http://127.0.0.1:2379"
修改为
FLANNEL_ETCD_ENDPOINTS="http://master节点的ip地址:2379"




4、启动flanneld服务,并加入到开机启动

//1、启动flanneld服务
systemctl start flanneld

//2、设置开机启动
systemctl enable flanneld



3.3.3、配置阿里云镜像加速器 和 添加HarBor仓库地址
//1、进入到docker目录下
cd /etc/docker/

//2、编辑daemon.json配置文件
vim daemon.json

//3、配置阿里云镜像加速器 和 添加HarBor仓库地址
{
  "registry-mirrors": ["你的阿里云镜像加速器地址"],
  "insecure-registries": ["harbor.yml配置文件中设置的hostname:http端口"]
}

获取阿里云镜像加速地址注意:"registry-mirrors"一行末尾有逗号,


3.3.4、重新加载daemon.json配置文件 并 重启docker服务
//重新加载daemon.json配置文件
systemctl daemon-reload

//重启服务器
systemctl restart docker



3.4、在Master节点上查看Node节点的状态
kubectl get nodes




                   如果有遇到不懂或者有问题时,可以扫描下方二维码,欢迎进群交流与分享,希望能够跟大家交流学习!

发布了76 篇原创文章 · 获赞 253 · 访问量 43万+

Guess you like

Origin blog.csdn.net/qq_39135287/article/details/104068628