From cee89cecb735f2334f77efc020770caf0cdaf08c Mon Sep 17 00:00:00 2001 From: XiaoLiang Hu <24374357@qq.com> Date: Wed, 31 Jan 2018 14:19:20 +0800 Subject: [PATCH] Improve k8s DNS example usability (#164) the command in example should be straightforward asap, user just do copy and paste to move on. This change is align with spark example to set CLUSTER_NAME and USER_NAME, refer to below link for the details: https://github.com/kubernetes/examples/tree/master/staging/spark --- staging/cluster-dns/README.md | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/staging/cluster-dns/README.md b/staging/cluster-dns/README.md index e1bcc08e..6b871e94 100644 --- a/staging/cluster-dns/README.md +++ b/staging/cluster-dns/README.md @@ -30,15 +30,16 @@ development name=development Active production name=production Active ``` -For kubectl client to work with each namespace, we define two contexts: +For kubectl client to work with each namespace, we define two contexts using our current context as a base: ```sh +$ CURRENT_CONTEXT=$(kubectl config view -o jsonpath='{.current-context}') +$ USER_NAME=$(kubectl config view -o jsonpath='{.contexts[?(@.name == "'"${CURRENT_CONTEXT}"'")].context.user}') +$ CLUSTER_NAME=$(kubectl config view -o jsonpath='{.contexts[?(@.name == "'"${CURRENT_CONTEXT}"'")].context.cluster}') $ kubectl config set-context dev --namespace=development --cluster=${CLUSTER_NAME} --user=${USER_NAME} $ kubectl config set-context prod --namespace=production --cluster=${CLUSTER_NAME} --user=${USER_NAME} ``` -You can view your cluster name and user name in Kubernetes config at ~/.kube/config. - ### Step Two: Create backend replication controller in each namespace Use the file [`examples/cluster-dns/dns-backend-rc.yaml`](dns-backend-rc.yaml) to create a backend server [replication controller](https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller/) in each namespace. @@ -143,7 +144,7 @@ If you prefer not using namespace, then all your services can be addressed using ### tl; dr; -For those of you who are impatient, here is the summary of the commands we ran in this tutorial. Remember to set first `$CLUSTER_NAME` and `$USER_NAME` to the values found in `~/.kube/config`. +For those of you who are impatient, here is the summary of the commands we ran in this tutorial. Remember the values of `$CLUSTER_NAME` and `$USER_NAME` are based on current context found in `~/.kube/config`. ```sh # create dev and prod namespaces @@ -151,6 +152,9 @@ kubectl create -f examples/cluster-dns/namespace-dev.yaml kubectl create -f examples/cluster-dns/namespace-prod.yaml # create two contexts +CURRENT_CONTEXT=$(kubectl config view -o jsonpath='{.current-context}') +USER_NAME=$(kubectl config view -o jsonpath='{.contexts[?(@.name == "'"${CURRENT_CONTEXT}"'")].context.user}') +CLUSTER_NAME=$(kubectl config view -o jsonpath='{.contexts[?(@.name == "'"${CURRENT_CONTEXT}"'")].context.cluster}') kubectl config set-context dev --namespace=development --cluster=${CLUSTER_NAME} --user=${USER_NAME} kubectl config set-context prod --namespace=production --cluster=${CLUSTER_NAME} --user=${USER_NAME}