Kubernetes架构图

kubeadm部署k8s 1.24高可用集群 基于Kubeadm工具从零开始快速部署单节点K8S集群(1)

基础概念

1.Cluster 集群  计算、存储和网络资源的集合,Kubernetes利用这些资源运行各种基于容器的应用。

2.Master  Master是Cluster的大脑,负责调度(决定将应用放在哪里运行),一般为了实现高可用,会有多个Master。

3.Node  负责运行具体的容器,Node由Master管理,它会监控并汇报容器的状态,同时根据Master的要求管理容器的生命周期。

4.Pod  Kubernetes的最小工作单元,每个Pod包含一个或多个容器。Pod中的容器会被作为一个整体被Master调度到一个Node上运行。

  (1)为何引入Pod?

  一是方便管理:

   有些容器天生联系紧密,需要在一起工作。Pod提供了比容器更高层次的抽象,将它们封装到一个部署单元中。K8S以Pod为最小单位进行调度、扩展、共享资源、管理生命周期。

  eg.正例:File Puller & Web Server => 需要部署在一起工作 反例:Tomecat & MySQL => 不需要部署在一起工作

  二是可以共享资源和通信:

   Pod中所有容器使用同一个网络namespace,即相同的IP和端口空间,可以直接用localhost通信,而且还可以共享存储(本质是通过将Volume挂载到Pod中的每个容器)

  (2)如何使用Pod?

   运行单个容器:one-container-per-Pod,K8S中最常见的模型,即使这种情形下,K8S管理的也是Pod而不是单个容器。

   运行多个容器:将联系非常紧密的多个容器部署到一个Pod中,可以直接共享资源。

5.Controller  K8S不会直接创建Pod,是通过Controller来管理Pod的。为了满足不同业务场景,K8S提供了多种Controller:

  (1)Deployment

   最常见的Controller,可以管理Pod的多个副本,并确保Pod按照期望的状态运行。

  (2)ReplicaSet

   实现了Pod的多副本管理,使用Deployment时会自动创建ReplicaSet。换句话说,Deployment是通过ReplicaSet来管理Pod的多个副本的,通常不需要直接使用ReplicaSet。

  (3)DaemonSet

   用于每个Node最多只运行一个Pod副本的场景,DaemonSet通常用于运行daemon(守护进程、后台程序)。

  (4)StatefuleSet

   用于保证Pod的每个副本在整个生命周期中名称是不变的,而其他的Controller不提供这个功能。(非StatefuleSet下,当某个Pod发生故障需要删除并重启时,Pod的名称是会变化的)

  (5)Job

   用于运行结束就删除的应用,其他Controller下Pod通常是长期持续运行的。

6.Service  K8S定义了外界访问一个或一组特定Pod的方式,就是Service。每个Service有自己的IP和端口,并且为Pod提供了负载均衡。

   如果说K8S运行Pod的任务是交给了Controller去做,那么访问Pod的任务则是交给了Service去做。

7.Namespace  Namespace将一个物理的Cluster从逻辑上划分为多个虚拟Cluster,每个虚拟Cluster就是一个Namespace,不同Namespace中的资源是完全隔离的。

K8S中会自动创建两个Namespace:

  (1)default:创建资源时如果不指定Namespace就会放到这里 (2)kube-system: K8S自己创建的系统资源都会放到这个Namespace中

部署Kubernetes的3种方式

  (1)Minikube

   Minikube是一个工具,可以在本地快速运行一个单点的K8S,供初步尝试K8S或日常开发的用户使用,不能用于生产环境。

  (2)Kubeadm

   Kubeadm是K8S官方社区推出的一套用于简化快速部署K8S集群的工具,Kubeadm的设计目的是为新用户开始尝试K8S提供一种简单的方法。

  (3)二进制包

   除了以上两种方式外,我们还可以通过从官方下载二进制包,手动部署每个组件组成K8S集群,这也是目前企业生产环境中广为使用的方式,但对K8S管理人员的要求较高。

   本次主要借助Kubeadm工具搭建K8S集群,以便后续实践部署ASP.NET Core应用集群。  

