From c180347b87d4445982929c37dbdac770408192ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=94=A1=E4=BC=9F=E6=88=90?= Date: Tue, 22 Jun 2021 11:19:08 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=91=BD=E5=90=8D?= =?UTF-8?q?=E7=A9=BA=E9=97=B4=E8=B0=83=E6=95=B4=EF=BC=8C=E6=96=B9=E4=BE=BF?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=A4=9A=E5=A5=97=E9=9B=86=E7=BE=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- redis.json | 3 ++- redis.py | 25 +++++++++++++++---------- template/redis-cluster.yaml | 24 +++++++++++++++--------- 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/redis.json b/redis.json index 361122c..d12c3fc 100644 --- a/redis.json +++ b/redis.json @@ -8,5 +8,6 @@ "redis_data_size": 2, "log_level": 0, "redis_cluster_replicas": 0, - "hostnetwork": false + "hostnetwork": false, + "k8s_namespace": "default" } \ No newline at end of file diff --git a/redis.py b/redis.py index 38b43a2..dce2e83 100644 --- a/redis.py +++ b/redis.py @@ -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"): @@ -218,6 +221,7 @@ 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): @@ -225,7 +229,7 @@ def scale_redis(new_replicas): 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 @@ -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() diff --git a/template/redis-cluster.yaml b/template/redis-cluster.yaml index 5db08a5..8fcbcc5 100644 --- a/template/redis-cluster.yaml +++ b/template/redis-cluster.yaml @@ -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: - '*' @@ -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 @@ -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 @@ -83,6 +86,7 @@ apiVersion: apps/v1beta1 kind: StatefulSet metadata: name: redis-cluster-node + namespace: {{ k8s_namespace }} spec: serviceName: "redis-cluster-svc" replicas: {{ redis_statefulset_replicas }} @@ -90,7 +94,7 @@ spec: metadata: labels: name: redis-cluster-node - app: redis + app: redis-cluster spec: hostNetwork: {{ hostnetwork }} dnsPolicy: ClusterFirstWithHostNet @@ -150,9 +154,10 @@ 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 @@ -165,9 +170,10 @@ 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 From 13c437211739601d07e7aec50f109c42f9bcbe64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=94=A1=E4=BC=9F=E6=88=90?= Date: Tue, 22 Jun 2021 11:26:30 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=8A=A0=E9=80=9Fapk=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E9=80=9F=E5=BA=A6=EF=BC=8C=E4=BD=BF=E7=94=A8=E9=98=BF=E9=87=8C?= =?UTF-8?q?=E4=BA=91=E4=BD=9C=E4=B8=BA=E6=8F=92=E4=BB=B6=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker/Dockerfile | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index 920b782..6a48792 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -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 From bb214a1218eac2f5ace34d21e597afd1f08f1639 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=94=A1=E4=BC=9F=E6=88=90?= Date: Tue, 22 Jun 2021 13:58:25 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0nodeport=E7=9A=84?= =?UTF-8?q?=E7=94=9F=E6=88=90=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- template/redis-cluster.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/template/redis-cluster.yaml b/template/redis-cluster.yaml index 8fcbcc5..719ad7a 100644 --- a/template/redis-cluster.yaml +++ b/template/redis-cluster.yaml @@ -165,6 +165,7 @@ spec: clusterIP: None selector: name: redis-cluster-node +{% if hostnetwork %} --- apiVersion: v1 kind: Service @@ -181,4 +182,5 @@ spec: nodePort: {{ redis_server_nodeport }} type: NodePort selector: - name: redis-cluster-node \ No newline at end of file + name: redis-cluster-node +{% endif %} \ No newline at end of file