美文网首页
kubebuilder(5)制作镜像&部署

kubebuilder(5)制作镜像&部署

作者: 运维开发笔记 | 来源:发表于2024-04-28 20:20 被阅读0次

<h2><span/><span>制作镜像</span><span/><span> </span></h2><p>好了,前面是使用make run进行测试运行。现在我们把operator打出镜像进行分发。</p><p>先修改一下Dockerfile,否则可能下载依赖有问题</p>ENV GO111MODULE=on
ENV GOPROXY=https://goproxy.cn,direct
<p>然后,默认的这个FROM gcr.io/distroless/static:nonroot也是下不到的</p><p>替换成这个</p>anjia0532/distroless.static:nonroot
<div class="image-package"><img src="https://img.haomeiwen.com/i5149787/9872b0b3f4a3d742.jpeg" img-data="{"format":"jpeg","size":72339,"width":838,"height":815,"space":"srgb","channels":3,"depth":"uchar","density":72,"chromaSubsampling":"4:2:0","isProgressive":false,"hasProfile":false,"hasAlpha":false}" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/>
</div><p>我这里是自己搭的私有harbor</p>make docker-build docker-push IMG=harbor-test.xxx.net/paas/demo-operator:1.0
<div class="image-package"><img src="https://img.haomeiwen.com/i5149787/03a5ef88ccb67d29.jpeg" img-data="{"format":"jpeg","size":54193,"width":827,"height":364,"space":"srgb","channels":3,"depth":"uchar","density":72,"chromaSubsampling":"4:2:0","isProgressive":false,"hasProfile":false,"hasAlpha":false}" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/>
</div><h2><span/><span>部署operator镜像</span><span/><span> </span></h2><p>部署有两种方案</p><h3><span/><span>make deploy</span><span/></h3><p>使用项目自带的deploy指令,这种方式是将operator部署到本地集群中,其实和make run差不多</p>make deploy IMG=harbor-test.xxx.net/paas/demo-operator:1.0
<p>也可修改~/.kube/config来连接其他集群,但还是太麻烦。</p>kustomize build config/default | kubectl apply -f -
namespace/demo-operator-system created
customresourcedefinition.apiextensions.k8s.io/demoes.tutorial.demo.com unchanged
serviceaccount/demo-operator-controller-manager created
role.rbac.authorization.k8s.io/demo-operator-leader-election-role created
clusterrole.rbac.authorization.k8s.io/demo-operator-manager-role created
clusterrole.rbac.authorization.k8s.io/demo-operator-metrics-reader created
clusterrole.rbac.authorization.k8s.io/demo-operator-proxy-role created
rolebinding.rbac.authorization.k8s.io/demo-operator-leader-election-rolebinding created
clusterrolebinding.rbac.authorization.k8s.io/demo-operator-manager-rolebinding created
clusterrolebinding.rbac.authorization.k8s.io/demo-operator-proxy-rolebinding created
configmap/demo-operator-manager-config created
service/demo-operator-controller-manager-metrics-service created
deployment.apps/demo-operator-controller-manager created
<p>查看部署情况</p><div class="image-package"><img src="https://img.haomeiwen.com/i5149787/cc197699b41ce8dd.jpeg" img-data="{"format":"jpeg","size":39063,"width":1080,"height":231,"space":"srgb","channels":3,"depth":"uchar","density":72,"chromaSubsampling":"4:2:0","isProgressive":false,"hasProfile":false,"hasAlpha":false}" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/>
</div><p>查看一下pod的日志</p><div class="image-package"><img src="https://img.haomeiwen.com/i5149787/656db5495ff6177d.jpeg" img-data="{"format":"jpeg","size":61872,"width":1080,"height":227,"space":"srgb","channels":3,"depth":"uchar","density":72,"chromaSubsampling":"4:2:0","isProgressive":false,"hasProfile":false,"hasAlpha":false}" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/>
</div><p>我们再部署一个demo测试一下</p>kubectl apply -f config/samples/tutorial_v1_demo.yaml
<div class="image-package"><img src="https://img.haomeiwen.com/i5149787/b381c98e888c4242.jpeg" img-data="{"format":"jpeg","size":59986,"width":1080,"height":250,"space":"srgb","channels":3,"depth":"uchar","density":72,"chromaSubsampling":"4:2:0","isProgressive":false,"hasProfile":false,"hasAlpha":false}" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/>
</div><p>执行调谐完成</p><p>如果你的部署遇到问题,可能会遇到镜像下载不下来的问题。</p><p>原因还是gcr.io/kubebuilder/kube-rbac-proxy被墙了</p><p>改一下</p><div class="image-package"><img src="https://img.haomeiwen.com/i5149787/124d1cf804a99991.jpeg" img-data="{"format":"jpeg","size":29632,"width":1080,"height":261,"space":"srgb","channels":3,"depth":"uchar","density":72,"chromaSubsampling":"4:2:0","isProgressive":false,"hasProfile":false,"hasAlpha":false}" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/>
</div><h3><span/><span>yaml部署</span><span/></h3><p>我们需要的当然是把写的operator分发到别的集群部署。</p><p>通过分析make deploy脚本,来编写yaml</p>.PHONY: deploy
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
cd config/manager && (KUSTOMIZE) edit set image controller={IMG}
$(KUSTOMIZE) build config/default | kubectl apply -f -
<p>这个脚本的本质就是用kustomize对config下的manager和default中的yaml进行变量替换</p><p>然后整合成一个yaml,传给kubectl apply执行</p><p>所以啊,我们只要执行下这两行就可以得到我们想要的yaml文件,然后就可以随便到别的集群执行了哦</p>cd config/manager && /usr/local/bin/kustomize edit set image controller=harbor-test.xxx.net/paas/demo-operator:1.0

cd ../.. && /usr/local/bin/kustomize build config/default > demo-operator.yaml
<p>输出这样一个yaml</p>apiVersion: v1
kind: Namespace
metadata:
labels:
control-plane: controller-manager
name: demo-operator-system
---
太长了,不贴了
<p>去别的集群,部署试试</p><p>部署operator</p>kubectl apply -f demo-operator.yaml
<p>部署一个demo crd</p>kubectl apply -f demo-simple.yaml
<div class="image-package"><img src="https://img.haomeiwen.com/i5149787/1802de5b18bc939f.jpeg" img-data="{"format":"jpeg","size":22000,"width":690,"height":137,"space":"srgb","channels":3,"depth":"uchar","density":72,"chromaSubsampling":"4:2:0","isProgressive":false,"hasProfile":false,"hasAlpha":false}" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/>
</div><p>完成</p><p>
</p>

相关文章

网友评论

      本文标题:kubebuilder(5)制作镜像&部署

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