k8s部署redis叢集
mkdir –p /root/redis-cluster
cd /root/redis-cluster
a.安裝nfs-共享儲存
centos系統中使用yum 安裝
yum -y install nfs-utils rpcbind
vim /etc/exports
/usr/local/kubernetes/redis/pv1 *(rw,no_root_squash,no_all_squash,sync)
/usr/local/kubernetes/redis/pv2 *(rw,no_root_squash,no_all_squash,sync)
/usr/local/kubernetes/redis/pv3 *(rw,no_root_squash,no_all_squash,sync)
/usr/local/kubernetes/redis/pv4 *(rw,no_root_squash,no_all_squash,sync)
/usr/local/kubernetes/redis/pv5 *(rw,no_root_squash,no_all_squash,sync)
/usr/local/kubernetes/redis/pv6 *(rw,no_root_squash,no_all_squash,sync)
啟動服務nfs rpcbind 服務
systemctl enable nfs
systemctl enable rpcbind
systemctl start nfs
systemctl start rpcbind
b.建立PV,建立6個供pvc掛載使用
vim pvxin.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: nfs-pv1
spec:
capacity:
storage: 200M #磁碟大小200M
accessModes:
- ReadWriteMany #多客戶可讀寫
nfs:
server: 192.168.1.32
path: "/usr/local/kubernetes/redis/pv1"
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: nfs-pv2
spec:
capacity:
storage: 200M
accessModes:
- ReadWriteMany
nfs:
server: 192.168.1.32
path: "/usr/local/kubernetes/redis/pv2"
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: nfs-pv3
spec:
capacity:
storage: 200M
accessModes:
- ReadWriteMany
nfs:
server: 192.168.1.32
path: "/usr/local/kubernetes/redis/pv3"
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: nfs-pv4
spec:
capacity:
storage: 200M
accessModes:
- ReadWriteMany
nfs:
server: 192.168.1.32
path: "/usr/local/kubernetes/redis/pv4"
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: nfs-pv5
spec:
capacity:
storage: 200M
accessModes:
- ReadWriteMany
nfs:
server: 192.168.1.32
path: "/usr/local/kubernetes/redis/pv5"
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: nfs-pv6
spec:
capacity:
storage: 200M
accessModes:
- ReadWriteMany
nfs:
server: 192.168.1.32
path: "/usr/local/kubernetes/redis/pv6"
kubectl apply -f pvxin.yaml
c.建立configmap 存放redis配置檔案
vim redis.conf
appendonly yes
cluster-enabled yes
cluster-config-file /var/lib/redis/nodes.conf
cluster-node-timeout 5000
dir /var/lib/redis
port 6379
建立名為redis-conf的Configmap:
kubectl create configmap redis-conf --from-file=redis.conf
kubectl get cm
檢視建立的configmap:
kubectl describe cm redis-conf
d.建立headless service
它是statefulset 實現穩定網路標識的基礎。檔案如下:
vim redis-headless-service.yaml
apiVersion: v1
kind: Service
metadata:
name: redis-service
labels:
app: redis
spec:
ports:
- name: redis-port
port: 6379
clusterIP: None
selector:
app: redis
appCluster: redis-cluster
kubectl apply –f redis-headless-service.yaml
kubectl get svc redis-service
f.建立redis叢集節點,配置如下:
vim redis-cluster-node.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: redis-app
spec:
serviceName: "redis-service"
replicas: 6
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
appCluster: redis-cluster
spec:
terminationGracePeriodSeconds: 20
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: app
operator: In
values:
- redis
topologyKey: kubernetes.io/hostname
containers:
- name: redis
image: redis
command:
- "redis-server"
args:
- "/etc/redis/redis.conf"
- "--protected-mode"
- "no"
resources:
requests:
cpu: "100m"
memory: "100Mi"
ports:
- name: redis
containerPort: 6379
protocol: "TCP"
- name: cluster
containerPort: 16379
protocol: "TCP"
volumeMounts:
- name: "redis-conf"
mountPath: "/etc/redis"
- name: "redis-data"
mountPath: "/var/lib/redis"
volumes:
- name: "redis-conf"
configMap:
name: "redis-conf"
items:
- key: "redis.conf"
path: "redis.conf"
volumeClaimTemplates:
- metadata:
name: redis-data
spec:
accessModes: [ "ReadWriteMany" ]
resources:
requests:
storage: 200M
kubectl apply -f redis-cluster-node.yaml
kubectl get pods -o wide
在K8S叢集內部,這些Pod就可以利用該域名互相通訊。我們可以使用busybox映象的nslookup檢驗這些域名。
kubectl run -i --tty --image busybox dns-test --restart=Never --rm bin/sh
這裡隨便用一個測試解析
/ # nslookup redis-app-0.redis-service.default.svc.cluster.local
使用kubectl get pv -o wide檢視繫結情況
kubectl get pv -o wide
e. 初始化redis 叢集,使用redis-tribe工具進行叢集的初始化,建立centos容器
kubectl run -i --tty centos --image=centos --restart=Never /bin/bash
進入容器
vi etc/yum.repos.d/epel.repo
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
baseurl=https://mirrors.tuna.tsinghua.edu.cn/epel/7/$basearch
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
安裝redis-trib
yum -y install redis-trib.noarch bind-utils
建立一個新叢集,--replicas 1 建立叢集中每個主節點分配一個從節點,達到3主3從
[root@centos ]# redis-trib create --replicas 1 `dig +short redis-app-0.redis-service.default.svc.cluster.local`:6379 `dig +short redis-app-1.redis-service.default.svc.cluster.local`:6379 `dig +short redis-app-2.redis-service.default.svc.cluster.local`:6379 `dig +short redis-app-3.redis-service.default.svc.cluster.local`:6379 `dig +short redis-app-4.redis-service.default.svc.cluster.local`:6379 `dig +short redis-app-5.redis-service.default.svc.cluster.local`:6379
輸入 yes
命令dig +short redis-app-0.redis-service.default.svc.cluster.local用於將Pod的域名轉化為IP,這是因為redis-trib不支援域名來建立叢集。
kubectl exec -it redis-app-2 /bin/bash
root@redis-app-2:/data# /usr/local/bin/redis-cli –c
127.0.0.1:6379> cluster info
127.0.0.1:6379> cluster nodes
exit
exit
f.建立可訪問的service,用於redis叢集提供訪問和負載均衡,這裡配置nodeport
vim redis-access-service.yaml
apiVersion: v1
kind: Service
metadata:
name: redis-access-service
labels:
app: redis
spec:
ports:
- name: redis-port
protocol: "TCP"
port: 6379
targetPort: 6379
nodePort: 6379
selector:
app: redis
appCluster: redis-cluster
type: NodePort
kubectl apply –f redis-access-service.yaml
kubectl get svc redis-access-service -o wide
服務名稱redis-access-service ,埠 6379,叢集內都可以通過10.0.0.129:6379來訪問了
叢集外使用 192.168.1.16:6379訪問,如圖
參考連結