环境介绍:

Master test2 192.168.2.195

Node node1 192.168.2.135

CentOS 7.5

2核 CPU,2GB 内存

环境准备:

 Master、Node两台主机都需要操作。

1.更改/etc/hosts文件添加主机名与IP映射关系

[root@test2~]#vim/etc/hosts 127.0.0.1localhostlocalhost.localdomainlocalhost4localhost4.localdomain4 ::1localhostlocalhost.localdomainlocalhost6localhost6.localdomain6 192.168.2.135node1 192.168.2.129test2

验证:

[root@test2~]#ping-c2node1 PINGnode1(192.168.2.135)56(84)bytesofdata. 64bytesfromnode1(192.168.2.135):icmp_seq=1ttl=64time=0.644ms 64bytesfromnode1(192.168.2.135):icmp_seq=2ttl=64time=0.370ms ... [root@node1~]#ping-c2test2 PINGtest2(192.168.2.129)56(84)bytesofdata. 64bytesfromtest2(192.168.2.129):icmp_seq=1ttl=64time=0.492ms 64bytesfromtest2(192.168.2.129):icmp_seq=2ttl=64time=9.45ms ...

2.清空Iptables规则并永久关闭防火墙和Selinux

[root@test2~]#iptables-F [root@test2~]#systemctlstopfirewalld [root@test2~]#systemctldisablefirewalld [root@test2~]#sed-i's/enforcing/disabled/'/etc/selinux/config [root@test2~]#setenforce0 [root@test2~]#getenforce Disabled

3.校正系统时间

 系统时间如果不一致,会导致node节点无法加入集群中。

[root@test2~]#yum-yinstallntp 作为依赖被安装: autogen-libopts.x86_640:5.18-5.el7ntpdate.x86_640:4.2.6p5-29.el7.centos.2 完毕! [root@test2~]#ntpdatecn.pool.ntp.org 17Jul14:46:45ntpdate[61183]:adjusttimeserver84.16.73.33offset-0.030998sec

4.关闭swap分区

 编辑etc/fstab将swap那一行注释掉,因为K8S中不支持swap分区。

[root@test2~]#swapoff-a//临时关闭 [root@test2~]#vim/etc/fstab//永久关闭 ... #/dev/mapper/centos-swapswapswapdefaults00

5.将桥接的IPv4流量传递到iptables的链

[root@test2~]#cat>/etc/sysctl.d/k8s.conf<<EOF >net.bridge.bridge-nf-call-ip6tables=1 >net.bridge.bridge-nf-call-iptables=1 >EOF [root@test2~]#sysctl--system *Applying/usr/lib/sysctl.d/00-system.conf... net.bridge.bridge-nf-call-ip6tables=0 net.bridge.bridge-nf-call-iptables=0 net.bridge.bridge-nf-call-arptables=0 *Applying/usr/lib/sysctl.d/10-default-yama-scope.conf... kernel.yama.ptrace_scope=0 *Applying/usr/lib/sysctl.d/50-default.conf... kernel.sysrq=16 kernel.core_uses_pid=1 net.ipv4.conf.default.rp_filter=1 net.ipv4.conf.all.rp_filter=1 net.ipv4.conf.default.accept_source_route=0 net.ipv4.conf.all.accept_source_route=0 net.ipv4.conf.default.promote_secondaries=1 net.ipv4.conf.all.promote_secondaries=1 fs.protected_hardlinks=1 fs.protected_symlinks=1 *Applying/etc/sysctl.d/99-sysctl.conf... net.bridge.bridge-nf-call-ip6tables=1 net.bridge.bridge-nf-call-iptables=1 net.bridge.bridge-nf-call-arptables=1 net.ipv4.ip_forward=1 net.ipv4.conf.all.arp_ignore=1 net.ipv4.conf.lo.arp_ignore=1 net.ipv4.conf.lo.arp_announce=2 net.ipv4.conf.all.arp_announce=2 *Applying/etc/sysctl.d/k8s.conf... net.bridge.bridge-nf-call-ip6tables=1 net.bridge.bridge-nf-call-iptables=1 *Applying/etc/sysctl.conf... net.bridge.bridge-nf-call-ip6tables=1 net.bridge.bridge-nf-call-iptables=1 net.bridge.bridge-nf-call-arptables=1 net.ipv4.ip_forward=1 net.ipv4.conf.all.arp_ignore=1 net.ipv4.conf.lo.arp_ignore=1 net.ipv4.conf.lo.arp_announce=2 net.ipv4.conf.all.arp_announce=2

