首頁 > 軟體

kubernetes叢集搭建Zabbix監控平臺的詳細過程

2022-07-06 14:01:04

一、zabbix介紹

1.zabbix簡介

Zabbix是一個基於Web介面的分散式系統監控的企業級開源軟體。可以監視各種系統與裝置的引數,保障伺服器及裝置的安全運營。

2.zabbix特點

(1)安裝與設定簡單。
(2)視覺化web管理介面。
(3)免費開源。
(4)支援中文。
(5)自動發現。
(6)分散式監控。
(7)實時繪圖。

3.zabbix的主要功能

1.硬體監控。如交換機、路由器、印表機等。
2.系統監控。如CPU,記憶體,磁碟。硬碟IO,系統負載等。
3.服務監控。如apache,nginx,tomcat,redis,TCP連線數等。
4.效能監控。如網站效能,伺服器效能,資料庫效能。
5.紀錄檔監控。如存取紀錄檔,錯誤紀錄檔。
6.安全監控。如使用者登入數,本地檔案改動,passwd檔案變化。
7.網路監控。如埠,SMTP,網路使用率,網路入流量,網路出流量。

4.zabbix架構圖

二、檢查本地k8s環境

1.檢查系統pod執行狀態

[root@k8s-master ~]# kubectl get pods -A
NAMESPACE     NAME                                       READY   STATUS    RESTARTS        AGE
kube-system   calico-kube-controllers-7bc6547ffb-2nf66   1/1     Running   1 (9m28s ago)   2d16h
kube-system   calico-node-8c4pn                          1/1     Running   1 (9m16s ago)   2d16h
kube-system   calico-node-f28qq                          1/1     Running   1 (9m10s ago)   2d16h
kube-system   calico-node-wmc2j                          1/1     Running   1 (9m29s ago)   2d16h
kube-system   coredns-6d8c4cb4d-6gm4x                    1/1     Running   1 (9m28s ago)   2d16h
kube-system   coredns-6d8c4cb4d-7vxlz                    1/1     Running   1 (9m29s ago)   2d16h
kube-system   etcd-k8s-master                            1/1     Running   1 (9m30s ago)   2d16h
kube-system   kube-apiserver-k8s-master                  1/1     Running   1 (9m29s ago)   2d16h
kube-system   kube-controller-manager-k8s-master         1/1     Running   1 (9m30s ago)   2d16h
kube-system   kube-proxy-8dfw8                           1/1     Running   1 (9m29s ago)   2d16h
kube-system   kube-proxy-ghzrv                           1/1     Running   1 (9m9s ago)    2d16h
kube-system   kube-proxy-j867z                           1/1     Running   1 (9m15s ago)   2d16h
kube-system   kube-scheduler-k8s-master                  1/1     Running   1 (9m28s ago)   2d16h

2.檢查node節點狀態

[root@k8s-master ~]# kubectl get nodes -owide
NAME         STATUS   ROLES                  AGE     VERSION   INTERNAL-IP     EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION          CONTAINER-RUNTIME
k8s-master   Ready    control-plane,master   2d16h   v1.23.1   192.168.3.201   <none>        CentOS Linux 7 (Core)   3.10.0-957.el7.x86_64   containerd://1.6.6
k8s-node01   Ready    <none>                 2d16h   v1.23.1   192.168.3.202   <none>        CentOS Linux 7 (Core)   3.10.0-957.el7.x86_64   containerd://1.6.6
k8s-node02   Ready    <none>                 2d16h   v1.23.1   192.168.3.203   <none>        CentOS Linux 7 (Core)   3.10.0-957.el7.x86_64   containerd://1.6.6

三、設定nfs共用儲存

1.安裝nfs

 yum install -y nfs-utils

2.建立共用目錄

mkdir -p /nfs/data

3.設定共用目錄

echo "/nfs/data/ *(insecure,rw,sync,no_root_squash)" > /etc/exports

4.啟動相關服務

systemctl enable rpcbind
systemctl enable nfs-server
systemctl start rpcbind

5.使設定生效

exportfs -r

6.檢視nfs

[root@k8s-master ~]# exportfs
/nfs/data     	<world>

7.其他節點檢查nfs共用

[root@k8s-node01 ~]#  showmount -e 192.168.3.201
Export list for 192.168.3.201:
/nfs/data *

