LoginSignup
0
0

More than 3 years have passed since last update.

jsonnetでkubernetesのyamlファイルを環境ごとに管理する

Last updated at Posted at 2021-06-04

jsonnetを使う意味

  • yamlだと値がダブってしまう
  • 環境ごとで使い分けるためにkustomizeというのがあるが難しい

ソース

  • jsonnetコマンドに引数で環境変数を渡せるので、分岐させて環境ごとの変数を埋め込むようにする
  • std.manifestYamlStreamを使うことで、リソースの区切り文字(---)も出力してくれるので、ひとまとまりで1つのファイルで管理することができる
  • env.jsonnetファイルをvar変数にimportすることで、var.portみたいな感じで使える
  • local appnameとすることでそのファイル内で変数として使える
env.jsonnet
if std.extVar('ENV') == 'dev' then
{
  port: 80,
  allow_ipaddress: ['12.34.56.78/32', '87.65.43.21/32'],
}
else if std.extVar('ENV') == 'stag' then
{
  port: 80,
  allow_ipaddress: ['22.34.56.78/32', '87.65.43.21/32'],
}
else if std.extVar('ENV') == 'prod' then
{
  port: 80,
  allow_ipaddress: ['32.34.56.78/32', '87.65.43.21/32'],
}
nginx.jsonnet
local var = (import 'env.jsonnet');

local appname = 'nginx-nlb';

local development = {
  apiVersion: 'apps/v1',
  kind: 'ReplicaSet',
  metadata: {
    name: 'nginx-replicaset-nlb',
  },
  spec: {
    replicas: 3,
    selector: {
      matchLabels: {
        app: appname,
      },
    },
    template: {
      metadata: {
        labels: {
          app: appname,
        },
      },
      spec: {
        containers: [
          {
            name: appname + '-container',
            image: 'nginx:1.12',
            ports: [
              {
                containerPort: 30082,
              },
            ],
          },
        ],
      },
    },
  },
};

local service = {
  apiVersion: 'v1',
  kind: 'Service',
  metadata: {
    name: 'aws-nlb',
    annotations: {
      'service.beta.kubernetes.io/aws-load-balancer-type': 'nlb',
    },
  },
  spec: {
    type: 'LoadBalancer',
    ports: [
      {
        name: 'http-port',
        protocol: 'TCP',
        port: var.port,
        targetPort: var.port,
        nodePort: 30082,
      },
    ],
    selector: {
      app: 'nginx-nlb',
    },
    loadBalancerSourceRanges: var.allow_ipaddress,
  },
};

std.manifestYamlStream(
  [development, service],
  indent_array_in_object=false,
  c_document_end=true
)

使うとき

  • --ext-strにつなげる
jsonnet -S nginx.jsonnet --ext-str ENV=dev

出力

---
"apiVersion": "apps/v1"
"kind": "ReplicaSet"
"metadata":
  "name": "nginx-replicaset-nlb"
"spec":
  "replicas": 3
  "selector":
    "matchLabels":
      "app": "nginx"
  "template":
    "metadata":
      "labels":
        "app": "nginx"
    "spec":
      "containers":
      - "image": "nginx:1.12"
        "name": "nginx-container"
        "ports":
        - "containerPort": 30082
---
"apiVersion": "v1"
"kind": "Service"
"metadata":
  "annotations":
    "service.beta.kubernetes.io/aws-load-balancer-type": "nlb"
  "name": "aws-nlb"
"spec":
  "loadBalancerSourceRanges":
  - "12.34.56.78/32"
  - "87.65.43.21/32"
  "ports":
  - "name": "http-port"
    "nodePort": 30082
    "port": 80
    "protocol": "TCP"
    "targetPort": 80
  "selector":
    "app": "nginx-nlb"
  "type": "LoadBalancer"
...

kubectl applyするとき

jsonnet -S nginx.jsonnet --ext-str ENV=dev | kubectl apply -f -
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0