kustomize来生成相应的k8s的相关配置

1、configmap

复制代码
使用文件生成configmap
#生成一个  application.properties 文件
cat <<EOF >application.properties
FOO=Bar
EOF

cat <<EOF >./kustomization.yaml
configMapGenerator:
- name: example-configmap-1
  files:
  - application.properties
EOF

基于键值对生成configmap
cat <<EOF >./kustomization.yaml
configMapGenerator:
- name: example-configmap-2
  literals:
  - FOO=Bar
EOF

2、在deployment中引用

复制代码
cat <<EOF >application.properties
FOO=Bar
EOF

cat <<EOF >deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
  labels:
    app: my-app
spec:
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: app
        image: my-app
        volumeMounts:
        - name: config
          mountPath: /config
      volumes:
      - name: config
        configMap:
          name: example-configmap-1
EOF

cat <<EOF >./kustomization.yaml
resources:
- deployment.yaml
configMapGenerator:
- name: example-configmap-1
  files:
  - application.properties
EOF

3、secret的创建

复制代码
基于文件的创建
# 创建一个 password.txt 文件
cat <<EOF >./password.txt
username=admin
password=secret
EOF

cat <<EOF >./kustomization.yaml
secretGenerator:
- name: example-secret-1
  files:
  - password.txt
EOF

基于键值对的创建
cat <<EOF >./kustomization.yaml
secretGenerator:
- name: example-secret-2
  literals:
  - username=admin
  - password=secret
EOF

4、贯穿性的字段应用在整个文件中的所有位置

复制代码
# 创建一个 deployment.yaml
cat <<EOF >./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
EOF

cat <<EOF >./kustomization.yaml
namespace: my-namespace
namePrefix: dev-
nameSuffix: "-001"
commonLabels:
  app: bingo
commonAnnotations:
  oncallPager: 800-555-1212
resources:
- deployment.yaml
EOF

最终结果
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    oncallPager: 800-555-1212
  labels:
    app: bingo
  name: dev-nginx-deployment-001
  namespace: my-namespace
spec:
  selector:
    matchLabels:
      app: bingo
  template:
    metadata:
      annotations:
        oncallPager: 800-555-1212
      labels:
        app: bingo
    spec:
      containers:
      - image: nginx
        name: nginx

kustomize edit set [command] [arguments]

可用命令

image: 更新或添加镜像。

label: 更新或添加标签。

annotation: 更新或添加注解。

replicas: 更新或添加副本数。

resource: 添加资源文件。

transformer: 添加转换器。

5、如果有多环境可以使用基准的方式

复制代码
# 创建一个包含基准的目录
mkdir base
# 创建 base/deployment.yaml
cat <<EOF > base/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
spec:
  selector:
    matchLabels:
      run: my-nginx
  replicas: 2
  template:
    metadata:
      labels:
        run: my-nginx
    spec:
      containers:
      - name: my-nginx
        image: nginx
EOF

# 创建 base/service.yaml 文件
cat <<EOF > base/service.yaml
apiVersion: v1
kind: Service
metadata:
  name: my-nginx
  labels:
    run: my-nginx
spec:
  ports:
  - port: 80
    protocol: TCP
  selector:
    run: my-nginx
EOF

# 创建 base/kustomization.yaml
cat <<EOF > base/kustomization.yaml
resources:
- deployment.yaml
- service.yaml
EOF
此基准可在多个覆盖中使用。你可以在不同的覆盖中添加不同的 namePrefix 或其他贯穿性字段。 下面是两个使用同一基准的覆盖:

mkdir dev
cat <<EOF > dev/kustomization.yaml
resources:
- ../base
namePrefix: dev-
EOF

mkdir prod
cat <<EOF > prod/kustomization.yaml
resources:
- ../base
namePrefix: prod-
EOF

字段 解释

namespace 为所有资源添加名字空间

namePrefix 此字段的值将被添加到所有资源名称前面

nameSuffix 此字段的值将被添加到所有资源名称后面

commonLabels 要添加到所有资源和选择算符的标签

commonAnnotations 要添加到所有资源的注解

resources 列表中的每个条目都必须能够解析为现有的资源配置文件

configMapGenerator 列表中的每个条目都会生成一个 ConfigMap

secretGenerator 列表中的每个条目都会生成一个 Secret

generatorOptions 更改所有 ConfigMap 和 Secret 生成器的行为

bases 列表中每个条目都应能解析为一个包含 kustomization.yaml 文件的目录

patchesStrategicMerge 列表中每个条目都能解析为某 Kubernetes 对象的策略性合并补丁

patchesJson6902 列表中每个条目都能解析为一个 Kubernetes 对象和一个 JSON 补丁

vars 每个条目用来从某资源的字段来析取文字

images 每个条目都用来更改镜像的名称、标记与/或摘要,不必生成补丁

configurations 列表中每个条目都应能解析为一个包含 Kustomize 转换器配置 的文件

crds 列表中每个条目都应能够解析为 Kubernetes 类别的 OpenAPI 定义文件

相关推荐
阿寻寻21 分钟前
【云原生技术】探针**就是:Kubelet(K8s 节点上的组件)会**进入容器里执行一条命令**,根据命令的退出码判断健康
云原生·kubernetes·kubelet
迎仔1 小时前
11-裸金属算力中心:K8s的实际价值与“管一切“的体现
云原生·容器·kubernetes
岁岁种桃花儿1 小时前
kubeadm构建单master多Node的k8s集群。
云原生·容器·kubernetes
礼拜天没时间.2 小时前
Docker Compose 实战:从单容器命令到多服务编排
运维·网络·docker·云原生·容器·centos
hrhcode2 小时前
【云原生】二.Kubernetes基础入门:架构详解与环境搭建
云原生·k8s
志栋智能2 小时前
智能巡检自动化解决方案:从“人海战术”到“AI智巡”的效能革命
大数据·运维·人工智能·网络安全·云原生·自动化
only_Klein2 小时前
kubernetes-Service
云原生·容器·kubernetes
迎仔3 小时前
10-算力中心运维三剑客:Ansible + Jenkins + K8s 高效实战
运维·kubernetes·ansible·jenkins
切糕师学AI3 小时前
成本治理(Cloud Cost Governance)是什么?
云原生·云计算
志栋智能3 小时前
AI驱动的监控系统自动化巡检:从“告警噪音”到“业务洞察”的智能跃迁
运维·人工智能·网络安全·云原生·自动化