Skip to content

Commit 8320d06

Browse files
committed
[helm] feat: add helm document.
1 parent 0638f08 commit 8320d06

2 files changed

Lines changed: 183 additions & 75 deletions

File tree

README.md

Lines changed: 182 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,8 @@
1-
# kubernetes workshop
2-
3-
- [kubernetes workshop](#kubernetes-workshop)
4-
- [准备工作](#准备工作)
5-
- [安装 docker](#安装-docker)
6-
- [安装 minikube](#安装-minikube)
7-
- [安装 k8s CLI 和 Terminal based UI](#安装-k8s-cli-和-terminal-based-ui)
8-
- [注册 docker hub 账号登录](#注册-docker-hub-账号登录)
9-
- [Container](#container)
10-
- [Pod](#pod)
11-
- [Pod 与 Container 的不同](#pod-与-container-的不同)
12-
- [Pod 其它命令](#pod-其它命令)
13-
- [作业一:Hellok8s Pod](#作业一hellok8s-pod)
14-
- [Deployment](#deployment)
15-
- [扩容](#扩容)
16-
- [升级版本](#升级版本)
17-
- [Rolling Update(滚动更新)](#rolling-update滚动更新)
18-
- [存活 / 就绪探针](#存活--就绪探针)
19-
- [Automatically blocking bad releases by readinessProbe](#automatically-blocking-bad-releases-by-readinessprobe)
20-
- [Service](#service)
21-
- [ingress](#ingress)
22-
- [Configmap](#configmap)
23-
- [env var](#env-var)
24-
- [configmap](#configmap-1)
25-
- [Getting all the variables from a ConfigMap](#getting-all-the-variables-from-a-configmap)
26-
- [Exposing ConfigMap as files](#exposing-configmap-as-files)
27-
- [Secret](#secret)
28-
- [Using stringData](#using-stringdata)
29-
- [helm](#helm)
1+
# kubernetes tutorials
302

313
## 准备工作
324

33-
本地环境是 MacOS 11.6.2 Intel 版本,教程也是本地环境。windows 或 linux 环境的小伙伴需要自行安装 docker 和 minikube。
34-
35-
如果本地
5+
由于我本地环境是 MacOS 11.6.2 Intel 版本,教程也是在本地环境进行。windows 或 linux 环境的小伙伴需要自行安装 docker 和 minikube。
366

377
### 安装 docker
388

@@ -48,6 +18,8 @@ brew install docker-compose
4818

4919
[minikube](https://minikube.sigs.k8s.io/docs/) 用于在本地环境中运行 Kubernetes 集群。但它也运行一个可用于运行容器的 docker 守护进程。在 macOS 上,minikube 运行在很多虚拟化技术上,可以选择[hyperkit](https://minikube.sigs.k8s.io/docs/drivers/hyperkit/),这里因为我本地之前已经安装过 virtualbox (brew install --cask virtualbox),所以我用的是 virtualbox 虚拟化技术。如果你本地之前已经安装了 docker desktop 的话,可以不需要下载 `hyperkit` 或者 `virtualbox`
5020

21+
>注意:如果本地已经安装了 docker desktop,那么可以使用 minikube start --vm-driver docker --container-runtime=docker 来快速启动 minikube
22+
5123
```bash
5224
# Install hyperkit and minikube (check which vm-driver to use, if install docker desktop already, you can just use vm-driver=docker instead of install hyperkiy)
5325
brew install hyperkit
@@ -70,8 +42,6 @@ echo "`minikube ip` docker.local" | sudo tee -a /etc/hosts > /dev/null
7042
docker run hello-world
7143
```
7244

73-
> 注意:如果本地已经安装了 docker desktop,那么可以使用 minikube start --vm-driver docker --container-runtime=docker 来快速启动 minikube
74-
7545
**minikube Cheatsheet**
7646

7747
`minikube stop` 不会删除任何数据,只是停止 VM 和 k8s 集群。
@@ -92,18 +62,6 @@ docker run hello-world
9262
brew install kubectl
9363
```
9464

95-
如果我们希望更直观的观察 kubernetes 中资源的变化,也可以安装一个 [k9s](https://k9scli.io/)(对于初学者而言,更建议使用 kubectl 来手动观察)
96-
97-
```bash
98-
brew install k9s
99-
```
100-
101-
如果需要练习 helm 的使用,可以先安装 helm
102-
103-
```bash
104-
brew install helm
105-
```
106-
10765
### 注册 docker hub 账号登录
10866

10967
在 docker hun(https://hub.docker.com/) 中注册账号,并且使用 login 登录账号。
@@ -1423,11 +1381,11 @@ kubectl get pods
14231381
# hello-cronjob-27694609--1-2nmdx 0/1 Completed 0 15s
14241382
```
14251383

1426-
## Helm
1384+
## Helm(TODO)
14271385

14281386
经过前面的教程,想必你已经对 kubernetes 的使用有了一定的理解。但是不知道你是否想过这样一个问题,就是我们前面教程中提到的所有资源,包括用 `pod`, `deployment`, `service`, `ingress`, `configmap`,`secret` 所有资源来部署一套完整的 `hellok8s` 服务的话,难道需要一个一个的 `kubectl apply -f` 来创建吗?如果换一个 namespace,或者说换一套 kubernetes 集群部署的话,又要重复性的操作创建的过程吗?
14291387

1430-
我们平常使用操作系统时,需要一个应用的话,可以直接使用 `apt` 或者 `brew` 来直接安装,而不需要关心这个应用需要哪些依赖,哪些配置。在使用 kubernetes 安装应用服务 `hellok8s` 时,我们自然也希望能够一个命令就安装完成,而提供这个能力的,就是 CNCF 的毕业项目 [Helm](https://github.com/helm/helm)
1388+
我们平常使用操作系统时,需要安装一个应用的话,可以直接使用 `apt` 或者 `brew` 来直接安装,而不需要关心这个应用需要哪些依赖,哪些配置。在使用 kubernetes 安装应用服务 `hellok8s` 时,我们自然也希望能够一个命令就安装完成,而提供这个能力的,就是 CNCF 的毕业项目 [Helm](https://github.com/helm/helm)
14311389

14321390
> Helm 帮助您管理 Kubernetes 应用—— Helm Chart,Helm 是查找、分享和使用软件构建 [Kubernetes](https://kubernetes.io/) 的最优方式。
14331391
>
@@ -1447,6 +1405,14 @@ brew install helm
14471405

14481406
Helm 的使用方式可以解释为:Helm 安装 *charts* 到 Kubernetes 集群中,每次安装都会创建一个新的 *release*。你可以在 Helm 的 chart *repositories* 中寻找新的 chart。
14491407

1408+
### 安装 hellok8s chart 快速开始(TODO)
1409+
1410+
//TODO
1411+
1412+
### 创建 helm charts
1413+
1414+
我们来手把手创建一个自己的 helm charts,完成我们之前的那些功能。
1415+
14501416
使用 `helm create` 命令默认会帮你创建一些 k8s 资源定义的初始文件,如下所示:
14511417

14521418
```
@@ -1466,26 +1432,186 @@ helm create hello-helm
14661432
└── values.yaml
14671433
```
14681434

1435+
而我们删减一下这些文件,用之前教程中 ingress + configmaps 那节中的资源文件替代它,最终的结构长这样:
14691436

1437+
```
1438+
.
1439+
├── Chart.yaml
1440+
├── _helpers.tpl
1441+
├── charts
1442+
├── templates
1443+
│   ├── hellok8s-configmaps.yaml
1444+
│   ├── hellok8s-deployment.yaml
1445+
│   ├── hellok8s-service.yaml
1446+
│   ├── ingress.yaml
1447+
│   ├── nginx-deployment.yaml
1448+
│   └── nginx-service.yaml
1449+
└── values.yaml
1450+
```
14701451

1471-
```shell
1472-
helm create helm-hello
1452+
其中 `hellok8s-configmaps.yaml`
14731453

1474-
helm upgrade --install hello-helm --values values.yaml .
1454+
```yaml
1455+
apiVersion: v1
1456+
kind: ConfigMap
1457+
metadata:
1458+
name: hellok8s-config
1459+
data:
1460+
DB_URL: "http://DB_ADDRESS_DEV"
1461+
```
1462+
1463+
`hellok8s-deployment.yaml`
1464+
1465+
```yaml
1466+
apiVersion: apps/v1
1467+
kind: Deployment
1468+
metadata:
1469+
name: hellok8s-deployment
1470+
spec:
1471+
replicas: 3
1472+
selector:
1473+
matchLabels:
1474+
app: hellok8s
1475+
template:
1476+
metadata:
1477+
labels:
1478+
app: hellok8s
1479+
spec:
1480+
containers:
1481+
- image: guangzhengli/hellok8s:v4
1482+
name: hellok8s-container
1483+
env:
1484+
- name: DB_URL
1485+
valueFrom:
1486+
configMapKeyRef:
1487+
name: hellok8s-config
1488+
key: DB_URL
1489+
```
14751490

1476-
helm list
1491+
`hellok8s-service.yaml`
14771492

1478-
helm rollback hello-helm
1493+
```yaml
1494+
apiVersion: v1
1495+
kind: Service
1496+
metadata:
1497+
name: service-hellok8s-clusterip
1498+
spec:
1499+
type: ClusterIP
1500+
selector:
1501+
app: hellok8s
1502+
ports:
1503+
- port: 3000
1504+
targetPort: 3000
14791505
```
14801506

1481-
TODO
1507+
`ingress.yaml`
1508+
1509+
```yaml
1510+
apiVersion: networking.k8s.io/v1
1511+
kind: Ingress
1512+
metadata:
1513+
name: hello-ingress
1514+
annotations:
1515+
# We are defining this annotation to prevent nginx
1516+
# from redirecting requests to `https` for now
1517+
nginx.ingress.kubernetes.io/ssl-redirect: "false"
1518+
spec:
1519+
rules:
1520+
- http:
1521+
paths:
1522+
- path: /hello
1523+
pathType: Prefix
1524+
backend:
1525+
service:
1526+
name: service-hellok8s-clusterip
1527+
port:
1528+
number: 3000
1529+
- path: /
1530+
pathType: Prefix
1531+
backend:
1532+
service:
1533+
name: service-nginx-clusterip
1534+
port:
1535+
number: 4000
1536+
```
1537+
1538+
`nginx-deployment.yaml`
1539+
1540+
```yaml
1541+
apiVersion: apps/v1
1542+
kind: Deployment
1543+
metadata:
1544+
name: nginx-deployment
1545+
spec:
1546+
replicas: 2
1547+
selector:
1548+
matchLabels:
1549+
app: nginx
1550+
template:
1551+
metadata:
1552+
labels:
1553+
app: nginx
1554+
spec:
1555+
containers:
1556+
- image: nginx
1557+
name: nginx-container
1558+
```
1559+
1560+
`nginx-service.yaml`
1561+
1562+
```yaml
1563+
apiVersion: v1
1564+
kind: Service
1565+
metadata:
1566+
name: service-nginx-clusterip
1567+
spec:
1568+
type: ClusterIP
1569+
selector:
1570+
app: nginx
1571+
ports:
1572+
- port: 4000
1573+
targetPort: 80
1574+
```
1575+
1576+
`Chart.yaml`
1577+
1578+
```yaml
1579+
apiVersion: v2
1580+
name: hello-helm
1581+
description: A k8s tutorials in https://github.com/guangzhengli/k8s-tutorials
1582+
type: application
1583+
version: 0.1.0
1584+
appVersion: "1.16.0"
1585+
1586+
```
1587+
1588+
在 `hello-helm` 的跟路径下执行命令进行安装 chart,执行 curl 命令便能直接得到结果!查看 pod 和 service 等资源,便会发现 helm 能一键安装所有资源!
1589+
1590+
```shell
1591+
helm upgrade --install hello-helm --values values.yaml .
1592+
1593+
curl http://192.168.59.100/hello
1594+
#[v4] Hello, Kubernetes! From host: hellok8s-deployment-f88f984c6-nzwg6, Get Database Connect URL: http://DB_ADDRESS_DEV
1595+
1596+
kubectl get pods
1597+
# NAME READY STATUS RESTARTS AGE
1598+
# hellok8s-deployment-f88f984c6-k8hpz 1/1 Running 0 32m
1599+
# hellok8s-deployment-f88f984c6-nzwg6 1/1 Running 0 32m
1600+
# hellok8s-deployment-f88f984c6-s89s7 1/1 Running 0 32m
1601+
# nginx-deployment-d47fd7f66-6w76b 1/1 Running 0 32m
1602+
# nginx-deployment-d47fd7f66-tsqj5 1/1 Running 0 32m
1603+
```
14821604

1605+
### 上传和下载其它 helm chart 使用 (TODO)
14831606

1607+
//TODO
14841608

1609+
## Dashboard(TODO)
14851610

1611+
//TODO: 介绍 kubernetes dashboard
14861612

1487-
## Dashboard
1613+
### K9s(TODO)
14881614

1489-
### K9s
1615+
//TODO: 介绍 k9s client
14901616

14911617
![83ybd4](https://cdn.jsdelivr.net/gh/guangzhengli/PicURL@master/uPic/83ybd4.png)

hello-helm/Chart.yaml

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,6 @@
11
apiVersion: v2
22
name: hello-helm
3-
description: A helm tutorial in https://github.com/guangzhengli/kubernetes_workshop
4-
5-
# A chart can be either an 'application' or a 'library' chart.
6-
#
7-
# Application charts are a collection of templates that can be packaged into versioned archives
8-
# to be deployed.
9-
#
10-
# Library charts provide useful utilities or functions for the chart developer. They're included as
11-
# a dependency of application charts to inject those utilities and functions into the rendering
12-
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
3+
description: A k8s tutorials in https://github.com/guangzhengli/k8s-tutorials
134
type: application
14-
15-
# This is the chart version. This version number should be incremented each time you make changes
16-
# to the chart and its templates, including the app version.
17-
# Versions are expected to follow Semantic Versioning (https://semver.org/)
185
version: 0.1.0
19-
20-
# This is the version number of the application being deployed. This version number should be
21-
# incremented each time you make changes to the application. Versions are not expected to
22-
# follow Semantic Versioning. They should reflect the version the application is using.
23-
# It is recommended to use it with quotes.
246
appVersion: "1.16.0"

0 commit comments

Comments
 (0)