k8s&dockerZABBIX

使用zabbix监控k8s

2020-05-18  本文已影响0人  阿乐_822e
[root@k8s-master0 ~]# kg node
NAME          STATUS   ROLES    AGE   VERSION
k8s-master0   Ready    master   25d   v1.17.0
k8s-master1   Ready    master   25d   v1.17.0
k8s-master2   Ready    master   25d   v1.17.0
k8s-node0     Ready    <none>   25d   v1.17.0
node1         Ready    <none>   9d    v1.17.0

说明:下文中用的一些命令别名,在https://www.jianshu.com/p/2f61a84bd739中有提到

[root@k8s-master0 ~]# cd zabbix-setup/
[root@k8s-master0 ~]# kc create ns szatc-deploy
[root@k8s-master0 zabbix-setup]# cat  mysql-secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: mysql-user-pwd
  namespace: szatc-deploy
data:
  mysql-root-pwd: cGFzc3dvcmQ=
[root@k8s-master0 zabbix-setup]# cat mysql-config.yaml  
apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-config
  namespace: szatc-deploy
data:
  custom.cnf: |
    [mysqld]
    default_storage_engine=innodb
    skip_external_locking
    skip_host_cache
    skip_name_resolve
    default_authentication_plugin=mysql_native_password
[root@k8s-master0 zabbix-setup]# cat mysql-pv-pvc.yaml 
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv-volume
  namespace: szatc-deploy
  labels:
    type: nfs
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  nfs:
    server: 172.20.10.175
    path: "/data/mysql"

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
  namespace: szatc-deploy
spec:
  #storageClassName: ""
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
[root@k8s-master0 zabbix-setup]# cat mysql-deployment.yaml
apiVersion: v1
kind: Service
metadata:
  name: mysql
  namespace: szatc-deploy
spec:
  type: NodePort
  ports:
  - port: 3306
    nodePort: 30006
    protocol: TCP
    targetPort: 3306 
  selector:
    app: mysql

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
  namespace: szatc-deploy
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - image: mysql:5.7
        name: mysql
        imagePullPolicy: IfNotPresent
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-user-pwd
              key: mysql-root-pwd
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-config
          mountPath: /etc/mysql/conf.d/
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
        - name: timezone
          mountPath: /etc/localtime
      volumes:
      - name: mysql-config
        configMap:
          name: mysql-config
      - name: timezone
        hostPath:
          path: /usr/share/zoneinfo/Asia/Shanghai
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim
[root@k8s-master0 zabbix-setup]# cat zabbix-server-deploy.yaml
apiVersion: v1
kind: Service
metadata:
  name: zabbix-server
  namespace: szatc-deploy
spec:
  type: NodePort
  ports:
  - port:  10051
    nodePort: 30051
    protocol: TCP
    targetPort: 10051
  selector:
    app: zabbix-server
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: zabbix-server
  namespace: szatc-deploy