6.每个节点唯一主机名、Mac地址、Product_uuid

master主机

[root@test2~]#iplink 1:lo:<LOOPBACK,UP,LOWER_UP>mtu65536qdiscnoqueuestateUNKNOWNmodeDEFAULTgroupdefaultqlen1000 link/loopback00:00:00:00:00:00brd00:00:00:00:00:00 2:ens33:<BROADCAST,MULTICAST,UP,LOWER_UP>mtu1500qdiscpfifo_faststateUPmodeDEFAULTgroupdefaultqlen1000 link/ether00:0c:29:1a:8b:61brdff:ff:ff:ff:ff:ff 3:ens37:<BROADCAST,MULTICAST,UP,LOWER_UP>mtu1500qdiscpfifo_faststateUPmodeDEFAULTgroupdefaultqlen1000 link/ether00:0c:29:1a:8b:6bbrdff:ff:ff:ff:ff:ff 4:docker0:<NO-CARRIER,BROADCAST,MULTICAST,UP>mtu1500qdiscnoqueuestateDOWNmodeDEFAULTgroupdefault link/ether02:42:49:66:18:1bbrdff:ff:ff:ff:ff:ff [root@test2~]#cat/sys/class/dmi/id/product_uuid C2344D56-4199-0E76-6398-22536B1A8B61

node1主机

[root@node1~]#iplink 1:lo:<LOOPBACK,UP,LOWER_UP>mtu65536qdiscnoqueuestateUNKNOWNmodeDEFAULTgroupdefaultqlen1000 link/loopback00:00:00:00:00:00brd00:00:00:00:00:00 2:ens33:<BROADCAST,MULTICAST,UP,LOWER_UP>mtu1500qdiscpfifo_faststateUPmodeDEFAULTgroupdefaultqlen1000 link/ether00:0c:29:48:a1:5bbrdff:ff:ff:ff:ff:ff 3:docker0:<NO-CARRIER,BROADCAST,MULTICAST,UP>mtu1500qdiscnoqueuestateDOWNmodeDEFAULTgroupdefault link/ether02:42:ef:0e:58:c3brdff:ff:ff:ff:ff:ff [root@node1~]#cat/sys/class/dmi/id/product_uuid F4504D56-B069-0BCD-F156-DB54D848A15B

安装Docker

 Master、Node两台主机都需要操作。

Docker与Kubernetes的关系:

kubeadm部署k8s 1.24高可用集群 基于Kubeadm工具从零开始快速部署单节点K8S集群(2)

Docker安装部署详解

注意:

 目前kubeadm最大支持docker-ce-18.06,所以要指定该版本安装.

[root@test2~]#yuminstall-yyum-utilsdevice-mapper-persistent-datalvm2 [root@test2~]#yum-config-manager--add-repo\ https://download.docker.com/linux/centos/docker-ce.repo [root@test2~]#yum-yinstalldocker-ce-18.06.ce//目前kubeadm最大支持docker-ce-18.06,所以要指定该版本安装 [root@test2~]#systemctlenabledocker&&systemctlstartdocker

由于字数限制,无法一篇文章讲解完,所以下一篇文章接这一篇的内容继续......

,