先刪除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
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
分別建立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
因實驗資源有限,故使用分庫的形式模擬分環境sql
數據庫再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
這裏用到了兩個新的域名,域名解析已經在添加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.%";
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
kubectl apply -f http://k8s-yaml.zq.com/apollo-portal/cm.yaml
7.200
運維機操做,app
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/
要修改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
# 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
任意節點應用配置
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註冊中心是否已有服務註冊
服務已經註冊進來了
要修改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
# 1.dp只須要修改namesapce空間 sed -ri 's#(namespace:) infra#\1 test#g' dp.yaml
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服務註冊
套路基本上都是同樣使用的
要修改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
# 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
任意節點應用配置
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註冊中心是否已有服務註冊
服務已經註冊進來了
要修改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
# 1.dp只須要修改namesapce空間 sed -ri 's#(namespace:) infra#\1 prod#g' dp.yaml
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服務註冊
兩個服務都已經註冊進來了後,刪除portal數據庫中存儲的關於以前項目的配置,再來啓動portal項目
mysql -uroot -p123456 > use ApolloPortalDB; > truncate table App; > truncate table AppNamespace;
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
打開http://apollo-portal.zq.com,建立兩個項目以下:
AppId | 應用名稱 | 部門 |
---|---|---|
dubbo-demo-service | dubbo服務提供者 | 研發部 |
dubbo-demo-web | dubbo服務消費者 | 運維部 |
項目建立成功後,能看到左側環境列表中有FAT
和PRO
,表示正確
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 | 註冊中心地址 |
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 | 註冊中心地址 |
cd /data/k8s-yaml/ cp ./dubbo-server/* ./test/dubbo-demo-server/ cp ./dubbo-consumer/* ./test/dubbo-demo-consumer/
只修改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
修改資源清單
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
cd /data/k8s-yaml/ cp ./dubbo-server/* ./prod/dubbo-demo-server/ cp ./dubbo-consumer/* ./prod/dubbo-demo-consumer/
只修改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
修改資源清單
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
-Dapollo.meta=http://apollo-testconfig.zq.com
-Dapollo.meta=http://apollo-configservice:8080
分別訪問如下域名,看是否能夠出來網頁內容
test:http://dubbo-testdemo.zq.com/hello?name=noah
prod:http://dubbo-proddemo.zq.com/hello?name=noah
任意修改碼雲上的dubbo-demo-web項目的say方法返回內容
路徑dubbo-client/src/main/java/com/od/dubbotest/action/HelloAction.java
參數以下:
參數名 | 參數值 |
---|---|
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 |
構建成功,而後咱們在測試環境發佈此版本鏡像
修改測試環境的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看是否有咱們更改的內容
鏡像在測試環境測試沒有問題後,直接使用該鏡像發佈生產環境,不在從新打包,避免發生錯誤
一樣修改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配置中心發佈流程已經可使用了,而且真正作到了一次構建,多平臺使用。