《kubebuilder 从零开始实战》 - 11. 使用 controllerutil 优化代码

使用 controllerutil 优化代码 代码在: https://github.com/tangx/kubebuilder-zero-to-one 在之前的代码中, 对于 OwnerReference 和 Finalizers 操作我们自己实现了一些方法。 其实这些操作官方已经封好成包了, 开箱即用。 复制 /controllers/helper 保存为 /controllers/helper2。 前者保存手工代码, 后者保存优化代码。 https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/controller/controllerutil Finalizers 操作 之前 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29……

阅读全文

《kubebuilder 从零开始实战》 - 12. 增加 k8s event 事件支持

增加 event 事件支持 代码在: https://github.com/tangx/kubebuilder-zero-to-one k8s 官方 controller 都实现了 Events 消息信息, 如下 1 2 3 4 5 6 7 kubectl describe deployment k8s-operator-demo-controller-manager Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 15m deployment-controller Scaled up replica set k8s-operator-demo-controller-manager-75cc59d8ff to 1 Normal ScalingReplicaSet 14m deployment-controller Scaled down replica set k8s-operator-demo-controller-manager-b9d9f7886 to 0 我们自定义的 Operator 同样可以实现。 operator 支持 event 在 /controllers/redis_controller.go 中定义 RedisReconcile 的时候, 添加 EventRecord 字段。 1 2 3 4 5 6 7 8 // RedisReconciler reconciles a Redis object type RedisReconciler struct { client.Client Scheme *runtime.Scheme // 添加事件 EventRecord record.EventRecorder } 在 /main.go 中, 创……

阅读全文

《kubebuilder 从零开始实战》 - 13. 添加 CRD 对象 Status 状态字段

添加 CRD 对象 Status 状态字段 代码在: https://github.com/tangx/kubebuilder-zero-to-one 添加 kd 状态字段 在 /api/v1/redis_types.go 的 RedisStatus 中添加需要展示的字段。 这里添加一个副本数量。 1 2 3 4 5 type RedisStatus struct { // INSERT ADDITIONAL STATUS FIELD - define observed state of cluster // Important: Run "make" to regenerate code after modifying this file Replicas int `json:"replicas"` } 偷懒, 没有在创建或删除 pod 时进行精细控制。 而是使用 defer 在 Reconcile 退出的时候进行一次最终的赋值管理。 1 2 3 4 5 6 7 8 9 10 11 12 13……

阅读全文

《kubebuilder 从零开始实战》 - 14. CRD 支持 kubectl scale 和 kubectl autoscale 命令

支持 kubectl scale 和 kubectl autoscale 命令 代码在: https://github.com/tangx/kubebuilder-zero-to-one 在 k8s 自定义资源中有关于 scale 和 hpa 的 subresources 字段, 只有这些字段被定义的时候才能支持 scale 和 autoscale 命令 官方定义如下 https://kubernetes.io/zh/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#scale-subresource 在 kubebuilde 中, 使用 //+kubebuilder:subresource:scale 增加注解, 生成对应的配置。 注意, 未知需要在 //+kubebuilder:subresource:status 下方 1 2 3 //+kubebuilder:object:root=true //+kubebuilder:subresource:status //+kubebuilder:subresource:scale:specpath=.spec.replicas,statuspath=.status.replicas,selectorpath=.status.selector 三个关键字段: specpath: specReplicasPath 指定定制资源内与 scale.spec.replicas 对应的 JSON 路径。 此字段为 必需值 。 只可以使用 .spec 下的 JSON……

阅读全文

Golang 库: 为什么 Golang slog 库不支持 Fatal 了

Golang 库: 为什么 Golang slog 库不支持 slog.Fatal API 原文链接: https://tangx.in/posts/2023/01/06/why-dont-golang-slog-support-fatal-api/ 使用 slog 默认不支持 Fatal 如果直接把 slog 当成 log 使用, 会有一点点头疼 1 2 3 4 5 6 7 8 9 10 11 func main() { slog.Debug("debug") slog.Info("info") slog.Warn("warn") slog.Error("err", fmt.Errorf("game over")) // slog.Fatal("don't support") } // 2023/01/06 07:41:50 INFO info // 2023/01/06 07:41:50 WARN warn // 2023/01/06 07:41:50 ERROR err err="game over" slog 默认日志级别 是 info, 无法输出 DEBUG 日志。 需要自定义 handler 实现日志级别判断。 参考 Golang 库: 怎么使用 golang slog 设置日志……

