-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
331 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# 示例: 改变镜像名称和标签 | ||
|
||
首先构建一个工作空间: | ||
|
||
<!-- @makeWorkplace @testAgainstLatestRelease --> | ||
```bash | ||
DEMO_HOME=$(mktemp -d) | ||
``` | ||
|
||
创建包含pod资源的 `kustomization` | ||
|
||
<!-- @createKustomization @testAgainstLatestRelease --> | ||
```bash | ||
cat <<EOF >$DEMO_HOME/kustomization.yaml | ||
resources: | ||
- pod.yaml | ||
EOF | ||
``` | ||
|
||
创建pod资源声明 | ||
|
||
<!-- @createDeployment @testAgainstLatestRelease --> | ||
```bash | ||
cat <<EOF >$DEMO_HOME/pod.yaml | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: myapp-pod | ||
labels: | ||
app: myapp | ||
spec: | ||
containers: | ||
- name: myapp-container | ||
image: busybox:1.29.0 | ||
command: ['sh', '-c', 'echo The app is running! && sleep 3600'] | ||
initContainers: | ||
- name: init-mydb | ||
image: busybox:1.29.0 | ||
command: ['sh', '-c', 'until nslookup mydb; do echo waiting for mydb; sleep 2; done;'] | ||
EOF | ||
``` | ||
|
||
`myapp-pod` 包含一个init容器和一个普通容器,两者都使用 `busybox:1.29.0` 镜像。 | ||
|
||
在 `kustomization.yaml` 中添加 `images` 字段来更改镜像 `busybox` 和标签 `1.29.0` 。 | ||
|
||
- 通过 `kustomize` 添加 `images`: | ||
<!-- @addImages @testAgainstLatestRelease --> | ||
```bash | ||
cd $DEMO_HOME | ||
kustomize edit set image busybox=alpine:3.6 | ||
``` | ||
|
||
- 将`images`字段将被添加到`kustomization.yaml`: | ||
> ```yaml | ||
> images: | ||
> - name: busybox | ||
> newName: alpine | ||
> newTag: 3.6 | ||
> ``` | ||
|
||
构建 `kustomization` | ||
<!-- @kustomizeBuild @testAgainstLatestRelease --> | ||
```bash | ||
kustomize build $DEMO_HOME | ||
``` | ||
|
||
确认`busybox`镜像和标签是否被替换为`alpine:3.6`: | ||
<!-- @confirmImages @testAgainstLatestRelease --> | ||
``` | ||
test 2 = \ | ||
$(kustomize build $DEMO_HOME | grep alpine:3.6 | wc -l); \ | ||
echo $? | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
[Strategic Merge Patch]: https://github.com/kubernetes/community/blob/master/contributors/devel/sig-api-machinery/strategic-merge-patch.md | ||
[JSON patches]: https://tools.ietf.org/html/rfc6902 | ||
[label selector]: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors | ||
|
||
|
||
# 示例:通过一个 patch 来修改多个资源 | ||
|
||
kustomization 文件支持通过 [Strategic Merge Patch] 和 [JSON patch] 来自定义资源。 现在,一个 patch 可以应用于多个资源。 | ||
|
||
可以通过指定 patch 和 target 选择器来完成,如下所示: | ||
```yaml | ||
patches: | ||
- path: <PatchFile> | ||
target: | ||
group: <Group> | ||
version: <Version> | ||
kind: <Kind> | ||
name: <Name> | ||
namespace: <Namespace> | ||
labelSelector: <LabelSelector> | ||
annotationSelector: <AnnotationSelector> | ||
``` | ||
`labelSelector` 和 `annotationSelector` 都应遵循 [label selector] 中的约定。Kustomize 选择匹配`target`中所有字段的目标来应用 patch 。 | ||
|
||
下面的示例展示了如何为所有部署资源注入 sidecar 容器。 | ||
|
||
创建一个包含 Deployment 资源的 `kustomization` 。 | ||
|
||
<!-- @createDeployment @testAgainstLatestRelease --> | ||
```bash | ||
DEMO_HOME=$(mktemp -d) | ||
cat <<EOF >$DEMO_HOME/kustomization.yaml | ||
resources: | ||
- deployments.yaml | ||
EOF | ||
cat <<EOF >$DEMO_HOME/deployments.yaml | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: deploy1 | ||
spec: | ||
template: | ||
metadata: | ||
labels: | ||
old-label: old-value | ||
spec: | ||
containers: | ||
- name: nginx | ||
image: nginx | ||
args: | ||
- one | ||
- two | ||
--- | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: deploy2 | ||
spec: | ||
template: | ||
metadata: | ||
labels: | ||
key: value | ||
spec: | ||
containers: | ||
- name: busybox | ||
image: busybox | ||
EOF | ||
``` | ||
|
||
声明 [Strategic Merge Patch] 文件以注入 sidecar 容器: | ||
|
||
<!-- @addPatch @testAgainstLatestRelease --> | ||
```bash | ||
cat <<EOF >$DEMO_HOME/patch.yaml | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: not-important | ||
spec: | ||
template: | ||
spec: | ||
containers: | ||
- name: istio-proxy | ||
image: docker.io/istio/proxyv2 | ||
args: | ||
- proxy | ||
- sidecar | ||
EOF | ||
``` | ||
|
||
在 kustomization.yaml 中添加 _patches_ 字段 | ||
|
||
<!-- @applyPatch @testAgainstLatestRelease --> | ||
```bash | ||
cat <<EOF >>$DEMO_HOME/kustomization.yaml | ||
patches: | ||
- path: patch.yaml | ||
target: | ||
kind: Deployment | ||
EOF | ||
``` | ||
|
||
运行 `kustomize build $DEMO_HOME`,可以在输出中确认两个 Deployment 资源都已正确应用。 | ||
|
||
<!-- @confirmPatch @testAgainstLatestRelease --> | ||
```bash | ||
test 2 == \ | ||
$(kustomize build $DEMO_HOME | grep "image: docker.io/istio/proxyv2" | wc -l); \ | ||
echo $? | ||
``` | ||
|
||
输出如下: | ||
|
||
```yaml | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: deploy1 | ||
spec: | ||
template: | ||
metadata: | ||
labels: | ||
old-label: old-value | ||
spec: | ||
containers: | ||
- args: | ||
- proxy | ||
- sidecar | ||
image: docker.io/istio/proxyv2 | ||
name: istio-proxy | ||
- args: | ||
- one | ||
- two | ||
image: nginx | ||
name: nginx | ||
--- | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: deploy2 | ||
spec: | ||
template: | ||
metadata: | ||
labels: | ||
key: value | ||
spec: | ||
containers: | ||
- args: | ||
- proxy | ||
- sidecar | ||
image: docker.io/istio/proxyv2 | ||
name: istio-proxy | ||
- image: busybox | ||
name: busybox | ||
``` | ||
|
||
## Target selector | ||
|
||
- 选择名称与 `name*` 匹配的资源 | ||
```yaml | ||
target: | ||
name: name* | ||
``` | ||
- 选择所有 Deployment 资源 | ||
```yaml | ||
target: | ||
kind: Deployment | ||
``` | ||
- 选择 label 与 `app=hello` 匹配的资源 | ||
```yaml | ||
target: | ||
labelSelector: app=hello | ||
``` | ||
- 选择 annotation 与 `app=hello` 匹配的资源 | ||
```yaml | ||
target: | ||
annotationSelector: app=hello | ||
``` | ||
- 选择所有 label 与 `app=hello` 匹配的 Deployment 资源 | ||
```yaml | ||
target: | ||
kind: Deployment | ||
labelSelector: app=hello | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# remote targets | ||
|
||
`kustomize build` 可以将 URL 作为参数传入并运行. | ||
|
||
运行效果与 clone repo,checkout 特定的 _ref_(commit hash, branch 名称, release tag 等),然后针对所需的目录运行 `kustomize build`。 | ||
|
||
如果想要要立即尝试此操作,可以按照 [multibases](../multibases/README.md) 示例运行 kustomization 运行构建。然后查看输出中的pod: | ||
|
||
<!-- @remoteOverlayBuild @testAgainstLatestRelease --> | ||
|
||
```bash | ||
target="github.com/kubernetes-sigs/kustomize//examples/multibases/dev/?ref=v1.0.6" | ||
test 1 == \ | ||
$(kustomize build $target | grep dev-myapp-pod | wc -l); \ | ||
echo $? | ||
``` | ||
|
||
在该示例中运行 overlay 将获得三个 pod(在此 overlay 结合了dev、staging 和 prod 的 bases,以便同时将它们全部发送给所有人): | ||
|
||
<!-- @remoteBuild @testAgainstLatestRelease --> | ||
```bash | ||
target="https://github.com/kubernetes-sigs/kustomize//examples/multibases?ref=v1.0.6" | ||
test 3 == \ | ||
$(kustomize build $target | grep cluster-a-.*-myapp-pod | wc -l); \ | ||
echo $? | ||
``` | ||
|
||
将 URL 作为 base : | ||
|
||
<!-- @createOverlay @testAgainstLatestRelease --> | ||
```bash | ||
DEMO_HOME=$(mktemp -d) | ||
|
||
cat <<EOF >$DEMO_HOME/kustomization.yaml | ||
resources: | ||
- github.com/kubernetes-sigs/kustomize//examples/multibases?ref=v1.0.6 | ||
namePrefix: remote- | ||
EOF | ||
``` | ||
|
||
构建该 base 以确定所有的三个 pod 都有 `remote-` 前缀。 | ||
|
||
<!-- @remoteBases @testAgainstLatestRelease --> | ||
```bash | ||
test 3 == \ | ||
$(kustomize build $DEMO_HOME | grep remote-.*-myapp-pod | wc -l); \ | ||
echo $? | ||
``` | ||
|
||
## URL format | ||
|
||
URL 需要遵循 [hashicorp/go-getter URL 格式](https://github.com/hashicorp/go-getter#url-format) 。下面是一些遵循此约定的 Github repos 示例url。 | ||
|
||
- kustomization.yaml 在根目录 | ||
|
||
`github.com/Liujingfang1/mysql` | ||
- kustomization.yaml 在 test 分支的根目录 | ||
|
||
`github.com/Liujingfang1/mysql?ref=test` | ||
- kustomization.yaml 在 v1.0.6 版本的子目录 | ||
|
||
`github.com/kubernetes-sigs/kustomize//examples/multibases?ref=v1.0.6` | ||
- kustomization.yaml repoUrl2 分支的子目录 | ||
|
||
`github.com/Liujingfang1/kustomize//examples/helloWorld?ref=repoUrl2` | ||
- kustomization.yaml commit `7050a45134e9848fca214ad7e7007e96e5042c03` 的子目录 | ||
|
||
`github.com/Liujingfang1/kustomize//examples/helloWorld?ref=7050a45134e9848fca214ad7e7007e96e5042c03` |