Zone Printer is a web application that displays the Google Cloud Platform (GCP) compute zone where the application is running.
Zone Printer application is a web server written in Go. It greets the user with the country name, flag and the zone of the server it is deployed in.
It queries the GCE metadata server to find out the zone name it is running on. Since GKE nodes are GCE instances, this application will print the name of the zone it is running on.
The Deployment
consists of a container image that is listening on port 80
.
The Service exposes the Deployment
pods on nodePort: 30061
on all the nodes
in the Kubernetes cluster.
The example also includes an Ingress
manifest (ingress/ingress.yaml
) that is
used by kubemci
to provision the multi-cluster Ingress.
-
GCP Project
Ensure that you have a GCP Project. If you don't have one already, you can create a project by following the instructions at Creating and Managing Projects.
After your project is created, run the following commands in your terminal:
PROJECT=<your-project-name>
-
Set up
gcloud
:You can install
gcloud
by following the instructions at Installing Cloud SDK and initialize it by running the following commands:gcloud init # Set $PROJECT as the default project gcloud auth login gcloud auth application-default login
-
Set up
kubectl
:You can install
kubectl
by running:gcloud components install kubectl
-
Install
kubemci
:kubemci is the primary command-line tool used to create and configure a multi-cluster ingress.
Download one of the binaries and place it in an executable path:
For example on linux you can install
kubemci
by running:wget https://storage.googleapis.com/kubemci-release/release/latest/bin/linux/amd64/kubemci
Ensure that the binary is executable. This can be done in the directory of the binary with the following command:
chmod +x ./kubemci
In google cloud shell you can move this to your local bin to automatically include it in your path
mv ./kubemci ~/bin/kubemci
kubemci
requires Kubernetes clusters that are v1.8.1 or newer. (You can check
your cluster version using the kubectl version
command.)
You will need at least two Kubernetes clusters in two different compute zones
to verify that kubemci
works.
The following commands will create two clusters:
cluster-1
inus-east4-a
cluster-2
ineurope-west1-c
and save their connection details to a file named clusters.yaml
.
# Create a cluster in us-east and get its credentials
KUBECONFIG=clusters.yaml gcloud container clusters create \
--cluster-version=1.9 \
--zone=us-east4-a \
cluster-1
# Create a cluster in eu-west and get its credentials
KUBECONFIG=clusters.yaml gcloud container clusters create \
--cluster-version=1.9 \
--zone=europe-west1-c \
cluster-2
Note: If you don't already have Kubernetes clusters on GCP, skip to the next subsection on creating new clusters.
If you already have existing Kubernetes Engine clusters, run get-credentials
with them to create a clusters.yaml
file with the connection information of
these clusters.
For example:
KUBECONFIG=clusters.yaml gcloud container clusters \
get-credentials cluster-1 --zone=us-east4-a
KUBECONFIG=clusters.yaml gcloud container clusters \
get-credentials cluster-2 --zone=europe-west1-c
# ...repeat for other clusters you would like to add to the Ingress
It is possible to use Kubernetes clusters hosted on Google Compute Engine (GCE)
with kubemci
. You need to create a kubeconfig file that contains the
connection information of the clusters you want to add to the multi-cluster Ingress.
First, make sure you have the kubeconfig entries of the clusters:
$ kubectl config get-contexts -o name
name-of-cluster-1
name-of-cluster-2
[...]
For each cluster you will use, export its configuration into a YAML file:
# do this for the cluster-1 in us-east:
kubectl config use-context [name-of-cluster-1]
kubectl config view --minify --flatten > cluster-1.yaml
# do this for the cluster-2 in eu-west:
kubectl config use-context [name-of-cluster-2]
kubectl config view --minify --flatten > cluster-2.yaml
Combine these cluster-1.yaml
and cluster-2.yaml
config files into one called clusters.yaml
:
KUBECONFIG=cluster-1.yaml:cluster-2.yaml kubectl config view \
--flatten > clusters.yaml
Deploy the application along with its NodePort Service in each of the two
clusters. You can get the cluster contexts from kubectl config get-contexts
and iterate through all the clusters to deploy the application
manifests. This could be accomplished by running the following loop:
for ctx in $(kubectl config get-contexts -o=name --kubeconfig clusters.yaml); do
kubectl --kubeconfig clusters.yaml --context="${ctx}" create -f manifests/
done
This command reserves a static IP address for the load balancer.
ZP_KUBEMCI_IP="zp-kubemci-ip"
gcloud compute addresses create --global "${ZP_KUBEMCI_IP}"
Replace the value of kubernetes.io/ingress.global-static-ip-name
annotation in ingress/ingress.yaml
with the value of $ZP_KUBEMCI_IP
.
sed -i -e "s/\$ZP_KUBEMCI_IP/${ZP_KUBEMCI_IP}/" ingress/ingress.yaml
Run kubemci
to deploy the ingress/ingress.yaml and
create the multi-cluster Ingress named zone-printer
:
kubemci create zone-printer \
--ingress=ingress/ingress.yaml \
--gcp-project=$PROJECT \
--kubeconfig=clusters.yaml
This command creates the multi-cluster Ingress.
You can view the status of the multi-cluster Ingress you just created using the
kubemci get-status
command:
$ kubemci get-status zone-printer --gcp-project=$PROJECT
Load balancer zone-printer has IPAddress 35.190.74.228 and
is spread across 2 clusters (gke_yourproject_us-east4-a_cluster-1,
gke_yourproject_europe-west1-c_cluster-2).
It will take several minutes from when the Ingress and Google Cloud HTTP Load Balancer are created until they are ready. (You may see HTTP 404 or HTTP 502 errors while the load balancer is getting ready.)
Once the load balancer is ready to serve traffic, use the IP address printed in the previous step and query the server:
$ curl http://35.190.74.228
Welcome to the global site! You are being served from us-central1-b.
Congratulations
Multi-cluster Ingress has routed your traffic to a cluster in the nearest GCP region.
To delete the multi-cluster Ingress you created, run:
kubemci delete zone-printer \
--ingress=ingress/ingress.yaml \
--gcp-project=$PROJECT \
--kubeconfig=clusters.yaml
This will also clean up the underlying networking resources provisioned by
kubemci
.
If you created new Kubernetes Engine clusters for this tutorial, clean them up:
gcloud container clusters delete cluster-1 --zone=us-east4-a -q
gcloud container clusters delete cluster-2 --zone=europe-west1-c -q