Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加命名空间调整,方便增加多套集群 #9

Open
wants to merge 3 commits into
base: 2.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ FROM alpine:3.6
# author caiqyxyx
# date 2017 12 17

RUN apk add --no-cache 'su-exec>=0.2' sed bash
RUN echo http://mirrors.aliyun.com/alpine/v3.10/main/ > /etc/apk/repositories && \
echo http://mirrors.aliyun.com/alpine/v3.10/community/ >> /etc/apk/repositories && \
apk update && apk upgrade && \
apk add --no-cache 'su-exec>=0.2' sed bash

ENV REDIS_VERSION 5.0.3
ENV REDIS_DOWNLOAD_URL http://download.redis.io/releases/redis-${REDIS_VERSION}.tar.gz
Expand Down
3 changes: 2 additions & 1 deletion redis.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
"redis_data_size": 2,
"log_level": 0,
"redis_cluster_replicas": 0,
"hostnetwork": false
"hostnetwork": false,
"k8s_namespace": "default"
}
25 changes: 15 additions & 10 deletions redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,18 +186,21 @@ def uninstall_redis():
:return: boolean ,the uninstallation result
"""
config = json_load("redis.json")
# result = os.system(
# "/root/local/bin/kubectl get statefulset -l app=redis-cluster | grep -v NAME | awk '{print $1}' | xargs /root/local/bin/kubectl delete statefulset ;"
# "/root/local/bin/kubectl get deployment -l app=redis-cluster | grep -v NAME | awk '{print $1}' | xargs /root/local/bin/kubectl delete deployment ;"
# "/root/local/bin/kubectl get service -l app=redis-cluster | grep -v NAME | awk '{print $1}' | xargs /root/local/bin/kubectl delete service ;"
# "/root/local/bin/kubectl get serviceaccount -l app=redis-cluster | grep -v NAME | awk '{print $1}' | xargs /root/local/bin/kubectl delete serviceaccount ;"
# "/root/local/bin/kubectl get clusterrole -l app=redis-cluster | grep -v NAME | awk '{print $1}' | xargs /root/local/bin/kubectl delete clusterrole ;"
# "/root/local/bin/kubectl get clusterrolebinding -l app=redis-cluster | grep -v NAME | awk '{print $1}' | xargs /root/local/bin/kubectl delete clusterrolebinding ;"
# )
result = os.system(
"/root/local/bin/kubectl get statefulset -l app=redis | grep -v NAME | awk '{print $1}' | xargs /root/local/bin/kubectl delete statefulset ;"
"/root/local/bin/kubectl get deployment -l app=redis | grep -v NAME | awk '{print $1}' | xargs /root/local/bin/kubectl delete deployment ;"
"/root/local/bin/kubectl get service -l app=redis | grep -v NAME | awk '{print $1}' | xargs /root/local/bin/kubectl delete service ;"
"/root/local/bin/kubectl get serviceaccount -l app=redis | grep -v NAME | awk '{print $1}' | xargs /root/local/bin/kubectl delete serviceaccount ;"
"/root/local/bin/kubectl get clusterrole -l app=redis | grep -v NAME | awk '{print $1}' | xargs /root/local/bin/kubectl delete clusterrole ;"
"/root/local/bin/kubectl get clusterrolebinding -l app=redis | grep -v NAME | awk '{print $1}' | xargs /root/local/bin/kubectl delete clusterrolebinding ;"
"/root/local/bin/kubectl delete -f #path#/yaml/"
.replace("#path#", current_path)
)

if config["persistent_flag"]:
result = os.system(
"/root/local/bin/kubectl get pvc -l app=redis | grep -v NAME | awk '{print $1}' | xargs /root/local/bin/kubectl delete pvc")
"/root/local/bin/kubectl get pvc -l app=redis-cluster | grep -v NAME | awk '{print $1}' | xargs /root/local/bin/kubectl delete pvc")
if result == 0:
while True:
if exists_resource("endpoints", "redisdata"):
Expand All @@ -218,14 +221,15 @@ def scale_redis(new_replicas):
try:
# turn into integer
new_replicas = int(new_replicas)
config = json_load("redis.json")

# check out redis
if not check_redis(return_code=False):
print("making sure that your redis cluster is healthy.")
return False

# check kubernetes healthy
nodes = exists_resource("node", "Ready", bool_result=False)
nodes = exists_resource("node", "Ready", bool_result=False ,namespace=config["k8s_namespace"] )
if nodes == 0:
print("Could not find kubernetes nodes.")
return False
Expand Down Expand Up @@ -270,7 +274,8 @@ def check_redis(return_code=False):
+------+---------------------------------------+
"""
try:
run = subprocess.Popen("/root/local/bin/kubectl exec -it $(/root/local/bin/kubectl get po | grep redis-ctrl-center | awk '{print $1}') /bin/sh /redis-plus.sh health 2>/dev/null",
config = json_load("redis.json")
run = subprocess.Popen("/root/local/bin/kubectl exec -n %s -it $(/root/local/bin/kubectl -n %s get po | grep redis-ctrl-center | awk '{print $1}') /bin/sh /redis-plus.sh health 2>/dev/null"%(config["k8s_namespace"] , config["k8s_namespace"]),
shell=True,
stdout=subprocess.PIPE)
rr = run.stdout.read()
Expand Down
28 changes: 18 additions & 10 deletions template/redis-cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ apiVersion: v1
kind: ServiceAccount
metadata:
name: redis-sa
namespace: {{ k8s_namespace }}
labels:
app: redis
app: redis-cluster
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: redis-cr
namespace: {{ k8s_namespace }}
labels:
app: redis
app: redis-cluster
rules:
- apiGroups:
- '*'
Expand All @@ -27,13 +29,13 @@ apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: read-crbind
namespace: default
namespace: {{ k8s_namespace }}
labels:
app: redis
app: redis-cluster
subjects:
- kind: ServiceAccount
name: redis-sa
namespace: default
namespace: {{ k8s_namespace }}
roleRef:
kind: ClusterRole
name: redis-cr
Expand All @@ -43,13 +45,14 @@ apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: redis-ctrl-center
namespace: {{ k8s_namespace }}
spec:
replicas: 1
template:
metadata:
labels:
name: redis-ctrl-center
app: redis
app: redis-cluster
spec:
terminationGracePeriodSeconds: 10
serviceAccountName: redis-sa
Expand Down Expand Up @@ -83,14 +86,15 @@ apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: redis-cluster-node
namespace: {{ k8s_namespace }}
spec:
serviceName: "redis-cluster-svc"
replicas: {{ redis_statefulset_replicas }}
template:
metadata:
labels:
name: redis-cluster-node
app: redis
app: redis-cluster
spec:
hostNetwork: {{ hostnetwork }}
dnsPolicy: ClusterFirstWithHostNet
Expand Down Expand Up @@ -150,29 +154,33 @@ apiVersion: v1
kind: Service
metadata:
name: redis-cluster-svc
namespace: {{ k8s_namespace }}
labels:
name: redis-cluster-svc
app: redis
app: redis-cluster
spec:
ports:
- port: 6379
targetPort: {{ redis_server_port }}
clusterIP: None
selector:
name: redis-cluster-node
{% if hostnetwork %}
---
apiVersion: v1
kind: Service
metadata:
name: redis-cluster-svc-np
namespace: {{ k8s_namespace }}
labels:
name: redis-cluster-svc-np
app: redis
app: redis-cluster
spec:
ports:
- port: 6379
targetPort: {{ redis_server_port }}
nodePort: {{ redis_server_nodeport }}
type: NodePort
selector:
name: redis-cluster-node
name: redis-cluster-node
{% endif %}