一款筑、部署方式
k8s 以statefulset方式部署redis集群
二、statefulset簡介
StatefulSet是Kubernetes提供的管理有狀態(tài)應(yīng)用的負(fù)載管理控制器API衙猪。在Pods管理的基礎(chǔ)上,保證Pods的順序和一致性管钳。與Deployment一樣虏肾,StatefulSet也是使用容器的Spec來創(chuàng)建Pod,與之不同StatefulSet創(chuàng)建的Pods在生命周期中會(huì)保持持久的標(biāo)記(例如Pod Name)羽圃。
StatefulSet適用于具有以下特點(diǎn)的應(yīng)用:
具有固定的網(wǎng)絡(luò)標(biāo)記(主機(jī)名)
具有持久化存儲(chǔ)
需要按順序部署和擴(kuò)展
需要按順序終止及刪除
需要按順序滾動(dòng)更新
二乾胶、安裝NFS
2.1?NFS安裝與配置?
2.2 創(chuàng)建?redis pv掛載目錄
mkdir?/data/tools/zk/pv
2.3?將redis pv掛載目錄
2.3.1?方法一
直接掛載到NFS共享目錄
2.3.2?方法二
將創(chuàng)建的redis pv掛載目錄再掛載到NFS共享目錄。
注:若都redis pv path都掛載到共享目錄统屈,則redis pv path不能相同
舉例:
zk3個(gè)節(jié)點(diǎn)的path?對應(yīng)NFS共享目錄
共享目錄1:/data/tools/pv/redis01
共享目錄2:/data/tools/pv/redis02
共享目錄3:/data/tools/pv/redis03
zk有3個(gè)節(jié)點(diǎn)要掛3個(gè)pv
pv1 name:?k8s-pv-redis1
pv1 path:/data/tools/pv/redis01
pv2 name:?k8s-pv-redis2
pv2 path:/data/tools/pv/redis02
pv3 name:?k8s-pv-redis3
pv3 path:/data/tools/pv/redis03
注:pv?path的路徑要與NFS共享目錄保持一致胚吁。
三牙躺、創(chuàng)建PV與PVC
? ?3.1 PV與PVC簡介
PersistentVolume(PV)是集群中由管理員配置的一段網(wǎng)絡(luò)存儲(chǔ)愁憔。 它是集群中的資源,就像節(jié)點(diǎn)是集群資源一樣孽拷。 PV是容量插件吨掌,如Volumes,但其生命周期獨(dú)立于使用PV的任何單個(gè)pod脓恕。 此API對象捕獲存儲(chǔ)實(shí)現(xiàn)的詳細(xì)信息膜宋,包括NFS,iSCSI或特定于云提供程序的存儲(chǔ)系統(tǒng)炼幔。
PersistentVolumeClaim(PVC)是由用戶進(jìn)行存儲(chǔ)的請求秋茫。 它類似于pod。 Pod消耗節(jié)點(diǎn)資源乃秀,PVC消耗PV資源肛著。Pod可以請求特定級別的資源(CPU和內(nèi)存)圆兵。聲明可以請求特定的大小和訪問模式(例如,可以一次讀/寫或多次只讀)枢贿。
3.2?編寫PV與PVC的yaml文件
pv-redis.yaml
kind: PersistentVolume
apiVersion: v1
metadata:
??name: pv-redis1
??namespace: tools
??annotations:
????volume.beta.kubernetes.io/storage-class: "anything"
??labels:
????type: local
spec:
??capacity:
????storage: 5Gi
??accessModes:
????- ReadWriteOnce
??hostPath:
path:/data/tools/pv/redis01
??persistentVolumeReclaimPolicy: Recycle
---
kind: PersistentVolume
apiVersion: v1
metadata:
??name: pv-redis2
??namespace: tools
??annotations:
????volume.beta.kubernetes.io/storage-class: "anything"
??labels:
????type: local
spec:
??capacity:
????storage: 5Gi
??accessModes:
????- ReadWriteOnce
??hostPath:
path:/data/tools/pv/redis02
??persistentVolumeReclaimPolicy: Recycle
---
kind: PersistentVolume
apiVersion: v1
metadata:
??name: pv-redis3
??namespace: tools
??annotations:
????volume.beta.kubernetes.io/storage-class: "anything"
??labels:
????type: local
spec:
??capacity:
????storage: 5Gi
??accessModes:
????- ReadWriteOnce
??hostPath:
path:/data/tools/pv/redis03
??persistentVolumeReclaimPolicy: Recycle
注:以上方式是同時(shí)創(chuàng)建PV與PVC的yaml文件
?3.3?創(chuàng)建pv與pvc
kubectl??apply? -fpv-redis.yaml
四殉农、創(chuàng)建redis集群
4.1?編寫redis.yaml文件
apiVersion: v1
kind: Service
metadata:
??name:k8s-redis-hs
??namespace: tools
??labels:
????app: k8s-redis
spec:
??ports:
??- port: 6305
????name: redis-port
??- port: 26305
????name: sentinel-port
??clusterIP: None
??selector:
????app: k8s-redis
---
apiVersion: v1
kind: Service
metadata:
??name: k8s-redis
??namespace: tools
??labels:
????app: k8s-redis
spec:
??ports:
??- port: 26305
????name: sentinel-port
??- port: 6305
????name: redis-port
??selector:
????app: k8s-redis
---
apiVersion: policy/v1beta1
kind: PodDisruptionBudget
metadata:
??name: k8s-redis-pdb
??namespace: tools
spec:
??minAvailable: 2
??selector:
????matchLabels:
??????app: k8s-redis
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
??name: ecs-k8s-redis???#存儲(chǔ)卷命名中間的???有狀態(tài)副本集名稱???容器組命名 -0 -1 -2
??namespace: tools
spec:
??selector:
????matchLabels:
??????app: k8s-redis
??serviceName: k8s-redis-hs
??replicas: 3
??updateStrategy:
????type: RollingUpdate
??podManagementPolicy: Parallel
??template:
????metadata:
??????labels:
????????app: k8s-redis
????spec:
??????containers:
??????- name: k8s-redis
????????imagePullPolicy: Always
????????image: "redis鏡像名稱"
????????resources:
??????????requests:
????????????memory: "1Gi"
??????????limits:
????????????memory: "1Gi"
????????ports:
????????- containerPort: 26305
??????????name: sentinel-port
????????- containerPort: 6305
??????????name: redis-port
????????command:
????????- sh
????????- -c
????????- "/data/redis-4.0.9/bin/redis.sh \
??????????--servers=3 \
??????????--password=ECS_DEPLOY_REDIS_PASSWD"
????????#k8s探針,監(jiān)控應(yīng)用運(yùn)行狀態(tài)
????????livenessProbe:
??????????initialDelaySeconds: 120???#容器起來多久才開始探測
??????????periodSeconds: 5????????????#探針活性探測每隔5秒探測一次
??????????timeoutSeconds: 5??????????#探針活性探測請求超時(shí)時(shí)間為5秒
??????????failureThreshold: 3?????????#探測失敗閾值局荚,超過3次就代表該容器掛掉了超凳,k8s將自動(dòng)重啟該容器
??????????successThreshold: 1???????#探測失敗后成功1次就認(rèn)為是成功的
??????????tcpSocket:
????????????port: 26305??
????????volumeMounts:
????????- name: datadir
??????????mountPath: /data/redis
??volumeClaimTemplates:
??- metadata:
??????name: datadir
??????annotations:
????????volume.beta.kubernetes.io/storage-class: "anything"
????spec:
??????accessModes: [ "ReadWriteOnce" ]
??????resources:
????????requests:
??????????storage: 2Gi
4.2?執(zhí)行redis.yaml文件
kubectl??apply??-f??redis.yaml
4.3?對外暴露訪問端口
#創(chuàng)建?redisService.yaml
touch?redisService.yaml
#redisService.yaml
apiVersion: v1
kind: Service
metadata:
??name: k8s-redis
??namespace: tools
??labels:
????app: k8s-redis
spec:
??type: NodePort
??ports:
??- port: 26305
????name: sentinel-port
??- port: 6305
????name: redis-port
????nodePort: 26305
??selector:
????app: k8s-redis
?注:nodePort為對外暴露端口,端口號(hào)必須5位數(shù)字耀态。建議與redis.yaml合并轮傍。
#執(zhí)行redisService.yaml文件
kubectl??apply??-fredisService.yaml