K8S(12)配置中心實戰-多環境交付apollo三組件

k8s配置中心實戰-多環境交付apollo三組件

1.環境準備工做

先刪除infra名稱空間中部署的apollo服務java

kubectl delete -f http://k8s-yaml.zq.com/apollo-configservice/dp.yaml
kubectl delete -f http://k8s-yaml.zq.com/apollo-adminservice/dp.yaml
kubectl delete -f http://k8s-yaml.zq.com/apollo-portal/dp.yaml

要進行分環境,須要將現有實驗環境進行拆分node

  1. portal服務,能夠各個環境共用,只部署一套
  2. adminservice和configservice必需要分開每一個環境一套
  3. zk和namespace也要區分環境

1.1 zk環境拆分

zk拆分最簡單,只須要在dns那裏修改解析規則便可:
同時添加好apollo、dubbo兩個環境的域名解析mysql

vi /var/named/zq.com.zone
zk-test				A    10.4.7.11
zk-prod				A    10.4.7.12
apollo-testconfig	A    10.4.7.10
apollo-prodconfig	A    10.4.7.10
dubbo-testdemo		A    10.4.7.10
dubbo-proddemo		A    10.4.7.10

# 重啓服務
systemctl restart named.service

1.2 namespace分環境

分別建立test 和prod兩個名稱空間git

kubectl create ns test
kubectl create ns prod

給新名稱空間建立secret受權web

kubectl create secret docker-registry harbor \
    --docker-server=harbor.zq.com \
    --docker-username=harbor \
    --docker-password=Harbor12345 \
    -n test

kubectl create secret docker-registry harbor \
    --docker-server=harbor.zq.com \
    --docker-username=harbor \
    --docker-password=Harbor12345 \
    -n prod

1.3 數據庫拆分

因實驗資源有限,故使用分庫的形式模擬分環境sql

1.3.1 修改初始化腳本並導入

數據庫再7.11上哦
分別建立ApolloConfigTestDB和ApolloConfigProdDBdocker

# 複製爲新sql
cp apolloconfig.sql apolloconfigTest.sql
cp apolloconfig.sql apolloconfigProd.sql

# 替換關鍵字
sed -i 's#ApolloConfigDB#ApolloConfigTestDB#g' apolloconfigTest.sql
sed -i 's#ApolloConfigDB#ApolloConfigProdDB#g' apolloconfigProd.sql

# 導入數據庫
mysql -uroot -p123456 < apolloconfigTest.sql
mysql -uroot -p123456 < apolloconfigProd.sql

1.3.2 修改數據庫中eureka地址

這裏用到了兩個新的域名,域名解析已經在添加zk域名那裏一塊兒加了數據庫

mysql -uroot -p123456

# 1.修改eureka註冊中心配置
> update ApolloConfigProdDB.ServerConfig set ServerConfig.Value="http://apollo-prodconfig.zq.com/eureka" where ServerConfig.Key="eureka.service.url";
> update ApolloConfigTestDB.ServerConfig set ServerConfig.Value="http://apollo-testconfig.zq.com/eureka" where ServerConfig.Key="eureka.service.url";

# 2.在portl庫中增長支持fat環境和pro環境
> update ApolloPortalDB.ServerConfig set Value='fat,pro' where Id=1;

# 3.受權數據庫訪問用戶
> grant INSERT,DELETE,UPDATE,SELECT on ApolloConfigProdDB.* to "apollo"@"10.4.7.%";
> grant INSERT,DELETE,UPDATE,SELECT on ApolloConfigTestDB.* to "apollo"@"10.4.7.%";

1.4 變更原有資源配置啓動

1.4.1 修改portal的cm資源配置清單

7.200運維機操做,增長兩個新環境的支持瀏覽器

cd /data/k8s-yaml/apollo-portal/
sed -i '$a\    fat.meta=http://apollo-testconfig.zq.com' cm.yaml
sed -i '$a\    pro.meta=http://apollo-prodconfig.zq.com' cm.yaml

