promethus轉發
標籤:promethus istio
promethus是非常重要的流量監控工具,但是它的瀏覽器界面只能夠在內網下打開,如何在外網訪問這個好用的流量監控工具呢
根據bookinfo遙測教學,在本機上訪問鏈接地址即可打開prometheus界面,我們的目標就是讓外網也能夠打開這個界面,這就需要建立外網到內網9090端口的映射。
直接修改svc/istio-ingressgateway
在ports中加入自己寫的一段,將外部端口31399映射到內部的9090
- name: prometheus
nodePort: 31399
port: 9090
protocol: TCP
targetPort: 9090
結果自然是被拒絕了,果然沒有這麼簡單。
加入自己的入口網關
遙測教學的做法是先在後臺運行這個kubectl -n istio-system port-forward $(kubectl -n istio-system get pod -l app=prometheus -o jsonpath='{.items[0].metadata.name}') 9090:9090
,將本地的9090端口與目標容器的9090端口作端口轉發,然後訪問本地的9090端口來訪問。好處是避免了pod的ip變動造成的失效,但是外部很顯然訪問不到這個服務。
自己仿照bookinfo的例子寫了個網關,參考了API document of VirtualService,目標是將外部端口9090訪問轉發到內部prometheus的9090端口上。
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: prometheus-gateway
spec:
selector:
istio: ingressgateway # use istio default controller
servers:
- port:
number: 90
name: http
protocol: HTTP
hosts:
- "*"
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: prometheus
spec:
hosts:
- "*"
gateways:
- prometheus-gateway
http:
- match:
- uri:
regex: "*"
route:
- destination:
host: prometheus
port:
number: 9090
看了下istio的入口網關,發現內部的9090對應的是外部的31399。而且看了一下之前博客裏面記錄下來的數據,這個是實時添加上去的。很好奇它的實現方式,找時間拆掉。
istio-ingressgateway LoadBalancer 10.109.139.108 129.204.7.185 15020:30937/TCP,80:31380/TCP,9090:31399/TCP,443:31390/TCP,31400:31400/TCP,15029:30721/TCP,15030:31100/TCP,15031:31773/TCP,15032:30877/TCP,15443:32541/TCP 6h41m
理論上來說訪問129.204.7.185:31399/productpage
就可以訪問到prometheus的服務了,大概。
但實際上試了好久好像都不行。
改變prometheus的服務
在網上看到了一個issue,說是在istio環境下如何訪問prometheus時,有人提到可以將prometheus的svc換成LoadBalancer,然後從外網ip訪問
修改如下:
- 將type從ClusterIP換成了LoadBalancer
- 修改了最後的status,原本時空對象
{}
,現在爲
loadBalancer:
ingress:
- ip: 203.195.219.185
結果發現配置時成功了,但是LoadBalancer的external-IP pending,說明沒有落實。應該按照之前博客的文章對ingress controller進行修改。
使用kubectl get configmaps -nmetallb-system
找到了原來配置的那個configmap,接下來就是將ip加入ip池中。
改完之後大概長這個樣子
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
data:
config: |
address-pools:
- name: default
protocol: layer2
addresses:
- 129.204.7.185-129.204.7.185
- 203.195.219.185-203.195.219.185
kind: ConfigMap
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"v1","data":{"config":"address-pools:\n- name: default\n protocol: layer2\n addresses:\n - 129.204.7.185-129.204.7.185\n"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"config","namespace":"metallb-system"}}
creationTimestamp: "2019-04-06T02:01:03Z"
name: config
namespace: metallb-system
resourceVersion: "6869478"
selfLink: /api/v1/namespaces/metallb-system/configmaps/config
uid: d526d2a2-580f-11e9-96eb-5254001218ec
改完之後發現externalIP果然顯示出來了,效果下:
prometheus LoadBalancer 10.102.237.167 203.195.219.185 9090:31588/TCP 7h8m
理論上來說,訪問外網ip203.195.219.185:31588
就可以訪問到prometheus了。
按照官方文檔的說法,可以訪問203.195.219.185:31588/graph
訪問圖像,訪問203.195.219.185:31588/metrics
訪問流量數據。然而並沒有成功。
參考文章再次改寫ingreess
參考了這篇文章,作者使用ingress將prometheus暴露到外網。不過原文似乎不是在istio環境下跑的,我應該考慮把自動注入關掉。
首先將自動注入關閉kubectl label namespace default istio-injection=disabled --overwrite
,希望我記得還有這件事,把它改回來。