补丁实战
策略合并补丁
基准文件:/test/bases/deploy.yml
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: shark
name: my-nginx
spec:
selector:
matchLabels:
run: my-nginx
replicas: 2
template:
metadata:
labels:
run: my-nginx
spec:
containers:
- name: my-nginx
image: nginx
ports:
- containerPort: 80
覆盖中的补丁文件:/test/dev/add_my-nginx2.yml
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: shark
name: my-nginx
spec:
selector:
matchLabels:
run: my-nginx
replicas: 3
template:
metadata:
labels:
run: my-nginx
spec:
containers:
- name: my-nginx2
image: nginx
ports:
- containerPort: 80
覆盖中的 /test/dev/kustomization.yaml
yaml
resources:
- ../bases
patches:
- path: add_my-nginx2.yml
执行构建
bash
kubectl kustomize /test/dev
得到如下 YAML
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
namespace: shark
spec:
replicas: 3
selector:
matchLabels:
run: my-nginx
template:
metadata:
labels:
run: my-nginx
spec:
containers:
- image: nginx # 补丁新增
name: my-nginx2
ports:
- containerPort: 80
- image: nginx
name: my-nginx
ports:
- containerPort: 80
JSON 补丁
非所有资源或者字段都支持策略性合并补丁。为了支持对任何资源的任何字段进行修改, Kustomize 提供通过 patchesJson6902 来应用 JSON 补丁的能力。 为了给 JSON 补丁找到正确的资源,需要在 kustomization.yaml 文件中指定资源的组(group)、 版本(version)、类别(kind)和名称(name)。
示例如下:
基准中的 Ingress 对象的文件: /test/bases/shark-ing.yml
yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
namespace: shark
name: shark-ingress-gateway
spec:
ingressClassName: nginx
rules:
- http:
paths:
- path: "/spms-standard"
pathType: Prefix
backend:
service:
name: shark-gateway
port:
number: 8080
覆盖中的 Ingress 对象的 JSON 补丁文件: /test/dev/shark-ing.yml
注意: 这里是增加了两个
yaml
- op: add
path: /spec/rules/0/http/paths/-
value:
path: "/spms-digital"
pathType: Prefix
backend:
service:
name: shark-gateway
port:
number: 8080
- op: add
path: /spec/rules/0/http/paths/-
value:
path: "/spms-shark"
pathType: Prefix
backend:
service:
name: shark-gateway
port:
number: 8080
- op 是预算符,值可以是:
add
remove
replace
- path 是被操作的对象的YAML文件的路径
/spec/rules/0/http/paths/
对象如下内容:
yaml
spec:
rules:
- http:
paths:
列表中的第一个元素的索引是 0
, -
表示列表的最后位置
覆盖中的 /test/dev/kustomization.yaml
yaml
resources:
- ../bases
patches:
- target:
group: networking.k8s.io
version: v1
kind: Ingress
namespace: shark
name: shark-ingress-gateway
path: shark-ing.yml
以上文件中的
group
对应基准中的 Ingress 对象文件:/test/bases/shark-ing.yml
中apiVersion
的值中networking.k8s.io
部分。version
对应基准中的 Ingress 对象文件:/test/bases/shark-ing.yml
中apiVersion
的值中v1
部分。kind
对应基准中的 Ingress 对象文件:/test/bases/shark-ing.yml
中kind
的值。namespace
对应基准中的 Ingress 对象文件:/test/bases/shark-ing.yml
中namespace
的值。name
对应基准中的 Ingress 对象文件:/test/bases/shark-ing.yml
中name
的值。
执行构建
bash
kubectl kustomize /test/dev
将会得到如下完整的 YAML 文件
yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: shark-ingress-gateway
namespace: shark
spec:
ingressClassName: nginx
rules:
- http:
paths:
- backend:
service:
name: shark-gateway
port:
number: 8080
path: /spms-standard
pathType: Prefix
- backend:
service:
name: shark-gateway
port:
number: 8080
path: /spms-digital
pathType: Prefix
- backend:
service:
name: shark-gateway
port:
number: 8080
path: /spms-shark
pathType: Prefix