# 查看結果
apollo-portal]# tail -4 cm.yaml
  apollo-env.properties: |
    dev.meta=http://apollo-config.zq.com
    fat.meta=http://apollo-testconfig.zq.com
    pro.meta=http://apollo-prodconfig.zq.com

1.4.2 任意節點應用修改

kubectl apply -f http://k8s-yaml.zq.com/apollo-portal/cm.yaml

2 部署新環境的APOLLO服務

7.200運維機操做,app

2.1 先建立出所需目錄和文件

cd /data/k8s-yaml
mkdir -p test/{apollo-adminservice,apollo-configservice,dubbo-demo-server,dubbo-demo-consumer}
mkdir -p prod/{apollo-adminservice,apollo-configservice,dubbo-demo-server,dubbo-demo-consumer}

# 查看結果
k8s-yaml]# ll {test,prod}
prod:
total 0
drwxr-xr-x 2 root root 6 May 13 22:50 apollo-adminservice
drwxr-xr-x 2 root root 6 May 13 22:50 apollo-configservice
drwxr-xr-x 2 root root 6 May 13 22:50 dubbo-demo-consumer
drwxr-xr-x 2 root root 6 May 13 22:50 dubbo-demo-server

test:
total 0
drwxr-xr-x 2 root root 6 May 13 22:50 apollo-adminservice
drwxr-xr-x 2 root root 6 May 13 22:50 apollo-configservice
drwxr-xr-x 2 root root 6 May 13 22:50 dubbo-demo-consumer
drwxr-xr-x 2 root root 6 May 13 22:50 dubbo-demo-server

將以前的資源配置清單cp到對應環境的目錄中,便於修改:

cp /data/k8s-yaml/apollo-configservice/* ./test/apollo-configservice/
cp /data/k8s-yaml/apollo-configservice/* ./prod/apollo-configservice/

cp /data/k8s-yaml/apollo-adminservice/* ./test/apollo-adminservice/
cp /data/k8s-yaml/apollo-adminservice/* ./prod/apollo-adminservice/

2.2 部署test環境的configservice

2.2.1 修改configmap資源清單

要修改ns,數據庫庫名,eureka地址

cd /data/k8s-yaml/test/apollo-configservice/
sed -ri 's#(namespace:) infra#\1 test#g' cm.yaml
sed -i 's#ApolloConfigDB#ApolloConfigTestDB#g' cm.yaml
sed -i 's#apollo-config.zq.com#apollo-testconfig.zq.com#g' cm.yaml

2.2.2 修改dp,ns,inress資源清單

# 1.dp只須要修改namesapce空間
sed -ri 's#(namespace:) infra#\1 test#g' dp.yaml

# 2.svc一樣只須要修改namespace
sed -ri 's#(namespace:) infra#\1 test#g' svc.yaml

# 3.ingress須要修改namespace和域名 
sed -ri 's#(namespace:) infra#\1 test#g' ingress.yaml
sed -i 's#apollo-config.zq.com#apollo-testconfig.zq.com#g' ingress.yaml

2.2.3 應用資源配置

任意節點應用配置

kubectl apply -f http://k8s-yaml.zq.com/test/apollo-configservice/cm.yaml
kubectl apply -f http://k8s-yaml.zq.com/test/apollo-configservice/dp.yaml
kubectl apply -f http://k8s-yaml.zq.com/test/apollo-configservice/svc.yaml
kubectl apply -f http://k8s-yaml.zq.com/test/apollo-configservice/ingress.yaml

瀏覽器檢查

瀏覽器輸入http://apollo-testconfig.zq.com,查看測試環境的apollo註冊中心是否已有服務註冊img

服務已經註冊進來了

2.3 部署test環境的adminservice

2.3.1 修改configmap資源清單

要修改ns,數據庫庫名,eureka地址

cd /data/k8s-yaml/test/apollo-adminservice/
sed -ri 's#(namespace:) infra#\1 test#g' cm.yaml
sed -i 's#ApolloConfigDB#ApolloConfigTestDB#g' cm.yaml
sed -i 's#apollo-config.zq.com#apollo-testconfig.zq.com#g' cm.yaml

2.3.2 修改dp資源清單

# 1.dp只須要修改namesapce空間
sed -ri 's#(namespace:) infra#\1 test#g' dp.yaml

2.3.3 應用資源配置清單

kubectl apply -f http://k8s-yaml.zq.com/test/apollo-adminservice/cm.yaml
kubectl apply -f http://k8s-yaml.zq.com/test/apollo-adminservice/dp.yaml

瀏覽器檢查:
瀏覽器輸入http://apollo-testconfig.zq.com,查看測試環境的apollo註冊中心是否已有adminservice服務註冊

2.4 部署prod環境的configservice

套路基本上都是同樣使用的

2.4.1 修改configmap資源清單

要修改ns,數據庫庫名,eureka地址

cd /data/k8s-yaml/prod/apollo-configservice/
sed -ri 's#(namespace:) infra#\1 prod#g' cm.yaml
sed -i 's#ApolloConfigDB#ApolloConfigProdDB#g' cm.yaml
sed -i 's#apollo-config.zq.com#apollo-prodconfig.zq.com#g' cm.yaml

2.4.2 修改dp,ns,inress資源清單

# 1.dp只須要修改namesapce空間
sed -ri 's#(namespace:) infra#\1 prod#g' dp.yaml

# 2.svc一樣只須要修改namespace
sed -ri 's#(namespace:) infra#\1 prod#g' svc.yaml

# 3.ingress須要修改namespace和域名 
sed -ri 's#(namespace:) infra#\1 prod#g' ingress.yaml
sed -i 's#apollo-config.zq.com#apollo-prodconfig.zq.com#g' ingress.yaml

2.4.3 應用資源配置

任意節點應用配置

kubectl apply -f http://k8s-yaml.zq.com/prod/apollo-configservice/cm.yaml
kubectl apply -f http://k8s-yaml.zq.com/prod/apollo-configservice/dp.yaml
kubectl apply -f http://k8s-yaml.zq.com/prod/apollo-configservice/svc.yaml
kubectl apply -f http://k8s-yaml.zq.com/prod/apollo-configservice/ingress.yaml

瀏覽器檢查

瀏覽器輸入http://apollo-prodconfig.zq.com,查看測試環境的apollo註冊中心是否已有服務註冊img

服務已經註冊進來了

2.5 部署prod環境的adminservice

2.5.1 修改configmap資源清單

要修改ns,數據庫庫名,eureka地址

cd /data/k8s-yaml/prod/apollo-adminservice/
sed -ri 's#(namespace:) infra#\1 prod#g' cm.yaml
sed -i 's#ApolloConfigDB#ApolloConfigProdDB#g' cm.yaml
sed -i 's#apollo-config.zq.com#apollo-prodconfig.zq.com#g' cm.yaml

2.5.2 修改dp資源清單

# 1.dp只須要修改namesapce空間
sed -ri 's#(namespace:) infra#\1 prod#g' dp.yaml

2.5.3 應用資源配置清單

kubectl apply -f http://k8s-yaml.zq.com/prod/apollo-adminservice/cm.yaml
kubectl apply -f http://k8s-yaml.zq.com/prod/apollo-adminservice/dp.yaml

瀏覽器檢查:
瀏覽器輸入http://apollo-prodconfig.zq.com,查看測試環境的apollo註冊中心是否已有adminservice服務註冊

2.6 啓動portal服務

兩個服務都已經註冊進來了後,刪除portal數據庫中存儲的關於以前項目的配置,再來啓動portal項目

2.6.1 清理數據庫

mysql -uroot -p123456
> use ApolloPortalDB;
> truncate table App;
> truncate table AppNamespace;

img

2.6.2 啓動portal

kubectl apply -f http://k8s-yaml.zq.com/apollo-portal/cm.yaml
kubectl apply -f http://k8s-yaml.zq.com/apollo-portal/dp.yaml
kubectl apply -f http://k8s-yaml.zq.com/apollo-portal/svc.yaml
kubectl apply -f http://k8s-yaml.zq.com/apollo-portal/ingress.yaml

2.6.3 驗證部署結果

打開http://apollo-portal.zq.com,建立兩個項目以下:

AppId 應用名稱 部門
dubbo-demo-service dubbo服務提供者 研發部
dubbo-demo-web dubbo服務消費者 運維部

項目建立成功後,能看到左側環境列表中有FATPRO,表示正確

2.6.4 添加test環境的配置

dubbo-demo-service

key value 備註
dubbo.registry zookeeper://zk-test.zq.com:2181 註冊中心地址
dubbo.port 20880 dubbo服務監聽端口

dubbo-demo-web

key value 備註
dubbo.registry zookeeper://zk-test.zq.com:2181 註冊中心地址

2.6.5 添加prod環境的配置

dubbo-demo-service

key value 備註
dubbo.registry zookeeper://zk-prod.zq.com:2181 註冊中心地址
dubbo.port 20880 dubbo服務監聽端口

dubbo-demo-web

key value 備註
dubbo.registry zookeeper://zk-prod.zq.com:2181 註冊中心地址

3 分環境交付dubbo服務

3.1 交付test環境dubbo服務

cd /data/k8s-yaml/
cp ./dubbo-server/*   ./test/dubbo-demo-server/
cp ./dubbo-consumer/* ./test/dubbo-demo-consumer/

3.1.1 修改server資源配置清單

只修改dp的ns配置和apollo配置

cd /data/k8s-yaml/test/dubbo-demo-server
sed -ri 's#(namespace:) app#\1 test#g' dp.yaml
sed -i 's#Denv=dev#Denv=fat#g' dp.yaml
sed -i 's#apollo-config.zq.com#apollo-testconfig.zq.com#g' dp.yaml

任意node應用資源清單:

kubectl apply -f http://k8s-yaml.zq.com/test/dubbo-demo-server/dp.yaml

3.1.2 修改consumer資源配置清單

修改資源清單

cd /data/k8s-yaml/test/dubbo-demo-consumer
# 1.修改dp中的ns配置和apollo配置
sed -ri 's#(namespace:) app#\1 test#g' dp.yaml
sed -i 's#Denv=dev#Denv=fat#g' dp.yaml
sed -i 's#apollo-config.zq.com#apollo-testconfig.zq.com#g' dp.yaml

# 2.修改svc中的ns配置
sed -ri 's#(namespace:) app#\1 test#g' svc.yaml

# 3.修改ingress中的ns配置和域名
sed -ri 's#(namespace:) app#\1 test#g' ingress.yaml
sed -i 's#dubbo-demo.zq.com#dubbo-testdemo.zq.com#g' ingress.yaml

添加域名解析

因爲最開始已經統一作了域名解析,這裏就不單獨添加了
若是沒有添加域名解析的話,須要去添加dubbo-testdemo.zq.com的解析

任意node應用資源清單:

kubectl apply -f http://k8s-yaml.zq.com/test/dubbo-demo-consumer/dp.yaml
kubectl apply -f http://k8s-yaml.zq.com/test/dubbo-demo-consumer/svc.yaml
kubectl apply -f http://k8s-yaml.zq.com/test/dubbo-demo-consumer/ingress.yaml

3.2 交付prod環境dubbo服務

cd /data/k8s-yaml/
cp ./dubbo-server/*   ./prod/dubbo-demo-server/
cp ./dubbo-consumer/* ./prod/dubbo-demo-consumer/

3.2.1 修改server資源配置清單

只修改dp的ns配置和apollo配置

cd /data/k8s-yaml/prod/dubbo-demo-server
sed -ri 's#(namespace:) app#\1 prod#g' dp.yaml
sed -i 's#Denv=dev#Denv=pro#g' dp.yaml
sed -i 's#apollo-config.zq.com#apollo-prodconfig.zq.com#g' dp.yaml

任意node應用資源清單:

kubectl apply -f http://k8s-yaml.zq.com/prod/dubbo-demo-server/dp.yaml

3.2.2 修改consumer資源配置清單

修改資源清單

cd /data/k8s-yaml/prod/dubbo-demo-consumer
# 1.修改dp中的ns配置和apollo配置
sed -ri 's#(namespace:) app#\1 prod#g' dp.yaml
sed -i 's#Denv=dev#Denv=pro#g' dp.yaml
sed -i 's#apollo-config.zq.com#apollo-prodconfig.zq.com#g' dp.yaml

# 2.修改svc中的ns配置
sed -ri 's#(namespace:) app#\1 prod#g' svc.yaml

# 3.修改ingress中的ns配置和域名
sed -ri 's#(namespace:) app#\1 prod#g' ingress.yaml
sed -i 's#dubbo-demo.zq.com#dubbo-proddemo.zq.com#g' ingress.yaml

添加域名解析

因爲最開始已經統一作了域名解析,這裏就不單獨添加了
若是沒有添加域名解析的話,須要去添加dubbo-proddemo.zq.com的解析

任意node應用資源清單:

kubectl apply -f http://k8s-yaml.zq.com/prod/dubbo-demo-consumer/dp.yaml
kubectl apply -f http://k8s-yaml.zq.com/prod/dubbo-demo-consumer/svc.yaml
kubectl apply -f http://k8s-yaml.zq.com/prod/dubbo-demo-consumer/ingress.yaml

3.3 關於dp中的apollo域名

  1. dp.yaml中配置的-Dapollo.meta=http://apollo-testconfig.zq.com
  2. 其實能夠直接使用-Dapollo.meta=http://apollo-configservice:8080
  3. 也直接使用svc資源名稱調用,這樣還能夠少走一次外網解析,至關於走內網
  4. 由於不一樣環境的apollo名稱空間都不同,而svc只在當前namespace中生效

img

4 驗證並模擬發佈

4.1 驗證訪問兩個環境

分別訪問如下域名,看是否能夠出來網頁內容
test:http://dubbo-testdemo.zq.com/hello?name=noah
prod:http://dubbo-proddemo.zq.com/hello?name=noah

4.2 模擬發版:

任意修改碼雲上的dubbo-demo-web項目的say方法返回內容
路徑dubbo-client/src/main/java/com/od/dubbotest/action/HelloAction.java

4.2.1 用jenkins構建新鏡像

參數以下:

參數名 參數值
app_name dubbo-demo-consumer
image_name app/dubbo-demo-consumer
git_repo git@gitee.com:noah-luo/dubbo-demo-web.git
git_ver apollo
add_tag 200513_1808
mvn_dir ./
target_dir ./dubbo-client/target
mvn_cmd mvn clean package -Dmaven.test.skip=true
base_image base/jre8:8u112
maven 3.6.1

4.2.2 發佈test環境

構建成功,而後咱們在測試環境發佈此版本鏡像
修改測試環境的dp.yaml

cd /data/k8s-yaml/test/dubbo-demo-consumer
sed -ri 's#(dubbo-demo-consumer:apollo).*#\1_200513_1808#g' dp.yaml

應用修改後的資源配置清單:

kubectl apply -f http://k8s-yaml.zq.com/test/dubbo-demo-consumer/dp.yaml

訪問http://dubbo-testdemo.zq.com/hello?name=noah看是否有咱們更改的內容

4.2.3 發佈prod環境

鏡像在測試環境測試沒有問題後,直接使用該鏡像發佈生產環境,不在從新打包,避免發生錯誤
一樣修改prod環境的dp.yaml,而且應用該資源配置清單

cd /data/k8s-yaml/prod/dubbo-demo-server
sed -ri 's#(dubbo-demo-consumer:apollo).*#\1_200513_1808#g' dp.yaml

應用修改後的資源配置清單:

kubectl apply -f http://k8s-yaml.zq.com/test/dubbo-demo-consumer/dp.yaml

已經上線到生產環境,這樣一套完整的分環境使用apollo配置中心發佈流程已經可使用了,而且真正作到了一次構建,多平臺使用。

相關文章
相關標籤/搜索