创建本地目录用于redis持久化
创建6个持久化文件mkdir -p /Users/renzhengxin/IdeaProjects/k8s/redis/v32/data/{pv1,pv2,pv3,pv4,pv5,pv6}
创建持久卷
创建6个持久卷定义vim redis-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: redis32-pv1
spec:
capacity:
storage: 200M
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
hostPath:
path: "/Users/renzhengxin/IdeaProjects/k8s/redis/v32/data/pv1"
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: redis32-pv2
spec:
capacity:
storage: 200M
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
hostPath:
path: "/Users/renzhengxin/IdeaProjects/k8s/redis/v32/data/pv2"
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: redis32-pv3
spec:
capacity:
storage: 200M
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
hostPath:
path: "/Users/renzhengxin/IdeaProjects/k8s/redis/v32/data/pv3"
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: redis32-pv4
spec:
capacity:
storage: 200M
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
hostPath:
path: "/Users/renzhengxin/IdeaProjects/k8s/redis/v32/data/pv4"
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: redis32-pv5
spec:
capacity:
storage: 200M
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
hostPath:
path: "/Users/renzhengxin/IdeaProjects/k8s/redis/v32/data/pv5"
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: redis32-pv6
spec:
capacity:
storage: 200M
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
hostPath:
path: "/Users/renzhengxin/IdeaProjects/k8s/redis/v32/data/pv6"
执行创建持久卷:kubectl create -f redis-pv.yaml 查看持久卷:kubectl get pv
创建ConfigMap
创建ConfigMap定义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
执行创建cm:kubectl create configmap redis-conf --from-file=redis.conf 查看cm:kubectl describe cm redis-conf
创建Headless服务
创建服务定义vim redis-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: redis32-svc
labels:
app: redis32
spec:
ports:
- name: redis-port
port: 6379
clusterIP: None
selector:
app: redis32
appCluster: redis32-cluster
执行创建服务:kubectl create -f redis-svc.yaml 查看服务:kubectl get svc
创建StatefulSet
创建StatefulSet定义vim redis-ss.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: redis32-app
spec:
serviceName: "redis32-svc"
replicas: 6
selector:
matchLabels:
app: redis32
appCluster: redis32-cluster
template:
metadata:
labels:
app: redis32
appCluster: redis32-cluster
spec:
terminationGracePeriodSeconds: 20
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: app
operator: In
values:
- redis32
topologyKey: kubernetes.io/hostname
containers:
- name: redis32
image: amd64/redis:3.2
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:
storageClassName: ""
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 200M
执行创建StatefulSet:kubectl create -f redis-ss.yaml 查看StatefulSet:kubectl get statefulSet 查看Pod:kubectl get po 查看PV:kubectl get pv
初始化redis集群
创建ubuntu容器kubectl run -it ubuntu1604 --image=ubuntu:16.04 --restart=Never /bin/bash apt-get更新apt-get update 安装必须的服务:apt-get install ruby wget dnsutils gem安装redis-3.2.0: wget https://rubygems.org/downloads/redis-3.2.0.gem gem install redis-3.2.0.gem 下载并解压redis-tirb.rb: wget http://download.redis.io/releases/redis-3.2.0.tar.gz tar -xvf redis-3.2.0.tar.gz 初始化redis集群:
./redis-3.2.0/src/redis-trib.rb create --replicas 1 `dig +short redis32-app-0.redis32-svc.default.svc.cluster.local`:6379 `dig +short redis32-app-1.redis32-svc.default.svc.cluster.local`:6379 `dig +short redis32-app-2.redis32-svc.default.svc.cluster.local`:6379 `dig +short redis32-app-3.redis32-svc.default.svc.cluster.local`:6379 `dig +short redis32-app-4.redis32-svc.default.svc.cluster.local`:6379 `dig +short redis32-app-5.redis32-svc.default.svc.cluster.local`:6379
验证集群创建成功
进入任意一个redis pod: kubectl exec -it redis32-app-2 /bin/bash /usr/local/bin/redis-cli -c cluster nodes cluster info
创建集群访问入口
创建服务定义vim redis-access-service.yaml
apiVersion: v1
kind: Service
metadata:
name: redis32-access-service
labels:
name: mysql32-svc
spec:
type: NodePort
ports:
- port: 6379
protocol: TCP
targetPort: 6379
name: http
# nodePort的端口号必须大于30000
nodePort: 30032
selector:
app: redis32
appCluster: redis32-cluster
执行创建服务:kubectl create -f redis-access-service.yaml 查看服务:kubectl get svc
访问集群
redis-cli -h localhost -p 30032 -c
|