Skip to content

Commit

Permalink
GITBOOK-240: No subject
Browse files Browse the repository at this point in the history
mouuii authored and gitbook-bot committed Jan 21, 2025
1 parent 3080249 commit 5654591
Showing 45 changed files with 47 additions and 21 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .gitbook/assets/image (1) (1) (1) (1) (1) (1) (1) (1) (1).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .gitbook/assets/image (1) (1) (1) (1) (1) (1) (1) (1).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .gitbook/assets/image (1) (1) (1) (1) (1) (1) (1).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .gitbook/assets/image (1) (1) (1) (1) (1) (1).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .gitbook/assets/image (1) (1) (1) (1) (1).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .gitbook/assets/image (1) (1) (1) (1).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .gitbook/assets/image (1) (1) (1).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .gitbook/assets/image (1) (1).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .gitbook/assets/image (1).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .gitbook/assets/image (2) (1) (1) (1) (1) (1) (1) (1) (1).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .gitbook/assets/image (2) (1) (1) (1) (1) (1) (1) (1).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .gitbook/assets/image (2) (1) (1) (1) (1) (1) (1).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .gitbook/assets/image (2) (1) (1) (1) (1) (1).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .gitbook/assets/image (2) (1) (1) (1) (1).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .gitbook/assets/image (2) (1) (1) (1).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .gitbook/assets/image (2) (1) (1).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .gitbook/assets/image (2) (1).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .gitbook/assets/image (2).png
Binary file added .gitbook/assets/image (3) (1) (1) (1) (1).png
Binary file modified .gitbook/assets/image (3) (1) (1) (1).png
Binary file modified .gitbook/assets/image (3) (1) (1).png
Binary file modified .gitbook/assets/image (3) (1).png
Binary file modified .gitbook/assets/image (3).png
Binary file modified .gitbook/assets/image.png
3 changes: 2 additions & 1 deletion SUMMARY.md
Original file line number Diff line number Diff line change
@@ -8,7 +8,8 @@

* [第零章 :阅读源码必知必会](k8s-yuan-ma-ren-cai-fu-hua-xun-lian-ying/k8s-yuan-ma-kai-fa-bi-zhi-bi-hui/README.md)
* [window 调试k8s 源码](k8s-yuan-ma-ren-cai-fu-hua-xun-lian-ying/k8s-yuan-ma-kai-fa-bi-zhi-bi-hui/window-tiao-shi-k8s-yuan-ma.md)
* [01-调试开发k8s](k8s-yuan-ma-ren-cai-fu-hua-xun-lian-ying/k8s-yuan-ma-kai-fa-bi-zhi-bi-hui/01-tiao-shi-kai-fa-k8s.md)
* [01-本地调试开发k8s](k8s-yuan-ma-ren-cai-fu-hua-xun-lian-ying/k8s-yuan-ma-kai-fa-bi-zhi-bi-hui/01-tiao-shi-kai-fa-k8s.md)
* [02-借助minikube调试源码](k8s-yuan-ma-ren-cai-fu-hua-xun-lian-ying/k8s-yuan-ma-kai-fa-bi-zhi-bi-hui/02-jie-zhu-minikube-tiao-shi-yuan-ma.md)
* [02- 当你运行 kubectl create deployment 命令时发生了什么](k8s-yuan-ma-ren-cai-fu-hua-xun-lian-ying/k8s-yuan-ma-kai-fa-bi-zhi-bi-hui/02-dang-ni-yun-xing-kubectl-create-deployment-ming-ling-shi-fa-sheng-le-shen-me.md)
* [第一章: client-go](k8s-yuan-ma-ren-cai-fu-hua-xun-lian-ying/client-go/README.md)
* [01-client-go](k8s-yuan-ma-ren-cai-fu-hua-xun-lian-ying/client-go/01-client-go.md)
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@

informer 设计之初是为了更好的写 controller,本章我们来手写一个 controller,下图是官方推荐的编程模式:

