美文网首页微服务收藏-开发篇
微服务治理-GPE部署-开发环境

微服务治理-GPE部署-开发环境

作者: do_young | 来源:发表于2020-01-20 22:38 被阅读0次

    部署方案

    Docker+Kubernetes


    image.png

    Grafana部署

    grafana主要用于提供WEB界面,对promethus收集的指标信息进行可视化展示。
    所以通过NodePort类型部署Service。

    deployment

    apiVersion: extensions/v1beta1
    kind: Deployment
    metadata:
      name: grafana-core
      namespace: t-paas
      labels:
        app: grafana
        component: core
    spec:
      replicas: 1
      template:
        metadata:
          labels:
            app: grafana
            component: core
        spec:
          containers:
          - image: grafana/grafana:6.5.0
            name: grafana-core
            imagePullPolicy: IfNotPresent
            ports:
            - containerPort: 3000
              protocol: TCP        
            env:
              - name: GF_SECURITY_ADMIN_PASSWORD
                value: "admin" 
            readinessProbe:
              httpGet:
                path: /login
                port: 3000
            volumeMounts:
            - name: grafana-persistent-storage
              mountPath: /var/lib/grafana
          volumes:
          - name: grafana-persistent-storage
            emptyDir: {}
    

    service

    apiVersion: v1
    kind: Service
    metadata:
      name: grafana-svc
      namespace: t-paas
      labels:
        app: grafana
        component: core
    spec:
      type: NodePort
      ports:
        - port: 3000
          targetPort: 3000
          nodePort: 30000   
      selector:
        app: grafana
        component: core
    

    Prometheus部署

    Prometheus主要通过拉的方式收集指标信息。

    configmap

    收集对象需要在prometheus应用的prometheus.yml文件进行配置。
    在k8s中,可以将配置文件定义在configmap中,通过volumne方式,挂载到应用容器中。
    在该prometheus.yml文件中,主要配置对kubernetes中的组件及部署的服务,容器等进行监控。

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: prometheus-config
      namespace: t-paas
    data:
      prometheus.yml: |
        global:
          scrape_interval:     15s
          evaluation_interval: 15s
        scrape_configs:
    
        - job_name: 'kubernetes-apiservers'
          kubernetes_sd_configs:
          - role: endpoints
          scheme: https
          tls_config:
            ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
          bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
          relabel_configs:
          - source_labels: [__meta_kubernetes_namespace, __meta_kubernetes_service_name, __meta_kubernetes_endpoint_port_name]
            action: keep
            regex: default;kubernetes;https
    
        - job_name: 'kubernetes-nodes'
          kubernetes_sd_configs:
          - role: node
          scheme: https
          tls_config:
            ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
          bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
          relabel_configs:
          - action: labelmap
            regex: __meta_kubernetes_node_label_(.+)
          - target_label: __address__
            replacement: kubernetes.default.svc:443
          - source_labels: [__meta_kubernetes_node_name]
            regex: (.+)
            target_label: __metrics_path__
            replacement: /api/v1/nodes/${1}/proxy/metrics
    
        - job_name: 'kubernetes-cadvisor'
          kubernetes_sd_configs:
          - role: node
          scheme: https
          tls_config:
            ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
          bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
          relabel_configs:
          - action: labelmap
            regex: __meta_kubernetes_node_label_(.+)
          - target_label: __address__
            replacement: kubernetes.default.svc:443
          - source_labels: [__meta_kubernetes_node_name]
            regex: (.+)
            target_label: __metrics_path__
            replacement: /api/v1/nodes/${1}/proxy/metrics/cadvisor
    
        - job_name: 'kubernetes-service-endpoints'
          kubernetes_sd_configs:
          - role: endpoints
          relabel_configs:
          - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scrape]
            action: keep
            regex: true
          - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scheme]
            action: replace
            target_label: __scheme__
            regex: (https?)
          - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_path]
            action: replace
            target_label: __metrics_path__
            regex: (.+)
          - source_labels: [__address__, __meta_kubernetes_service_annotation_prometheus_io_port]
            action: replace
            target_label: __address__
            regex: ([^:]+)(?::\d+)?;(\d+)
            replacement: $1:$2
          - action: labelmap
            regex: __meta_kubernetes_service_label_(.+)
          - source_labels: [__meta_kubernetes_namespace]
            action: replace
            target_label: kubernetes_namespace
          - source_labels: [__meta_kubernetes_service_name]
            action: replace
            target_label: kubernetes_name
    
        - job_name: 'kubernetes-services'
          kubernetes_sd_configs:
          - role: service
          metrics_path: /probe
          params:
            module: [http_2xx]
          relabel_configs:
          - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_probe]
            action: keep
            regex: true
          - source_labels: [__address__]
            target_label: __param_target
          - target_label: __address__
            replacement: blackbox-exporter.example.com:9115
          - source_labels: [__param_target]
            target_label: instance
          - action: labelmap
            regex: __meta_kubernetes_service_label_(.+)
          - source_labels: [__meta_kubernetes_namespace]
            target_label: kubernetes_namespace
          - source_labels: [__meta_kubernetes_service_name]
            target_label: kubernetes_name
    
        - job_name: 'kubernetes-ingresses'
          kubernetes_sd_configs:
          - role: ingress
          relabel_configs:
          - source_labels: [__meta_kubernetes_ingress_annotation_prometheus_io_probe]
            action: keep
            regex: true
          - source_labels: [__meta_kubernetes_ingress_scheme,__address__,__meta_kubernetes_ingress_path]
            regex: (.+);(.+);(.+)
            replacement: ${1}://${2}${3}
            target_label: __param_target
          - target_label: __address__
            replacement: blackbox-exporter.example.com:9115
          - source_labels: [__param_target]
            target_label: instance
          - action: labelmap
            regex: __meta_kubernetes_ingress_label_(.+)
          - source_labels: [__meta_kubernetes_namespace]
            target_label: kubernetes_namespace
          - source_labels: [__meta_kubernetes_ingress_name]
            target_label: kubernetes_name
    
        - job_name: 'kubernetes-pods'
          kubernetes_sd_configs:
          - role: pod
          relabel_configs:
          - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
            action: keep
            regex: true
          - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
            action: replace
            target_label: __metrics_path__
            regex: (.+)
          - source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]
            action: replace
            regex: ([^:]+)(?::\d+)?;(\d+)
            replacement: $1:$2
            target_label: __address__
          - action: labelmap
            regex: __meta_kubernetes_pod_label_(.+)
          - source_labels: [__meta_kubernetes_namespace]
            action: replace
            target_label: kubernetes_namespace
          - source_labels: [__meta_kubernetes_pod_name]
            action: replace
            target_label: kubernetes_pod_name
    
    

    rbac

    为promethues应用创建角色及分配权限。

    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRole
    metadata:
      name: prometheus
    rules:
    - apiGroups: [""]
      resources:
      - nodes
      - nodes/proxy
      - services
      - endpoints
      - pods
      verbs: ["get", "list", "watch"]
    - apiGroups:
      - extensions
      resources:
      - ingresses
      verbs: ["get", "list", "watch"]
    - nonResourceURLs: ["/metrics"]
      verbs: ["get"]
    ---
    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: prometheus
      namespace: t-paas
    ---
    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRoleBinding
    metadata:
      name: prometheus
    roleRef:
      apiGroup: rbac.authorization.k8s.io
      kind: ClusterRole
      name: prometheus
    subjects:
    - kind: ServiceAccount
      name: prometheus
      namespace: t-paas
    

    deploy

    部署prometheus应用,可以通过9090端口访问收集到的指标数据信息。

    ---
    apiVersion: apps/v1beta2
    kind: Deployment
    metadata:
      labels:
        name: prometheus-deployment
      name: prometheus
      namespace: t-paas
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: prometheus
      template:
        metadata:
          labels:
            app: prometheus
        spec:
          containers:
          - image: prom/prometheus:v2.15.0
            name: prometheus
            command:
            - "/bin/prometheus"
            args:
            - "--config.file=/etc/prometheus/prometheus.yml"
            - "--storage.tsdb.path=/prometheus"
            - "--storage.tsdb.retention=24h"
            ports:
            - containerPort: 9090
              protocol: TCP
            volumeMounts:
            - mountPath: "/prometheus"
              name: data
            - mountPath: "/etc/prometheus"
              name: config-volume
          serviceAccountName: prometheus    
          volumes:
          - name: data
            emptyDir: {}
          - name: config-volume
            configMap:
              name: prometheus-config   
    

    service

    kind: Service
    apiVersion: v1
    metadata:
      labels:
        app: prometheus
      name: prometheus-svc
      namespace: t-paas
    spec:
      ports:
      - name: p9090
        port: 9090
        targetPort: 9090
      selector:
        app: prometheus
    

    Exporter部署

    部署node-exporter 收集主机中部署的k8s指标信息。

    depolyment

    apiVersion: extensions/v1beta1
    kind: DaemonSet
    metadata:
      name: node-exporter
      namespace: t-paas
      labels:
        k8s-app: node-exporter
    spec:
      template:
        metadata:
          labels:
            k8s-app: node-exporter
        spec:
          containers:
          - image: prom/node-exporter:v0.18.0
            name: node-exporter
            ports:
            - containerPort: 9100
              protocol: TCP
              name: http
    

    service

    apiVersion: v1
    kind: Service
    metadata:
      labels:
        k8s-app: node-exporter
      name: node-exporter
      namespace: t-paas
    spec:
      ports:
      - name: p9100
        port: 9100
        targetPort: 9100    
      selector:
        k8s-app: node-exporter
    

    启动

    按照以下顺序启动各服务

    #node-exporter
    kubectl create -f  /gpe/exporter/node-exporter-deployment.yaml
    kubectl create -f  /gpe/exporter/node-exporter-service.yaml
    #prometheus组件
    kubectl create -f  /gpe/prometheus/rbac-setup.yaml
    kubectl create -f  /gpe/prometheus/configmap.yaml 
    kubectl create -f  /gpe/prometheus/prometheus.deploy.yml 
    kubectl create -f  /gpe/prometheus/prometheus.svc.yml 
    #安装 组件
    kubectl create -f   /gpe/grafana/grafana-deploy.yaml
    kubectl create -f   /gpe/grafana/grafana-svc.yaml
    

    配置

    1. 访问grafana http://ip:30000(默认用户密码 admin admin)

    2. 配置grafana 数据源为prometheus


      配置grafana 数据源为prometheus
    3. 导入dashboard面板


      导入监控模板
    4. 配置模板数据源


      配置模板数据源
    5. 查看grafana 监控图标


      查看grafana 监控图标

    相关文章

      网友评论

        本文标题:微服务治理-GPE部署-开发环境

        本文链接:https://www.haomeiwen.com/subject/nmlszctx.html