Skip to content

Commit 4d40e70

Browse files
authored
en: information security with vault and csi driver (#126)
1 parent 2c8dec8 commit 4d40e70

File tree

1 file changed

+109
-97
lines changed

1 file changed

+109
-97
lines changed
Lines changed: 109 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,142 +1,146 @@
11
# Vault CSI Provider
22

3-
本指南将向你展示,KCL/Kusion 通过集成 Vault CSI Provider,解决敏感信息的传输问题。
4-
本次演示是将数据库的用户名和密码传输到 Pod 中,涉及 3 个 Kubernetes 内置资源和 1 个 自定义资源:
3+
This guide will show you that KCL/Kusion solves the secret management problem by integrating Vault CSI Provider.
4+
We will pass the database username and password into the Pod, involving 3 kubernetes built-in resources and 1 custom resource:
55

6-
- 命名空间(Namespace
7-
- 无状态应用(Deployment
8-
- 服务账号(ServiceAccount
9-
- 自定义资源(SecretProviderClass
6+
- Namespace
7+
- Deployment
8+
- ServiceAccount
9+
- SecretProviderClass
1010

1111
:::tip
12-
本指南要求你对 Kubernetes 有基本的了解。不清楚相关概念的,可以前往 Kubernetes 官方网站,查看相关说明:
12+
13+
This guide requires you to have a basic understanding of Kubernetes.
14+
If you are not familiar with the relevant concepts, please refer to the links below:
1315
- [Learn Kubernetes Basics](https://kubernetes.io/docs/tutorials/kubernetes-basics/)
1416
- [Namespace](https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/)
1517
- [Deployment](https://kubernetes.io/docs/concepts/workloads/controllers/deployment/)
1618
- [ServiceAccount](https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/)
1719
- [SecretProviderClass](https://secrets-store-csi-driver.sigs.k8s.io/concepts.html#custom-resource-definitions-crds)
1820
:::
1921

20-
## 1. 准备开始
21-
22-
在开始之前,我们需要做以下准备工作:
22+
## Prerequisites
2323

24-
1、安装 Kusion 工具链
24+
Before we start, we need to complete the following steps first:
2525

26-
我们推荐使用 kusion 的官方安装工具 `kusionup`,可实现 kusion 多版本管理等关键能力。
27-
详情信息请参阅[下载和安装](/docs/user_docs/getting-started/install)
26+
1、Install Kusion
2827

29-
2、下载开源 Konfig 大库
28+
We recommend using the official installation tool _kusionup_ which supports multi-version management.
29+
See [Download and Install](/docs/user_docs/getting-started/install) for more details.
3030

31-
在本篇指南中,需要用到部分已经抽象实现的 KCL 模型。
32-
有关 KCL 语言的介绍,可以参考 [Tour of KCL](/reference/lang/lang/tour.md)
31+
2、Clone Konfig repo
3332

34-
仓库地址: https://github.com/KusionStack/konfig.git
33+
In this guide, we need some KCL models that [Konfig](https://github.com/KusionStack/konfig.git) offers.
34+
For more details on KCL language, please refer to [Tour of KCL](/docs/reference/lang/lang/tour).
3535

36-
3、可用的 Kubernetes 集群
36+
3、Running Kubernetes cluster
3737

38-
必须要有一个 Kubernetes 集群,同时 Kubernetes 集群最好带有
39-
[kubectl](https://kubernetes.io/docs/tasks/tools/#kubectl) 命令行工具。
40-
如果你还没有集群,你可以通过 [Minikube](https://minikube.sigs.k8s.io/docs/tutorials/multi_node/)
41-
构建一个你自己的集群。
38+
There must be a running Kubernetes cluster and a [kubectl](https://Kubernetes.io/docs/tasks/tools/#kubectl) command line tool.
39+
If you don't have a cluster yet, you can use [Minikube](https://minikube.sigs.k8s.io/docs/tutorials/multi_node/) to start one of your own.
4240

43-
4、可用的 Helm CLI
41+
4、Available Helm CLI
4442

45-
Helm 工具用来部署 Vault Server 和 CSI Driver。
46-
如果你还没有安装 Helm,请参阅 [Helm 官方地址](https://helm.sh/docs/intro/install/)
43+
The Helm tool is used to deploy the Vault server and CSI driver.
44+
If you haven't installed Helm, please refer to [Install Helm](https://helm.sh/docs/intro/install/).
4745

48-
## 2. 安装 Vault CSI Driver
46+
## Install Vault server and CSI driver
4947

50-
推荐使用 Helm Chart 在 Kubernetes 上部署 Vault Server 和 CSI Driver
51-
[Helm](https://helm.sh/docs/helm/) 是一个包管理器,
52-
它可以安装和配置 Vault 及其相关组件,以不同模式运行。
53-
Helm Chart 实现了模板的条件化和参数化。这些参数可以通过命令行参数设置或在 YAML 中定义。
48+
We recommend deploying the Vault server and CSI driver on Kubernetes by _Helm Chart_.
49+
[Helm](https://helm.sh/docs/helm/) is a package manager,
50+
which can install and configure Vault and its related components in different modes.
51+
Helm chart implements conditionalization and parameterization of templates.
52+
These parameters can be set via command line arguments or defined in YAML files.
5453

55-
### 2.1 安装 Vault
54+
### Install Vault server
5655

57-
1、添加 HashiCorp Helm 存储库:
56+
1、Add HashiCorp helm repository:
5857
```bash
5958
helm repo add hashicorp https://helm.releases.hashicorp.com
6059
```
6160

62-
2、更新所有存储库以确保 helm 缓存了最新版本:
61+
2、Update to cache HashiCorp's latest version:
6362
```bash
6463
helm repo update
6564
```
6665

67-
3、安装最新版本的 Vault Server,以开发模式运行,禁用 Injector 服务并启用 CSI
66+
3、Install Vault server, start in development mode, disable Injector and enable CSI:
6867
```bash
6968
helm install vault hashicorp/vault \
7069
--set "server.dev.enabled=true" \
7170
--set "injector.enabled=false" \
7271
--set "csi.enabled=true"
7372
```
74-
`server.dev.enabled=true` 表示 Vault 在单 Pod 上以开发者模式启动;
75-
`injector.enabled=false` 表示禁用 Injector 服务;
76-
`csi.enabled=true` 表示启用 Vault CSI Pod
77-
如果你已经安装了 Vault,可以使用 `helm upgrade` 命令来更新 Vault 的部署模式。
73+
`server.dev.enabled=true` indicates that Vault is started in developer mode on a single pod.
74+
`injector.enabled=false` indicates that the Injector service is disabled;
75+
`csi.enabled=true` Indicates that the Vault CSI Pod is enabled.
76+
If you already have Vault installed, you can use the `helm upgrade` command to update Vault's deployment mode.
7877

79-
4、检查 Default 命名空间中的所有 Pod:
78+
4、Check all pods in the default namespace:
8079
```bash
8180
kubectl get pod
8281
NAME READY STATUS RESTARTS AGE
8382
vault-0 1/1 Running 0 17m
8483
vault-csi-provider-456hl 1/1 Running 0 17m
8584
```
86-
等到 `vault-0` 的状态是 `Running` 并且准备就绪(`1/1`),再继续本指南。
8785

88-
### 2.2 安装 CSI Driver
86+
Wait until the status of `vault-0` is `Running` and ready (`1/1`) before continuing with this guide.
8987

90-
[Secrets Store CSI 驱动程序](https://secrets-store-csi-driver.sigs.k8s.io/introduction.html)
91-
`secrets-store.csi.k8s.io` 允许 Kubernetes 将存储在外部机密存储中的多个机密、密钥和证书作为卷挂载到其 Pod 中。
92-
附加卷后,其中的数据将被挂载到容器的文件系统中。
88+
### Install CSI driver
89+
90+
[Secrets Store CSI Driver](https://secrets-store-csi-driver.sigs.k8s.io/introduction.html)
91+
`secrets-store.csi.k8s.io` allows Kubernetes to mount multiple secrets, keys,
92+
and certs stored in enterprise-grade external secrets stores into their pods as a volume.
93+
Once the volume is attached, the data in it is mounted into the container’s file system.
9394

9495
:::tip
95-
[容器存储接口(CSI)](https://github.com/container-storage-interface/spec/blob/master/spec.md)
96-
是一种标准,用于将任意块和文件存储系统暴露给 Kubernetes 等容器编排系统 (CO) 上的容器化工作负载。
97-
使用 CSI 第三方存储提供商可以编写和部署插件,在 Kubernetes 中公开新的存储系统,而无需接触核心 Kubernetes 代码。
96+
97+
The [Container Storage Interface (CSI)](https://github.com/container-storage-interface/spec/blob/master/spec.md)
98+
is a standard for exposing arbitrary block and file storage systems
99+
to containerized workloads on Container Orchestration Systems (COs) like Kubernetes.
100+
Using CSI third-party storage providers can write and deploy plugins exposing new storage systems in Kubernetes
101+
without ever having to touch the core Kubernetes code.
98102
:::
99103

100-
1、添加 CSI 驱动的 Helm 存储库:
104+
1、Add CSI driver helm repository:
101105
```bash
102106
helm repo add secrets-store-csi-driver https://kubernetes-sigs.github.io/secrets-store-csi-driver/charts
103107
```
104108

105-
2、安装最新版本的 Kubernetes-Secrets-Store-CSI-Driver:
109+
2、Install Kubernetes-Secrets-Store-CSI-Driver:
106110
```bash
107111
helm install csi secrets-store-csi-driver/secrets-store-csi-driver --namespace kube-system
108112
```
109-
`csi-secrets-store-csi-driver` 是以 DemonSet 形式部署在 `kube-system` 命名空间。
110113

111-
3、检查 CSI Driver 的 Pod 是否启动:
114+
3、Check CSI driver pods:
112115
```bash
113116
kubectl --namespace=kube-system get pods -l "app=secrets-store-csi-driver"
114117
NAME READY STATUS RESTARTS AGE
115118
csi-secrets-store-csi-driver-2wl2f 3/3 Running 0 2m
116119
```
117-
等待 `csi-secrets-store-csi-driver-2wl2f` 的状态是 `Running`,并且已经准备就绪(`3/3`),再继续本指南。
118120

119-
## 3. 配置 Vault
121+
Wait until the status of pod `csi-secrets-store-csi-driver-2wl2f` is `Running` and is ready (`3/3`) before continuing with this guide.
120122

121-
Vault 将机密数据保存在自己的数据库中,用户需要先配置相关机密数据,并启用 Vault 的 Kubernetes 认证。
123+
## Configure Vault
122124

123-
### 3.1 配置机密数据 {#set-secret-data}
125+
Vault stores confidential data in its database, and users need to configure the relevant confidential data and enable Vault's Kubernetes authentication.
124126

125-
[创建挂载 Vault Secret 的 Pod](#create-pod-with-secret-mounted)小节,挂载到 Pod 中的卷,
126-
引用了保存在 `secret/data/db-pass` 路径下的 secret 。
127-
Vault 以开发模式运行时,kv 引擎会启用默认路径 `/secret`
127+
### Create a secret
128128

129-
1、在 `vault-0` 启动交互式 shell 终端:
129+
In [Create a pod with a secret mounted](#create-a-pod-with-a-secret-mounted) section,
130+
the volume mounted in Pod expects secret stored at path `secret/data/db-pass`.
131+
When Vault is run in development a K/V secret engine is enabled at the path `/secret`.
132+
133+
1、start an interactive shell session on the `vault-0` pod:
130134
```bash
131135
kubectl exec -it vault-0 -- /bin/sh
132136
```
133137

134-
2、`secret/db-pass` 路径创建带有密码的 secret:
138+
2、Create a secret at the path `secret/db-pass` with a password:
135139
```bash
136140
vault kv put secret/db-pass password="db-secret-password"
137141
```
138142

139-
输出类似于:
143+
The output is similar to:
140144
```
141145
Key Value
142146
--- -----
@@ -147,12 +151,12 @@ destroyed false
147151
version 1
148152
```
149153

150-
3、验证 secret 在路径 `/secret/db-pass` 上是否可读:
154+
3、Verify that the secret is readable at the path `secret/db-pass`.
151155
```bash
152156
vault kv get secret/db-pass
153157
```
154158

155-
输出类似于:
159+
The output is similar to:
156160
```
157161
======= Metadata =======
158162
Key Value
@@ -168,24 +172,24 @@ Key Value
168172
--- -----
169173
password db-secret-password
170174
```
171-
到此,机密数据创建完毕,暂且不需要退出 Pod。
175+
For now, the confidential data is created, please don't exit the vault pod immediately.
172176

173-
### 3.2 启用 kubernetes 身份认证
177+
### Enable Kubernetes authentication
174178

175-
Vault 提供了 Kubernetes 身份验证方法,使客户端能够使用 Kubernetes ServiceAccount 令牌进行身份验证。
176-
此令牌在创建时提供给每个 Pod。
179+
Vault provides a Kubernetes authentication method that enables clients to authenticate with a Kubernetes ServiceAccount Token.
180+
The Kubernetes resources that access the secret and create the volume authenticate through this method through a `role`.
177181

178-
1、继续上一小节的 Terminal,启用 Kubernetes 身份验证:
182+
1、Continue with the terminal in the previous step, and enable the Kubernetes authentication method:
179183
```bash
180184
vault auth enable kubernetes
181185
```
182-
输出类似于:
186+
187+
The output is similar to:
183188
```
184189
Success! Enabled kubernetes auth method at: kubernetes/
185190
```
186191

187-
2、配置 kubernetes 身份认证规则,依赖 Kubernetes API 地址、ServiceAccount 令牌、
188-
证书以及 Kubernetes ServiceAccount 的颁发者(Kubernetes 1.21+ 需要):
192+
2、Configure authentication rules, depending on the Kubernetes API address, ServiceAccount token, certificate, and the issuer of the Kubernetes ServiceAccount(required for Kubernetes 1.21+):
189193
```bash
190194
vault write auth/kubernetes/config \
191195
kubernetes_host="https://$KUBERNETES_PORT_443_TCP_ADDR:443" \
@@ -194,18 +198,15 @@ vault write auth/kubernetes/config \
194198
issuer="https://kubernetes.default.svc.cluster.local"
195199
```
196200

197-
输出类似于:
201+
The output is similar to:
198202
```
199203
Success! Data written to: auth/kubernetes/config
200204
```
201-
Kubernetes 创建容器时,将 `token_reviewer_jwt``kubernetes_ca_cert` 挂载到容器中。
202-
环境变量 `KUBERNETES_PORT_443_TCP_ADDR` 引用的是 Kubernetes 主机的内部网络地址。
203-
204-
3、设置读权限的 _policy_
205205

206-
Kubernetes-Secrets-Store-CSI-Driver 需要读取密钥,保证它对挂载的卷和卷中密钥有读权限。
206+
When Kubernetes creates pods, mount `token_reviewer_jwt` and `Kubernetes_ca_cert` into them.
207+
The environment variable `KUBERNETES_PORT_443_TCP_ADDR` references the internal network address of the Kubernetes host.
207208

208-
创建名为 `kcl-vault-csi-policy`_policy_
209+
3、Create a policy named `kcl-vault-csi-policy`:
209210
```bash
210211
vault policy write kcl-vault-csi-policy - <<EOF
211212
path "secret/data/db-pass" {
@@ -214,7 +215,9 @@ path "secret/data/db-pass" {
214215
EOF
215216
```
216217

217-
4、再创建名为 `kcl-vault-csi-role`_role_ ,关联上一步创建的 _policy_,并绑定 Namespace 和 ServiceAccount:
218+
Kubernetes-Secrets-Store-CSI-Driver needs to read the secret key, which must have read access to the mounted volume.
219+
220+
4、Create a role named `kcl-vault-csi-role`:
218221
```bash
219222
vault write auth/kubernetes/role/kcl-vault-csi-role \
220223
bound_service_account_names=kcl-vault-csi-sa \
@@ -223,28 +226,31 @@ vault write auth/kubernetes/role/kcl-vault-csi-role \
223226
ttl=24h
224227
```
225228

226-
输出类似于:
229+
The output is similar to:
227230
```
228231
Success! Data written to: auth/kubernetes/role/kcl-vault-csi-role
229232
```
230-
该角色将 Kubernetes 服务帐户 _kcl-vault-csi-sa_ 和命名空间 _kcl-vault-csi_ 与 Vault 策略 _kcl-vault-csi-role_ 关联起来。
231-
此 Kubernetes 服务帐户将会在后面创建。认证成功后返回的令牌有效期为 24 小时。最后,执行 `exit` 退出 Pod。
232233

233-
## 4. 结果验证 {#verify-result}
234+
This role associates the Kubernetes service account `kcl-vault-csi-sa` and namespace `kcl-vault-csi` with the Vault policy `kcl-vault-csi-role`.
235+
This Kubernetes service account will be created later. The token returned after successful authentication is valid for 24 hours.
236+
Finally, exit the `vault-0` pod.
237+
238+
## Verify Secret
234239

235-
上一节我们已经在 Vault 中保存机密数据,并且配置 Vault 角色,完成了 Namespace + ServiceAccount + Policy 的绑定。
236-
这一节,我们直接使用开源大库中的 Vault 演示项目,部署应用并检验结果。
240+
In the previous section, we created a secret in the Vault server,
241+
configured the Vault `role` and `policy`, and completed the binding of `Namespace` and `ServiceAccount`.
242+
In this section, we directly use the Vault demo project `kcl-vault-csi` in Konfig to deploy the application and verify the results.
237243

238-
### 4.1 创建挂载 Vault Secret 的 Pod {#create-pod-with-secret-mounted}
244+
### Create a pod with a secret mounted
239245

240-
1、进入开源大库的 Vault 演示项目的 Stack 目录 `base/examples/kcl-vault-csi/dev`,并下发配置:
246+
1、Enter stack dir `base/examples/kcl-vault-csi/dev` and apply stack configs:
241247
```bash
242248
cd base/examples/kcl-vault-csi/dev && kusion apply --yes=true
243249
```
244250

245-
输出类似于:
251+
The output is similar to:
246252
```
247-
SUCCESS Compiling in stack dev...
253+
SUCCESS Compiling in stack dev...
248254
249255
Stack: dev Provider Type Name Plan
250256
* ├─ kubernetes v1:Namespace kcl-vault-csi[0] Create
@@ -261,29 +267,35 @@ Creating Deployment/kcl-vault-csi-dev [4/4] ████████████
261267
262268
Apply complete! Resources: 4 created, 0 updated, 0 deleted.
263269
```
264-
四个资源创建成功,待 Deployment 创建出 Pod 后,会将 SecretProviderClass 中声明的卷挂载到容器的文件系统中。
265270

266-
### 4.2 校验注入结果
271+
The four resources are created successfully.
272+
After the deployment controller finished syncing pods, it will mount the volume declared in SecretProviderClass to the pod's file system.
273+
274+
### Verify mount results
267275

268-
1、检查实验 Pod 是否运行:
276+
1、Check pod status:
269277
```bash
270278
kubectl get pod -n kcl-vault-csi
271279
```
272280

273-
输出类似于:
281+
The output is similar to:
274282
```
275283
NAME READY STATUS RESTARTS AGE
276284
kcl-vault-csi-dev-64b66968d8-p27fv 1/1 Running 0 12s
277285
```
278286

279-
2、查看写入 Pod 的文件系统路径 `/mnt/secrets-store/db-password` 的内容,
280-
检查是否是[配置机密数据](#set-secret-data) 小节写入的 paasword:
287+
2、Read file content from `/mnt/secrets-store/db-password`, see if it is the password written in the section [Create a secret](#create-a-secret):
281288
```bash
282289
kubectl exec -it kcl-vault-csi-dev-64b66968d8-p27fv -n kcl-vault-csi -- cat /mnt/secrets-store/db-password
283290
```
284291

285-
输出类似于:
292+
The output is similar to:
286293
```
287294
db-secret-password
288295
```
289-
可以看到,我们成功地将机密数据 `password` 通过 CSI 卷的方式,成功注入到 Pod 的文件系统中,完成了机密信息的传输。
296+
297+
It can be seen that we successfully injected the secret data `password` into the file system of the pod by the CSI driver.
298+
299+
## What's Next
300+
301+
- Learn about secret management with [Vault Agent](/docs/user_docs/guides/sensitive-data-solution/vault-agent)

0 commit comments

Comments
 (0)