Skip to content

Commit de0ebc4

Browse files
committed
Initial Commit
0 parents  commit de0ebc4

40 files changed

+3313
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

Lab01/README.md

Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
# Lab 01: Prometheus + Podman Kube
2+
3+
<!-- TOC -->
4+
5+
- [Lab 01: Prometheus + Podman Kube](#lab-01-prometheus--podman-kube)
6+
- [簡介套件 & 部署方式](#%E7%B0%A1%E4%BB%8B%E5%A5%97%E4%BB%B6--%E9%83%A8%E7%BD%B2%E6%96%B9%E5%BC%8F)
7+
- [Podman Compose](#podman-compose)
8+
- [Podman Kube](#podman-kube)
9+
- [Prometheus](#prometheus)
10+
- [設計架構](#%E8%A8%AD%E8%A8%88%E6%9E%B6%E6%A7%8B)
11+
- [安裝 Prometheus 在 Podman](#%E5%AE%89%E8%A3%9D-prometheus-%E5%9C%A8-podman)
12+
- [原理講解](#%E5%8E%9F%E7%90%86%E8%AC%9B%E8%A7%A3)
13+
- [ConfigMap](#configmap)
14+
- [環境變數 Environment variable](#%E7%92%B0%E5%A2%83%E8%AE%8A%E6%95%B8-environment-variable)
15+
- [資料卷 Volume](#%E8%B3%87%E6%96%99%E5%8D%B7-volume)
16+
- [PersistentVolumeClaim](#persistentvolumeclaim)
17+
- [Pod](#pod)
18+
- [Reference](#reference)
19+
20+
<!-- /TOC -->
21+
22+
## 簡介套件 & 部署方式
23+
24+
Podman 筆者所知道的部署方式有三種:
25+
26+
1. Podman
27+
2. Podman Compose
28+
3. Podman Kube
29+
30+
### Podman Compose
31+
32+
Podman Compose 跟 Docker Compose 會很接近,但這部分屬於社群維護並非 Podman 團隊管理。雖然是基於 Compose 的 spec 實作,但內容支援性還沒有到齊全(E.g. top level 的 configs 目前沒有寫,secrets 會受到 spec 限制,跟後面要說的 kube 支援度差異很多)。
33+
34+
### Podman Kube
35+
36+
Kube 全名就是 Kubernetes,也就是 K8s,用 K8s 的 YAML 來做部署,這部分由 Podman 團隊維護,這也是 Red Hat 目前推薦的方式。
37+
38+
目前官網上有這些種類可以支援:
39+
40+
- Pod
41+
- Deployment(複製出數個相同的 Pod)
42+
- PersistentVolumeClaim(儲存)
43+
- ConfigMap(應用程式設定檔)
44+
- Secret(機密)
45+
46+
如果對於 K8s 不熟悉也沒關係,筆者這次都會幫你準備,讓你可以動手實作。
47+
48+
### Prometheus
49+
50+
Prometheus 是個 Time-series 的 database,最早是由 SoundCloud 研發,後續許多開發者青睞使用,2016 年加入 CNCF 專案中,2018 年從 CNCF 專案畢業 (graduated)。
51+
52+
以時間作為資料儲存方式,可以是記錄連續或不連續資料。
53+
54+
- 連續資料:CPU、Memory、Network 等
55+
- 非連續資料:Application log、Syslog 等
56+
57+
![](images/architecture.png)
58+
59+
Prometheus 主要會用在**連續資料**上,用於非連續資料也是可以,但筆者會推薦另一個 Log system - Loki,不過篇幅有限這次不會出現(需要的話問卷當中填寫回饋,說不定有機會在 CNTUG 辦 workshop 專場)。
60+
61+
## 設計架構
62+
63+
Lab 01 中我們將會用 Pod 來部署 Prometheus,為了把資料保存而非佔用 tmp 空間,會使用 PersistentVolumeClaim 放在系統內並掛載到 container 的 `/prometheus`,ConfigMap 用於 Promtheus 應用程式設定檔。
64+
65+
## 安裝 Prometheus 在 Podman
66+
67+
1. 進入 `attachments/kube` 資料夾
68+
69+
```bash
70+
cd attachments/kube
71+
```
72+
73+
2. 把設定檔給 Podman 部署
74+
75+
```bash
76+
sudo podman kube play prom-data-pvc.yml
77+
sudo podman kube play prometheus-kube.yml --configmap prom-cm.yml
78+
```
79+
80+
## 原理講解
81+
82+
最基礎的 Kube YAML 會有這幾項:
83+
84+
- apiVersion:承接 K8s 的設定,通常為 v1
85+
- kind:說明這個 YAML 是哪種類別
86+
- metadata:給予該項目名字 (name)、標籤 (label)、註解 (annotation),其中註解某些情況會看特定字串。
87+
88+
底下就會根據不同的 kind,後方會有不同的寫法。
89+
90+
### ConfigMap
91+
92+
通常會用於「應用程式設定檔」,跟 Pod 有很多不同搭配方式,E.g. 環境變數 (environment variable)、資料卷 (volume) 等,除了純文字以外可以用二進位檔案 (binary data)。
93+
94+
純文字型態後面會接的是 `data`,如果要放入的是二進位檔案就會是 `binaryData`,把二進位資料使用 base64 做編碼 (encode) 放入 YAML。
95+
96+
#### 環境變數 (Environment variable)
97+
98+
`data` 中就會接 key-value,環境變數如果想要都從 ConfigMap 取得,就可以這樣寫:
99+
100+
```yaml
101+
apiVersion: v1
102+
kind: ConfigMap
103+
metadata:
104+
name: foo # ConfigMap name
105+
data:
106+
FOO: bar # key: value
107+
---
108+
apiVersion: v1
109+
kind: Pod
110+
metadata:
111+
name: foobar
112+
spec:
113+
containers:
114+
- name: container-1
115+
image: foobar
116+
envFrom:
117+
- configMapRef:
118+
name: foo # ConfigMap name
119+
optional: false
120+
```
121+
122+
或者要一個一個設計對應(這樣可以混合 Secret 一起搭配):
123+
124+
```yaml
125+
apiVersion: v1
126+
kind: ConfigMap
127+
metadata:
128+
name: foo # ConfigMap name
129+
data:
130+
THIS_FOO: bar # key: value
131+
---
132+
apiVersion: v1
133+
kind: Pod
134+
metadata:
135+
name: foobar
136+
spec:
137+
containers:
138+
- name: container-1
139+
image: foobar
140+
env:
141+
- name: FOO
142+
valueFrom:
143+
configMapKeyRef:
144+
name: foo # ConfigMap name
145+
key: THIS_FOO # which key
146+
optional: false
147+
```
148+
149+
#### 資料卷 (Volume)
150+
151+
如果變成資料卷 (volume) 去掛載,key 就會變成檔案名稱,value 就會變成檔案內容。
152+
153+
以 Lab 01 來說,`prometheus.yml` 會變成檔案名稱,後方的 `global...` 則會變成檔案內容。
154+
155+
```yaml
156+
apiVersion: v1
157+
kind: ConfigMap
158+
metadata:
159+
name: prometheus-config # configmap name
160+
data:
161+
prometheus.yml: |-
162+
global:
163+
scrape_interval: 15s
164+
scrape_timeout: 10s
165+
...
166+
```
167+
168+
要在 Pod 當中以資料卷 (volume) 去掛載 ConfigMap:
169+
170+
```yaml
171+
...
172+
volumes:
173+
- name: config-volume # pod volume name
174+
configMap:
175+
name: prometheus-config # configmap name
176+
...
177+
```
178+
179+
最後在 container 中使用 `volumeMounts` 即可成功掛載:
180+
181+
```yaml
182+
containers:
183+
- image: docker.io/prom/prometheus:v2.44.0
184+
...
185+
volumeMounts:
186+
- mountPath: /etc/prometheus # container path
187+
name: config-volume # pod volume name
188+
```
189+
190+
要注意的是,Podman 目前沒有特別為 ConfigMap 做特定型態,不能像 K8s 可以是獨立的型態,必須要有 Pod 搭配才能使用。
191+
192+
### PersistentVolumeClaim
193+
194+
PersistentVolumeClaim 簡稱為 PVC,以 K8s 來說,PVC 需要搭配 PersistVolume (PV) 才能使用,不過 Podman 這段簡化成可以只用 PVC 做資料保存。
195+
196+
PVC 會看註解 (annotation),`volume.podman.io/driver` 設定為 `local` 就代表以本機磁碟作為作為儲存,會存放在 `/var/lib/containers/storage/volumes`。
197+
198+
`accessModes` 是 K8s 設定,Podman 通常只要寫 `ReadWriteOnce`,其他選項 E.g. `ReadWriteMany` 基本上是沒有效果。
199+
200+
`resources.requests.storage` 是 K8s 設定,需要的磁碟空間,K8s 就會需要設定 PV 大小。
201+
202+
```yaml
203+
apiVersion: v1
204+
kind: PersistentVolumeClaim
205+
metadata:
206+
annotations:
207+
volume.podman.io/driver: local
208+
name: prom-data # pvc name
209+
spec:
210+
accessModes:
211+
- ReadWriteOnce
212+
resources:
213+
requests:
214+
storage: 5G
215+
```
216+
217+
掛載方式就要在 Pod 中說明有 volume,對應的 PVC:
218+
219+
```yaml
220+
...
221+
volumes:
222+
- name: prom-data-pvc # pod volume name
223+
persistentVolumeClaim:
224+
claimName: prom-data # pvc name
225+
...
226+
```
227+
228+
接下來在 container 裡面寫上要掛載的路徑即可使用:
229+
230+
```yaml
231+
...
232+
containers:
233+
- image: docker.io/prom/prometheus:v2.44.0
234+
name: prometheus
235+
...
236+
volumeMounts:
237+
- mountPath: /prometheus # container path
238+
name: prom-data-pvc # pod volume name
239+
...
240+
```
241+
242+
### Pod
243+
244+
1 個 Pod 通常搭配 1 個 container,有些會為了做 Service Mesh 就會放多個 container。
245+
246+
- image:映像來源。
247+
- name:容器名稱。
248+
- command:相當於 Docker 中的 Entrypoint。
249+
- args:相當於 Docker 中的 Cmd。
250+
- securityContext:安全性配置,這部分筆者通常不會設定,可以對容器做細部的權限調整。
251+
- volumeMounts:掛載容器卷 (volume) 到容器內。
252+
253+
```yaml
254+
spec:
255+
containers:
256+
- image: docker.io/prom/prometheus:v2.44.0
257+
name: prometheus
258+
# command: [] # Docker Entrypoint
259+
# args: [] # Docker Cmd
260+
ports:
261+
- containerPort: 9090
262+
hostPort: 9090
263+
securityContext: {}
264+
volumeMounts:
265+
- mountPath: /etc/prometheus
266+
name: config-volume
267+
volumes:
268+
- name: config-volume
269+
configMap:
270+
name: prometheus-config
271+
```
272+
273+
## Reference
274+
275+
- https://www.influxdata.com/time-series-database/
276+
- https://medium.com/@henry-chou/%E4%BE%86%E5%BE%9Etimescaledb%E8%AA%8D%E8%AD%98time-series-database%E5%90%A7-603603506d09
277+
- https://prometheus.io/docs/introduction/overview/

Lab01/attachments/kube/prom-cm.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: prometheus-config
5+
data:
6+
prometheus.yml: |-
7+
global:
8+
scrape_interval: 15s
9+
scrape_timeout: 10s
10+
evaluation_interval: 15s
11+
alerting:
12+
alertmanagers:
13+
- follow_redirects: true
14+
enable_http2: true
15+
scheme: http
16+
timeout: 10s
17+
api_version: v2
18+
static_configs:
19+
- targets: []
20+
scrape_configs:
21+
- job_name: prometheus
22+
honor_timestamps: true
23+
scrape_interval: 15s
24+
scrape_timeout: 10s
25+
metrics_path: /metrics
26+
scheme: http
27+
follow_redirects: true
28+
enable_http2: true
29+
static_configs:
30+
- targets:
31+
- localhost:9090
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: v1
2+
kind: PersistentVolumeClaim
3+
metadata:
4+
annotations:
5+
volume.podman.io/driver: local
6+
name: prom-data
7+
spec:
8+
accessModes:
9+
- ReadWriteOnce
10+
resources:
11+
requests:
12+
storage: 5G
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
labels:
5+
app: prometheus
6+
name: prometheus
7+
spec:
8+
containers:
9+
- image: docker.io/prom/prometheus:v2.44.0
10+
name: prometheus
11+
ports:
12+
- containerPort: 9090
13+
hostPort: 9090
14+
securityContext: {}
15+
volumeMounts:
16+
- mountPath: /etc/prometheus
17+
name: config-volume
18+
- mountPath: /prometheus
19+
name: prom-data-pvc
20+
volumes:
21+
- name: config-volume
22+
configMap:
23+
name: prometheus-config
24+
- name: prom-data-pvc
25+
persistentVolumeClaim:
26+
claimName: prom-data
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
global:
2+
scrape_interval: 15s
3+
scrape_timeout: 10s
4+
evaluation_interval: 15s
5+
alerting:
6+
alertmanagers:
7+
- follow_redirects: true
8+
enable_http2: true
9+
scheme: http
10+
timeout: 10s
11+
api_version: v2
12+
static_configs:
13+
- targets: []
14+
scrape_configs:
15+
- job_name: prometheus
16+
honor_timestamps: true
17+
scrape_interval: 15s
18+
scrape_timeout: 10s
19+
metrics_path: /metrics
20+
scheme: http
21+
follow_redirects: true
22+
enable_http2: true
23+
static_configs:
24+
- targets:
25+
- localhost:9090

Lab01/images/architecture.png

94.6 KB
Loading

0 commit comments

Comments
 (0)