美文网首页
2021-05-01-2日

2021-05-01-2日

作者: 文茶君 | 来源:发表于2021-05-02 23:25 被阅读0次

windows安装参考https://cloud.tencent.com/developer/article/1585177
运行创建的operator
operator-sdk add --api-version=k8s.imooc.com/v1alpha1 --kind=ImoocPod

operator-sdk add --api-version=k8s.imooc.com(自己命名)/v1alpha1(版本号) --kind=ImoocPod

会自己生成k8s/v1alpha1文件夹,有doc.go,imoocpod.type.go,register.go,zz_generated.deepcopy.go四个文件
添加controller
operator-sdk add controller --api-version=k8s.imooc.com/v1alpha1 --kind=ImoocPod

会加imooccpod_controller.go文件夹
开发controller
还有crd.yaml

Windows平台下使用GoLand生成Linux可执行文件
https://blog.csdn.net/hacker234/article/details/103444066

开发controller

当*_types.go发生变化执行:operator=sdk generate k8s
建立镜像
operator-sdk build rubinus/imoocpod-operator
push上去
docker push rubinus/imoocpod-operator

当*_types.go发生变化是指
imooco_type(自己项目名).go中
type ImoocPodSpec struct{}----运行的状态
与 type ImoocPodStatus struct{}-----期望的状态,由yaml文件填充
通过比较两者状态差异,做某一些响应

在imoocpod.go中改Reconcile
Reconcile:去实现用户自己的控制器逻辑
Define a new pod
pod:=newPodForCR(instance)
Reconcile识别到cr,然后根据cr的namespace去getImoocPod

部署operator

kubectl apply -f deploy/service_account.yaml
kubectl apply -f deploy/role.yaml
kubectl apply -f deploy/role_binding.yaml
kubectl apply -f deploy/operator.yaml

部署CDR以及CR
kubectl apply -f deploy/crds/k8s.imooc.com_imoocpods_crd.yaml
kubectl apply -f deploy/crds/k8s.imooc.com_v1alpha1_imoocpod_cr.yaml
·····

截取部分代码
//获取name对应的所有pod列表
lbls:=labels.Set{
"app":instance.Name,//资源的label来查找对应的pod
}
existingPod:=&corev1.PodList{}//获取pod列表
err :=r.client.List(context.TODO(),existingPod,&client.ListOptions{
Namespace:request.Namespace,
LabelSelector:labels.SelectorFormSet(lbls),

})
if err !=nil{
regLogger.Error(err,msg:"取已存在的pod失败")
return reconcile.Result{},err
}

//取到pod列表中的pod name
var existPodNames [] string
for _, pod:= range existingPods.Items {
pod.GetObjectMeta().getDeletionTimestamp()!=nil{
continue
}
if pod.Status.Phase==corev1.PodPending || pod.Status.Phase =corev1.PodRunning{
//如果pod 是pending或running
existingPodNames= append(existingPodNames,pod.GetObjectMeta().GetName())
}
}

//update pod.status !=运行中的status
//比较DeepEqual
status:=k8sv1alpha1.ImoocPodStatus{//最先定义,期望的status
PodNames:existingPodNames,
Replicas:len(existingPodNames),
}
if reflect.DeepEqual(instance.Status,status){
instance.Status= status//把期望状态给运行态
err: r.client.Status().Update(context.TODO(),instance)
if err !=nil{
regLogger.Error(err,msg:"更新pod失败")
return reconcile.Result{},err
}
}

//len(pod)>运行中的len(pod.replicas),期望值小,需要scale down delete
if len(existingPodNames)>instance.Spec.Replicas{
//delete
regLogger.Info(err,msg:"正在删除pod,当前podnames和期望的rc",existingPodNames,instance.Spec.Replicas)
pod:=existingPods.Items[0]
err:= r.client.Delet(context.TODO(),&pod)
if err !=nil{
regLogger.Error(err,msg:"删除pod失败")
return reconcile.Result{},err
}

}
//len(pod)<运行中的len(pod.replicas),期望值大,需要scale up create

if len(existingPodNames)>instance.Spec.Replicas{
//create
regLogger.Info(err,msg:"正在创建pod,当前podnames和期望的rc",existingPodNames,instance.Spec.Replicas)
pod :=newPodForCR(instance)
//set imoocpod instance as the owner and contronller
if err: =contronerutil,SetControllerReference(instance,pod,r.scheme);err!=nil{
}

err=r.client.Create(context.TODO(),pod)
if err !=nil{
regLogger.Error(err,msg:"创建pod失败")
return reconcile.Result{},err
}

}

相关文章

  • 2021-05-01-2日

    windows安装参考https://cloud.tencent.com/developer/article/15...

  • 日根日根日根日。

    昨天,昨天并没有偷懒,是因为系统服务器不好。 昨天,昨天并没有偷懒,是因为系统服务器不好。 昨天更新的时候已经12...

  • 日读日行日思日写

    作为一个写作小白,常有写不出东西的“无力感”。记得学生时代还曾写过很多诗,现在回想起来,那时正是我读书最多...

  • 日复日

    睡了 跑到明天 醒了 来到昨天

  • 《日复日》

    清晨微风拂面,白云悠悠 广播体操的悦耳音乐响起 如同顽皮的小精灵雀跃于天空 一跳一跳的围绕我们的周身 俏皮的望着我...

  • 日曜日

    已经很累了,好想停下来歇一会。可是攀爬在陡峭的崖壁,回首即是万丈深渊。

  • 日了日

    天上的云真好看 真好看 遮住了发情的太阳 不过这他妈是谁 把这片云日的高潮不断 激情后又带它消失不见 留这浪荡的日...

  • 日复日

    日复日 年复年 往事俱成烟 故人何处去 天涯无踪现 待到雁南归 壶酒伴月残 日复日 年复年 黄土殁红颜 春风等闲...

  • 日曜日

    命运馈赠的背后都有明码标价 斯人若彩虹,遇上方知有 大概果真如此吧

  • 日曜日

    海纳百川,有容乃大 壁立千仞,无欲则刚

网友评论

      本文标题:2021-05-01-2日

      本文链接:https://www.haomeiwen.com/subject/vtoarltx.html