zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

k8s 安装redis-operator并以operator方式部署redis-standalone redis-cluster集群完整操作记录

Redisk8s安装集群部署 操作 方式 记录
2023-09-14 09:01:49 时间

安装redis-operator

install.sh

[root@linux:redis-operator]$ cat install.sh 
#!/usr/bin/env bash


# This script is for installing OLM from a GitHub release

set -e

default_base_url=https://github.com/operator-framework/operator-lifecycle-manager/releases/download

if [[ ${#@} -lt 1 || ${#@} -gt 2 ]]; then
    echo "Usage: $0 version [base_url]"
    echo "* version: the github release version"
    echo "* base_url: the github base URL (Default: $default_base_url)"
    exit 1
fi

if kubectl get deployment olm-operator -n openshift-operator-lifecycle-manager > /dev/null 2>&1; then
    echo "OLM is already installed in a different configuration. This is common if you are not running a vanilla Kubernetes cluster. Exiting..."
    exit 1
fi

release="$1"
base_url="${2:-${default_base_url}}"
url="${base_url}/${release}"
namespace=olm

if kubectl get deployment olm-operator -n ${namespace} > /dev/null 2>&1; then
    echo "OLM is already installed in ${namespace} namespace. Exiting..."
    exit 1
fi

kubectl apply -f "${url}/crds.yaml"
kubectl wait --for=condition=Established -f "${url}/crds.yaml"
kubectl apply -f "${url}/olm.yaml"

# wait for deployments to be ready
kubectl rollout status -w deployment/olm-operator --namespace="${namespace}"
kubectl rollout status -w deployment/catalog-operator --namespace="${namespace}"

retries=30
until [[ $retries == 0 ]]; do
    new_csv_phase=$(kubectl get csv -n "${namespace}" packageserver -o jsonpath='{.status.phase}' 2>/dev/null || echo "Waiting for CSV to appear")
    if [[ $new_csv_phase != "$csv_phase" ]]; then
        csv_phase=$new_csv_phase
        echo "Package server phase: $csv_phase"
    fi
    if [[ "$new_csv_phase" == "Succeeded" ]]; then
        break
    fi
    sleep 10
    retries=$((retries - 1))
done

if [ $retries == 0 ]; then
    echo "CSV \"packageserver\" failed to reach phase succeeded"
    exit 1
fi

kubectl rollout status -w deployment/packageserver --namespace="${namespace}"

redis-operator.yaml

[root@linux:redis-operator]$ cat redis-operator.yaml 
apiVersion: operators.coreos.com/v1alpha1
kind: Subscription
metadata:
  name: my-redis-operator
  namespace: operators
spec:
  channel: stable
  name: redis-operator
  source: operatorhubio-catalog
  sourceNamespace: olm

Install redis-operator on Kubernetes

  1. Install Operator Lifecycle Manager (OLM), a tool to help manage the Operators running on your cluster.

    $ curl -sL https://github.com/operator-framework/operator-lifecycle-manager/releases/download/v0.19.1/install.sh | bash -s v0.19.1
    
  2. Install the operator by running the following command:What happens when I execute this command?

    $ kubectl create -f https://operatorhub.io/install/redis-operator.yaml
    

    This Operator will be installed in the “operators” namespace and will be usable from all namespaces in the cluster.

  3. After install, watch your operator come up using next command.

    $ kubectl get csv -n operators
    

    To use it, checkout the custom resource definitions (CRDs) introduced by this operator to start using it.

部署redis

redis-standalone.yml

[root@linux:redis-operator]$ cat redis-single.yml 
apiVersion: redis.redis.opstreelabs.in/v1beta1
kind: Redis
metadata:
  name: redis-standalone
spec:
  kubernetesConfig:
    image: 'quay.io/opstree/redis:v6.2.5'
    imagePullPolicy: IfNotPresent
    resources:
      requests:
        cpu: 101m
        memory: 128Mi
      limits:
        cpu: 101m
        memory: 128Mi
    serviceType: ClusterIP
  storage:
    volumeClaimTemplate:
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 1Gi
  redisExporter:
    enabled: false
    image: 'quay.io/opstree/redis-exporter:1.0'

redis-cluster.yml

[root@linux:redis-operator]$ cat redis-cluster.yml 
apiVersion: redis.redis.opstreelabs.in/v1beta1
kind: RedisCluster
metadata:
  name: redis-cluster
spec:
  clusterSize: 3
  kubernetesConfig:
    image: 'quay.io/opstree/redis:v6.2.5'
    imagePullPolicy: IfNotPresent
    resources:
      requests:
        cpu: 101m
        memory: 128Mi
      limits:
        cpu: 101m
        memory: 128Mi
    serviceType: ClusterIP
  redisExporter:
    enabled: false
    image: 'quay.io/opstree/redis-exporter:1.0'
  redisLeader:
    serviceType: ClusterIP
  redisFollower:
    serviceType: ClusterIP
  storage:
    volumeClaimTemplate:
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 1Gi

执行结果:

[root@linux:redis-operator]$ kubectl get po 
NAME                       READY   STATUS    RESTARTS   AGE
redis-cluster-follower-0   1/1     Running   0          98s
redis-cluster-follower-1   1/1     Running   0          67s
redis-cluster-follower-2   1/1     Running   0          35s
redis-cluster-leader-0     1/1     Running   0          98s
redis-cluster-leader-1     1/1     Running   0          67s
redis-cluster-leader-2     1/1     Running   0          35s

进入集群,-c集群模式连接:

bash-4.4# redis-cli -c
127.0.0.1:6379> 
127.0.0.1:6379> 
127.0.0.1:6379> set k1 v1
-> Redirected to slot [12706] located at 172.17.0.13:6379
OK
172.17.0.13:6379> 
172.17.0.13:6379> 
172.17.0.13:6379> get k1
"v1"
172.17.0.13:6379>

参考链接:https://operatorhub.io/operator/redis-operator