Verified Commit a4a04a87 authored by Xueshan Feng's avatar Xueshan Feng
Browse files

added node cordon, uncordon, recycle.

parent 28e87305
Loading
Loading
Loading
Loading
+79 −0
Original line number Diff line number Diff line


################################################################################
## kube-cluster.mk
# Makefile targets useful for kube cluster ops
################################################################################

# ifndef GCP_ENVIRONMENT
# 	missing_vars := ${missing_vars} GCP_ENVIRONMENT
# endif

ifndef CLUSTER_NAME
	missing_vars := ${missing_vars} CLUSTER_NAME
endif

# ifndef GKE_CLUSTER_ZONE
# 	missing_vars := ${missing_vars} GKE_CLUSTER_ZONE
# endif

ifndef TILLER_IMAGE
	TILLER_IMAGE := gcr.io/kubernetes-helm/tiller:v2.7.2
endif

.PHONY: cordon-node
cordon-node: NODES = $(shell kubectl get node -o json  | jq -r '.items[].metadata.name')
cordon-node: ## Disable scheduling on a node
	@kubectl get nodes 
	@for i in ${NODES}; do \
		echo "" ; \
		echo "Cordon $$i?" ; \
		$(MAKE) confirm > /dev/null 2>&1 || continue ; \
		kubectl cordon $$i ; \
		kubectl drain $$i --ignore-daemonsets --delete-local-data ; \
	done

.PHONY: uncordon-node
uncordon-node: NODES = $(shell kubectl get node -o json  | jq -r '.items[].metadata.name')
uncordon-node: ## Enable scheduling on a node
	@kubectl get nodes 
	@for i in ${NODES}; do \
		echo "" ; \
		echo "Uncordon $$i?" ; \
		$(MAKE) confirm > /dev/null 2>&1 || continue ; \
		kubectl uncordon $$i ; \
	done

.PHONY: cluster-upgrade-nodes
recycle-node: NODES = $(shell kubectl get node -o json  | jq -r '.items[].metadata.name')
recycle-node: ## Replace node with a new instance
	@kubectl get nodes
	@for i in ${NODES}; do \
		echo "" ; \
		echo "Recycle $$i?" ; \
		$(MAKE) confirm > /dev/null 2>&1 || continue ; \
		external_id=`kubectl describe node $$i |grep ExternalID | awk '{print $$2}'` ; \
		aws autoscaling terminate-instance-in-auto-scaling-group  \
		  --instance-id $$external_id --no-should-decrement-desired-capacity ; \
	done

.PHONY: get-images
get-images: config-kube ## List all images running in the cluster
    @kubectl get pods --all-namespaces -o jsonpath="{..image}" | tr -s '[[:space:]]' '\n' | sort | uniq -c

.PHONY: tiller
tiller: KUBECONFIG = $$HOME/.kube/${CLUSTER_NAME}
tiller: ## Instal Helm tiller controller
	helm init --upgrade && helm version

.PHONY: delete-tiller
delete-tiller: ## Uninstall tiller
	kubectl delete deployment tiller-deploy -n kube-system --ignore-not-found

.PHONY: tag-nodes
tag-nodes: config-kube ## tag the cluster nodes for NAT
	tag-nodes-for-nat.sh ${GCP_NAT_TAGS}

.PHONY: check-images ## check all running images
check-images: config-kube
	kube-images-report.sh