內容目錄
為什麼要使用Redis?
它的速度非常快。它是用ANSI C編寫的,並且可以在POSIX系統上運行,例如Linux,Mac OS X和Solaris。
Redis通常被排名為最流行的鍵/值數據庫和最流行的與容器一起使用的NoSQL數據庫。
其緩存解決方案減少了對雲數據庫後端的調用次數。
應用程序可以通過其客戶端API庫對其進行訪問。
所有流行的編程語言都支持Redis。
它是開源且穩定的。
在Kubernetes中部署Redis集群面臨挑戰,因為每個Redis實例都依賴於一個配置文件,該文件可以跟踪其他集群實例及其角色。
為此,我們需要結合使用Kubernetes StatefulSets和PersistentVolumes。
部署Reids
$ kubectl apply -f redis-sts.yaml
configmap/redis-cluster created
statefulset.apps/redis-cluster created
$ kubectl apply -f redis-svc.yaml
service/redis-cluster created
兩個yaml設定檔如下
redis-sts.yaml
--- apiVersion: v1 kind: ConfigMap metadata: name: redis-cluster data: update-node.sh: | #!/bin/sh REDIS_NODES="/data/nodes.conf" sed -i -e "/myself/ s/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/${POD_IP}/" ${REDIS_NODES} exec "$@" redis.conf: |+ cluster-enabled yes cluster-require-full-coverage no cluster-node-timeout 15000 cluster-config-file /data/nodes.conf cluster-migration-barrier 1 appendonly yes protected-mode no --- apiVersion: apps/v1 kind: StatefulSet metadata: name: redis-cluster spec: serviceName: redis-cluster replicas: 6 selector: matchLabels: app: redis-cluster template: metadata: labels: app: redis-cluster spec: containers: - name: redis image: redis:5.0.1-alpine ports: - containerPort: 6379 name: client - containerPort: 16379 name: gossip command: ["/conf/update-node.sh", "redis-server", "/conf/redis.conf"] env: - name: POD_IP valueFrom: fieldRef: fieldPath: status.podIP volumeMounts: - name: conf mountPath: /conf readOnly: false - name: data mountPath: /data readOnly: false volumes: - name: conf configMap: name: redis-cluster defaultMode: 0755 volumeClaimTemplates: - metadata: name: data spec: accessModes: [ "ReadWriteOnce" ] resources: requests: storage: 1Gi
redis-svc.yaml
--- apiVersion: v1 kind: Service metadata: name: redis-cluster spec: type: ClusterIP ports: - port: 6379 targetPort: 6379 name: client - port: 16379 targetPort: 16379 name: gossip selector: app: redis-cluster
驗證部署
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
redis-cluster-0 1/1 Running 0 7m
redis-cluster-1 1/1 Running 0 7m
redis-cluster-2 1/1 Running 0 6m
redis-cluster-3 1/1 Running 0 6m
redis-cluster-4 1/1 Running 0 6m
redis-cluster-5 1/1 Running 0 5m
以下是建立起來的6個pv
$ kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-ae61ad5c-f0a5-11e8-a6e0-42010aa40039 1Gi RWO Delete Bound default/data-redis-cluster-0 standard 7m
pvc-b74b6ef1-f0a5-11e8-a6e0-42010aa40039 1Gi RWO Delete Bound default/data-redis-cluster-1 standard 7m
pvc-c4f9b982-f0a5-11e8-a6e0-42010aa40039 1Gi RWO Delete Bound default/data-redis-cluster-2 standard 6m
pvc-cd7af12d-f0a5-11e8-a6e0-42010aa40039 1Gi RWO Delete Bound default/data-redis-cluster-3 standard 6m
pvc-d5bd0ad3-f0a5-11e8-a6e0-42010aa40039 1Gi RWO Delete Bound default/data-redis-cluster-4 standard 6m
pvc-e3206080-f0a5-11e8-a6e0-42010aa40039 1Gi RWO Delete
檢查pod attach的volume
$ kubectl describe pods redis-cluster-0 | grep pvc
Normal SuccessfulAttachVolume 29m attachdetach-controller
部署Redis集群
下一步是形成Redis集群。為此,我們運行以下命令並鍵入yes以接受配置。前三個節點成為主節點,後三個節點成為從節點。
$ kubectl exec -it redis-cluster-0 -- redis-cli --cluster create --cluster-replicas 1 $(kubectl get pods -l app=redis-cluster -o jsonpath='{range.items[*]}{.status.podIP}:6379 ')
驗證集群部署
$ kubectl exec -it redis-cluster-0 -- redis-cli cluster info
輸出如下:
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:192
cluster_stats_messages_pong_sent:204
cluster_stats_messages_sent:396
cluster_stats_messages_ping_received:199
cluster_stats_messages_pong_received:192
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:396
$ for x in $(seq 0 5); do echo "redis-cluster-$x"; kubectl exec redis-cluster-$x -- redis-cli role; echo; done
輸出如下:
redis-cluster-0
master
420
10.60.1.166
6379
420
redis-cluster-1
master
420
10.60.0.24
6379
420
redis-cluster-2
master
420
10.60.2.44
6379
420
redis-cluster-3
slave
10.60.1.164
6379
connected
420
redis-cluster-4
slave
10.60.2.43
6379
connected
420
redis-cluster-5
slave
10.60.1.165
6379
connected
434
登入redis
$ kubectl exec -it redis-cluster-0 -- redis-cli #無密碼登入 $ kubectl exec -it redis-cluster-0 -- redis-cli -a yourpassword #使用密碼登入
測試Redis集群
我們想使用集群,然後模擬節點的故障。對於前一項任務,我們將部署一個簡單的Python應用程序,而對於後者,我們將刪除一個節點並觀察集群行為。
1.部署點擊計數器應用
我們將一個簡單的應用程序部署到集群中,並在其前面放置一個負載平衡器。此應用程序的目的是在將計數器值作為HTTP響應返回之前,增加計數器並將其存儲在Redis集群中。
$ kubectl apply -f app-deployment-service.yaml
service/hit-counter-lb created
deployment.apps/hit-counter-app created
2.模擬節點故障
刪除節點測試是否其它節點會自動替換為master
當我們刪除redis-cluster-1最初是master,我們會看到Kubernetes晉升redis-cluster-4為master
$ kubectl delete pod redis-cluster-1
刪除前
刪除後
檢查狀態
$ kubectl describe pods redis-cluster-0 | grep IP $ kubectl describe pods redis-cluster-3 | grep IP $ kubectl exec -it redis-cluster-0 -- redis-cli role
帳密設定
設定密碼有兩種方式
1.redis指令設定
config get requirepass #檢視當前redis有沒有設定密碼
1) "requirepass"
2) ""
config get requirepass #檢視當前redis有沒有設定密碼
1) "requirepass"
2) ""
config set requirepass tonyit888 #設定密碼
config get requirepass #再次檢視當前redis就提示需要密碼
(error) NOAUTH Authentication required.
127.0.0.1:6379>
2.永久固定配置在conf裡面
redis-sts.yaml 在configmap的redes.conf加入以下
requirepass tonyit888 #指定密碼tonyit888
設定Redis認證密碼後,客戶端登入時需要使用-a引數輸入認證密碼,不新增該引數雖然也可以登入成功,但是沒有任何操作許可權。如下:
127.0.0.1:6379> config get requirepass
(error) NOAUTH Authentication required.
127.0.0.1:6379> keys *
(error) NOAUTH Authentication required.
帳密測試正常如下
$ kubectl exec -it redis-cluster-0 -- redis-cli -a tonyit888
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> keys *
1) "hits"
127.0.0.1:6379> keys *
1) "hits"
[…] 以下命令查看實時流量指標 linkerd -n emojivoto stat deploy 佈署redis gluster(細節請參考另一篇安裝) 將redis gluster的命名空間default 使用linkerd inject 加入到linkerd […]