四、安裝zabbix-mysql

1.編寫zabbix-mysql的yaml檔案

[root@k8s-master zabbix]# cat zabbix_mysql.yaml 
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: zabbixdb-pv
spec:
  capacity:   #建立儲存的大小
    storage: 10Gi
  volumeMode: Filesystem  #儲存的型別
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  nfs:
    path: /nfs/data/zabbix
    server: 192.168.3.201
  mountOptions:
    - hard
    - nfsvers=4.1
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: zabbixdb-pvc
  namespace: zabbix
spec:
  accessModes:
    - ReadWriteOnce
  volumeMode: Filesystem   #儲存型別 ,塊儲存Block
  resources:
    requests:
      storage: 8Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: zabbixdb
  name: zabbixdb
  namespace: zabbix
spec:
  replicas: 1
  selector:
    matchLabels:
      app: zabbixdb
  strategy: {}
  template:
    metadata:
      labels:
        app: zabbixdb
    spec:
      volumes:
      - name: zabbixdb-data
        persistentVolumeClaim:
          claimName: zabbixdb-pvc
      containers:
      - image: mysql:8.0
        name: mysql
        ports:
        - name: mysql
          containerPort: 3306
          protocol: TCP
        volumeMounts:
        - name: zabbixdb-data
          mountPath: /var/lib/mysql
        env:
        - name: MYSQL_DATABASE
          value: 'zabbix'
        - name: MYSQL_USER
          value: 'zabbix'
        - name: MYSQL_PASSWORD
          value: 'zabbix'
        - name: MYSQL_ROOT_PASSWORD
          value: 'zabbix'
        resources: 
          requests:
             cpu: 0.1
             memory: 128Mi
          limits:
             cpu: 0.3
             memory:  512Mi
---
          
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: zabbixdb
  name: zabbixdb
  namespace: zabbix
spec:
  clusterIP: 10.96.1.99
  ports:
  - name: 3306-3306
    port: 3306
    protocol: TCP
    targetPort: 3306
  selector:
    app: zabbixdb
  type: ClusterIP
status:
  loadBalancer: {}

2.建立名稱空間

[root@k8s-master zabbix]# kubectl create namespace zabbix
namespace/zabbix created

3.建立zabbix資料庫

kubectl apply -f zabbix_mysql.yaml

4.檢查pod狀態

[root@k8s-master zabbix]# kubectl get pods -n zabbix 
NAME                        READY   STATUS    RESTARTS   AGE
zabbixdb-69b7cd8dff-jnpjd   1/1     Running   0          11m

五、檢查zabbix資料庫的service域名

1.執行測試pod

[root@k8s-master ~]# kubectl run busybox --image=busybox:1.28 -- sleep 3600
pod/busybox created

2.進入busybox的pod

[root@k8s-master ~]# kubectl exec -it busybox -- /bin/sh
/ # 

3.檢視域名解析

[root@k8s-master ~]# kubectl exec -it busybox -- /bin/sh
/ # nslookup zabbixdb.zabbix.svc.cluster.local
Server:    10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local

Name:      zabbixdb.zabbix.svc.cluster.local
Address 1: 10.96.1.99 zabbixdb.zabbix.svc.cluster.local
/ # 

六、安裝zabbix-server

1.編寫zabbix_server.yaml檔案

[root@k8s-master zabbix]# cat zabbix_server.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: zabbix-server
  name: zabbix-server
  namespace: zabbix
spec:
  replicas: 1
  selector:
    matchLabels:
      app: zabbix-server
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: zabbix-server
    spec:
      volumes:
      - name: zabbix-scripts
        hostPath:
          path: /usr/lib/zabbix/
      - name: zabbix-file
        hostPath:
          path: /var/lib/zabbix 
      nodeSelector:
        zabbix-server: "true"
      hostNetwork: true
      containers:
      - image: zabbix/zabbix-server-mysql:6.0-ubuntu-latest
        name: zabbix-server-mysql
        env:
        - name: DB_SERVER_HOST
          value:  10.96.1.99
        - name: MYSQL_USER
          value: zabbix
        - name: MYSQL_PASSWORD
          value: zabbix
        resources: {}
status: {}

2.給node02節點打上標籤

