K8S 源码探秘 之 nginx-ingress 工作原理分析_nginx与ingress

复制代码
...
for {
	select {
	...
	case event := <-n.updateCh.Out():
		if n.isShuttingDown {
			break
		}
		if evt, ok := event.(store.Event); ok {
			if evt.Type == store.ConfigurationEvent {
				n.syncQueue.EnqueueTask(task.GetDummyObject("configmap-change"))
				continue
			}
			n.syncQueue.EnqueueSkippableTask(evt.Obj)
		} 
	...
}

}

复制代码
       可以看到,NginxController 首先启动了 Store 协程,然后启动了 syncQueue 协程,最后监听 updateCh,当收到事件后,经过简单判断就向 syncQueue 写入了一个 task。


       再来看 Store 协程,跟踪到 internal/ingress/controller/store/store.go#Run()

func (s k8sStore) Run(stopCh chan struct{}) {

s.informers.Run(stopCh)

...

}

复制代码
       可以看到,继续调用了 informer 的 Run 方法,继续跟踪,还在这个文件,移步到 148 行左右

// Run initiates the synchronization of the informers against the API server.

func (i *Informer) Run(stopCh chan struct{}) {

go i.Endpoint.Run(stopCh)

go i.Service.Run(stopCh)

go i.Secret.Run(stopCh)

go i.ConfigMap.Run(stopCh)

...

go i.Ingress.Run(stopCh)

...

}

复制代码
       我们不难发现,informer 的 Run 方法,会起更多的协程,去监听不同资源的变化,包括 Endpoint、Service、Secret、ConfigMap、Ingress。我们以 Ingress 为例,跟踪到其定义处,仍在这个文件,找到 New() 方法

// New creates a new object store to be used in the ingress controller

func New(... updateCh *channels.RingChannel ...) Storer {

...

store.informers.Ingress = infFactory.Extensions().V1beta1().Ingresses().Informer()

...

ingEventHandler := cache.ResourceEventHandlerFuncs{

AddFunc: func(obj interface{}) {

...

updateCh.In() <- Event{

Type: CreateEvent,

Obj: obj,

}

},

DeleteFunc: func(obj interface{}) {

...

updateCh.In() <- Event{

Type: DeleteEvent,

Obj: obj,

}

},

UpdateFunc: func(old, cur interface{}) {

...

updateCh.In() <- Event{

Type: UpdateEvent,

Obj: cur,

}

},

}

...

store.informers.Ingress.AddEventHandler(ingEventHandler)

...

}

复制代码
        可以看出,Ingress 协程定义了监听 ingress 信息的 informer 对象,并注册了相关事件的回调方法,在回调方法内向之前提到的 updateCh 写入了事件,进而也就达到了当资源变化时通知 Controller 主程向同步队列写入task的目的。


        反过头来,看一下 syncQueue ,首先找到其定义,跟踪到 internal/ingress/controller/nginx.go#NewNGINXController()

// NewNGINXController creates a new NGINX Ingress controller.

func NewNGINXController(config *Configuration, mc metric.Collector, fs file.Filesystem) *NGINXController {

...

n.syncQueue = task.NewTaskQueue(n.syncIngress)

...

}

复制代码
       不难发现,队列的创建是通过 task.NewTaskQueue() 完成的,而且传入了关键的处理函数 n.syncIngress


       继续跟踪到 internal/task/queue.go#NewTaskQueue()

// NewTaskQueue creates a new task queue with the given sync function.

// The sync function is called for every element inserted into the queue.

func NewTaskQueue(syncFn func(interface{}) error) *Queue {

return NewCustomTaskQueue(syncFn, nil)

}

// NewCustomTaskQueue ...

func NewCustomTaskQueue(syncFn func(interface{}) error, fn func(interface{}) (interface{}, error)) *Queue {

q := &Queue{

queue: workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()),

sync: syncFn,

workerDone: make(chan bool),

fn: fn,

}

...

return q

}

复制代码
       可以看出,传入的处理函数 n.syncIngress 被赋值给 Queue 的 sync 属性了。实际上,syncQueue 的执行就是在反复执行该方法以消费队列里的元素。Queue 的 Run 定义可以在本文件中找到:

// Run starts processing elements in the queue

