ASP.NET Core on K8S depth study (1) K8S basics and build clusters

In "on a small series of articles On the ASP.NET Core on K8S learning " by K8S built on the Windows environment through a single node Docker for Windows, and initial attempts to deploy ASP.NET Core WebAPI project to K8S, playing a bit rapid deployment and scalable instance. This series began, we will continue to learn and build K8S cluster on Linux to further play. Benpian will review the basic concepts and architecture K8S composition, will then build a cluster with a K8S for later learning to play it quickly by Kubeadm.

A, K8S basic concept review

  1.Cluster cluster

  Computing, storage and collection of network resources, these resources Kubernetes run a variety of container-based applications.

  2.Master

  Master Cluster brain is responsible for scheduling (decide where to run the application on), the general in order to achieve high availability, there will be multiple Master.

  3.Node

  Responsible for running specific container, Node managed by the Master, it will monitor and report on the state of the vessel, at the request of the vessel Master of Management life cycle.
  eg. in an interactive experiment, the Cluster is only one host, it is both Master also Node.

  4.Pod

  Kubernetes minimum operating units, each comprising one or more containers Pod. Pod in the container will be scheduled to run on Node as a whole is a Master.
   (1) why the introduction of Pod?
  One is easy to manage:
  Some containers born closely, need to work together. Pod provides a higher level of abstraction than the container, the encapsulating them into a deployment unit. K8S with Pod is the smallest unit of scheduling, expansion, resource sharing, management lifecycle.
  eg positive examples:. File Puller & Web Server => need to work together to deploy
    Anti Example: Tomecat & MySQL => need to deploy work together
  The second is to share resources and communications:
  Pod all using the same network namespace containers, i.e., the same IP and port space can be directly used localhost communication, but also a shared storage (essentially by Volume Pod mounted to each of the container)
   (2) How to use Pod?
  Run a single container: one-container-per-Pod, K8S the most common model, even in this case, K8S management is also Pod rather than a single container.
  Running multiple containers: a plurality of containers are very closely linked to the deployment of a Pod, you can share resources directly.

  5.Controller

  K8S Pod is not created directly by the Controller to manage Pod. In order to meet different business scenarios, K8S offers a variety Controller:
  (1)Deployment
  The most common Controller, you can manage multiple copies Pod and ensure Pod run in accordance with the desired state.
  (2)ReplicaSet
  Pod achieve a multi-copy management, automatically creates ReplicaSet use Deployment. In other words, the Deployment ReplicaSet managed by multiple copies of the Pod, normally no need to use ReplicaSet.
  (3)DaemonSet
  Scene for each Node is running at most only a copy of the Pod, DaemonSet typically used to run the daemon (daemon, daemon).
  (4)StatefuleSet
  Pod to ensure that each copy of the entire life cycle of the same name, while others Controller does not provide this functionality. (Non-StatefuleSet, when a failure need to remove the Pod and restart, name Pod that will change)
  (5)Job
  End applications to run on the deleted, other Controller Pod is usually long-term continuous operation.

  6.Service

  K8S defines the outside world to access one or a specific set of Pod way is Service. Each Service has its own IP and port, and provides load balancing for the Pod.
  If K8S Pod task is to run the Controller to do, then visit Pod task is given to Service to do it.

  7.Namespace

  Cluster Namespace a physical logically divided into a plurality of virtual Cluster, each virtual Cluster is a Namespace, Namespace different resources are completely isolated.
K8S will automatically create two Namespace:
  (1) default: If you do not specify when creating a resource will be put here Namespace
  (2) kube-system: K8S system resources that you have created will be placed in this Namespace

Two, K8S resolve cluster architecture

  The following shows a minimal K8S cluster, a master node node and two nodes:
* There the master kubelet and kube-proxy because the master is also a Node

  1.Master node

  K8S cluster " brain ," multiple Daemon run the following services:
  • Server API (KUBE-apiserver)
    • Provided Restful API => Kubernetes API, calling for a variety of other components of the Cluster Resource Management
  • Scheduler(kube-scheduler)
    • The Pod is responsible for deciding on which Node Run up
    • Selects Pod (eg. Cluster topology, each node load, the HA, etc.) according to a specified schedule algorithm
  • Controller Manager(kube-controller-manager)
    • Responsible for managing cluster resources to ensure that resources are expected state
    • Composed by a variety Controller
      • Replication Controller: Management Deployment, StatefuleSet, DaemonSet life cycle
      • Endpoints Controller
      • Namespace Controller: Namespace Management Resources
      • Serviceaccounts Controller
  • Etcd
    • 负责保存K8S集群中的配置信息和各种资源的状态信息
    • 当数据发生变化时,会及时通知K8S相关组件
  • Pod网络
    • 保证Pod能够相互通信,Flannel是一个可选方案

  2.Node节点

  运行Pod的主战场,主要运行以下K8S组件:
  • kubelet
    • Node的Agent,负责创建运行容器与向Master报告运行状态
  • kube-proxy
    • 每个Node都会运行proxy,它负责请求转发到后端的容器
  • Pod网络
    • 保证Pod能够相互通信,Flannel是一个可选方案