阅读全文

Golang 库: 怎么使用 golang slog 设置日志 Debug 等级

Golang 库: 怎么使用 golang slog 设置日志 Debug 等级 原文链接: https://tangx.in/posts/2023/01/06/how-to-set-debug-level-in-golang-slog/ 在 golang 中, 日志统一 一直都是一个头疼的问题。 在 exp 中, Go 加入了一个 新库 exp/slog , 希望能转正。 这里有一些关于 slog 的介绍, 可以参考 Go 十年了,终于想起要统一 log 库了! 使用 slog 习惯误区, 默认日志级别是 Info 如果直接把 slog 当成 log 使用, 可能又一点问题。 1 2 3 4 5 6 7 8 9 10……

阅读全文

腾讯企业邮箱收到不 Gmail 邮件, DNS 记录 CNAME 记录与 MX 记录冲突

腾讯企业邮箱收到不 Gmail 邮件, DNS 记录 CNAME 记录与 MX 记录冲突 原文链接: https://tangx.in/posts/2023/01/06/dns-record-cname-confilic-with-mx/ 为了统一, 我申请了腾讯企业邮箱, 绑定了本站的域名。 但是在测试邮件的时候, 却发现 企业邮箱 发送邮件 一切正常 可以收到来自国内邮箱的邮件, 例如 QQ, 163, 却 收不到 来自 Gmail 的邮件。 查询资料后, 确定是在 DNS 解析记录中, CNAME 与 MX 记录冲突。 由……

阅读全文

从零开始写 k8s 发布工具 - 1.0. kustz 介绍和设计思想

从零开始写 k8s 发布工具(1) - kustz 介绍和设计思想 介绍 如果要在 Kubernets 发布一个应用, 并对外提供服务, 需要配置诸如 Dep, Ing, Svc 等 Config API。 他们之间又是通过 Label 组合选择而实现的 松耦合。 如果想要这些 Config API 之间的关系更加紧密, 我们可以自己再向上抽象, 通过自己的配置将他们整合在一起。 更重要的是, 我们可以通过这层……

阅读全文

从零开始写 k8s 发布工具 - 2.1. 模仿 kubectl create 创建 Deployment 样例

2.1. 模仿 kubectl create 创建 Deployment 样例 为了简单, 我们假定所管理的 Deployment 都是 单容器 的。 首先参考 kubectl create 命令 1 $ kubectl create deployment my-dep --image=busybox --replicas 1 --dry-run=client -o yaml 安装 client-go API 访问 client-go https://github.com/kubernetes/client-go 1 $ go get k8s.io/[email protected] 这里直接选用最新版本 v0.25.4。 对于其他版本的兼容, 留在以后再做。 定义 Kustz Config 参考 kubectl create 命令, 创建配置文件 kustz.yml 结构如下 1 2 3 4 5 6 7 8 # kustz.yml namespace: demo-demo name: srv-webapp-demo service: name: nginx image: docker.io/library/nginx:alpine……

阅读全文

从零开始写 k8s 发布工具 - 2.2. 定义字符串创建 Service

2.2. 定义字符串创建 Service 大家好, 我是老麦, 一个小运维。 今天我们为 kustz 增加 service 解析功能。 通过 kubectl create service 命令可以看到, service 的模式还是挺多的。 1 2 3 4 5 6 7 8 9 10 11 $ kubectl create service -h Create a service using a specified subcommand. Aliases: service, svc Available Commands: clusterip Create a ClusterIP service externalname Create an ExternalName service loadbalancer Create a LoadBalancer service nodeport Create a NodePort service 除了以上列出来的四种之外, 还用一种 Headless Service( https://kubernetes.io/docs/concepts/services-networking/service/#headless-services )。 Headless Service 是当 类型 为 Clu……

阅读全文

福利派送

  • (免费星球)「运维成长路线」

  • 又拍云免费 CDN

最近文章

分类

标签

其它