<figure><img src="../../.gitbook/assets/image (2) (1) (1) (1) (1) (1) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>
<figure><img src="../../.gitbook/assets/image (2) (1) (1) (1) (1) (1) (1) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>

* 控制器包含三部分: informer ,workqueue,control loop
* informer 负责同步远端集群资源到本地缓存Indexer
Original file line number Diff line number Diff line change
@@ -307,7 +307,7 @@ Indices: {
}
```
<figure><img src="../../.gitbook/assets/image (3) (1) (1).png" alt=""><figcaption></figcaption></figure>
<figure><img src="../../.gitbook/assets/image (3) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>
了解了底层数据接口,我们再回过头来看看其暴露给外部的方法:
Original file line number Diff line number Diff line change
@@ -105,7 +105,7 @@ DeltaFIFO与其他队列最大的不同之处是,它会保留所有关于资

queue字段存储资源对象的key,该key通过KeyOf函数计算得到。items字段通过map数据结构的方式存储,value存储的是对象的Deltas数组。DeltaFIFO存储结构如图所示。

<figure><img src="../../.gitbook/assets/image (3) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>
<figure><img src="../../.gitbook/assets/image (3) (1) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>

### 生产者

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 07-workqueue

<figure><img src="../../.gitbook/assets/image (1) (1) (1) (1) (1) (1) (1) (1) (1) (1) (1) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>
<figure><img src="../../.gitbook/assets/image (1) (1) (1) (1) (1) (1) (1) (1) (1) (1) (1) (1) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>

今天我们来详细研究下 workqueue 相关代码。client-go 的 util/workqueue 包里主要有三个队列,分别是普通队列,延时队列,限速队列,后一个队列以前一个队列的实现为基础,层层添加新功能,我们按照 Queue、DelayingQueue、RateLimitingQueue 的顺序层层拨开来看限速队列是如何实现的。

@@ -419,7 +419,7 @@ func (r *TypedBucketRateLimiter[T]) Forget(item T) {

```

<figure><img src="../../.gitbook/assets/image (1) (1) (1) (1) (1) (1) (1) (1) (1) (1) (1) (1) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>
<figure><img src="../../.gitbook/assets/image (1) (1) (1) (1) (1) (1) (1) (1) (1) (1) (1) (1) (1) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>

令牌桶算法内部实现了一个存放token(令牌)的“桶”,初始时“桶”是空的,token会以固定速率往“桶”里填充,直到将其填满为止,多余的token会被丢弃。每个元素都会从令牌桶得到一个token,只有得到token的元素才允许通过(accept),而没有得到token的元素处于等待状态。令牌桶算法通过控制发放token来达到限速目的。