三、K8S集群环境搭建

3.1 K8S环境搭建的几种方式

  搭建K8S环境有几种常见的方式如下:

  (1)Minikube

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

  (2)Kubeadm

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

  (3)二进制包

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

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

3.2 搭建前的准备工作

  (1)准备三台Linux服务器

  这里我选择通过VMware Workstaion来搭建3个虚拟机,每个配置2CPU和2G内存,如下图:

  

  (2)配置主机名与静态IP地址如下表所示:

角色 主机名 IP地址
Master k8s-master 192.168.2.100
Node k8s-node1 192.168.2.101
Node k8s-node2 192.168.2.102

  然后,更改hosts文件添加主机名与IP映射关系

# vim /etc/hosts
192.168
.2.100 k8s-master 192.168.2.101 k8s-node1 192.168.2.102 k8s-node2

  (3)关闭防火墙

systemctl stop firewalld
systemctl disable firewalld

  (4)关闭selinux

sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0

  (5)关闭swap => K8S中不支持swap分区

# vim /etc/fstab
#/dev/mapper/centos-swap swap                    swap    defaults        0 0

  *.编辑etc/fstab将swap那一行注释掉或者删除掉

  (6)将桥接的IPv4流量传递到iptables的链

# cat > /etc/sysctl.d/k8s.conf << EOF
   net.bridge.bridge-nf-call-ip6tables = 1
   net.bridge.bridge-nf-call-iptables = 1
   EOF

# sysctl --system

3.3 安装Docker&Kubeadm&Kubelet

  以下步骤请在所有节点中操作:

  (1)安装Docker

# wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O
/etc/yum.repos.d/docker-ce.repo
# yum -y install docker-ce-18.06.1.ce-3.el7
# systemctl enable docker && systemctl start docker
# docker --version
Docker version 18.06.1-ce, build e68fc7a

  *.这里安装的是18.06社区版,如果你之前有安装低版本的Docker,为了配合本次实验的K8S版本(1.13.x),建议先卸载掉,卸载过程可以参考这篇文章《CentOS7 Docker升级》。

  (2)添加阿里云Yum软件源

# cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

  (3)安装Kubeadm&Kubelet&Kubectl

  注意:本次部署K8S版本号为1.13.3

# yum install -y kubelet-1.13.3 kubeadm-1.13.3 kubectl-1.13.3
# systemctl enable kubelet

  遇到的一些坑如下:

  ① 碰到需要kubernetes-cni的问题:

#####错误:软件包:kubelet-1.13.3-0.x86_64 (kubernetes)
需要:kubernetes-cni = 0.6.0
可用: kubernetes-cni-0.3.0.1-0.07a8a2.x86_64 (kubernetes)
kubernetes-cni = 0.3.0.1-0.07a8a2
可用: kubernetes-cni-0.5.1-0.x86_64 (kubernetes)
kubernetes-cni = 0.5.1-0
可用: kubernetes-cni-0.5.1-1.x86_64 (kubernetes)
kubernetes-cni = 0.5.1-1
可用: kubernetes-cni-0.6.0-0.x86_64 (kubernetes)
kubernetes-cni = 0.6.0-0
正在安装: kubernetes-cni-0.7.5-0.x86_64 (kubernetes)
kubernetes-cni = 0.7.5-0
您可以尝试添加 --skip-broken 选项来解决该问题
您可以尝试执行:rpm -Va --nofiles --nodigest

  解决:手动安装kubernetes-cni对应的版本

yum install -y kubelet-1.13.3 kubeadm-1.13.3 kubectl-1.13.3 kubernetes-cni-0.6.0 

  ② 使用yum安装程序时,提示xxx.rpm公钥尚未安装

从 https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg 检索密钥
导入 GPG key 0xA7317B0F:
 用户ID     : "Google Cloud Packages Automatic Signing Key <[email protected]>"
 指纹       : d0bc 747f d8ca f711 7500 d6fa 3746 c208 a731 7b0f
 来自       : https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg

e3438a5f740b3a907758799c3be2512a4b5c64dbe30352b2428788775c6b359e-kubectl-1.13.3-0.x86_64.rpm 的公钥尚未安装

 失败的软件包是:kubectl-1.13.3-0.x86_64
 GPG  密钥配置为:https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg

  解决:使用 yum install xxx.rpm --nogpgcheck 命令格式跳过公钥检查,比如跳过kubectl和kubeadm的公钥检查如下命令:

yum install kubectl-1.13.3-0.x86_64 --nogpgcheck
yum install kubeadm-1.13.3-0.x86_64 --nogpgcheck

