istio-生產(chǎn)環(huán)境搭建

相關組件

整個 istio 環(huán)境需要以下組件

  • prometheus -- 用于 K8s 平臺和 istio 平臺監(jiān)控
  • jaeger -- 用于服務間的鏈路追蹤
  • elasticsearch -- 用于存儲鏈路追蹤的數(shù)據(jù),(也可增加部署 fluntd 和 kibana 收集監(jiān)控 K8s 平臺日志)
  • istio -- 平臺本體

由于需要用于生產(chǎn)環(huán)境中,istio 部署包中所集成的 Prometheus 和 jaeger 均無法滿足需求,其中平臺中其他服務需要復用 Prometheus;istio 中集成的 jaeger 為 allinone 模式(用于測試),無法滿足生產(chǎn)需求,因此單獨拆出來。

Prometheus 搭建

Prometheus是由SoundCloud開發(fā)的開源監(jiān)控報警系統(tǒng)和時序列數(shù)據(jù)庫(TSDB)。Prometheus使用Go語言開發(fā),是Google BorgMon監(jiān)控系統(tǒng)的開源版本。

2016年由Google發(fā)起Linux基金會旗下的原生云基金會(Cloud Native Computing Foundation), 將Prometheus納入其下第二大開源項目,且在開源社區(qū)相當活躍。

Prometheus和Heapster(Heapster是K8S的一個子項目,用于獲取集群的性能數(shù)據(jù)。)相比功能更完善、更全面。Prometheus性能也足夠支撐上萬臺規(guī)模的集群。

其基本原理是通過HTTP協(xié)議周期性抓取被監(jiān)控組件的狀態(tài),任意組件只要提供對應的HTTP接口就可以接入監(jiān)控。不需要任何SDK或者其他的集成過程。這樣做非常適合做虛擬化環(huán)境監(jiān)控系統(tǒng),比如VM、Docker、Kubernetes等。輸出被監(jiān)控組件信息的HTTP接口被叫做exporter 。目前互聯(lián)網(wǎng)公司常用的組件大部分都有exporter可以直接使用,比如Varnish、Haproxy、Nginx、MySQL、Linux系統(tǒng)信息(包括磁盤、內(nèi)存、CPU、網(wǎng)絡等等

部署

Prometheus 此處采用的是 coreos 提供的 kube-prometheus,其定義了一組 CRD,且提供了 Operator 便于部署。

git clone 下來項目后,就可以無腦部署了:

# 部署 Prometheus 所需的 crd 和 namespace(monitoring)
kubectl create -f manifests/setup
# 檢查 crd serverMonitor 是否可用
until kubectl get servicemonitors --all-namespaces ; do date; sleep 1; echo ""; done
# 部署其他組件
kubectl create -f manifests/

在 kube-prometheus 中,主要定義了 以下幾個 crd:

# 常用
prometheusrules.monitoring.coreos.com -- 用于定義告警規(guī)則
servicemonitors.monitoring.coreos.com -- 用于定義 metrics 獲取的 api
# 不常用
alertmanagers.monitoring.coreos.com -- 用于 告警配置
podmonitors.monitoring.coreos.com   -- 用于 pod 監(jiān)控配置
prometheuses.monitoring.coreos.com  -- 用于創(chuàng)建相關 adapter 

部署成功后,在 monitoring namespace 下會生成如下 pod 以及 service

# pod
NAME                                   READY   STATUS    RESTARTS   AGE
alertmanager-main-0                    2/2     Running   2          4d17h
alertmanager-main-1                    2/2     Running   0          4d17h
alertmanager-main-2                    2/2     Running   0          4d17h
grafana-5db74b88f4-sfwl5               1/1     Running   0          4d17h
kube-state-metrics-54f98c4687-8x7b7    3/3     Running   0          4d17h
node-exporter-5p8b8                    2/2     Running   0          4d17h
node-exporter-65r4g                    2/2     Running   0          4d17h
node-exporter-946rm                    2/2     Running   0          4d17h
node-exporter-95x66                    2/2     Running   4          4d17h
node-exporter-lzgv7                    2/2     Running   0          4d17h
prometheus-adapter-8667948d79-hdk62    1/1     Running   0          4d17h
prometheus-k8s-0                       3/3     Running   1          4d17h
prometheus-k8s-1                       3/3     Running   1          4d17h
prometheus-operator-548c6dc45c-ltmv8   1/1     Running   2          4d17h

其中 pod/prometheus-k8s-0 即周期性調(diào)取 k8s 平臺的 api 獲取 metrics 信息。通過kubectl get -n monitoring servicemonitors.monitoring.coreos.com獲取 serviceMonitor :

NAME                      AGE
alertmanager              4d17h
grafana                   4d17h
coredns                   4d17h |
kube-apiserver            4d17h |
kube-controller-manager   4d17h |
kube-scheduler            4d17h | --> 對 k8s 相關組件的監(jiān)控配置
kube-state-metrics        4d17h |
kubelet                   4d17h |
node-exporter             4d17h   |
prometheus                4d17h   | --> 對Prometheus 自身組件的監(jiān)控配置
prometheus-operator       4d17h   |

注意:此時我們還沒有加入對 isito 組件的監(jiān)控配置

elasticsearch

es 在環(huán)境中的角色是存儲鏈路追蹤的信息。因為在鏈路追蹤的選型上我們選擇了 jaeger,jaeger 所支持的后端存儲為 elasticsearch 和 cassandra,相比之下 es 的可復用性更高。

這里使用的是官方鏡像,gcr.io/fluentd-elasticsearch/elasticsearch:v6.6.1

部署成功后,為我們提供了 service elasticsearch-logging

這里推薦一個 chrome 插件,可以很方便的查詢 es 集群狀態(tài)以及數(shù)據(jù) ElasticSearch Head

jaeger

目前 es 已經(jīng)部署完畢,jaeger 的部署可以參考我之前寫過的一篇{% post_link jaeger-istio jaeger 在 istio 上的實踐 %},此處不再贅述

通過部署,我們在 jaeger namespace 下得到以下三個服務:

NAME               TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)                        AGE
jaeger-collector   ClusterIP      10.233.15.246   <none>        14267/TCP,14268/TCP,9411/TCP   2d3h
jaeger-query       LoadBalancer   10.233.34.138   <pending>     80:31882/TCP                   2d3h
zipkin             ClusterIP      10.233.53.53    <none>        9411/TCP                       2d3h

