zl程序教程

您现在的位置是:首页 >  后端

当前栏目

云原生(三十九) | Kubernetes篇之kustomize入门了解

Kubernetes入门 了解 原生
2023-06-13 09:14:14 时间

​kustomize入门了解

一、kustomize是什么

Kubernetes本地的配置管理工具,相当于轻量版的helm。

官网地址:Kustomize - Kubernetes native configuration management

以后我们公司自己部署的一些中间件等,可以封装为 kustomize 管理的文件结构。 只需要kubectl apply -k 即可快速部署不同环境应用

二、用法

1、文件结构

2、文件内容

#service.yaml
kind: Service
apiVersion: v1
metadata:
  name: the-service
spec:
  selector:
    deployment: hello
  type: ClusterIP
  ports:
  - protocol: TCP
    port: 8666
    targetPort: 8080

#kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
metadata:
  name: arbitrary
# Example configuration for the webserver
# at https://github.com/monopole/hello
commonLabels:
  app: hello  # 构建出来的每个资源上都有app=hello标签
resources:
- deployment.yaml
- service.yaml
- configMap.yaml

#configMap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: the-map
data:
  altGreeting: "Good Morning!"
  enableRisky: "false"

#deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: the-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      deployment: hello
  template:
    metadata:
      labels:
        deployment: hello
    spec:
      containers:
      - name: the-container
        image: monopole/hello:1
        command: ["/hello",
                  "--port=8080",
                  "--enableRiskyFeature=$(ENABLE_RISKY)"]
        ports:
        - containerPort: 8080
        env:
        - name: ALT_GREETING
          valueFrom:
            configMapKeyRef:
              name: the-map
              key: altGreeting
        - name: ENABLE_RISKY
          valueFrom:
            configMapKeyRef:
              name: the-map
              key: enableRisky

3、使用

kubectl apply -k demo/

4、注意事项

  • kustomization.yaml 文件名是固定的;
  • kubectl apply -k path 会自动找path下的kustomization.yaml

5、高级-环境分离

  • 创建 overlay,分离各个环境。原来的可以抽取为base环境。其他环境层可只定义变量覆盖
  • 每个环境层定义自己的 kustomization.yaml
  • 新的层级结构
#production/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namePrefix: production-
commonLabels:
  variant: production
  org: acmeCorporation
commonAnnotations:
  note: Hello, I am production!
bases:
- ../../base
patchesStrategicMerge:
- deployment.yaml

#production/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: the-deployment
spec:
  replicas: 10
## 只需要定义可变部分

#staging/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namePrefix: staging-   #所有资源的前缀
commonLabels:   #所有资源的标签
  variant: staging   
  org: acmeCorporation
commonAnnotations:  #所有资源的注解
  note: Hello, I am staging!
bases:
- ../../base  #基础配置的位置
patchesStrategicMerge:
- map.yaml  #需要额外引入部署的内容,如果引入的内容基础内容有配置,则使用这个最新的

#staging/map.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: the-map
data:
  altGreeting: "Have a pineapple!"
  enableRisky: "true"

  • 执行命令
kubectl apply -k overlays/staging -n hello   #可以在部署的时候统一制定名称空间

kustomzition文件能写的内容