3.4 部署Kubernetes Master

  以下步骤请在k8s-master节点上操作:

kubeadm init \
--apiserver-advertise-address=192.168.2.100 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.13.3 \
--service-cidr=10.1.0.0/16 \
--pod-network-cidr=10.244.0.0/16

PS:由于默认拉取镜像地址k8s.gcr.io国内无法访问,这里指定阿里云镜像仓库地址(registry.aliyuncs.com/google_containers)。官方建议服务器至少2CPU+2G内存,当然内存1G也是可以的,但是会出Warning,建议还是老老实实升2G内存把。

  

  接下来,为了顺利使用kubectl命令,执行以下命令:

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
# kubectl get nodes

  这时你可以使用kubectl了,当你执行完kubectl get nodes之后,你会看到如下状态:

  

3.5 部署Pod网络插件(CNI)

  同样,继续在k8s-master上操作:

kubectl apply -f \
https://raw.githubusercontent.com/coreos/flannel/a70459be0084506e4ec919aa1c114638878db11b/Documentation/kube-flannel.yml

  然后通过以下命令验证:全部为Running则OK,其中一个不为Running,比如:Pending、ImagePullBackOff都表明Pod没有就绪

  

  如果其中有的Pod没有Running,可以通过以下命令查看具体错误原因,比如这里我想查看kube-flannel-ds-amd64-8bmbm这个pod的错误信息:

kubectl describe pod kube-flannel-ds-amd64-8bmbm -n kube-system

  在此过程中可能会遇到无法从qury.io拉取flannel镜像从而导致无法正常Running,解决办法如下:

  使用国内云服务商提供的镜像源然后通过修改tag的方式曲线救国

docker pull quay-mirror.qiniu.com/coreos/flannel:v0.11.0-amd64
docker tag quay-mirror.qiniu.com/coreos/flannel:v0.11.0-amd64 quay.io/coreos/flannel:v0.10.0-amd64
docker rmi quay-mirror.qiniu.com/coreos/flannell:v0.11.0-amd64

  这时,我们再看看master节点的状态就会从NotReady变为Ready:

  

  那么,恭喜你,Master节点部署结束了。如果你只想要一个单节点的K8S,那么这里就完成了部署了。

3.6 加入Kubernetes Node

  在两台Node节点上执行join命令:

kubeadm join 192.168.2.100:6443 --token ekqxk2.iiu5wx5bbnbdtxsw --discovery-token-ca-cert-hash \
sha256:c50bb83d04f64f4a714b745f04682b27768c1298f331e697419451f3550f2d05

  这里需要注意的就是,带上在Master节点Init成功后输出的Token。如果找不到了,没关系,可以通过以下命令来查看:

kubeadm token list

  Node节点上成功join之后会得到以下信息:

  

  这时,我们在master节点上执行以下命令可以看到集群各个节点的状态了:

  

  如果看到两个Node状态不是Ready,那么可能需要检查哪些Pod没有正常运行:

kubectl get pod --all-namespaces

  然后按照3.5中的检查方式进行检查并修复,最终kubectl get nodes效果应该状态都是Running。注意的是在检查时需要注意是哪个Node上的错误,然后在对应的Node进行修复,比如拉取flannel镜像。

  至此,一个最小化的K8S集群已经搭建完毕。

3.7 测试Kubernetes集群

  这里为了快速地验证一下我们的K8S集群是否可用,创建一个示例Pod(这里默认是一个副本):

kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl get pod,svc

  

  如果想要看到更多的信息,比如pod被部署在了哪个Node上,可以通过 kubectl get pods,svc -o wide来查看。

  

   因为是NodePort方式,因此其映射的端口号会在30000-32767范围内随机取一个,我们可以直接通过浏览器输入IP地址访问,比如这时我们通过浏览器来访问一下任一Node的IP地址加端口号,例如192.168.2.101:31174或192.168.2.102:31174

  

  

   如果能够成功看到,那么恭喜你,你的K8S集群能够成功运行了,万里长征走完了第一步!

四、小结

  本文快速地介绍了一下Kubernetes的核心构成组件及其作用,然后通过在三台Linux主机上通过Kubeadm搭建了一个Master节点两个Node节点的集群,最后通过部署一个Deployment来快速地验证了一下集群是否可用。下一篇会通过一个ASP.NET Core的部署例子来演示和介绍一下各个组件之间是如何协作的,以及部署Dashboard。

参考资料

(1)CloudMan,《每天5分钟玩转Kubernetes

(2)李振良,《一天入门Kubernets教程

(3)李振良,《30分钟部署一个Kubernetes集群

(4)cao_xiaobo,《CentOS7 部署K8S集群

 

Guess you like

Origin www.cnblogs.com/edisonchou/p/aspnet_core_on_k8s_deepstudy_part1.html