企業(yè)宣傳網(wǎng)站制作外鏈系統(tǒng)
文章目錄
- 1.ConfigMap
- 1.1 創(chuàng)建ConfigMap方式
- 1.2 使用ConfigMap的方式
- 1.3 ConfigMap使用要點建議
- 2 分布式配置中心解決方案
- 2.1 什么時候選擇配置中心
- 2.2 Apollo配置中心系統(tǒng)的能力
- 2.2.1 Apollo創(chuàng)建配置項目
- 2.2.2 項目使用
- 2.2.3 K8s中使用Apollo
1.ConfigMap
ConfigMap是K8s提供的內(nèi)置的配置管理的方案
1.1 創(chuàng)建ConfigMap方式
- 從文件夾創(chuàng)建
- 從文件創(chuàng)建
- 從鍵值對 ? 提供一個鍵值對的文件,將鍵值對的文件內(nèi)容作為ConfigMap的Key和Value
1.2 使用ConfigMap的方式
- 映射為文件 ? 將ConfigMap的Key的value映射為文件
- 映射為環(huán)境變量
- 映射為命令行參數(shù)
1.3 ConfigMap使用要點建議
- 版本化管理配置文件,以支持快速回滾
- 共享配置使用環(huán)境變量注入
ConfigMap的創(chuàng)建腳本代碼
// 創(chuàng)建ConfigMap映射
kubectl create configmap geektime-ordering-api-config --from-file=geektime-ordering-api/configs -o yaml --dry-run | kubectl apply -f -
kubectl create configmap geektime-identity-api-config --from-file=geektime-identity-api/configs -o yaml --dry-run | kubectl apply -f -
kubectl create configmap geektime-mobile-apiaggregator-config --from-file=geektime-mobile-apiaggregator/configs -o yaml --dry-run | kubectl apply -f -
kubectl create configmap geektime-config --from-env-file=env.txt -o yaml --dry-run | kubectl apply -f -
kubectl create configmap geektime-mobile-gateway-config --from-file=geektime-mobile-gateway/configs -o yaml --dry-run | kubectl apply -f -
kubectl create configmap geektime-healthcheckshost-config --from-file=geektime-healthcheckshost/configs -o yaml --dry-run | kubectl apply -f - helm install geektime-ordering-api .\charts\geektime-ordering-api -n default
helm install geektime-identity-api .\charts\geektime-identity-api -n default
helm install geektime-mobile-apiaggregator .\charts\geektime-mobile-apiaggregator -n default
helm install geektime-mobile-gateway .\charts\geektime-mobile-gateway -n default
helm install geektime-healthcheckshost .\charts\geektime-healthcheckshost -n default"Any key to exit" ;
Read-Host | Out-Null ;
Exit
create configmap geektime-ordering-api-config 創(chuàng)建名為geektime-ordering-api-config的ConfigMap
from-file指定一個目錄,將該目錄下的所有文件的文件名做為Key,文件內(nèi)容為Value映射到ConfigMapp中
–from-env-file=env.txt -o yaml --dry-run | kubectl apply -f - 通過Key-Value鍵值對方式創(chuàng)建ConfigMap,比較使用用于定義公共的環(huán)境變量
ConfigMap的使用
定義了兩種方式使用ConfigMap的方式,一種是將其映射到環(huán)境變量中,
env:- name: ENV_ABC // 環(huán)境變量映射方式valueFrom:configMapKeyRef:name: geektime-configkey: ENV_ABC
volumeMounts://存儲卷映射方式,將文件映射到當(dāng)前應(yīng)用目錄下- mountPath: "/app/appsettings.json"name: appsettingssubPath: appsettings-{{.Chart.AppVersion}}.json //subPath指的是ComfigMap的Key....volumes: // 定義存儲卷- name: appsettingsconfigMap:name: {{ include "geektime-mobile-gateway.fullname" . }}-config
定義名為ENV_ABC的環(huán)境變量,valueFrom定義的是configMapKeyRef,也就是通過之前定義的名為geektime-config的ConfigMap,取它的Key值為ENV_ABC
存儲卷定義方式,首先定義一個存儲卷volumes,通過過將ConfigMap映射到存儲卷,意味著這個名為appsettings的存儲卷下面會有ConfigMap中的appsetting配置文件
subPath: appsettings-{{.Chart.AppVersion}}.json 這里使用了Chart.AppVersion變量,是因為建議的做法是鏡像的版本和配置的版本以及Helm的版本都應(yīng)該是一致的,這樣在修改Helm版本后就能讀到對應(yīng)的匹配值版本
如果配置的是環(huán)境變量時,如果配置發(fā)生變更,需要重啟應(yīng)用程序才能獲取到新的配置信息
2 分布式配置中心解決方案
2.1 什么時候選擇配置中心
- 多項目組并行協(xié)作
- 運維開發(fā)分工職能明確
- 對風(fēng)險控制有更高訴求
- 對線上配置熱更新有訴求
2.2 Apollo配置中心系統(tǒng)的能力
- 權(quán)限與審計
- 版本管理
- 熱更新
- 原生支持Java、.Net客戶端
- 目前項目仍然很活躍
2.2.1 Apollo創(chuàng)建配置項目
前置條件,需要安裝docker環(huán)境和docker-compose支持
在當(dāng)前目錄執(zhí)行start.ps1,啟動服務(wù)
dashboard:
http://localhost:8070
用戶名: apollo
密碼: admin
configServer:
http://localhost:8080
start.ps1文件內(nèi)容
docker-compose up
Apollo頁面
點擊"創(chuàng)建項目",創(chuàng)建需要的項目
2.2.2 項目使用
- 項目引用Apollo的包,Com.Ctrip.Framework.Apollo.Configuration
- Program文件添加命名空間,并在CreateHostBuilder中注入
public static IHostBuilder CreateHostBuilder(string[] args) =>Host.CreateDefaultBuilder(args).ConfigureAppConfiguration((hostBuilderContext, configurationBuilder) =>{LogManager.UseConsoleLogging(Com.Ctrip.Framework.Apollo.Logging.LogLevel.Trace);// 定義日志級別//var c = configurationBuilder.Build().GetSection("Apollo").Get<ApolloOptions>();configurationBuilder.AddApollo(configurationBuilder.Build().GetSection("Apollo")).AddDefault(Com.Ctrip.Framework.Apollo.Enums.ConfigFileFormat.Properties);}).ConfigureWebHostDefaults(webBuilder =>{webBuilder.UseStartup<Startup>();});
- 在appsetting.json配置Apollo接點
"Apollo": {"AppId": "geektime-mobile-gateway",// 應(yīng)用程序在配置中心的唯一標(biāo)識"Env": "DEV","MetaServer": "http://172.168.190.76:8080","ConfigServer": [ "http://172.168.190.76:8080" ]},
2.2.3 K8s中使用Apollo
- 在配置中添加Apollo配置
"Apollo": {"AppId": "geektime-mobile-gateway","Env": "DEV","MetaServer": "http://192.168.67.76:8080","ConfigServer": [ "http://192.168.67.76:8080" ]},
- 構(gòu)建鏡像
- 創(chuàng)建ConfigMap
- 訪問獲取