Skip to content

Commit 4dfa13b

Browse files
committed
[helm] feat: add helm docs for helm upgrade hello-helm.
1 parent 1747c14 commit 4dfa13b

2 files changed

Lines changed: 349 additions & 166 deletions

File tree

README.md

Lines changed: 176 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<h4 align=center>🌈 Kubernetes | 📰 Tutorials</h4>
66

7-
k8s 作为云原生时代的操作系统,学习它的必要性不言而喻,如果你遇到了任何问题,可以在 [Discussions](https://github.com/guangzhengli/k8s-tutorials/discussions) 中评论或者 Issue 中提出,如果你觉得这个仓库对你有价值,欢迎 start 或者提 PR / Issue,让它变得更好!
7+
k8s 作为云原生时代的操作系统,学习它的必要性不言而喻,如果你遇到了任何问题,可以在 [Discussions](https://github.com/guangzhengli/k8s-tutorials/discussions) 中评论或者 Issue 中提出,如果你觉得这个仓库对你有价值,欢迎 star 或者提 PR / Issue,让它变得更好!
88

99
在学习本教程前,需要注意本教程侧重于实战引导,以渐进式修改代码的方式,将从最基础的 container 容器的定义开始,经过 `pod`, `deployment`, `servcie`, `ingress`, `configmap`, `secret` 等资源直到用 `helm` 来打包部署一套完整服务。所以如果你对容器和 k8s 的基础理论知识不甚了解的话,建议先从 [官网文档](https://kubernetes.io/zh-cn/docs/home/) 或者其它教程获取基础理论知识,再通过实战加深对知识的掌握!
1010

@@ -57,7 +57,8 @@ k8s 作为云原生时代的操作系统,学习它的必要性不言而喻,
5757
- [Helm(TODO)](#helmtodo)
5858
- [安装 hellok8s chart 快速开始(TODO)](#安装-hellok8s-chart-快速开始todo)
5959
- [创建 helm charts](#创建-helm-charts)
60-
- [上传和下载其它 helm chart 使用 (TODO)](#上传和下载其它-helm-chart-使用-todo)
60+
- [rollback](#rollback)
61+
- [helm chart 打包和发布](#helm-chart-打包和发布)
6162
- [Dashboard(TODO)](#dashboardtodo)
6263
- [K9s(TODO)](#k9stodo)
6364

@@ -1570,64 +1571,138 @@ Helm 的使用方式可以解释为:Helm 安装 *charts* 到 Kubernetes 集群
15701571

15711572
### 创建 helm charts
15721573

1573-
我们来手把手创建一个自己的 helm charts,完成我们之前的那些功能
1574+
在使用已经创建好的 hello-helm charts 来创建整个 hellok8s 资源后,你可能还是有很多的疑惑,包括 Chart 是如何生成和发布的,如何创建一个新的 Chart?在这节教程中,我们会尝试自己来创建 hello-helm Chart 来完成之前的操作
15741575

1575-
使用 `helm create` 命令默认会帮你创建一些 k8s 资源定义的初始文件,如下所示:
1576+
首先建议使用 `helm create` 命令来创建 Chart,默认会帮你创建一些 k8s 资源定义的初始文件,并且会生成官网推荐的目录结构,如下所示:
15761577

1577-
```
1578+
```shell
15781579
helm create hello-helm
15791580

15801581
.
15811582
├── Chart.yaml
1582-
├── _helpers.tpl
15831583
├── charts
15841584
├── templates
1585-
│   ├── hellok8s-configmaps.yaml
1586-
│   ├── hellok8s-deployment.yaml
1587-
│   ├── hellok8s-service.yaml
1585+
│   ├── NOTES.txt
1586+
│   ├── _helpers.tpl
1587+
│   ├── deployment.yaml
1588+
│   ├── hpa.yaml
15881589
│   ├── ingress.yaml
1589-
│   ├── nginx-deployment.yaml
1590-
│   └── nginx-service.yaml
1590+
│   ├── service.yaml
1591+
│   ├── serviceaccount.yaml
1592+
│   └── tests
1593+
│   └── test-connection.yaml
15911594
└── values.yaml
15921595
```
15931596

1594-
而我们删减一下这些文件,用之前教程中 ingress + configmaps 那节中的资源文件替代它,最终的结构长这样:
1597+
我们将默认生成在 templates 目录下面的 `yaml` 文件删除,用之前教程中 `yaml` 文件替换它,最终的结构长这样:
15951598

1596-
```
1599+
```shell
15971600
.
15981601
├── Chart.yaml
1602+
├── Dockerfile
15991603
├── _helpers.tpl
16001604
├── charts
1605+
├── hello-helm-0.1.0.tgz
1606+
├── index.yaml
1607+
├── main.go
16011608
├── templates
16021609
│   ├── hellok8s-configmaps.yaml
16031610
│   ├── hellok8s-deployment.yaml
1611+
│   ├── hellok8s-secret.yaml
16041612
│   ├── hellok8s-service.yaml
16051613
│   ├── ingress.yaml
16061614
│   ├── nginx-deployment.yaml
16071615
│   └── nginx-service.yaml
16081616
└── values.yaml
16091617
```
16101618

1611-
其中 `hellok8s-configmaps.yaml`
1619+
其中 `main.go` 定义的是 `hellok8s:v6` 版本的代码,主要是从系统中拿到 message,namespace,dbURL,dbPassword 这几个环境变量,拼接成字符串返回。
1620+
1621+
```go
1622+
package main
1623+
1624+
import (
1625+
"fmt"
1626+
"io"
1627+
"net/http"
1628+
"os"
1629+
)
1630+
1631+
func hello(w http.ResponseWriter, r *http.Request) {
1632+
host, _ := os.Hostname()
1633+
message := os.Getenv("MESSAGE")
1634+
namespace := os.Getenv("NAMESPACE")
1635+
dbURL := os.Getenv("DB_URL")
1636+
dbPassword := os.Getenv("DB_PASSWORD")
1637+
1638+
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))
1639+
}
1640+
1641+
func main() {
1642+
http.HandleFunc("/", hello)
1643+
http.ListenAndServe(":3000", nil)
1644+
}
1645+
```
1646+
1647+
为了让大家更加了解 helm charts values 使用和熟悉 k8s 资源配置,这几个环境变量 `MESSAGE`, `NAMESPACE`, `DB_URL`, `DB_PASSWORD` 分别有不同的来源。
1648+
1649+
首先修改根目录下的 `values.yaml` 文件,定义自定义的配置信息,从之前教程的 k8s 资源文件中,将一些易于变化的参数提取出来,放在 `values.yaml` 文件中。全部配置信息如下所示:
1650+
1651+
```yaml
1652+
application:
1653+
name: hellok8s
1654+
hellok8s:
1655+
image: guangzhengli/hellok8s:v6
1656+
replicas: 3
1657+
message: "It works with Helm Values!"
1658+
database:
1659+
url: "http://DB_ADDRESS_DEFAULT"
1660+
password: "db_password"
1661+
nginx:
1662+
image: nginx
1663+
replicas: 2
1664+
```
1665+
1666+
那自定义好了这些配置后,如何在 k8s 资源定义中使用这些配置信息呢?Helm 默认使用 [Go template 的方式](https://helm.sh/zh/docs/howto/charts_tips_and_tricks/) 来完成。
1667+
1668+
例如之前教程中,将环境变量 `DB_URL` 定义在 k8s configmaps 中,那么该资源可以定义成如文件所示 `hellok8s-configmaps.yaml`。其中 `metadata.name` 的值是 `{{ .Values.application.name }}-config`,意思是从 `values.yaml` 文件中获取 `application.name` 的值 `hellok8s`,拼接 `-config` 字符串,这样创建出来的 configmaps 资源名称就是 `hellok8s-config`。
1669+
1670+
同理 `{{ .Values.application.hellok8s.database.url }}` 就是获取值为 `http://DB_ADDRESS_DEFAULT` 放入 configmaps 对应 key 为 DB_URL 的 value 中。
16121671

16131672
```yaml
16141673
apiVersion: v1
16151674
kind: ConfigMap
16161675
metadata:
1617-
name: hellok8s-config
1676+
name: {{ .Values.application.name }}-config
16181677
data:
1619-
DB_URL: "http://DB_ADDRESS_DEV"
1678+
DB_URL: {{ .Values.application.hellok8s.database.url }}
1679+
```
1680+
1681+
上面定义的最终效果和之前在 `configmaps` 教程中定义的资源没有区别,这种做的好处是可以将所有可变的参数定义在 `values.yaml` 文件中,使用该 helm charts 的人无需了解具体 k8s 的定义,只需改变成自己想要的参数,即可创建自定义的资源!
1682+
1683+
同样,我们可以根据之前的教程将 `DB_PASSWORD` 放入 secret 中,并且通过 `b64enc` 方法将值 Base64 编码。
1684+
1685+
```shell
1686+
# hellok8s-secret.yaml
1687+
apiVersion: v1
1688+
kind: Secret
1689+
metadata:
1690+
name: {{ .Values.application.name }}-secret
1691+
data:
1692+
DB_PASSWORD: {{ .Values.application.hellok8s.database.password | b64enc }}
16201693
```
16211694

1622-
`hellok8s-deployment.yaml`
1695+
最后,修改 `hellok8s-deployment` 文件,根据前面的教程,将 `metadata.name` `replicas` `image` `configMapKeyRef.name` `secretKeyRef.name` 等值修改成从 `values.yaml` 文件中获取。
1696+
1697+
再添加代码中需要的 `NAMESPACE` 环境变量,从 `.Release.Namespace` [内置对象](https://helm.sh/zh/docs/chart_template_guide/builtin_objects/) 中获取。最后添加 `MESSAGE` 环境变量,直接从 `{{ .Values.application.hellok8s.message }}` 中获取。
16231698

16241699
```yaml
16251700
apiVersion: apps/v1
16261701
kind: Deployment
16271702
metadata:
1628-
name: hellok8s-deployment
1703+
name: {{ .Values.application.name }}-deployment
16291704
spec:
1630-
replicas: 3
1705+
replicas: {{ .Values.application.hellok8s.replicas }}
16311706
selector:
16321707
matchLabels:
16331708
app: hellok8s
@@ -1637,61 +1712,35 @@ spec:
16371712
app: hellok8s
16381713
spec:
16391714
containers:
1640-
- image: guangzhengli/hellok8s:v4
1715+
- image: {{ .Values.application.hellok8s.image }}
16411716
name: hellok8s-container
16421717
env:
16431718
- name: DB_URL
16441719
valueFrom:
16451720
configMapKeyRef:
1646-
name: hellok8s-config
1721+
name: {{ .Values.application.name }}-config
16471722
key: DB_URL
1723+
- name: DB_PASSWORD
1724+
valueFrom:
1725+
secretKeyRef:
1726+
name: {{ .Values.application.name }}-secret
1727+
key: DB_PASSWORD
1728+
- name: NAMESPACE
1729+
value: {{ .Release.Namespace }}
1730+
- name: MESSAGE
1731+
value: {{ .Values.application.hellok8s.message }}
16481732
```
16491733

1650-
`hellok8s-service.yaml`
1651-
1652-
```yaml
1653-
apiVersion: v1
1654-
kind: Service
1655-
metadata:
1656-
name: service-hellok8s-clusterip
1657-
spec:
1658-
type: ClusterIP
1659-
selector:
1660-
app: hellok8s
1661-
ports:
1662-
- port: 3000
1663-
targetPort: 3000
1664-
```
1665-
1666-
`ingress.yaml`
1734+
修改 `ingress.yaml` 将 `metadata.name` 的值,其它没有变化
16671735

1668-
```yaml
1736+
``` yaml
16691737
apiVersion: networking.k8s.io/v1
16701738
kind: Ingress
16711739
metadata:
1672-
name: hello-ingress
1673-
annotations:
1674-
# We are defining this annotation to prevent nginx
1675-
# from redirecting requests to `https` for now
1676-
nginx.ingress.kubernetes.io/ssl-redirect: "false"
1677-
spec:
1678-
rules:
1679-
- http:
1680-
paths:
1681-
- path: /hello
1682-
pathType: Prefix
1683-
backend:
1684-
service:
1685-
name: service-hellok8s-clusterip
1686-
port:
1687-
number: 3000
1688-
- path: /
1689-
pathType: Prefix
1690-
backend:
1691-
service:
1692-
name: service-nginx-clusterip
1693-
port:
1694-
number: 4000
1740+
name: {{ .Values.application.name }}-ingress
1741+
...
1742+
...
1743+
...
16951744
```
16961745

16971746
`nginx-deployment.yaml`
@@ -1702,7 +1751,7 @@ kind: Deployment
17021751
metadata:
17031752
name: nginx-deployment
17041753
spec:
1705-
replicas: 2
1754+
replicas: {{ .Values.application.nginx.replicas }}
17061755
selector:
17071756
matchLabels:
17081757
app: nginx
@@ -1712,27 +1761,13 @@ spec:
17121761
app: nginx
17131762
spec:
17141763
containers:
1715-
- image: nginx
1764+
- image: {{ .Values.application.nginx.image }}
17161765
name: nginx-container
17171766
```
17181767

1719-
`nginx-service.yaml`
1720-
1721-
```yaml
1722-
apiVersion: v1
1723-
kind: Service
1724-
metadata:
1725-
name: service-nginx-clusterip
1726-
spec:
1727-
type: ClusterIP
1728-
selector:
1729-
app: nginx
1730-
ports:
1731-
- port: 4000
1732-
targetPort: 80
1733-
```
1768+
`nginx-service.yaml` 和 `hellok8s-service.yaml` 没有变化。所有代码可以在 [这里](https://github.com/guangzhengli/k8s-tutorials/tree/main/hello-helm) 查看。
17341769

1735-
`Chart.yaml`
1770+
稍微修改下默认生成的`Chart.yaml`
17361771

17371772
```yaml
17381773
apiVersion: v2
@@ -1741,16 +1776,22 @@ description: A k8s tutorials in https://github.com/guangzhengli/k8s-tutorials
17411776
type: application
17421777
version: 0.1.0
17431778
appVersion: "1.16.0"
1744-
17451779
```
17461780

1747-
在 `hello-helm` 的跟路径下执行命令进行安装 chart,执行 curl 命令便能直接得到结果!查看 pod 和 service 等资源,便会发现 helm 能一键安装所有资源!
1781+
定义完成所有的 helm 资源后,首先**将 `hellok8s:v6` 镜像打包推送到 DockerHub**。
1782+
1783+
之后即可在 `hello-helm` 的目录下执行 `helm upgrade` 命令进行安装,安装成功后,执行 curl 命令便能直接得到结果!查看 pod 和 service 等资源,便会发现 helm 能一键安装所有资源!
17481784

17491785
```shell
17501786
helm upgrade --install hello-helm --values values.yaml .
1787+
# Release "hello-helm" does not exist. Installing it now.
1788+
# NAME: hello-helm
1789+
# NAMESPACE: default
1790+
# STATUS: deployed
1791+
# REVISION: 1
17511792
17521793
curl http://192.168.59.100/hello
1753-
#[v4] Hello, Kubernetes! From host: hellok8s-deployment-f88f984c6-nzwg6, Get Database Connect URL: http://DB_ADDRESS_DEV
1794+
# [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
17541795
17551796
kubectl get pods
17561797
# NAME READY STATUS RESTARTS AGE
@@ -1761,7 +1802,58 @@ kubectl get pods
17611802
# nginx-deployment-d47fd7f66-tsqj5 1/1 Running 0 32m
17621803
```
17631804

1764-
### 上传和下载其它 helm chart 使用 (TODO)
1805+
#### rollback
1806+
1807+
Helm 也提供了 Rollback 的功能,我们先修改一下 `message: "It works with Helm Values[v2]!"` 加上 [v2]。
1808+
1809+
```
1810+
application:
1811+
name: hellok8s
1812+
hellok8s:
1813+
image: guangzhengli/hellok8s:v6
1814+
replicas: 3
1815+
message: "It works with Helm Values[v2]!"
1816+
database:
1817+
url: "http://DB_ADDRESS_DEFAULT"
1818+
password: "db_password"
1819+
nginx:
1820+
image: nginx
1821+
replicas: 2
1822+
```
1823+
1824+
再执行 `helm upgrade` 命令更新 k8s 资源,通过 `curl http://192.168.59.100/hello` 可以看到资源已经更新。
1825+
1826+
```shell
1827+
➜ hello-helm git:(main) ✗ helm upgrade --install hello-helm --values values.yaml .
1828+
# Release "hello-helm" has been upgraded. Happy Helming!
1829+
NAME: hello-helm
1830+
NAMESPACE: default
1831+
STATUS: deployed
1832+
REVISION: 2
1833+
1834+
curl http://192.168.59.100/hello
1835+
# [v6] Hello, Helm! Message from helm values: It works with Helm Values[v2]!, From namespace: default, From host: hellok8s-deployment-598bbd6884-4b9bw, Get Database Connect URL: http://DB_ADDRESS_DEFAULT, Database Connect Password: db_password
1836+
```
1837+
1838+
如果这一次更新有问题的话,可以通过 ` helm rollback` 快速回滚。通过下面命令看到,和 deployment 的 rollback 一样,回滚后 REVISION 版本都会增大到 3 而不是回滚到 1,回滚后使用 `curl` 命令返回的 v1 版本的字符串。
1839+
1840+
```shell
1841+
helm ls
1842+
# NAME NAMESPACE REVISION STATUS CHART APP VERSION
1843+
# hello-helm default 2 deployed hello-helm-0.1.0 1.16.0
1844+
1845+
helm rollback hello-helm 1
1846+
# Rollback was a success! Happy Helming!
1847+
1848+
helm ls
1849+
# NAME NAMESPACE REVISION STATUS CHART APP VERSION
1850+
# hello-helm default 3 deployed hello-helm-0.1.0 1.16.0
1851+
1852+
curl http://192.168.59.100/hello
1853+
# [v6] Hello, Helm! Message from helm values: It works with Helm Values!, From namespace: default, From host: hellok8s-deployment-57d7df7964-482xw, Get Database Connect URL: http://DB_ADDRESS_DEFAULT, Database Connect Password: db_password
1854+
```
1855+
1856+
### helm chart 打包和发布
17651857

17661858
//TODO
17671859

0 commit comments

Comments
 (0)