自制申威架构k8s-reloader
# 背景
- 申威:是我国自主研发的高性能计算机芯片;
- 申威、龙芯等国产芯片的出现打破了国外的技术封锁和知识产权壁垒,实现我国高性能计算机的自主可控;
- 但是国产芯片发展较晚,大多数开源产品适配覆盖率较低;
# reloader
# 1. reloader 简介
github仓库地址:https://github.com/stakater/Reloader
- 在 k8s 中使用
ConfigMap
作为配置文件的时候会遇到一个问题:修改ConfigMap
后无法实现热部署,也就是更改了ConfigMap
配置文件后需要手动重启 Pod 配置才会生效;reloader 便是为了解决此问题的开源项目 - Reloader 可以观察 ConfigMap 和 Secret 中的变化,并对 pod 及其关联的
DeploymentConfigs
,Deployments
,Daemonsets
,Statefulsets
和Rollouts
进行滚动升级
# 2. 实现原理
当 Reloader 检测到 ConfigMap 发生变化的时候,会使用 SHA1 计算 ConfigMap 的哈希值(使用 SHA1 是因为它高效且不易发生冲突),计算完哈希值之后,Reloader 获取所有的
Deployments
,Daemonsets
,Statefulsets
和Rollouts
列表,并查找其 anotations 中是否配置了 Reloader 相关的注解,比如配置了如下 annotations :metadata: annotations: reloader.stakater.com/auto: "true"
1
2
3接着 Reloader 会查找配置了 Reloader 相关 annotations 的
Deployments
,Daemonsets
,Statefulsets
中一个特殊的环境变量如果找到这个环境变量,则获取其值并将其与前面计算的新 ConfigMap 哈希值进行比较,如果环境变量中的旧值与新哈希值不同,则 Reloader 会更新环境变量
如果环境变量不存在,那么它会从 ConfigMap 创建一个具有最新哈希值的新环境变量并更新相关的
deployment
,daemonset
或者statefulset
k8s 检测到这个环境变量发生变化,则会触发 pod 关联的
deployment
,daemonset
或者statefulset
的滚动升级
# 3. 安装使用
- 参考:https://github.com/stakater/Reloader
# sw架构reloader镜像制作
# 1. 环境
系统
[root@host-20-189 ~]# uname -a Linux host-20-189 5.10.0-46.3.uel20.jk.sw_64 #1 SMP Fri Jul 21 00:21:02 CST 2023 sw_64 sw_64 sw_64 GNU/Linux
1
2docker-sw
k8s环境
golang-sw
- GOOS=linux
- GOARCH=sw64
基础镜像
- linux/sw64 基础镜像
- go-sw 基础镜像
# 2. 构建可执行文件
示例采用tag:v0.0.118
拉取代码
git clone -b ${你想要的分支} https://github.com/stakater/Reloader.gitxxxxxxxxxx git clone git clone https://github.com/stakater/Reloader.git
1解决依赖
编辑go.mod, go-sw适配, 添加以下代码
replace ( golang.org/x/net => ${你自己的地址}/net v0.0.0-20231031065517-85b08744b3ac golang.org/x/sys => ${你自己的地址}/sys v0.0.0-20231031064032-abfdf9d2148e )
1
2
3
4执行
go mod tidy
;
构建二进制文件
CGO_ENABLED=0 GOOS=linux GOARCH=sw64 GOPROXY="https://proxy.golang.org,direct" GOPRIVATE="" GO111MODULE=on go build -mod=mod -a -o manager main.go
1
# 3. 镜像构建
制作Dockerfile
FROM hub.deepin.com/military/library/minbase:1050-slim COPY manager manager USER 65532:65532 EXPOSE 9090 ENTRYPOINT ["/manager"]
1
2
3
4
5
6
7
8
9构建镜像:
docker build . -t k8s-reloader:test
# reloader pod启动
# 1. reloader.yml 制作
官方yml:https://raw.githubusercontent.com/stakater/Reloader/master/deployments/kubernetes/reloader.yaml
改造官方yml
- 修改所有标签值
- 修改镜像名称(自己构建的reloader镜像:k8s-reloader:test);
- 其他配置自行更改
示例(reload版本v0.0.118):
--- # Source: reloader/templates/serviceaccount.yaml apiVersion: v1 kind: ServiceAccount metadata: annotations: meta.helm.sh/release-namespace: "default" meta.helm.sh/release-name: "reloader" labels: app: reloader-reloader chart: "reloader-1.0.51" release: "reloader" heritage: "Helm" app.kubernetes.io/managed-by: "Helm" name: reloader-reloader namespace: default --- # Source: reloader/templates/clusterrole.yaml apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: annotations: meta.helm.sh/release-namespace: "default" meta.helm.sh/release-name: "reloader" labels: app: reloader-reloader chart: "reloader-1.0.51" release: "reloader" heritage: "Helm" app.kubernetes.io/managed-by: "Helm" name: reloader-reloader-role rules: - apiGroups: - "" resources: - secrets - configmaps verbs: - list - get - watch - apiGroups: - "apps" resources: - deployments - daemonsets - statefulsets verbs: - list - get - update - patch - apiGroups: - "extensions" resources: - deployments - daemonsets verbs: - list - get - update - patch - apiGroups: - "batch" resources: - cronjobs verbs: - list - get - apiGroups: - "batch" resources: - jobs verbs: - create - apiGroups: - "" resources: - events verbs: - create - patch --- # Source: reloader/templates/clusterrolebinding.yaml apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: annotations: meta.helm.sh/release-namespace: "default" meta.helm.sh/release-name: "reloader" labels: app: reloader-reloader chart: "reloader-1.0.51" release: "reloader" heritage: "Helm" app.kubernetes.io/managed-by: "Helm" name: reloader-reloader-role-binding roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: reloader-reloader-role subjects: - kind: ServiceAccount name: reloader-reloader namespace: default --- # Source: reloader/templates/deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: annotations: meta.helm.sh/release-namespace: "default" meta.helm.sh/release-name: "reloader" labels: app: reloader-reloader chart: "reloader-1.0.51" release: "reloader" heritage: "Helm" app.kubernetes.io/managed-by: "Helm" group: com.stakater.platform provider: stakater version: v1.0.51 name: reloader-reloader namespace: default spec: replicas: 1 revisionHistoryLimit: 2 selector: matchLabels: app: reloader-reloader release: "reloader" template: metadata: labels: app: reloader-reloader chart: "reloader-1.0.51" release: "reloader" heritage: "Helm" app.kubernetes.io/managed-by: "Helm" group: com.stakater.platform provider: stakater version: v1.0.51 spec: containers: - image: "k8s-reloader:test" # 镜像名称修改 imagePullPolicy: IfNotPresent name: reloader-reloader ports: - name: http containerPort: 9090 livenessProbe: httpGet: path: /live port: http timeoutSeconds: 5 failureThreshold: 5 periodSeconds: 10 successThreshold: 1 initialDelaySeconds: 10 readinessProbe: httpGet: path: /metrics port: http timeoutSeconds: 5 failureThreshold: 5 periodSeconds: 10 successThreshold: 1 initialDelaySeconds: 10 securityContext: {} securityContext: runAsNonRoot: true runAsUser: 65534 serviceAccountName: reloader-reloader
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# 2. pod 启动
启动reloader pod
kubectl apply -f reloader.yml
1查看
kubectl get pods -A |grep reloader # default reloader-reloader-57f68d6848-m2j9r 1/1 Running 23 68m
1
2
# 可能的问题
# 1. go mod 依赖问题
如果在sw机器出现go mod依赖问题可以按照以下思路解决:
首先在本地解决go依赖
然后生成vendor
go mod tidy
将reloader项目拷贝到sw机器上
执行构建命令:
CGO_ENABLED=0 GOOS=linux GOARCH=sw64 GOPROXY="https://proxy.golang.org,direct" GOPRIVATE="" GO111MODULE=on go build -mod=vendor -a -o manager main.go
1
# 2. sw机器没有go-sw环境
如果在sw机器没有go-sw
运行go-sw基础镜像并进入
docker run -it -v /root/swsp/k8s/Reloader/:/opt/ ${go-sw镜像} bash
1在容器内部构建出Reloader二进制文件
拷贝Reloader二进制文件到物理机
docker copy ${go-sw镜像}/opt/Reloader/manager ./
1