標題 :
1.利用consul實現k8s服務自動發現
目錄 :
微服務架構設計
序號 :
1
]
}
}
]
}
}
- consul自身支持ACL,但目前,Helm圖表不支持其中一些功能,需要額外的手動配置,
有關詳細信息可參閱:https://www.consul.io/docs/platform/k8s/helm.html
- 我們使用basic-auth作了授權,當用戶訪問consul-ui時需要提供用戶名和密碼
```shell
yum -y install httpd
echo "$(htpasswd -nb -C 5 consul yourpassword)" >> auth
kubectl create secret generic basic-auth --from-file=auth
- 當您使用basic-auth時,建議使用Https,否則您的用戶名和密碼可能會被竊聽
[按需]卸載consul
helm del --purge registry
kubectl delete pvc -l app=consul
kubectl delete svc -l consul=true
查看服務是否被注冊
登錄到consul的ui,https://yourdomain/ui/dc1/services
可以看到我們的服務已經被注冊到了consul中
以下事項需要您注意:
- 如果您的服務沒有被正確注冊,請檢查registry-consul-sync-catalog-*的日志,另外如果您的服務本身存在問題(例如:服務本身不能正確反代到pod)服務也不能被注冊
- 您需要為pod創建service, consul才能發現. 也就是說consul發現的資源對象時svc,並非pod
服務注冊時的一些規范
- 從上圖可以看出,我們的服務名稱很長.這將影響我們使用網關進行路由的體驗(有關網關的設計會在后面章節提及),您可以更改此設置.請更改您的service的親和屬性:"consul.hashicorp.com/service-name": "yourservicename"
- 為方便辨識服務,可以為服務打上標簽.方便檢索
- 有些服務不應該被注冊,例如網關服務(因為網關作為外部流量的入口,應該由它路由到具體的服務).您可以更改親和屬性:"consul.hashicorp.com/service-sync": "false"
"annotations": {
"consul.hashicorp.com/service-name": "terminal",
"consul.hashicorp.com/service-tags": "honeysuckle,terminalserver"
}
最后的效果應該類型這樣:
引用鏈接
https://github.com/helm/charts/tree/master/stable/consul
https://www.sunnyos.com/article-show-85.html