Cloud Native 23 min read

Master Kubernetes: From Architecture to Redis Operator – A Hands‑On Guide

This tutorial walks through Kubernetes architecture, cluster setup methods, and a step‑by‑step Redis deployment using Pods, Deployments, StatefulSets, and an Operator SDK example, illustrating core concepts and practical commands for building reliable cloud‑native applications.

Alibaba Cloud Developer
Alibaba Cloud Developer
Alibaba Cloud Developer
Master Kubernetes: From Architecture to Redis Operator – A Hands‑On Guide

1 k8s Architecture

We look at the Kubernetes cluster architecture, which consists of a Master (Control Plane) and Node components.

Apiserver : Handles all API requests, authentication, and communicates with etcd; only the apiserver talks directly to etcd.

Controller‑manager : Runs various controllers to drive the current state toward the desired state.

Scheduler : Scores and assigns resources to pods.

Etcd : The cluster’s key‑value store, which can be deployed separately from the master.

Node components typically include Docker, kube‑proxy, and kubelet.

2 Building a k8s Cluster

Common ways to create a cluster are:

Enable Kubernetes in Docker Desktop.

Use MiniKube with its one‑click script.

Purchase a managed service such as Alibaba Cloud ACK.

Use kubeadm, the community‑recommended production‑grade tool.

Manually download binaries and install each component (versions must match the tutorial).

This article uses the Docker Desktop method.

kubectl version
Client Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.4", ...}
Server Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.4", ...}

3 From a Requirement

Deploy a Redis service that is highly available and provides a unified endpoint.

1 Deploy Single‑Node Redis

kubectl run redis --image=redis
pod/redis created
kubectl get pods
NAME   READY   STATUS    RESTARTS   AGE
redis  1/1     Running   0          5s
kubectl exec -it redis -- bash
root@redis:/data# redis-cli
127.0.0.1:6379> ping
PONG

In Kubernetes the Redis process runs inside a Pod.

2 Pod and Deployment

A Pod is the smallest scheduling unit and can contain multiple containers that share a network namespace and volumes. Using a Deployment adds a ReplicaSet, which ensures the desired number of pod replicas and provides automatic recovery.

kubectl create deployment redis-deployment --image=redis
deployment.apps/redis-deployment created
kubectl get pods
NAME                                 READY   STATUS    RESTARTS   AGE
redis-deployment-866c4c6cf9-8z8k5   1/1     Running   0          8s

3 Using YAML

apiVersion: v1
kind: Pod
metadata:
  name: redis
spec:
  containers:
  - name: redis
    image: redis

4 k8s Component Call Flow

When kubectl create deployment redis-deployment --image=redis is executed, the following occurs:

The controller‑manager, scheduler, and kubelet start a List‑Watch with the apiserver to observe current and desired states.

The apiserver authenticates the request and stores the desired state in etcd.

The controller‑manager sees that the current pod count is 0, creates a ReplicaSet for the desired count, and thus creates a Pod.

The scheduler selects a Node for the new Pod.

The kubelet on that Node pulls the Docker image and starts the container.

5 Deploy Master‑Slave Version

1 StatefulSet

StatefulSet provides ordered identity and stable storage for stateful applications.

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis-sfs
spec:
  serviceName: redis-sfs
  replicas: 2
  selector:
    matchLabels:
      app: redis-sfs
  template:
    metadata:
      labels:
        app: redis-sfs
    spec:
      containers:
      - name: redis-sfs
        image: redis
        command:
        - bash
        - "-c"
        - |
          set -ex
          ordinal=`hostname | awk -F '-' '{print $NF}'`
          if [[ $ordinal -eq 0 ]]; then
            echo > /tmp/redis.conf
          else
            echo "slaveof redis-sfs-0.redis-sfs 6379" > /tmp/redis.conf
          fi
          redis-server /tmp/redis.conf

After creation, pods redis-sfs-0 and redis-sfs-1 appear.

kubectl get pods
NAME            READY   STATUS    RESTARTS   AGE
redis-sfs-0     1/1     Running   0          33s
redis-sfs-1     1/1     Running   0          28s

2 Headless Service

A Headless Service (clusterIP: None) provides DNS entries for each pod, enabling the replica to discover the master via redis-sfs-0.redis-sfs.default.svc.cluster.local.

apiVersion: v1
kind: Service
metadata:
  name: redis-sfs
spec:
  clusterIP: None
  ports:
  - port: 6379
    name: redis-sfs
  selector:
    app: redis-sfs

After creating the service, the replica syncs successfully:

1:S 05 Nov 2021 08:23:31.341 * Connecting to MASTER redis-sfs-0.redis-sfs:6379
1:S 05 Nov 2021 08:23:31.345 * MASTER <-> REPLICA sync started
...

6 Operator

Operators extend Kubernetes with custom resources. The following example builds a Memcached operator using operator‑sdk.

1 Preparation

Install Go.

Install operator‑sdk.

2 Initialize Project

operator-sdk init --domain yangbodong22011 --repo github.com/yangbodong22011/memcached-operator --skip-go-version-check

3 Create API and Controller

operator-sdk create api --group cache --version v1alpha1 --kind Memcached --resource --controller

4 Implement Controller

Edit api/v1alpha1/memcached_types.go to define size and nodes fields, then run make generate and make manifests.

5 Build and Push Image

docker login
make docker-build docker-push

6 Deploy Operator

kubectl apply -f config/manager/manager.yaml
kubectl apply -f config/default/manager_auth_proxy_patch.yaml
make deploy

7 Create Memcached Cluster

apiVersion: cache.yangbodong22011/v1alpha1
kind: Memcached
metadata:
  name: memcached-sample
spec:
  size: 1
kubectl apply -f config/samples/cache_v1alpha1_memcached.yaml
kubectl get pods

7 Summary

The article introduced Kubernetes architecture and component functions, demonstrated a progressive Redis example covering Pods, Deployments, and StatefulSets, and provided a complete operator‑sdk walkthrough for building a custom Memcached operator.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Cloud NativeDeploymentKubernetesredisStatefulSetOperator SDK
Alibaba Cloud Developer
Written by

Alibaba Cloud Developer

Alibaba's official tech channel, featuring all of its technology innovations.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.