K8S基础概念和环境搭建

K8S的基础概念

1. 什么是K8S

K8S的全称是Kubernetes

K8S是一个开源的容器编排平台,用于自动化部署、扩缩、管理容器化应用程序。

2. 集群和节点

集群:K8S将多个机器统筹和管理起来,彼此保持通讯,这样的关系称之为集群

节点:集群中的机器称之为节点,在集群中至少存在一个节点。

3. 控制面和普通节点

集群中的机器分为2种角色:

控制面:负责调度

普通节点:负责干活(运行容器)

  • 容器以Pod为单位,在普通节点中运行

  • 同一个Pod中的所有容器,在同一个节点中运行

通常情况下,要避免同一个机器,边干活,边调度。

所以负责调度的机器不会再负责干活,也就是说一个集群中至少要存在两个节点(一个调度、一个干活)

但是如果负责干活的机器只有一个,若该机器出现了故障,就没有机器可以干活了。

所以理想的情况下,至少要存在两个不同的干活节点,也就是在K8S集群中存在3个节点(一个调度、两个干活)

4. K8S组件

在控制面中包含如下组件:

  1. API Server:
  • 核心组件之一,为集群各类资源提高同一个的接口(HTTP REST)
  1. etcd:
  • 分布式(K-V)数据库,特点:高一致性,高可用(容错好)

  • redis也是分布式(K-V)数据库,特点:读写性能好,但是并不符合K8S的需要

  1. kube-scheduler:
  • 监听新Pod创建需求,为其分配合适工作节点

  • 考虑Pod的资源需求、调整条件、负载状况等,尽量选出合适的工作节点

  1. kube-controller-mange:
  • 包含多个不同的"控制器"的管理器

  • 可用创建、管理、监视"控制器"的工作情况

  1. cloud-controller-mange:
  • 创建、管理、监视"云服务"的工作情况

在工作节点中包含如下组件:

  1. kubelet:
  • 接收、执行、监控、反馈来自控制面的调度(Pod)

  • 上报节点自身状态

  1. kuke-proxy:
  • 为Serive提供网络入口,提供负载均衡,调整网路规则
  1. 容器运行时
  • 比如 docker、containerd

5. K8S API

K8S API 是控制面中【API-Server】组件 提供的 HTTP API

K8S API 供用户、集群中不同的部分、集群外不同的组件,相互通信

用户对集群的管理、配置、调度等操,背后都是通过K8S API实现的

搭建K8S开发测试环境

所需一个Ubuntu+Docker的云服务器,内存建议在4G及以上

此处搭建环境的方法是:minikube

其他的搭建方法:Microk8S、K3S等

1. 安装minikube

下载:

XML 复制代码
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64

安装:

XML 复制代码
sudo install minikube-linux-amd64 /usr/local/bin/minikube

查看是否安装成功:

XML 复制代码
minikube

2. 启动minikube

XML 复制代码
minikube start

出现类似下图的内容就代表安装成功了

在执行minikube过程中如果出现镜像下载失败的问题,可以详见我的另一篇文章:

minikube start下载镜像失败-CSDN博客

3. 执行kubectl

XML 复制代码
minikube kubectl

执行结果可以参考下面的内容

XML 复制代码
ubuntu@VM-20-9-ubuntu:~$ minikube kubectl
kubectl controls the Kubernetes cluster manager.

 Find more information at: https://kubernetes.io/docs/reference/kubectl/

Basic Commands (Beginner):
  create          Create a resource from a file or from stdin
  expose          Take a replication controller, service, deployment or pod and expose it as a new Kubernetes service
  run             Run a particular image on the cluster
  set             Set specific features on objects

Basic Commands (Intermediate):
  explain         Get documentation for a resource
  get             Display one or many resources
  edit            Edit a resource on the server
  delete          Delete resources by file names, stdin, resources and names, or by resources and label selector

Deploy Commands:
  rollout         Manage the rollout of a resource
  scale           Set a new size for a deployment, replica set, or replication controller
  autoscale       Auto-scale a deployment, replica set, stateful set, or replication controller

Cluster Management Commands:
  certificate     Modify certificate resources
  cluster-info    Display cluster information
  top             Display resource (CPU/memory) usage
  cordon          Mark node as unschedulable
  uncordon        Mark node as schedulable
  drain           Drain node in preparation for maintenance
  taint           Update the taints on one or more nodes

Troubleshooting and Debugging Commands:
  describe        Show details of a specific resource or group of resources
  logs            Print the logs for a container in a pod
  attach          Attach to a running container
  exec            Execute a command in a container
  port-forward    Forward one or more local ports to a pod
  proxy           Run a proxy to the Kubernetes API server
  cp              Copy files and directories to and from containers
  auth            Inspect authorization
  debug           Create debugging sessions for troubleshooting workloads and nodes
  events          List events

Advanced Commands:
  diff            Diff the live version against a would-be applied version
  apply           Apply a configuration to a resource by file name or stdin
  patch           Update fields of a resource
  replace         Replace a resource by file name or stdin
  wait            Experimental: Wait for a specific condition on one or many resources
  kustomize       Build a kustomization target from a directory or URL

Settings Commands:
  label           Update the labels on a resource
  annotate        Update the annotations on a resource
  completion      Output shell completion code for the specified shell (bash, zsh, fish, or powershell)

Subcommands provided by plugins:

Other Commands:
  api-resources   Print the supported API resources on the server
  api-versions    Print the supported API versions on the server, in the form of "group/version"
  config          Modify kubeconfig files
  plugin          Provides utilities for interacting with plugins
  version         Print the client and server version information

Usage:
  kubectl [flags] [options]

Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).

创建别名

XML 复制代码
alias kubectl="minikube kubectl --"

4. 测试环境是否搭建成功

我们可以通过如下命令,测试环境是否搭建成功

XML 复制代码
kubectl get pod -A
或者
minikube kubectl -- get pod -A
相关推荐
唯情于酒8 分钟前
Docker学习
学习·docker·容器
喵叔哟32 分钟前
20.部署与运维
运维·docker·容器·.net
广州服务器托管4 小时前
NVIDIA最新591.74显卡驱动精简版:支持DLSS 4.5、所有RTX显卡都可使用,最新N卡驱动下载
计算机网络·网络安全·云原生·个人开发·可信计算技术
运维栈记5 小时前
虚拟化网络的根基-网络命名空间
网络·docker·容器
lbb 小魔仙6 小时前
【Linux】云原生运维效率提升:Linux 终端工具链(kubectl + tmux + fzf)组合拳教程
linux·运维·云原生
Joren的学习记录7 小时前
【Linux运维大神系列】Kubernetes详解3(kubeadm部署k8s1.23高可用集群)
linux·运维·kubernetes
Hellc0077 小时前
Docker网络冲突排查与解决方案:完整指南
网络·docker·容器
hanyi_qwe7 小时前
发布策略 【K8S (三)】
docker·容器·kubernetes
眠りたいです7 小时前
Docker核心技术和实现原理第二部分:docker镜像与网络原理
运维·网络·docker·容器
Mr. Cao code9 小时前
Docker数据管理:持久化存储最佳实践
java·docker·容器