{"id":22885026,"url":"https://github.com/mark8s/nginx-controller","last_synced_at":"2025-03-31T17:55:14.651Z","repository":{"id":112108030,"uuid":"498692938","full_name":"mark8s/nginx-controller","owner":"mark8s","description":"A nginx pod controller ,it can autoscale nginx pod ","archived":false,"fork":false,"pushed_at":"2022-06-01T12:19:52.000Z","size":419,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-06T22:42:24.630Z","etag":null,"topics":["code-generator","controller","crd"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mark8s.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-06-01T10:36:27.000Z","updated_at":"2022-06-01T10:37:58.000Z","dependencies_parsed_at":null,"dependency_job_id":"de3b3bbf-12c3-4b9f-80f6-b3ea33b458d1","html_url":"https://github.com/mark8s/nginx-controller","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mark8s%2Fnginx-controller","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mark8s%2Fnginx-controller/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mark8s%2Fnginx-controller/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mark8s%2Fnginx-controller/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mark8s","download_url":"https://codeload.github.com/mark8s/nginx-controller/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246514214,"owners_count":20790016,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["code-generator","controller","crd"],"created_at":"2024-12-13T19:31:02.170Z","updated_at":"2025-03-31T17:55:14.627Z","avatar_url":"https://github.com/mark8s.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Nginx Controller\n\n一个监控nginx数量的controller\n\n## Usage\n启动controller\n```shell\n$ go build .\n$ ./nginx-controller -kubeconfig /root/.kube/config -alsologtostderr true\n```\n\napply crd\n```shell\nkubectl apply -f ./artifacts/example/crd.yaml\n```\n\napply cr\n```shell\nkubectl apply -f ./artifacts/example/example-nginx.yaml\n```\n\n## Test\n当cr中定义的副本数为2时，会自动启动2个nginx的pod\n```shell\n$ vim example-nginx.yaml \n$ kubectl apply -f example-nginx.yaml \n$ cat example-nginx.yaml \napiVersion: demo.mark8s.io/v1alpha1\nkind: Nginx\nmetadata:\n  name: my-nginx\nspec:\n  replicas: 2\n$ kubectl get po\nNAME                            READY   STATUS    RESTARTS   AGE\nnginx-pod-1654078536671008205   1/1     Running   0          14m\nnginx-pod-1654079421678202242   1/1     Running   0          9s\n```\n\n当cr中定义的副本数为5时，会自动启动5个nginx的pod.效果类似 replicaSet\n```shell\n$ vim example-nginx.yaml \n$ kubectl apply -f example-nginx.yaml \n$ cat example-nginx.yaml \napiVersion: demo.mark8s.io/v1alpha1\nkind: Nginx\nmetadata:\n  name: my-nginx\nspec:\n  replicas: 5\n$ kubectl get po\nNAME                            READY   STATUS    RESTARTS   AGE\nnginx-pod-1654078536671008205   1/1     Running   0          17m\nnginx-pod-1654079421678202242   1/1     Running   0          2m43s\nnginx-pod-1654079527716529235   1/1     Running   0          57s\nnginx-pod-1654079527722074237   1/1     Running   0          57s\nnginx-pod-1654079527724902515   1/1     Running   0          57s\n```\n\n## 原理\n![controller](images/controller.png)\n\n1.informer：生成的代码，监听apiserver中特定资源变化，然后会存储到一个线程安全的local cache中，最后回调我们自己实现的event handler。\n \n2.local cache：生成的代码，informer实时同步apiserver（也就是etcd）中的数据到内存中存储，可以有效降低apiserver的查询压力，但缺点就是实时性不好，本地会比远 程的数据落后一点点但会最终与etcd一致，所以需要根据情况具体分析是走Local cache还是apiserver实时获取数据。\n\n3.lister：生成的代码，提供了CURD操作访问local cache。\n\n4.controller：一个逻辑概念，就是指调度某种资源的实现而已，需要我们自己开发。Controller做的事情主要包括：  \n   \n   a)\t实现event handler处理资源的CURD操作 \n\n   b)\t在event handler，可以使用workqueue类库实现相同资源对象的连续event的去重，以及event处理异常后的失败重试\n\n5.Workqueue：一个单独的类库，是可选使用的，但通常都会使用，原因上面说了。我们需要在实现event handler的时候把发生变化的资源标识放入 ，供下面的processor消费。\n\n6.Clientset：默认clientset只能CRUD k8s提供的资源类型，比如deployments，daemonset等；生成的代码为我们自定义的资源（CRD）生成了单独的clientset，从而让我们使用结构化的代码CURD自定义资源。也就是说，想操作内建资源就用k8s自带的clientset，想操作CRD就用生成代码里的clientset。\n\n7.Processor：我们实现的go协程，消费workqueue中的事件，workqueue提供了按资源标识的去重。\n\n\n## Reference\n\n[sample-controller](https://github.com/kubernetes/sample-controller)\n\n[owenliang/k8s-client-go](https://github.com/owenliang/k8s-client-go/tree/master/demo10)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmark8s%2Fnginx-controller","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmark8s%2Fnginx-controller","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmark8s%2Fnginx-controller/lists"}