• 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

    image

    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

    image

    查看创建的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

    image

    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

    image

    在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

    image

    使用kubectl get pv -o wide查看绑定情况

    kubectl get pv -o wide

    image

    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不支持域名来创建集群。


    随便进入一个redis pod中检验一下

    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

    image

    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

    image

    服务名称redis-access-service ,端口 6379,集群内都可以通过10.0.0.129:6379来访问了

    集群外使用 192.168.1.16:6379访问,如图

    image

    image


    参考链接 

    https://www.modb.pro/db/76406

  • 相关阅读:
    内置函数——filter和map
    递归函数
    内置函数和匿名函数
    迭代器和生成器
    装饰器函数
    函数进阶
    COGS 2533. [HZOI 2016]小鱼之美
    COGS 2532. [HZOI 2016]树之美 树形dp
    COGS2531. [HZOI 2016]函数的美 打表+欧拉函数
    bzoj1303: [CQOI2009]中位数图
  • 原文地址:https://www.cnblogs.com/pengrj/p/15716039.html
Copyright © 2020-2023  润新知