Skip to content

Commit a58c68a

Browse files
committed
fix: remove md which can not render.
1 parent 45d5103 commit a58c68a

2 files changed

Lines changed: 2 additions & 230 deletions

File tree

.vitepress/config.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { defineConfig } from 'vitepress'
44
export default defineConfig({
55
title: "Kubernetes 练习手册",
66
description: "A tutorials for k8s",
7+
srcExclude: ['**/README.md'],
78
themeConfig: {
89
// https://vitepress.dev/reference/default-theme-config
910
nav: [

docs/helm.md

Lines changed: 1 addition & 230 deletions
Original file line numberDiff line numberDiff line change
@@ -90,236 +90,7 @@ curl http://192.168.59.100/hello
9090

9191
## 创建 helm charts
9292

93-
在使用已经创建好的 hello-helm charts 来创建整个 hellok8s 资源后,你可能还是有很多的疑惑,包括 Chart 是如何生成和发布的,如何创建一个新的 Chart?在这节教程中,我们会尝试自己来创建 hello-helm Chart 来完成之前的操作。
94-
95-
首先建议使用 `helm create` 命令来创建一个初始的 Chart,该命令默认会创建一些 k8s 资源定义的初始文件,并且会生成官网推荐的目录结构,如下所示:
96-
97-
```shell
98-
helm create hello-helm
99-
100-
.
101-
├── Chart.yaml
102-
├── charts
103-
├── templates
104-
│   ├── NOTES.txt
105-
│   ├── _helpers.tpl
106-
│   ├── deployment.yaml
107-
│   ├── hpa.yaml
108-
│   ├── ingress.yaml
109-
│   ├── service.yaml
110-
│   ├── serviceaccount.yaml
111-
│   └── tests
112-
│   └── test-connection.yaml
113-
└── values.yaml
114-
```
115-
116-
我们将默认生成在 templates 目录下面的 `yaml` 文件删除,用之前教程中 `yaml` 文件替换它,最终的结构长这样:
117-
118-
```shell
119-
.
120-
├── Chart.yaml
121-
├── Dockerfile
122-
├── _helpers.tpl
123-
├── charts
124-
├── hello-helm-0.1.0.tgz
125-
├── index.yaml
126-
├── main.go
127-
├── templates
128-
│   ├── hellok8s-configmaps.yaml
129-
│   ├── hellok8s-deployment.yaml
130-
│   ├── hellok8s-secret.yaml
131-
│   ├── hellok8s-service.yaml
132-
│   ├── ingress.yaml
133-
│   ├── nginx-deployment.yaml
134-
│   └── nginx-service.yaml
135-
└── values.yaml
136-
```
137-
138-
其中 `main.go` 定义的是 `hellok8s:v6` 版本的代码,主要是从系统中拿到 message,namespace,dbURL,dbPassword 这几个环境变量,拼接成字符串返回。
139-
140-
```go
141-
package main
142-
143-
import (
144-
"fmt"
145-
"io"
146-
"net/http"
147-
"os"
148-
)
149-
150-
func hello(w http.ResponseWriter, r *http.Request) {
151-
host, _ := os.Hostname()
152-
message := os.Getenv("MESSAGE")
153-
namespace := os.Getenv("NAMESPACE")
154-
dbURL := os.Getenv("DB_URL")
155-
dbPassword := os.Getenv("DB_PASSWORD")
156-
157-
io.WriteString(w, fmt.Sprintf("[v6] Hello, Helm! Message from helm values: %s, From namespace: %s, From host: %s, Get Database Connect URL: %s, Database Connect Password: %s", message, namespace, host, dbURL, dbPassword))
158-
}
159-
160-
func main() {
161-
http.HandleFunc("/", hello)
162-
http.ListenAndServe(":3000", nil)
163-
}
164-
```
165-
166-
为了让大家更加了解 helm charts values 的使用和熟悉 k8s 资源配置,这几个环境变量 `MESSAGE`, `NAMESPACE`, `DB_URL`, `DB_PASSWORD` 分别有不同的来源。
167-
168-
首先修改根目录下的 `values.yaml` 文件,定义自定义的配置信息,从之前教程的 k8s 资源文件中,将一些易于变化的参数提取出来,放在 `values.yaml` 文件中。全部配置信息如下所示:
169-
170-
```yaml
171-
application:
172-
name: hellok8s
173-
hellok8s:
174-
image: guangzhengli/hellok8s:v6
175-
replicas: 3
176-
message: "It works with Helm Values!"
177-
database:
178-
url: "http://DB_ADDRESS_DEFAULT"
179-
password: "db_password"
180-
nginx:
181-
image: nginx
182-
replicas: 2
183-
```
184-
185-
那自定义好了这些配置后,如何在 k8s 资源定义中使用这些配置信息呢?Helm 默认使用 [Go template 的方式](https://helm.sh/zh/docs/howto/charts_tips_and_tricks/) 来完成。
186-
187-
例如之前教程中,将环境变量 `DB_URL` 定义在 k8s configmaps 中,那么该资源可以定义成如文件所示 `hellok8s-configmaps.yaml`。其中 `metadata.name` 的值是 `{{ .Values.application.name }}-config`,意思是从 `values.yaml` 文件中获取 `application.name` 的值 `hellok8s`,拼接 `-config` 字符串,这样创建出来的 configmaps 资源名称就是 `hellok8s-config`。
188-
189-
同理 `{{ .Values.application.hellok8s.database.url }}` 就是获取值为 `http://DB_ADDRESS_DEFAULT` 放入 configmaps 对应 key 为 DB_URL 的 value 中。
190-
191-
```yaml
192-
apiVersion: v1
193-
kind: ConfigMap
194-
metadata:
195-
name: {{ .Values.application.name }}-config
196-
data:
197-
DB_URL: {{ .Values.application.hellok8s.database.url }}
198-
```
199-
200-
上面定义的最终效果和之前在 `configmaps` 教程中定义的资源没有区别,这种做的好处是可以将所有可变的参数定义在 `values.yaml` 文件中,使用该 helm charts 的人无需了解具体 k8s 的定义,只需改变成自己想要的参数,即可创建自定义的资源!
201-
202-
同样,我们可以根据之前的教程将 `DB_PASSWORD` 放入 secret 中,并且通过 `b64enc` 方法将值 Base64 编码。
203-
204-
```shell
205-
# hellok8s-secret.yaml
206-
apiVersion: v1
207-
kind: Secret
208-
metadata:
209-
name: {{ .Values.application.name }}-secret
210-
data:
211-
DB_PASSWORD: {{ .Values.application.hellok8s.database.password | b64enc }}
212-
```
213-
214-
最后,修改 `hellok8s-deployment` 文件,根据前面的教程,将 `metadata.name` `replicas` `image` `configMapKeyRef.name` `secretKeyRef.name` 等值修改成从 `values.yaml` 文件中获取。
215-
216-
再添加代码中需要的 `NAMESPACE` 环境变量,从 `.Release.Namespace` [内置对象](https://helm.sh/zh/docs/chart_template_guide/builtin_objects/) 中获取。最后添加 `MESSAGE` 环境变量,直接从 `{{ .Values.application.hellok8s.message }}` 中获取。
217-
218-
```yaml
219-
apiVersion: apps/v1
220-
kind: Deployment
221-
metadata:
222-
name: {{ .Values.application.name }}-deployment
223-
spec:
224-
replicas: {{ .Values.application.hellok8s.replicas }}
225-
selector:
226-
matchLabels:
227-
app: hellok8s
228-
template:
229-
metadata:
230-
labels:
231-
app: hellok8s
232-
spec:
233-
containers:
234-
- image: {{ .Values.application.hellok8s.image }}
235-
name: hellok8s-container
236-
env:
237-
- name: DB_URL
238-
valueFrom:
239-
configMapKeyRef:
240-
name: {{ .Values.application.name }}-config
241-
key: DB_URL
242-
- name: DB_PASSWORD
243-
valueFrom:
244-
secretKeyRef:
245-
name: {{ .Values.application.name }}-secret
246-
key: DB_PASSWORD
247-
- name: NAMESPACE
248-
value: {{ .Release.Namespace }}
249-
- name: MESSAGE
250-
value: {{ .Values.application.hellok8s.message }}
251-
```
252-
253-
修改 `ingress.yaml` 将 `metadata.name` 的值,其它没有变化
254-
255-
``` yaml
256-
apiVersion: networking.k8s.io/v1
257-
kind: Ingress
258-
metadata:
259-
name: {{ .Values.application.name }}-ingress
260-
...
261-
...
262-
...
263-
```
264-
265-
`nginx-deployment.yaml`
266-
267-
```yaml
268-
apiVersion: apps/v1
269-
kind: Deployment
270-
metadata:
271-
name: nginx-deployment
272-
spec:
273-
replicas: {{ .Values.application.nginx.replicas }}
274-
selector:
275-
matchLabels:
276-
app: nginx
277-
template:
278-
metadata:
279-
labels:
280-
app: nginx
281-
spec:
282-
containers:
283-
- image: {{ .Values.application.nginx.image }}
284-
name: nginx-container
285-
```
286-
287-
`nginx-service.yaml` 和 `hellok8s-service.yaml` 没有变化。所有代码可以在 [这里](https://github.com/guangzhengli/k8s-tutorials/tree/main/helm-charts/hello-helm) 查看。
288-
289-
稍微修改下默认生成的`Chart.yaml`
290-
291-
```yaml
292-
apiVersion: v2
293-
name: hello-helm
294-
description: A k8s tutorials in https://github.com/guangzhengli/k8s-tutorials
295-
type: application
296-
version: 0.1.0
297-
appVersion: "1.16.0"
298-
```
299-
300-
定义完成所有的 helm 资源后,首先**将 `hellok8s:v6` 镜像打包推送到 DockerHub**。
301-
302-
之后即可在 `hello-helm` 的目录下执行 `helm upgrade` 命令进行安装,安装成功后,执行 curl 命令便能直接得到结果!查看 pod 和 service 等资源,便会发现 helm 能一键安装所有资源!
303-
304-
```shell
305-
helm upgrade --install hello-helm --values values.yaml .
306-
# Release "hello-helm" does not exist. Installing it now.
307-
# NAME: hello-helm
308-
# NAMESPACE: default
309-
# STATUS: deployed
310-
# REVISION: 1
311-
312-
curl http://192.168.59.100/hello
313-
# [v6] Hello, Helm! Message from helm values: It works with Helm Values!, From namespace: default, From host: hellok8s-deployment-57d7df7964-m6gcc, Get Database Connect URL: http://DB_ADDRESS_DEFAULT, Database Connect Password: db_password
314-
315-
kubectl get pods
316-
# NAME READY STATUS RESTARTS AGE
317-
# hellok8s-deployment-f88f984c6-k8hpz 1/1 Running 0 32m
318-
# hellok8s-deployment-f88f984c6-nzwg6 1/1 Running 0 32m
319-
# hellok8s-deployment-f88f984c6-s89s7 1/1 Running 0 32m
320-
# nginx-deployment-d47fd7f66-6w76b 1/1 Running 0 32m
321-
# nginx-deployment-d47fd7f66-tsqj5 1/1 Running 0 32m
322-
```
93+
这段代码无法渲染,请查看:https://github.com/guangzhengli/k8s-tutorials#helm
32394

32495
## rollback
32596

0 commit comments

Comments
 (0)