kustomize-多环境部署重用配置文件技巧

在多环境部署中,经常碰到不同的环境下只有deployment中的Image Tag不一样,其他是一样的。这样我们只需要在kustomization.yaml文件中配置重用另外一个环境的配置所有文件并覆盖 image Tag,就可以达到效果,这样可以精简配置文件数量。

例如,测试环境和生产环境只有image的标签不一样:

  • 测试环境标签:hello-1.0.0-rc1
  • 生产环境标签:hello-1.0.0

文件组织结构如下:

 hello
     ├── base
     │   ├── deployment.yaml
     │   ├── kustomization.yaml
     │   └── service.yaml
     ├── production
     │   ├── hello1
     │   │   ├── envs.yaml
     │   │   ├── kustomization.yaml
     │   ├── hello2
     │   │   ├── envs.yaml
     │   │   ├── kustomization.yaml
     │   └── kustomization.yaml
     └── staging
         └── kustomization.yaml

其中:

  • hello/base: 基础文件,staging和production利用base文件夹下的文件为公共模版。
  • hello/staging: 发布到测试环境的配置文件。
  • hello/production: 发布到生产环境的配置文件。

production/hello1/kustomizetion.yaml文件配置内容如下(重用hello/base的文件):

$ cat production/hello1/kustomization.yaml 
namePrefix: hello1-
commonLabels:
  release: hello1
bases:
- ../../base/
patches:
- envs.yaml

production/kustomization.yaml 文件配置如下:

bases:
- ./hello1
- ./hello2

images:
- name: reg.hello.com/hello
  newTag: 1.0.0

在 staging/kustomization.yaml中配置引用production的配置文件,并覆盖image 的Tag,配置如下:

bases:
- ../production

images:
- name: reg.hello.com/hello
  newTag: 1.0.0-RC1

生成的k8s描述文件,只有image的Tag,不一样。(通常RC版发布到测试环境)。

$ kustomize build staging
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: hello
    release: hello1
  name: hello1-hello
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hello
      release: hello
  template:
    metadata:
      labels:
        app: hello
        release: hello1
    spec:
      containers:
        image: reg.hello.com/hello:1.0.0-RC1
......

$ kustomize build production
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: hello
    release: hello1
  name: hello1-hello
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hello
      release: hello
  template:
    metadata:
      labels:
        app: hello
        release: hello1
    spec:
      containers:
        image: reg.hello.com/hello:1.0.0
......

注:kustomizeation的image字段,是根据name查找父配置的image,并覆盖。这里是:reg.hello.com/hello

当然,你也可以在staging/kustomization.yaml 中定义更多字段,如:commonLabels、namePrefix...覆盖或增加更多选项。

更多kustomization字段说明:https://github.com/kubernetes-sigs/kustomize/blob/master/docs/fields.md#transformers

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ansible是一个强大的自动化运维工具,常用于Linux服务器的配置和部署。在LAMP(Linux, Apache, MySQL, PHP)架构中部署,你可以使用Ansible来管理整个环境的配置,包括安装软件包、配置服务、数据库等。Ansible配置文件通常使用YAML(一种人类可读的数据序列化语言)编写,它的核心是playbooks,这是一种包含一系列任务的文本文件。 在Ansible中,一个基本的LAMP部署playbook可能包含以下几个部分: 1. `hosts`: 定义要部署到的服务器列表,可以是主机名或IP地址。 2. `tasks`: 包含一系列`block`,每个`block`代表一个操作,例如安装软件包(`apt`或`yum`)、创建用户或目录、配置Apache或MySQL等。 3. `roles`: 如果有多个任务可以复用,可以将它们组织成角色(role),提高代码的可重用性和可维护性。 4. `vars`: 可以定义变量,如数据库用户名、密码等,这些可以在多个任务中引用。 5. `handlers`: 当某个任务完成后,可能会触发其他任务执行,如重启服务。 一个基本的LAMP部署playbook示例可能如下: ```yaml --- - name: Deploy LAMP stack hosts: web_servers vars: mysql_root_password: 'your_mysql_password' php_version: '7.4' tasks: - name: Install OS packages apt: name: - apache2 - mysql-server - php{{ php_version }} state: present - name: Configure Apache template: src: apache.conf.j2 dest: /etc/apache2/sites- name: Start and enable Apache service: name: apache2 state: started enabled: yes - name: Secure MySQL mysql_user: name: root password: "{{ mysql_root_password }}" priv: '*.*:ALL' grant: 'GRANT' - name: Restart MySQL for changes service: name: mysql state: restarted handlers: - name: restart_apache service: name: apache2 state: restarted ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值