开发 k8s 管理平台 - k8sailor 05. RESTFul API 接口规范与请求应答约定

开发 k8s 管理平台 - k8sailor 05. RESTFul API 接口规范与请求应答约定 原文地址: https://tangx.in/posts/books/k8sailor/chapter01/05-design-restful-api-and-response-data/ tag: https://github.com/tangx/k8sailor/tree/feat/05-design-restful-api-and-response-data 强烈建议使用 RESTful 风格来设计 API 文档。 RESTful api 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 # kubectl create deployment nginx-tools --image nginx:alpine --output=yaml --dry-run=client apiVersion: apps/v1 kind: Deployment metadata: creationTimestamp: null labels: app: nginx-tools name: nginx-tools # ... 省略 # kubectl create namespace hello --dry-run=client -o yaml apiVersion: v1 kind: Namespace metadata: creationTimestamp: null name: hello # ... 省略 可以看到, k8s api 中都有一个对应的 kind 描述资源类型, 这个正好符……

阅读全文

开发 k8s 管理平台 - k8sailor 04. 使用 gin 创建第一个 API 接口

开发 k8s 管理平台 - k8sailor 04. 使用 gin 创建第一个 API 接口 原文地址: https://tangx.in/posts/books/k8sailor/chapter01/04-init-httpserver/ tag: https://github.com/tangx/k8sailor/tree/feat/04-httpserver-initial 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 cd cmd/k8sailor && go run . httpserver 启动 web 服务器 Usage: k8sailor httpserver [flags] Flags: -h, --help help for httpserver Global Flags: --config string k8s 配置授权文件 (default "./k8sconfig/config.yml") 2021/09/24 07:56:51 open config/local.yml: no such file or directory [GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached. [GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production. - using env: export GIN_MODE=release - using code: gin.SetMode(gin.ReleaseMode) [GIN-debug] GET /k8sailor/v0/ping --> github.com/tangx/k8sailor/cmd/k8sailor/apis.RootGroup.func1 (3 handlers) [GIN-debug] Listening……

阅读全文

开发 k8s 管理平台 - k8sailor 03. 使用 client-go sdk 链接集群

开发 k8s 管理平台 - k8sailor 03. 使用 client-go sdk 链接集群 原文地址: https://tangx.in/posts/books/k8sailor/chapter01/03-connect-cluster/ tag: https://github.com/tangx/k8sailor/tree/feat/02-connect-cluster 使用 sdk 链接 k3s cluster 并获取 deployment 信息 1 2 3 4 cd cmd/k8sailor && go run . * my-nginx-1 (1 replicas) * my-nginx-2 (2 replicas) 下载 client-go sdk 之前在安装 k3s 集群的时候,版本是 v0.21.4。 因此。 这里选择 client-go sdk 的版本也是 v0.21.4 如果还有其他环境, 可以使用 go mod edit 命令锁定 client-go 的版本 1 2 3 go get k8s.io/[email protected] go mod edit -replace=k8s.io/client-go=k8s.io/[email protected] 连接集群并获取……

阅读全文

开发 k8s 管理平台 - k8sailor 02. 使用 cobra 初始化程序命令与参数

开发 k8s 管理平台 - k8sailor 02. 使用 cobra 初始化程序命令与参数 原文地址: https://tangx.in/posts/books/k8sailor/chapter01/02-design-cobra-command/ tag: https://github.com/tangx/k8sailor/tree/feat/01-cobra-command 为了更加方便的管理配置文件的来源, 这里使用 cobra 进行命令行构建 效果如下 1 2 3 4 5 6 7 8 9 cd cmd/k8sailor && go run . k8s 管理平台 Usage: k8sailor [flags] Flags: --config string k8s 配置授权文件 (default "./k8sconfig/config.yml") -h, --help help for k8sailor 编码 变量管理 在 cmd/k8sailor/global 目录中管理 全局 变量。 其中,定义一个 CmdFlag 结构体管理所有 cobra fl……

阅读全文

开发 k8s 管理平台 - k8sailor - 01. 使用 k3s 快速搭建项目环境

开发 k8s 管理平台 - k8sailor - 01. 使用 k3s 快速搭建项目环境 原文地址: https://tangx.in/posts/books/k8sailor/chapter01/01-install-k3s-cluster/ 安装 k3s 安装过程参考 https://tangx.in/2021/06/07/k3s-architecture-single-server/ k3s 集群版本为 v1.21.4。 因此 k8s client-go sdk 的版本也需要安装对应版本 1 2 3 4 5 6 7 8 9 10 11 # curl -sfL http://rancher-mirror.cnrancher.com/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh - [INFO] Finding release for channel stable [INFO] Using v1.21.4+k3s1 as release [INFO] Downloading hash http://rancher-mirror.cnrancher.com/k3s/v1.21.4-k3s1/sha256sum-amd64.txt [INFO] Downloading binary http://rancher-mirror.cnrancher.com/k3s/v1.21.4-k3s1/k3s [INFO] Verifying binary download [INFO] Installing k3s to /usr/local/bin/k3s ... 省略 初始化环境 通过命令创建一些工作负载, 以便后……

阅读全文

这些关于 Golang timezone 时区的坑, 我已经帮你踩过了

这些关于 Golang timezone 时区的坑, 我已经帮你踩过了 原文链接: https://tangx.in/posts/2023/01/09/golang-timezone-issue/ Golang 中一些不太注意的时区问题 1. time/tzdata 库 Golang 内置的一个时区文件。 可以在程序中任意位置被导入。 导入后, 如果程序 找不到本地 时区文件, 就会使用该库的数据。 本地 指的是 运行环境, 可能是实际的服务器, 也可能是容器。 通常, 应该在 main.go 中被导入。 如果是 库代码 则……

阅读全文

《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……

阅读全文

《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 从零开始实战》 - 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 从零开始实战》 - 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……

阅读全文

福利派送

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

  • 又拍云免费 CDN

最近文章

分类

标签

其它