spec:
  replicas: 1
  selector:
    matchLabels:
      app: zabbix-server
  template:
    metadata:
      labels:
        app: zabbix-server
    spec:
      containers:
        - name: zabbix-server
          image: zabbix/zabbix-server-mysql
          imagePullPolicy: IfNotPresent
          ports:
          - containerPort: 10051
            name: server
            protocol: TCP
          readinessProbe:
            tcpSocket:
              port: server
            initialDelaySeconds: 5
            periodSeconds: 10
          livenessProbe:
            tcpSocket:
              port: server
            initialDelaySeconds: 15
            periodSeconds: 20
          env:
          - name: DB_SERVER_HOST
            value: "mysql"
          - name: MYSQL_ROOT_PASSWORD
            valueFrom:
              secretKeyRef:
                name: mysql-user-pwd
                key: mysql-root-pwd
          - name: MYSQL_USER
            value: "zabbix"
          - name: MYSQL_PASSWORD
            value: "zabbix"
          - name: MYSQL_DATABASE
            value: "zabbix"
          - name: ZBX_CACHESIZE
            value: "1024M"
          - name: ZBX_TRENDCACHESIZE
            value: "1024M"
          - name: ZBX_HISTORYCACHESIZE
            value: "2048M"
          - name: ZBX_HISTORYINDEXCACHESIZE
            value: "1024M"
          - name: ZBX_STARTTRAPPERS
            value: "5"
          - name: ZBX_STARTPREPROCESSORS
            value: "10"
          - name: ZBX_STARTDBSYNCERS
            value: "10"
          - name: DB_SERVER_PORT
            value: "3306"
        - name: zabbix-agent
          image: zabbix/zabbix-agent
          imagePullPolicy: IfNotPresent
          ports:
          - containerPort: 10050
            name: zabbix-agent
          env:
          - name: ZBX_HOSTNAME
            value: "Zabbix server"
          - name: ZBX_SERVER_HOST
            value: "127.0.0.1"
          - name: ZBX_PASSIVE_ALLOW
            value: "true"
          - name: ZBX_STARTAGENTS
            value: "3"
          - name: ZBX_TIMEOUT
            value: "10"
          securityContext:
            privileged: true
[root@k8s-master0 zabbix-setup]# cat zabbix-user-example.yml 
apiVersion: v1
kind: ServiceAccount
metadata:
  name: zabbix-user
  namespace: default
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: zabbix-user
rules:
- apiGroups:
  - ""
  resources:
  - pods
  - services
  - nodes
  - namespaces
  - apiservices
  - componentstatuses
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - "apiregistration.k8s.io"
  resources:
  - apiservices
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - "apps"
  resources:
  - deployments
  verbs:
  - get
  - list
  - watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: zabbix-user
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: zabbix-user
subjects:
- kind: ServiceAccount
  name: zabbix-user
  namespace: default
[root@k8s-master0 zabbix-setup]# cat zabbix-web.yaml
apiVersion: v1
kind: Service
metadata:
  name: zabbix-web
  namespace: szatc-deploy
spec:
  type: NodePort
  ports:
  - port: 80
    protocol: TCP
    nodePort: 30080
    targetPort: 80
  selector:
    app: zabbix-web
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: zabbix-web
  namespace: szatc-deploy
spec:
  replicas: 1
  selector:
    matchLabels:
      app: zabbix-web
  template:
    metadata:
      labels:
        app: zabbix-web
    spec:
      containers:
      - image: zabbix/zabbix-web-nginx-mysql
        name: zabbix-web
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
          name: web
          protocol: TCP
        env:
        - name: DB_SERVER_HOST
          value: "mysql"
        - name:  ZBX_SERVER_HOST
          value: "zabbix-server"
        - name: MYSQL_USER
          value: "zabbix"
        - name: MYSQL_PASSWORD
          value: "zabbix"
        - name: TZ
          value: "Asia/Shanghai"
[root@k8s-master0 zabbix-setup]#  kf .
  1. api_server = 'https://172.20.10.160:6443' # 这里改成自己的apiServer地址
  2. token 地址:
[root@k8s-master0 zabbix-setup]# kg secret
NAME                      TYPE                                  DATA   AGE
default-token-z7cft       kubernetes.io/service-account-token   3      25d
zabbix-user-token-gpp4m   kubernetes.io/service-account-token   3      6d5h
[root@k8s-master0 zabbix-setup]#  kg secret zabbix-user-token-gpp4m  -o yaml
# 复制下其中的token字段,再作下编码
[root@k8s-master0 zabbix-setup]# echo "ZXlK......==" |base64 -d
# 把编码后的字符作为k8s-stats.py中的token地址
# 还有一点要注意的是把Zabbix-agent安装的目录设为Zabbix用户所有,过程不赘述
# 复制k8s-zabbix-template.xml模板文件保存到本地,并导入到Zabbix中,再应用到master01机器 ...
上一篇 下一篇

猜你喜欢

热点阅读