普罗米修斯搭建

安装前准备

1.创建prom命名空间
kubectl create namespace prom
2.node节点创建目录
mkdir -p /data/k8s/prometheus

一、编写configmap资源配置清单

cat > prom-cm.yml << 'EOF'
apiVersion: v1 
kind: ConfigMap 
metadata: 
  name: prometheus-config 
  namespace: prom
data: 
  prometheus.yml: | 
    global: 
      scrape_interval: 15s #抓取数据间隔 
      scrape_timeout: 15s #抓取超时时间 
    scrape_configs: #拉取配置 
    - job_name: 'prometheus' #任务名称 
      static_configs: #静态配置 
      - targets: ['localhost:9090'] #抓取数据节点的IP端口
EOF

二、创建PV和PVC

cat > prom-pv-pvc << 'EOF'
apiVersion: v1 
kind: PersistentVolume 
metadata: 
  name: prometheus-local 
  labels: 
    app: prometheus 
spec: 
  accessModes: 
  - ReadWriteOnce 
  capacity: 
    storage: 10Gi 
  storageClassName: local-storage 
  local: 
    path: /data/k8s/prometheus 
  nodeAffinity: 
    required: 
      nodeSelectorTerms: 
      - matchExpressions: 
        - key: kubernetes.io/hostname 
          operator: In 
          values: 
          - node2 
  persistentVolumeReclaimPolicy: Retain 
--- 
apiVersion: v1 
kind: PersistentVolumeClaim 
metadata: 
  name: prometheus-data 
  namespace: prom 
spec: 
  selector: 
    matchLabels: 
      app: prometheus 
  accessModes: 
  - ReadWriteOnce 
  resources: 
    requests: 
      storage: 10Gi 
  storageClassName: local-storage
EOF

三、编写RBAC

cat > prom-rbac.yml << 'EOF'
apiVersion: v1 
kind: ServiceAccount 
metadata: 
  name: prometheus 
  namespace: prom 
--- 
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: 
  - "extensions" 
  resources: 
  - ingresses 
  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 
roleRef: 
  apiGroup: rbac.authorization.k8s.io 
  kind: ClusterRole 
  name: prometheus 
subjects: 
- kind: ServiceAccount 
  name: prometheus 
  namespace: prom
EOF

四、编写deployment

cat > prom-dp.yml << 'EOF'
apiVersion: apps/v1 
kind: Deployment 
metadata: 
  name: prometheus 
  namespace: prom 
  labels: 
    app: prometheus 
spec: 
  selector: 
    matchLabels: 
      app: prometheus 
  template: 
    metadata: 
      labels: 
        app: prometheus 
    spec: 
      serviceAccountName: prometheus #引用RBAC创建的ServiceAccount 
      volumes: 
      - name: data 
        persistentVolumeClaim: 
          claimName: prometheus-data 
      - name: config-volume 
        configMap: 
          name: prometheus-config 
      initContainers: #由初始化容器将数据目录的属性修改为nobody用 户和组 
      - name: fix-permissions 
        image: busybox 
        command: [chown, -R, "nobody:nobody", /prometheus] 
        volumeMounts: 
        - name: data 
          mountPath: /prometheus 
      containers: 
      - name: prometheus 
        image: prom/prometheus:v2.24.1 
        resources: 
          requests: 
            cpu: 100m 
            memory: 512Mi 
          limits: 
            cpu: 100m 
            memory: 512Mi 
        ports: 
        - name: http 
          containerPort: 9090 
        args: 
        - "--config.file=/etc/prometheus/prometheus.yml" #指定配置文件 
        - "--storage.tsdb.path=/prometheus" #tsdb数据库保存路径 
        - "--storage.tsdb.retention.time=24h" #数据保留时间,默认15天 
        - "--web.enable-admin-api" #控制对admin HTTP API的访问 
        - "--web.enable-lifecycle" #支持热更新,直接执行localhost:9090/-/reload立即 生效        
        volumeMounts: 
        - name: config-volume
          mountPath: "/etc/prometheus" 
        - name: data 
          mountPath: "/prometheus"
EOF

五、编写Service

cat > prom-svc.yml << 'EOF'
apiVersion: v1 
kind: Service 
metadata: 
  name: prometheus 
  namespace: prom 
  labels: 
    app: prometheus 
spec: 
  selector: 
    app: prometheus 
  ports: 
  - name: web 
    port: 9090 
    targetPort: http
    nodePort: 30000 
  type: NodePort
EOF

六、应用资源配置清单

kubectl apply -f prom-cm.yml
kubectl apply -f prom-pv-pvc.yml
kubectl apply -f prom-rbac.yml
kubectl apply -f prom-dp.yml
kubectl apply -f prom-svc.yml

七、浏览器访问

server-ip:30000
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