其中 zipkin 服務是用于收集使用 zipkin 數(shù)據(jù)結(jié)構的鏈路信息,jaeger 兼容 zipkin。 jaeger-collector 用于收集 jaeger 使用的數(shù)據(jù)結(jié)構的鏈路信息;jaeger-query 用于查詢。

istio

現(xiàn)在所有的準備已經(jīng)就緒,終于可以部署 istio 了。istio 所使用的版本是 1.3.4。
我們選擇通過 Helm chart 模板安裝部署,首先需要自定義部署參數(shù),修改install/kubernetes/helm/values.yaml

# 關閉 grafana prometheus tracing 模塊,其他模塊可根據(jù)需求部署安裝
grafana:
  enabled: false

prometheus:
  enabled: false

tracing:
  enabled: false

# 配置 tracing 上報鏈路信息服務地址
tracer:
  zipkin: 
    address: zipkin.jaeger:9411  # 格式為servicename.namespace:port

執(zhí)行以下命令進行部署

# 添加官方helm 倉庫
helm repo add istio.io https://storage.googleapis.com/istio-release/releases/1.3.4/charts/

# 部署 istio crd
helm install install/kubernetes/helm/istio-init --name istio-init --namespace istio-system

# 檢查資源 需要等待結(jié)果為 23
kubectl get crds | grep 'istio.io' | wc -l

# 部署istio,pilot.traceSampling 配置鏈路追蹤的采樣率,100表示100%;
# kiali.xxx 配置 kiali 連接外部的 jaeger 和 Prometheus
helm install install/kubernetes/helm/istio --name istio --namespace istio-system --set pilot.traceSampling=100,kiali.dashboard.jaegerURL=http://jaeger-query.jaeger:80,kiali.prometheusAddr=http://prometheus-k8s.monitoring:9090

至此,istio部署完畢,jaeger 已經(jīng)接入 istio,但是 Prometheus 仍未配置收集 istio 的 metrics。我們需要創(chuàng)建自己的 serviceMonitor:

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: istio-monitor  
  labels:
    app: prometheus-istio
spec:
  selector:
    matchLabels: # 采集包含以下標簽的 pod 的 /metrics 
      app: mixer # 由于 istio 中 業(yè)務服務組件的 metrics 均匯總至 mixer 中,因此只采集該組件即可
      istio: mixer
  endpoints:
  - port: prometheus # mixer 組件包含名為 prometheus 的端口,便于采集
    interval: 10s    # 采集周期
  namespaceSelector:
    matchNames:
    - istio-system   # 作用域

此外,由于 Prometheus 沒有權限采集 monitoring 和 kube-system 以外的 endpoint,需要增加 rbac 來增加其權限:

apiVersion: v1
kind: ServiceAccount
metadata:
  name: prometheus
  namespace: istio-system
  labels:
    app: prometheus

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: prometheus-istio
  labels:
    app: prometheus
rules:
- apiGroups: [""]
  resources:
  - nodes
  - services
  - endpoints
  - pods
  - nodes/proxy
  verbs: ["get", "list", "watch"]
- apiGroups: [""]
  resources:
  - configmaps
  verbs: ["get"]
- nonResourceURLs: ["/metrics"]
  verbs: ["get"]

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: prometheus-istio
  namespace: istio-system
  labels:
    app: prometheus
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: prometheus-istio
subjects:
- kind: ServiceAccount
  name: prometheus-k8s
  namespace: monitoring

至此,平臺搭建完成,可以通過部署 istio 示例程序 bookinfo 進行測試

kubectl apply -f samples/bookinfo/platform/kube/bookinfo.yaml -n <your namespace>

喜歡的話,訪問我的主頁吧,請多關照!

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容