[root@k8s-master zabbix]# kubectl label nodes k8s-node02  zabbix-server=true
node/k8s-node02 labeled

3.安裝zabbix-server

[root@k8s-master zabbix]# kubectl apply -f zabbix_server.yaml 
deployment.apps/zabbix-server created

4.檢查pod狀態

[root@k8s-master zabbix]# kubectl get pods -n zabbix
NAME                             READY   STATUS    RESTARTS   AGE
zabbix-server-5f48d9d57d-rkr5p   1/1     Running   0          66s
zabbixdb-69b7cd8dff-krt2w        1/1     Running   0          4m22s

七、部署zabbix-web

1.編寫zabbix_web.yaml檔案

[root@k8s-master zabbix]# cat zabbix_web.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: zabbix-web
  name: zabbix-web
  namespace: zabbix
spec:
  replicas: 1
  selector:
    matchLabels:
      app: zabbix-web
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: zabbix-web
    spec:
      containers:
      - image: zabbix/zabbix-web-nginx-mysql:6.0-ubuntu-latest
        name: zabbix-web-nginx-mysql
        env:
        - name: DB_SERVER_HOST
          value: zabbixdb.zabbix.svc.cluster.local
        - name: MYSQL_USER
          value: zabbix
        - name: MYSQL_PASSWORD
          value: zabbix
        - name: ZBX_SERVER_HOST
          value: 192.168.3.201
        - name: PHP_TZ
          value: Asia/shanghai
        resources: {}
status: {}
---
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: zabbix-web
  name: zabbix-web
  namespace: zabbix
spec:
  ports:
  - name: 8080-8080
    port: 8080
    protocol: TCP
    targetPort: 8080
  selector:
    app: zabbix-web
  type: NodePort
status:
  loadBalancer: {}

2.安裝zabbix-web

[root@k8s-master zabbix]# kubectl apply -f zabbix_web.yaml 
deployment.apps/zabbix-web created
service/zabbix-web created

3.檢視pod狀態

[root@k8s-master zabbix]# kubectl get pods -n zabbix
NAME                             READY   STATUS    RESTARTS   AGE
zabbix-server-5f48d9d57d-rkr5p   1/1     Running   0          2m36s
zabbix-web-55cd66f74f-9f284      1/1     Running   0          18s
zabbixdb-69b7cd8dff-krt2w        1/1     Running   0          5m52s

八、部署zabbix-agent

1.編輯zabbix_agent.yaml

[root@k8s-master zabbix]# cat zabbix_agent.yaml 
apiVersion: apps/v1
kind: DaemonSet
metadata:
  labels:
    app: zabbix-agent
  name: zabbix-agent
  namespace: zabbix
spec:
  selector:
    matchLabels:
      app: zabbix-agent
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: zabbix-agent
    spec:
      hostNetwork: true
      containers:
      - image: zabbix/zabbix-agent:6.0-ubuntu-latest
        name: zabbix-agent
        env:
        - name: ZBX_SERVER_HOST
          value: 192.168.3.201
        resources: {}

2.安裝zabbix-agent

 kubectl apply -f zabbix_agent.yaml

3.檢視pod狀態

[root@k8s-master zabbix]# kubectl get pods -n zabbix
NAME                             READY   STATUS    RESTARTS   AGE
zabbix-agent-h2qwf               1/1     Running   0          3s
zabbix-agent-plnbd               1/1     Running   0          3s
zabbix-server-5f48d9d57d-rkr5p   1/1     Running   0          3m2s
zabbix-web-55cd66f74f-9f284      1/1     Running   0          44s
zabbixdb-69b7cd8dff-krt2w        1/1     Running   0          6m18s

九、存取zabbix的web

1.檢視svc

[root@k8s-master zabbix]#  kubectl get svc -n zabbix 
NAME         TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)          AGE
zabbix-web   NodePort    10.100.50.7   <none>        8080:30775/TCP   59s
zabbixdb     ClusterIP   10.96.1.99    <none>        3306/TCP         6m33s

2.登入web

http://192.168.3.202:30775

3.登入zabbix

初始賬號admin/zabbix

4.查詢zabbix-server的監控項圖表

到此這篇關於kubernetes叢集搭建Zabbix監控平臺的文章就介紹到這了,更多相關kubernetes叢集搭建Zabbix監控內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com