这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos
名称 | 链接 | 备注 |
---|---|---|
项目主页 | https://github.com/zq2599/blog_demos | 该项目在GitHub上的主页 |
git仓库地址(https) | https://github.com/zq2599/blog_demos.git | 该项目源码的仓库地址,https协议 |
git仓库地址(ssh) | [email protected]:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh协议 |
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 1
template:
metadata:
labels:
app: nginx
bind-service: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: NodePort
selector:
app: nginx
ports:
- name: http
port: 80
targetPort: 80
nodePort: 30000
kubectl apply -f deployment-svc.yaml
回顾前面写的deployment-svc.yaml文件,如下图所示,deployment有自己的标签,service通过标签选择器找到了deployment,与之匹配,使得外部请求可以顺利到达nginx的pod
上图中的标签选择器,就是咱们前面提到的LabelSelector,很显然,yaml中的selector就是一种资源类型,在代码中对应的就是LabelSelector这个结构体
kubernetes官方对标签选择器的描述在这里:https://kubernetes.io/zh-cn/docs/concepts/overview/working-with-objects/labels/,这里面细说了关于标签的各种写法和约束,值得认真看一遍
apiVersion: apps/v1
kind: Deployment
metadata:
name: other-deployment
spec:
selector:
matchLabels:
app: other
replicas: 1
template:
metadata:
labels:
app: other
bind-service: none
spec:
containers:
- name: other
image: nginx:latest
ports:
- containerPort: 80
kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-544dc8b7c4-xlkj8 1/1 Running 0 2m20s
other-deployment-7659c57b9d-2slm8 1/1 Running 0 5s
tree client-go-tutorials
client-go-tutorials
├── action
│ ├── action.go
│ ├── conflict.go
│ ├── controller.go
│ ├── controller_demo.go
│ ├── label.go
│ └── list_pod.go
├── go.mod
├── go.sum
└── main.go
1 directory, 9 files
type Lable struct{}
// listPods 根据传入的selector过滤
func listPods(clientset *kubernetes.Clientset, selector labels.Selector, prefix string) {
namespace := "default"
// 查询pod列表
pods, err := clientset.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{
// 传入的selector在这里用到
LabelSelector: selector.String(),
})
if err != nil {
panic(err.Error())
}
nums := len(pods.Items)
log.Printf("[%v] 查到[%d]个pod\n", prefix, nums)
// 如果没有pod就返回了
if nums < 1 {
return
}
// 遍历列表中的每个pod
for index, pod := range pods.Items {
log.Printf("[%v] %v. pod : %v\n", prefix, index+1, pod.Name)
}
}
func (lable Lable) DoAction(clientset *kubernetes.Clientset) error {
// 第一种: 创建Requirement对象,指定类型是Equals(等于)
equalRequirement, err := labels.NewRequirement("app", selection.Equals, []string{"other"})
if err != nil {
log.Println("1. create equalRequirement fail, ", err)
return err
}
selector := labels.NewSelector().Add(*equalRequirement)
// 验证,应该只查到app等于other的pod
listPods(clientset, selector, "用Requirement创建,Equal操作")
// 第一种: 创建Requirement对象,指定类型是In,not_exists不会有任何pod匹配到
inRequirement, err := labels.NewRequirement("app", selection.In, []string{"other", "nginx", "not_exists"})
if err != nil {
log.Println("2. create equalRequirement fail, ", err)
return err
}
selector = labels.NewSelector().Add(*inRequirement)
// 验证,应该查到app=other的pod
listPods(clientset, selector, "用Requirement创建,In操作")
// 第二种:labels.Parse方法
parsedSelector, err := labels.Parse("bind-service=none,app notin (not_exists)")
if err != nil {
log.Println("3. create equalRequirement fail, ", err)
return err
}
// 验证,应该查到app=other的pod
listPods(clientset, parsedSelector, "用Parse创建")
// 第三种:labels.SelectorFromSet方法
setSelector := labels.SelectorFromSet(labels.Set(map[string]string{"app": "nginx"}))
// 验证,应该查到app=nginx的pod
listPods(clientset, setSelector, "用SelectorFromSet创建")
// 第四种:metav1.LabelSelectorAsSelector方法
// 适用于当前环境已有资源对象的场景,可以取出LabelSelector对象来转换成labels.Selector
// 先创建一个LabelSelector
labelSelector := &metav1.LabelSelector{
MatchLabels: map[string]string{"app": "other"},
}
// 将LabelSelector转为labels.Selector
convertSelector, err := metav1.LabelSelectorAsSelector(labelSelector)
if err != nil {
log.Println("4. create equalRequirement fail, ", err)
return err
}
// 验证,应该查到app=nginx的pod
listPods(clientset, convertSelector, "用LabelSelector转换")
// labels.Selector的第五种用法:用labels.Selector匹配
// 准备好一个selector
matchSelector := labels.SelectorFromSet(labels.Set(map[string]string{"app": "nginx"}))
// 查询pod列表
pods, err := clientset.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
// 遍历列表中的每个pod
for _, pod := range pods.Items {
if matchSelector.Matches(labels.Set(pod.GetLabels())) {
log.Printf("app=nginx匹配成功[%s]\n", pod.Name)
} else {
log.Printf("app=nginx匹配失败[%s]\n", pod.Name)
}
}
return nil
}
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}",
"args": ["-action=label"]
}
]
}
2023/03/11 19:57:53 解析命令完毕,开始加载配置文件
2023/03/11 19:57:53 加载配置文件完毕,即将执行业务 [label]
2023/03/11 19:57:53 [用Requirement创建,Equal操作] 查到[1]个pod
2023/03/11 19:57:53 [用Requirement创建,Equal操作] 1. pod : other-deployment-7b57cc4f89-bdxj8
2023/03/11 19:57:53 [用Requirement创建,In操作] 查到[2]个pod
2023/03/11 19:57:53 [用Requirement创建,In操作] 1. pod : nginx-deployment-5659dc6c45-hsx7j
2023/03/11 19:57:53 [用Requirement创建,In操作] 2. pod : other-deployment-7b57cc4f89-bdxj8
2023/03/11 19:57:53 [用Parse创建] 查到[1]个pod
2023/03/11 19:57:53 [用Parse创建] 1. pod : other-deployment-7b57cc4f89-bdxj8
2023/03/11 19:57:53 [用SelectorFromSet创建] 查到[1]个pod
2023/03/11 19:57:53 [用SelectorFromSet创建] 1. pod : nginx-deployment-5659dc6c45-hsx7j
2023/03/11 19:57:53 [用LabelSelector转换] 查到[1]个pod
2023/03/11 19:57:53 [用LabelSelector转换] 1. pod : other-deployment-7b57cc4f89-bdxj8
2023/03/11 19:57:53 执行完成
func (controllerDemo ControllerDemo) DoAction(clientset *kubernetes.Clientset) error {
setSelector := labels.SelectorFromSet(labels.Set(map[string]string{"app": "nginx"}))
optionsModifer := func(options *metav1.ListOptions) {
options.LabelSelector = setSelector.String()
}
podListWatcher := cache.NewFilteredListWatchFromClient(clientset.CoreV1().RESTClient(), "pods", metav1.NamespaceDefault, optionsModifer)
// 创建ListWatch对象,指定要监控的资源类型是pod,namespace是default
// podListWatcher := cache.NewListWatchFromClient(clientset.CoreV1().RESTClient(), "pods", v1.NamespaceDefault, fields.Everything())
// 创建工作队列
queue := workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
// 创建informer,并将返回的存储对象保存在变量indexer中
indexer, informer := cache.NewIndexerInformer(podListWatcher, &v1.Pod{}, 0, cache.ResourceEventHandlerFuncs{
// 响应新增资源事件的方法,可以按照业务需求来定制,
// 这里的做法比较常见:写入工作队列
AddFunc: func(obj interface{}) {
key, err := cache.MetaNamespaceKeyFunc(obj)
if err == nil {
queue.Add(key)
}
},
// 响应修改资源事件的方法,可以按照业务需求来定制,
// 这里的做法比较常见:写入工作队列
UpdateFunc: func(old interface{}, new interface{}) {
key, err := cache.MetaNamespaceKeyFunc(new)
if err == nil {
queue.Add(key)
}
},
// 响应修改资源事件的方法,可以按照业务需求来定制,
// 这里的做法比较常见:写入工作队列,注意删除的时候生成key的方法和新增修改不一样
DeleteFunc: func(obj interface{}) {
// IndexerInformer uses a delta queue, therefore for deletes we have to use this
// key function.
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
if err == nil {
queue.Add(key)
}
},
}, cache.Indexers{})
// 创建Controller对象,将所需的三个变量对象传入
controller := NewController(queue, indexer, informer)
// Now let's start the controller
stop := make(chan struct{})
defer close(stop)
// 在协程中启动controller
go controller.Run(1, stop)
// Wait forever
select {}
return nil
}