springboot在k8s使用configmap为配置中心

spring boot 在 k8s 使用 kubernets configmap作为配置中心

引用& 参考

https://amit4aws.medium.com/spring-cloud-kubernetes-configwatcher-7a206fe269e3

https://github.com/spring-cloud/spring-cloud-kubernetes#kubernetes-propertysource-implementations

https://github.com/amit4aws/ConfigWatcher/tree/main/demo

https://www.cnblogs.com/larrydpk/p/13611431.html

spring boot 服务程序读取 kubernets configmap 配置

 <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-kubernetes-config</artifactId>
   <version>1.1.9.RELEASE</version>
 </dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

Spring boot 版本使用:

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.11.RELEASE</version>

创建项目配置文件 bootstrap.yml

spring:
  application:
    name: projectName
  cloud:
    kubernetes:
      reload:
        
k8s中的ConfigMap是一种用于存储配置数据的资源对象,可以在容器中使用这些配置数据。PV(Persistent Volume)是一种持久化存储资源,用于将存储卷与Pod进行绑定。下面是关于k8s中的nginx ConfigMap和PV的介绍和演示: 1. 创建nginx ConfigMap: ```shell kubectl create configmap nginx-config --from-file=/etc/config/ ``` 这将创建一个名为nginx-configConfigMap,并将/etc/config/目录下的所有文件作为配置数据。 2. 查看ConfigMap信息: ```shell kubectl get configmap nginx-config ``` 这将显示名为nginx-configConfigMap的详细信息。 3. 在Pod中使用ConfigMap: 可以在Pod的配置文件中使用ConfigMap来注入配置数据。例如,在Pod的配置文件中添加以下内容: ```yaml apiVersion: v1 kind: Pod metadata: name: nginx-pod spec: containers: - name: nginx-container image: nginx volumeMounts: - name: config-volume mountPath: /etc/config volumes: - name: config-volume configMap: name: nginx-config ``` 这将在Pod中创建一个名为config-volume的卷,并将nginx-config ConfigMap的配置数据挂载到Pod的/etc/config目录下。 4. 创建PV: ```shell kubectl apply -f pv.yaml ``` 其中pv.yaml是一个包含PV定义的YAML文件。 5. 查看PV信息: ```shell kubectl get pv ``` 这将显示所有PV的详细信息。 6. 将PV与Pod绑定: 可以在Pod的配置文件中使用PV来绑定持久化存储。例如,在Pod的配置文件中添加以下内容: ```yaml apiVersion: v1 kind: Pod metadata: name: nginx-pod spec: containers: - name: nginx-container image: nginx volumeMounts: - name: data-volume mountPath: /data volumes: - name: data-volume persistentVolumeClaim: claimName: my-pvc ``` 这将在Pod中创建一个名为data-volume的卷,并将名为my-pvc的PersistentVolumeClaim与之绑定。
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值