func (t *Queue) Run(period time.Duration, stopCh <-chan struct{}) {

wait.Until(t.worker, period, stopCh)

}

// worker processes work in the queue through sync.

func (t *Queue) worker() {

for {

key, quit := t.queue.Get()

...

if err := t.sync(key); err != nil {

t.queue.AddRateLimited(Element{

Key: item.Key,

Timestamp: time.Now().UnixNano(),

})

} else {

t.queue.Forget(key)

t.lastSync = ts

}

复制代码
	t.queue.Done(key)
}

}

复制代码
        同步队列协程的主要工作就是定期取出队列里的元素,并利用传入的 n.syncIngress (即 t.sync(key))方法处理队列里的元素。


        n.syncIngress 方法的定义在 internal/ingress/controller/controller.go#syncIngress()

// syncIngress collects all the pieces required to assemble the NGINX

// configuration file and passes the resulting data structures to the backend

// (OnUpdate) when a reload is deemed necessary.

func (n *NGINXController) syncIngress(interface{}) error {

复制代码
// 获取最新配置信息
....
// 构造 nginx 配置
pcfg := &ingress.Configuration{
	Backends:              upstreams,
	Servers:               servers,
	PassthroughBackends:   passUpstreams,
	BackendConfigChecksum: n.store.GetBackendConfiguration().Checksum,
}
...
// 不能避免 reload,就执行 reload 更新配置
if !n.IsDynamicConfigurationEnough(pcfg) {
	...
	err := n.OnUpdate(*pcfg)
	...
}
...
// 动态更新配置
err := wait.ExponentialBackoff(retry, func() (bool, error) {
	err := configureDynamically(pcfg, n.cfg.ListenPorts.Status, n.cfg.DynamicCertificatesEnabled)
	...
})
...

}

复制代码
       reload 配置的函数定义位于 internal/ingress/controller/nginx.go#OnUpdate(),具体就不解释了,读者可自行查阅。


       这里主要看下动态更新是怎么完成的,函数定义位于 internal/ingress/controller/nginx.go#configureDynamically()

// configureDynamically encodes new Backends in JSON format and POSTs the

// payload to an internal HTTP endpoint handled by Lua.

func configureDynamically(pcfg *ingress.Configuration, port int, isDynamicCertificatesEnabled bool) error {

backends := make(\[\]*ingress.Backend, len(pcfg.Backends))

复制代码
for i, backend := range pcfg.Backends {
	var service *apiv1.Service
	if backend.Service != nil {
		service = &apiv1.Service{Spec: backend.Service.Spec}
	}
	luaBackend := &ingress.Backend{
		Name:                 backend.Name,
		Port:                 backend.Port,
		SSLPassthrough:       backend.SSLPassthrough,
		SessionAffinity:      backend.SessionAffinity,
		UpstreamHashBy:       backend.UpstreamHashBy,



既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上运维知识点,真正体系化!

加入社区》https://bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0

25636469319)]

外链图片转存中...(img-IRNnslMq-1725636469320)

外链图片转存中...(img-61IZCJBY-1725636469320)

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上运维知识点,真正体系化!

加入社区》https://bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0

相关推荐
虎头金猫4 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
还是大剑师兰特4 天前
解决nginx错误:http://localhost:7000正常,http://localhost:7000/map 报错404
nginx·大剑师
xing-xing4 天前
Docker容器中Nginx站点根目录网页配置访问
nginx·docker
赵文宇(温玉)4 天前
CoreDNS的hosts插件的缺行配置导致k8s集群异常
容器·kubernetes·rancher
guo_wen_qiang4 天前
上传本地镜像到harbor中
docker·容器·持续部署
张洛闻Eren4 天前
k8s云原生【第十课】:水平 Pod 自动扩缩容
运维·数据库·云原生·kubernetes·github
PC2005-cloud4 天前
Nginx 防盗链配置实战:用 referer 模块保护网站静态资源
nginx
忆~遂愿4 天前
本地片库怎么在外面打开?Plex + cpolar 搭一套可远程访问的私人影音库
docker·容器
Thneonl4 天前
你的 Pod 为什么 Pending:调度器两阶段
后端·kubernetes
不吃香菜kkk、4 天前
CI/CD(GitOps)学习与部署手册
运维·云原生·容器·kubernetes·云计算·jenkins·argocd