kubernetes operator influxdb-operator 实践
1.代码,镜像, 二进制文件
https://github.com/xishengcai/influxdata-operator.git fork 之官方
2. build 包中已经编译好二进制文件
cd influxdata-operator docker build ./build
docker images
docker tag {image_id} influxdb-operator:v1.0
3. 创建存储
创建 pv,pvc, storageclass
kuectl create -f deploy/storage.yaml
修改pv nodeAffinity
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: beta.kubernetes.io/arch
operator: In
values:
- amd64
pvc 中声明storageclass
pv 中也声明storageclass
pvc 最终绑定到符合storageclass条件的pv, 如果没有符合条件的pv则会pending
storageclass 动态绑定,将pv归类,pvc 绑定符合声明的那一类pv



4. 创建目录
mkdir /var/lib/influxdb
5. 创建influxdb-operator
kubectl create -f bundle.yaml
crd:Influxd
规定了 镜像,副本数目
apiVersion: influxdata.com/v1alpha1
kind: Influxdb
metadata:
name: influxdb
spec:
# Add fields here
size: 1
baseImage: influxdb:1.6.3-alpine
imagePullPolicy: IfNotPresent
pod:
resources:
limits:
cpu: 8
memory: 16Gi
requests:
cpu: 0.1
memory: 256Mi
persistentVolumeClaim:
metadata:
name: "influxdb-data-pvc"
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: standard-resize
resources:
requests:
storage: 8Gi
operator: influxdata-operator
启动operator deployment, 根据crd中的信息控制influxdb statefulset 副本状态
operator源码中的controller-manager 监听 influxdb statefulset 的状态,而 crd中存的就是期望的目标状态,如果不一致则进行update。
listwatch下有add, delete, update,主要靠这三个方法实现期望状态。
apiVersion: apps/v1
kind: Deployment
metadata:
name: influxdata-operator
spec:
replicas: 1
selector:
matchLabels:
name: influxdata-operator
template:
metadata:
labels:
name: influxdata-operator
spec:
serviceAccountName: influxdata-operator
containers:
- name: influxdata-operator
# Replace this with the built image name
image: influxdb-operator:v1.0
ports:
- containerPort: 60000
name: metrics
command:
- influxdata-operator
imagePullPolicy: IfNotPresent
env:
- name: WATCH_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: OPERATOR_NAME
value: "influxdata-operator"
6. 检查pod状态
kubectl get pod



7. 获取service ip
kubectl get svc
通过kubectl get all 可以看到default namespace创建的所有的资源对象
8.golang 代码测试
代码地址:https://github.com/xishengcai/go_learn/blob/master/client/influxdb.go

9. 数据备份
创建pv, pvc

创建 crd: Backup
kubectl create -f influxdata_v1alpha1_backup_cr_pv.yaml
apiVersion: influxdata.com/v1alpha1
kind: Backup
metadata:
name: influxdb-backup
spec:
podname: "influxdb-0"
containername: "influxdb"
# [ -database <db_name> ] Optional: If not specified, all databases are backed up.
databases:
# [ -shard <ID> ] Optional: If specified, then -retention <name> is required.
shard:
# [ -retention <rp_name> ] Optional: If not specified, the default is to use all retention policies. If specified, then -database is required.
retention:
# [ -start <timestamp> ] Optional: Not compatible with -since.
start:
# [ -end <timestamp> ] Optional: Not compatible with -since. If used without -start, all data will be backed up starting from 1970-01-01.
end:
# [ -since <timestamp> ] Optional: Use -start instead, unless needed for legacy backup support.
since:
storage:
provider: in2
log:kubectl logs -f influxdata-operator-b5c7cdfdf-7sr85


10. 问题
1.不同pod的pv没有实现数据同步
2.没有读写分离

浙公网安备 33010602011771号