一、InfluxDB介紹
InfluxDB是一款用Go語言編寫的開源分布式時序、事件和指標數據庫。
官方提供1.X和2.X兩個版本,版本區別較大。改動最為明顯的是語法的變化,1.X版本使用的是SQL語句,2.X版本使用的JavaScript。
兩個版本官方均在提供技術支持。其中,1.X目前最新版本為1.8.4,2.X目前最新版本為2.0.4。
本文檔使用的InfluxDB版本為1.8.4。
二、InfluxDB部署
訪問hub.docker.com查看InfluxDB官方鏡像文檔說明,根據官方文檔介紹,導出默認配置文件。
docker pull influxdb:1.8.4-alpine #拉取最新版本鏡像 docker run --rm influxdb:1.8.4-alpine influxd config > influxdb.conf #導出默認配置文件
根據導出的配置文件內容創建對應的K8S ConfigMap,以便日后對部署在K8S內的InfluDB進行配置管理
kubectl create namespace influxdb kubectl create configmap influxdb-config --from-file influxdb.conf -n influxdb kubectl get cm influxdb-config -n influxdb NAME DATA AGE influxdb-config 1 26s kubectl get cm influxdb-config -n influxdb -oyaml #查看CM內容,內容省略
通過默認配置文件內容可以知道,InfluxDB數據保存在/var/lib/influxdb目錄中,為了實現持久化,需要為InfluxDB創建storageclass和pvc,其中storageclass使用的是K8S已部署好的后端ceph存儲。
cat influxdb-sc.yaml apiVersion: storage.k8s.io/v1 kind: StorageClass metadata: name: influxdb-sc provisioner: ceph.com/cephfs parameters: adminId: admin adminSecretName: ceph-secret adminSecretNamespace: kube-system claimRoot: /volumes/kubernetes/influxdb-sc monitors: 192.168.208.27:6789,192.168.208.28:6789,192.168.208.29:6789
cat influxdb-pvc.yaml apiVersion: v1 kind: PersistentVolumeClaim metadata: name: influxdb-pvc namespace: influxdb annotations: volume.beta.kubernetes.io/storage-class: "influxdb-sc" spec: accessModes: - ReadWriteMany resources: requests: storage: 20Gi
配置InfluxDB deployment和service的yaml文件
cat influxdb-dp.yaml apiVersion: apps/v1 kind: Deployment metadata: name: influxdb-dp namespace: influxdb labels: spec: replicas: 1 selector: matchLabels: app: influxdb template: metadata: labels: app: influxdb spec: containers: - name: influxdb image: influxdb:1.8.4-alpine imagePullPolicy: IfNotPresent ports: - name: influxdb containerPort: 8086 protocol: TCP volumeMounts: - name: influxdb-data mountPath: /var/lib/influxdb subPath: influxdb - name: influxdb-config mountPath: /etc/influxdb volumes: - name: influxdb-data persistentVolumeClaim: claimName: influxdb-pvc - name: influxdb-config configMap: name: influxdb-config
cat influxdb-svc.yaml apiVersion: v1 kind: Service metadata: name: influxdb-svc namespace: influxdb spec: type: NodePort ports: - port: 8086 targetPort: 8086 name: influxdb selector: app: influxdb
創建InfluxDB對應資源,查看pod狀態,svc狀態,endpoint狀態是否正常
kubectl apply -f influxdb-*.yaml kubectl get pod -n influxdb NAME READY STATUS RESTARTS AGE influxdb-dp-6c8756cfcd-pc8gf 1/1 Running 0 6m28s kubectl get svc -n influxdb NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE influxdb-svc NodePort 10.108.71.107 <none> 8086:31945/TCP 6h11m kubectl get ep influxdb-svc -n influxdb NAME ENDPOINTS AGE influxdb-svc 172.17.5.186:8086 6h11m
三、Influxdb開啟認證
首先進入pod,登錄influxdb服務器創建用戶
kubectl -n influxdb exec influxdb-dp-6c8756cfcd-pc8gf -it -- /bin/bash bash-5.0# influx Connected to http://localhost:8086 version 1.8.4 InfluxDB shell version: 1.8.4 > CREATE USER root WITH PASSWORD '123456' WITH ALL PRIVILEGES > show users user admin ---- ----- root true > exit
修改configmap內容,開啟InfluxDB登錄認證功能,此步驟能順便測試configmap是否掛載正常
kubectl edit cm influxdb-config -n influxdb #....其他內容省略.... [http] enabled = true bind-address = ":8086" auth-enabled = true #將此項false改為true #....其他內容省略....
重啟pod以重新加載configmap配置文件,此步驟能順便測試pvc持久卷是否掛載正常
kubectl -n influxdb delete pod influxdb-dp-6c8756cfcd-pc8gf kubectl -n influxdb get pod NAME READY STATUS RESTARTS AGE influxdb-dp-6c8756cfcd-fhvws 1/1 Running 0 30s
進入pod,測試認證功能是否開啟
kubectl -n influxdb exec influxdb-dp-6c8756cfcd-fhvws -it -- /bin/bash bash-5.0# influx Connected to http://localhost:8086 version 1.8.4 InfluxDB shell version: 1.8.4 > show database; ERR: unable to parse authentication credentials Warning: It is possible this error is due to not setting a database. #因為沒有登錄,所以報錯了 Please set a database with the command "use <database>". > auth #輸入auth進行認證,填寫創建好的賬號密碼 username: root password: > show databases #認證后可以正常列出databases name: databases name ---- _internal > exit