4

我们以这种方式将 kustomize 用于我们的 kubernetes 部署:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:${IMAGE_VERSION}
        ports:
        - containerPort: 80

并部署此 yaml 用 1.7.9 替换变量 IMAGE_VERSION

kustomize build ./nginx/overlays/dev/ | sed -e 's|${IMAGE_VERSION}'"|1.7.9|g" | kubectl apply -f -

由于 kubectl 1.14 支持 kustomize。

现在我们可以像这样做一些非常好的事情

kubectl apply -k ./

但是如何用这个新命令替换 IMAGE_VERSION 变量?

4

1 回答 1

3

您必须创建一个kustomization.yaml包含自定义项的文件。

IE:

# kustomization.yaml
bases:
- ../base
images:
  - name: nginx-pod
    newTag: 1.15
    newName: nginx-pod-2

对于模板,您创建一个包含 kustomization.yaml 的基本文件夹,并参考部署和依赖项,即:

# ../base/kustomization.yaml
resources:
- deployment.yaml

# ../base/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx-pod

运行命令:

kubectl apply -k

上述命令将编译定制并生成以下 yaml 以应用于集群:

# Modified Base Resource
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      # The image image tag has been changed for the container
      - name: nginx
        image: nginx-pod-2:1.15
于 2019-04-23T10:55:37.643 回答