go, k8s

基于helm的operator

使用Operator SDK创建一个基于helm的nginx-operator。

初始化

mkdir nginx-operator
cd nginx-operator
operator-sdk init --domain wgh9626 --plugins helm
# --domain 将用作将在其中创建自定义资源的 API 组的前缀,注意格式。
# FATA[0000] failed to create API: unable to inject the resource to "base.helm.sdk.operatorframework.io/v1": either Group or Domain is invalid: [a DNS-1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'my-name',  or 'abc-123', regex used for validation is '[a-z0-9](?:[-a-z0-9]*[a-z0-9])?(?:\.[a-z0-9](?:[-a-z0-9]*[a-z0-9])?)*')]

file

创建api

operator-sdk create api --group demo --version v1alpha1 --kind Nginx

file

构建并推送镜像,我的k8s版本是1.24.16,用的containerd,所以需要修改Makefile中的docker命令为nerdctl。

vim Makefile
make docker-build docker-push IMG="registry-1.docker.io/wgh9626/nginx-operator:v0.0.1"

file

file

下面有两种部署方式,一种是通过olm部署,一种是直接部署。

olm部署

安装olm。

operator-sdk olm install

file

file

绑定operator,构建并推送镜像。同样需要修改docker命令。和上面初始化时make docker-build docker-push效果是一样的。

vim Makefile
# 注意这里只需要修改bundle-build中的docker命令,bundle-push中的docker-push上面已经修改过了。
.PHONY: bundle-build
bundle-build: ## Build the bundle image.
        nerdctl build -f bundle.Dockerfile -t $(BUNDLE_IMG) .

make bundle IMG="registry-1.docker.io/wgh9626/nginx-operator:v0.0.1"
make bundle-build bundle-push IMG="registry-1.docker.io/wgh9626/nginx-operator:v0.0.1"

file

注意这里推送会报错找不到repository,因为推送的镜像不对。

file

查看Makefile,BUNDLE_IMG使用的镜像名为初始化时的domain,而dockerhub这个域名现在是registry-1.docker.io,不是docker.io了,所以推送失败。要想使用这个命令推送的话需要修改IMAGE_TAG_BASE。但是上面已经推送过了,这里也就不用再执行了。

file

运行bundle,注意这里的镜像时bundle镜像,而不是operator镜像。

operator-sdk run bundle registry-1.docker.io/wgh9626/nginx-operator-bundle:v0.0.1
# 创建示例 Nginx 自定义资源:
kubectl apply -f config/samples/demo_v1alpha1_nginx.yaml
# 卸载
operator-sdk cleanup nginx-operator

这里可能会出现拉取镜像失败的报错,需要科学上网。

file

再次运行,如果报错安装计划不可用就是node节点上pod拉取镜像失败。

file

file

node节点同样设置代理,再次运行。发现仍然报错镜像无法拉取,我设置了dockerproxy和阿里云的镜像加速器,都无法拉取镜像。搜索了下发现是由于神秘力量dockerhub被封了,导致国内的镜像加速都无法使用了…

使用这个代理:https://github.com/ketches/registry-proxy?tab=readme-ov-file, 在集群中部署registry-proxy

export VERSION=$(curl -s https://api.github.com/repos/ketches/registry-proxy/releases/latest | jq -r .tag_name)

kubectl apply -f https://raw.githubusercontent.com/ketches/registry-proxy/$VERSION/deploy/manifests.yaml

# 代理地址
kubectl apply -f https://ghproxy.ketches.cn/https://raw.githubusercontent.com/ketches/registry-proxy/$VERSION/deploy/manifests.yaml

file

再次部署成功。

file

file

直接部署

make deploy IMG="registry-1.docker.io/wgh9626/nginx-operator:v0.0.1"
# 创建示例 Nginx 自定义资源:
kubectl apply -f config/samples/demo_v1alpha1_nginx.yaml
# 卸载
make undeploy
0 0 投票数
文章评分
订阅评论
提醒
guest

0 评论
内联反馈
查看所有评论

相关文章

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部
0
希望看到您的想法,请您发表评论x