@@ -474,7 +474,7 @@ func (r *TypedItemExponentialFailureRateLimiter[T]) Forget(item T) {

排队指数算法将相同元素的排队数作为指数,排队数增大,速率限制呈指数级增长,但其最大值不会超过maxDelay。

<figure><img src="../../.gitbook/assets/image (2) (1) (1) (1) (1) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>
<figure><img src="../../.gitbook/assets/image (2) (1) (1) (1) (1) (1) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>

### 3.`TypedItemFastSlowRateLimiter` 和上面的指数级限速器很像,都是用于错误尝试的,但是二者的限速策略不同,`ItemFastSlowRateLimiter` 是尝试次数超过阈值后用长延迟,否则用短延迟,具体的实现如下所示:

Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ description: controller

比如当某个Pod出现故障时,Deployment Controller会及时发现故障并执行自动化修复流程,确保集群里的Pod始终处于预期的工作状态。

<figure><img src="../../.gitbook/assets/image (1) (1) (1) (1) (1) (1) (1) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>
<figure><img src="../../.gitbook/assets/image (1) (1) (1) (1) (1) (1) (1) (1) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>

## 工作原理

Original file line number Diff line number Diff line change
@@ -90,7 +90,7 @@ StatefulSet controller 是 kube-controller-manager 众多控制器中的一个
StatefulSet controller 的大致组成和处理流程如下图,当事件发生时,控制器会watch到然后将对应的StatefulSet 对象放入到 queue 中,然后调用`syncStatefulSet`方法协调。

<figure><img src="../../.gitbook/assets/image (2) (1) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>
<figure><img src="../../.gitbook/assets/image (2) (1) (1) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>

### **pod 的命名规则、pod 创建与删除**

@@ -746,5 +746,5 @@ func (ssc *defaultStatefulSetControl) updateStatefulSet(
}
</code></pre>

<figure><img src="../../.gitbook/assets/image (1) (1) (1) (1) (1) (1) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>
<figure><img src="../../.gitbook/assets/image (1) (1) (1) (1) (1) (1) (1) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>

Original file line number Diff line number Diff line change
@@ -127,7 +127,7 @@ kubectl delete deployment nginx-deployment --cascade=orphan

### GarbageCollector 架构图 <a href="#garbagecollector" id="garbagecollector"></a>

<figure><img src="../../.gitbook/assets/image (1) (1) (1) (1) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>
<figure><img src="../../.gitbook/assets/image (1) (1) (1) (1) (1) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>

<pre class="language-go"><code class="lang-go"><strong>// GarbageCollector 对象表示垃圾回收控制器,是实现垃圾回收功能的核心对象。
</strong><strong>// 通过 reflector 监视受管API对象的更改,将结果汇集到单线程的dependencyGraphBuilder中
@@ -161,7 +161,7 @@ B对应一个node,owner列表里有A,dependent列表里有C;
C对应一个node,owner列表里有B,无dependent。
```
<figure><img src="../../.gitbook/assets/image (1) (1) (1) (1) (1) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>
<figure><img src="../../.gitbook/assets/image (1) (1) (1) (1) (1) (1) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>
#### **2个处理器**
Original file line number Diff line number Diff line change
@@ -131,7 +131,7 @@ kubectl rollout undo deployment &#x3C;deployment-name> --to-revision=&#x3C;revis

## 工作流程

<figure><img src="../../.gitbook/assets/image (2) (1) (1) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>
<figure><img src="../../.gitbook/assets/image (2) (1) (1) (1) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>

dc 的 Informer 主要监听三种资源,Deployment,ReplicaSet,Pod。其中 Deployment,ReplicaSet 监听 Add, Update, Delete。 Pod 只监听 Delete 事件。

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
description: 本节给大家介绍在 linux 环境下如何开发调试 k8s源码,写下这篇文章的时候,k8s 最新的版本是 1
---

# 02-借助minikube调试源码

### 1. 安装 go 语言

<figure><img src="../../.gitbook/assets/image (1).png" alt=""><figcaption></figcaption></figure>

### 2. 安装 goland



### 3. 下载 k8s 源码

`git clone https://github.com/kubernetes/kubernetes.git`

### 1. 启动 minikube

```
minikube start --container-runtime=docker --image-mirror-country='cn' --kubernetes-version=v1.32.0
```

其中 `--kubernetes-version` 可以选择其他版本,我这里用的是 `v1.32.0`
Original file line number Diff line number Diff line change
@@ -93,7 +93,7 @@ Node affinity 通过更灵活的接口扩展了nodeSelector。您仍然可以告



<figure><img src="../../../.gitbook/assets/image (2) (1) (1) (1) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>
<figure><img src="../../../.gitbook/assets/image (2) (1) (1) (1) (1) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>

此外,Tolerations 可以通过三种效果来排斥 Pod:

Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@

当前容器产品在企业中的使用已经非常普遍,对于运维和开发团队而言,熟练掌握容器技术的使用也成为一门必不可少的技能。

<figure><img src="../../.gitbook/assets/image (1) (1) (1).png" alt="" width="563"><figcaption></figcaption></figure>
<figure><img src="../../.gitbook/assets/image (1) (1) (1) (1).png" alt="" width="563"><figcaption></figcaption></figure>

## Docker desktop

@@ -36,7 +36,7 @@ cd welcome-to-docker

在你的 IDE 中打开示例应用程序。注意它已经有一个 **Dockerfile**。对于你自己的项目,你需要自己创建这个文件。

<figure><img src="../../.gitbook/assets/image (2) (1).png" alt="" width="474"><figcaption></figcaption></figure>
<figure><img src="../../.gitbook/assets/image (2) (1) (1).png" alt="" width="474"><figcaption></figcaption></figure>

你可以使用以下 **docker build** 命令通过 CLI 在你的项目文件夹中构建一个镜像。

@@ -46,7 +46,7 @@ docker build -t welcome-to-docker .

**-t** 标志为你的镜像指定一个名称(在这个例子中是 **welcome-to-docker**)。而 **.** 让 Docker 知道它可以在哪里找到 Dockerfile。构建完成后,镜像将出现在 **镜像** 标签中。选择镜像名称以查看其详细信息。选择 **运行** 以将其作为容器运行。在 **可选设置** 中记得指定一个端口号(比如 **8080**)。

<figure><img src="../../.gitbook/assets/image (1).png" alt="" width="563"><figcaption></figcaption></figure>
<figure><img src="../../.gitbook/assets/image (1) (1).png" alt="" width="563"><figcaption></figcaption></figure>

你也可以通过命令来启动一个容器:

@@ -58,13 +58,13 @@ docker run -d -p 8080:80 docker/welcome-to-docker

您可以通过转到 Docker 桌面仪表板的**容器**视图来查看所有容器。

<figure><img src="../../.gitbook/assets/image (1) (1).png" alt=""><figcaption></figcaption></figure>
<figure><img src="../../.gitbook/assets/image (1) (1) (1).png" alt=""><figcaption></figcaption></figure>

该容器运行一个显示简单网站的 Web 服务器。当处理更复杂的项目时,您将在不同的容器中运行不同的部分。例如,您可以为前端、后端和数据库运行不同的容器。

对于此容器,可以通过端口`8080`访问前端。要打开该网站,请选择**容器端口**列中的链接或访问 localhsot:8080 在您的浏览器中。

<figure><img src="../../.gitbook/assets/image (2).png" alt=""><figcaption></figcaption></figure>
<figure><img src="../../.gitbook/assets/image (2) (1).png" alt=""><figcaption></figcaption></figure>

## 探索容器

@@ -74,7 +74,7 @@ Docker Desktop 可让您探索容器的不同方面并与之交互。自己尝
2. 选择您的容器。
3.&#x62E9;**“文件”**&#x9009;项卡以探索容器的独立文件系统。

<figure><img src="../../.gitbook/assets/image (3).png" alt="" width="563"><figcaption></figcaption></figure>
<figure><img src="../../.gitbook/assets/image (3) (1).png" alt="" width="563"><figcaption></figcaption></figure>

## 停止容器 <a href="#stop-your-container" id="stop-your-container"></a>

2 changes: 1 addition & 1 deletion readme/1-zai-k8s-shang-bu-shu-ying-yong.md
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ Kubernetes 是一个用于部署容器的平台。所有部署到 Kubernetes 的

这就引出了容器。容器是一种只打包你的应用程序及其所需的依赖,然后在一个隔离的环境中运行的方式,就像虚拟机一样,但不需要安装和管理操作系统。

<figure><img src="../.gitbook/assets/image (1) (1) (1) (1) (1) (1) (1) (1) (1) (1) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>
<figure><img src="../.gitbook/assets/image (1) (1) (1) (1) (1) (1) (1) (1) (1) (1) (1) (1) (1) (1).png" alt=""><figcaption></figcaption></figure>

## 1.2 容器的好处

0 comments on commit 5654591

Please sign in to comment.