k8s自动化部署tendis/redis集群

自动化部署 tendis集群和redis集群初始化集群步骤一样,创建出3主3从的节点后,再创建一个初始化的job,对集群进行初始化配置。(redis集群创建修改镜像即可)

创建svc

以下是svc的yaml

kind: Service

		apiVersion: v1

		metadata:

		  name: tendis-cluster-auto-9n44

		  namespace: tendis

		  labels:

		    app: tendis-cluster-auto

		  annotations:

		    kubesphere.io/alias-name: tendis-cluster-auto

		    kubesphere.io/serviceType: statefulservice

		spec:

		  ports:

		    - name: http-0

		      protocol: TCP

		      port: 30032

		      targetPort: 51002

		  selector:

		    app: tendis-cluster-auto

		  clusterIP: None

		  clusterIPs:

		    - None

		  type: ClusterIP

		  sessionAffinity: None

		  ipFamilies:

		    - IPv4

		  ipFamilyPolicy: SingleStack

		  internalTrafficPolicy: Cluster</p></div></div></th></tr></thead><tbody></tbody></table></div><h2 id="9a46r" name="%E5%88%9B%E5%BB%BA6%E5%89%AF%E6%9C%AC%E7%9A%84statefulset">创建6副本的statefulset</h2><p>通过pvc模板自动创建对应的pvc挂载,可自行修改</p><div class="table-wrapper"><table><thead><tr><th style="text-align:left"><div><div class="table-header"><p>kind: StatefulSet

		apiVersion: apps/v1

		metadata:

		  name: tendis-cluster-auto

		  namespace: tendis

		  labels:

		    app: tendis-cluster-auto

		spec:

		  replicas: 6

		  selector:

		    matchLabels:

		      app: tendis-cluster-auto

		  template:

		    metadata:

		      creationTimestamp: null

		      labels:

		        app: tendis-cluster-auto

		      annotations:

		        kubesphere.io/imagepullsecrets: &#39;{}&#39;

		        logging.kubesphere.io/logsidecar-config: &#39;{}&#39;

		    spec:

		      volumes:

		        - name: host-time

		          hostPath:

		            path: /etc/localtime

		            type: &#39;&#39;

		      containers:

		        - name: container-s140dj

		          image: &#39;tencentdbforkv/tendisplus:latest&#39;

		          ports:

		            - name: http-0

		              containerPort: 51002

		              protocol: TCP

		          env:

		            - name: CLUSTER

		               value:yes

		            - name: REDIS_PASSWORD

		               value:test12345

		          resources: {}

		          volumeMounts:

		            - name: host-time

		              mountPath: /etc/localtime

		            - name: tendis-pvc

		              mountPath: /data1/tendis/51002

		          terminationMessagePath: /dev/termination-log

		          terminationMessagePolicy: File

		          imagePullPolicy: IfNotPresent

		      restartPolicy: Always

		      terminationGracePeriodSeconds: 30

		      dnsPolicy: ClusterFirst

		      serviceAccountName: default

		      serviceAccount: default

		      securityContext: {}

		      schedulerName: default-scheduler

		  volumeClaimTemplates:

		    - kind: PersistentVolumeClaim

		      apiVersion: v1

		      metadata:

		        name: tendis-pvc

		        namespace: tendis

		        creationTimestamp: null

		      spec:

		        accessModes:

		          - ReadWriteOnce

		        resources:

		          requests:

		            storage: 10Gi

		        storageClassName: default

		        volumeMode: Filesystem

		      status:

		        phase: Pending

		  serviceName: tendis-cluster-auto-9n44

		  podManagementPolicy: OrderedReady

		  updateStrategy:

		    type: RollingUpdate

		    rollingUpdate:

		      partition: 0

		  revisionHistoryLimit: 10</p></div></div></th></tr></thead><tbody></tbody></table></div><h2 id="7o1ti" name="%E8%87%AA%E5%88%B6%E5%88%9D%E5%A7%8B%E5%8C%96%E9%95%9C%E5%83%8F">自制初始化镜像</h2><p>docker pull tencentdbforkv/build:latest</p><p>这个镜像的系统是Centos8</p><p>在这个镜像的基础上更新yum源,然后安装dig命令,commit 提交成一个有dig命令的镜像。</p><h3 id="7r8cv" name="%E4%BF%AE%E6%94%B9build.sh-%E8%84%9A%E6%9C%AC">修改build.sh 脚本</h3><p>通过docker run -itd tencentdbforkv/build:latest sleep 36000s  然后进入容器</p><p>该脚本需传入4个环境变量参数 </p><p>PODNAME,PASSWORD,NAMESPACE,SVCNAME,用于</p><p>拼接k8s的DNS域名解析地址${PODNAME}-$i.${SVCNAME}.${NAMESPACE}.svc.cluster.local</p><p>就能通过DNS域名去解析到各个节点去执行初始化操作。</p><p>然后替换里面的build.sh脚本为下面的脚本</p><div class="table-wrapper"><table><thead><tr><th style="text-align:left"><div><div class="table-header"><p>#!/bin/bash

		set -ue

		

		sleep 10

		PORT=51002

		#PODNAME=$1

		SIZE=3

		#PASSWORD=$2

		#NAMESPACE=$3

		#SVCNAME=$4

		

		slot_num=$((16384 / ${SIZE}))

		

		for (( i = 0; i &lt; 6; i++)); do

		    declare &#34;ip$i&#34;=”$(dig +short  ${PODNAME}-$i.${SVCNAME}.${NAMESPACE}.svc.cluster.local)“

		done

		

		

		for (( i = 0; i &lt; 6; i++ )); do

		       ip_var=&#34;ip$i&#34;

		        ./redis-cli -h ${ip0} -p ${PORT} -a ${PASSWORD} cluster meet ${!ip_var} ${PORT}

		done

		

		echo &#34;cluster meet done......&#34;

		sleep 10

		

		./redis-cli -h ${ip0} -p ${PORT} -a ${PASSWORD}  cluster nodes

		

		for (( i = 0; i &lt; 3; i++ )); do

		        uuid[i]=$(./redis-cli -h ${ip0} -p ${PORT} -a ${PASSWORD}  cluster nodes | grep &#34;ip$i&#34; | awk &#39;{print $1}&#39;)

		done

		

		for (( i = 3; i &lt; 6; i++ )); do

		       ./redis-cli -h &#34;ip$i&#34; -p ${PORT}  -a ${PASSWORD} cluster replicate &#34;${uuid[$((i-3))]}&#34;

		done

		

		./redis-cli -h ${ip0} -p ${PORT} -a ${PASSWORD} cluster addslots {0..5461}

		./redis-cli -h ${ip1} -p ${PORT} -a ${PASSWORD} cluster addslots {5462..10922}

		./redis-cli -h ${ip2} -p ${PORT} -a ${PASSWORD} cluster addslots {10923..16383}

		

		

		echo &#34;cluster build done..&#34;

		sleep 5

		./redis-cli -h ${IP}.10 -p ${PORT}  -a ${PASSWORD} cluster nodes</p></div></div></th></tr></thead><tbody></tbody></table></div><p>保存脚本然后commit为最新的镜像</p><h2 id="dd25p" name="%E5%88%9B%E5%BB%BAjob%E5%88%9D%E5%A7%8B%E5%8C%96%E9%9B%86%E7%BE%A4%EF%BC%8C%E5%B9%B6%E4%BC%A0%E5%85%A54%E4%B8%AA%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F">创建job初始化集群,并传入4个环境变量</h2><div class="table-wrapper"><table><thead><tr><th style="text-align:left"><div><div class="table-header"><p>kind: Job

		apiVersion: batch/v1

		metadata:

		  name: tendisbuild

		  namespace: tendis

		  labels:

		    app: tendisbuild

		  parallelism: 1

		  completions: 1

		  activeDeadlineSeconds: 60

		  backoffLimit: 2

		  selector:

		    matchLabels:

		      controller-uid: b2e57703-21fd-4011-8505-85353c6075f2

		  template:

		    metadata:

		      creationTimestamp: null

		      labels:

		        app: tendisbuild

		        controller-uid: b2e57703-21fd-4011-8505-85353c6075f2

		        job-name: tendisbuild

		    spec:

		      containers:

		        - name: container-j8xf02

		          image: &#39;harbor/build-tedis:v5&#39;

		          command:

		            - /bin/bash

		            - &#39;-c&#39;

		          args:

		            - cd /data1/build &amp;&amp; sh build.sh

		          ports:

		            - name: http-0

		              containerPort: 20003

		              protocol: TCP

		          env:

		            - name: PASSWORD

		              value: test12345

		            - name: DNS

		              value: tendis-cluster-auto-9n44

		            - name: PODNAME

		              value: tendis-cluster-auto

		            - name: NAMESPACE

		              value: tendis

		          resources: {}

		          terminationMessagePath: /dev/termination-log

		          terminationMessagePolicy: File

		          imagePullPolicy: IfNotPresent

		      restartPolicy: OnFailure

		      terminationGracePeriodSeconds: 30

		      dnsPolicy: ClusterFirst

		      serviceAccountName: default

		      serviceAccount: default

		      securityContext: {}

		      schedulerName: default-scheduler

		  completionMode: NonIndexed

		  suspend: false</p></div></div></th></tr></thead><tbody></tbody></table></div><p>查看自动初始化后的集群信息</p><p></p><figure class=""><div class="rno-markdown-img-url" style="text-align:center"><div class="rno-markdown-img-url-inner" style="width:100%"><div style="width:100%"><img src="https://cdn.static.attains.cn/app/developer-bbs/upload/1722855438635615228.png" /></div></div></div></figure><figure class=""><div class="rno-markdown-img-url" style="text-align:center"><div class="rno-markdown-img-url-inner" style="width:0.13%"><div style="width:100%"><img src="https://cdn.static.attains.cn/app/developer-bbs/upload/1722855439121693234.gif" /></div></div></div></figure><p>​编辑</p><p>​</p>