iT邦幫忙

2021 iThome 鐵人賽

DAY 18
0
自我挑戰組

從雲端開始的菜鳥任務系列 第 18

Day 18 利用 Kubernetes 建立 Prometheus Service

  • 分享至 

  • xImage
  •  

Kubernetes and Prometheus

安裝 Kubernetes 及 建立叢集

https://ithelp.ithome.com.tw/articles/10244493

什麼是 Prometheus

https://www.metricfire.com/blog/what-is-prometheus/

部署 Prometheus & Grafana 監控叢集

環境:
Ubuntu 20.04
Kubernetes v1.15.6

https://ithelp.ithome.com.tw/articles/10248278

建立 Namespace

kubectl create ns kube-ops

建立 ConfigMap

prometheus-cm.yaml

apiVersion: v1       
kind: ConfigMap
metadata:
  name: prometheus-config
  namespace: kube-ops
data:
  prometheus.yml: |
    global: 
      scrape_interval: 15s 
      scrape_timeout: 15s 
    scrape_configs: 
    - job_name: 'prometheus'
      static_configs: 
      - targets: ['localhost:9090']

建立 Prometheus Deployment

prometheus-deploy.yaml

apiVersion: apps/v1        
kind: Deployment
metadata:
  labels:
    app: prometheus
  name: prometheus
  namespace: kube-ops
spec:
  replicas: 1
  selector:
    matchLabels:
      app: prometheus
  template:
    metadata:
      labels:
        app: prometheus
    spec:
      serviceAccountName: prometheus
      containers:
      - image: prom/prometheus:v2.21.0
        name: prometheus
        command: 
        - "/bin/prometheus"
        args: 
        - "--config.file=/etc/prometheus/prometheus.yml"
        - "--storage.tsdb.path=/prometheus"
        - "--storage.tsdb.retention=24h"
        - "--web.enable-lifecycle"
        ports: 
        - containerPort: 9090
          protocol: TCP 
          name: http
        volumeMounts:
        - mountPath: "/prometheus"
          subPath: prometheus
          name: data
        - mountPath: "/etc/prometheus"
          name: config-volume
        resources:
          requests:
            cpu: 100m
            memory: 512Mi
          limits: 
            cpu: 100m
            memory: 512Mi
      securityContext:
        runAsUser: 0
      volumes:
      - name: data
        emptyDir: {}
      - configMap:
          name: prometheus-config
        name: config-volume    

建立 ServiceAccount

prometheus-rbac.yaml

apiVersion: v1
kind: ServiceAccount
metadata:
  name: prometheus
  namespace: kube-ops

---

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

---

apiVersion: rbac.authorization.k8s.io/v1
kind:  ClusterRoleBinding
metadata:
  name: prometheus
subjects:
- kind: ServiceAccount
  name: prometheus
  namespace: kube-ops
roleRef:
  kind: ClusterRole
  name: prometheus
  apiGroup: rbac.authorization.k8s.io   

建立 Prometheus Service (NodePort)

prometheus-svc.yaml

apiVersion: v1       
kind: Service
metadata: 
  name: prometheus
  namespace: kube-ops
  labels:
    app: prometheus
spec: 
  selector: 
    app: prometheus
  type: NodePort 
  ports:
    - name: web 
      port: 9090
      targetPort: 9090
      nodePort: 31111
kubectl apply -f .

部署結果

root@MGCHUNG-2:/home/ubuntu# kubectl get svc -n kube-ops
NAME         TYPE       CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
prometheus   NodePort   10.101.214.225   <none>        9090:31111/TCP   9m4s

上一篇
Day 17 利用 helm 安裝 Jenkins
下一篇
Day 19 Knative Serving DNS 測試
系列文
從雲端開始的菜鳥任務30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言