For a full tutorial on using Ansible to provision a Kubernetes cluster on Google Cloud, check out Dzero Labs' blog post on Medium.
Be sure to also check out our post on Shifting from Infrastructure as Code to Infrastructure as Data.
If you'd like to skip the tutorial altogether, you can use the quickstart guide below.
For your convenience, I've created a Dockerfile
with Ansible
and the gcloud
CLI installed.
- A Google Cloud project
- A Google Cloud service account
- Docker installed on your local machine
You must replace the following values in setup.sh
:
<gcp_project_name>
: Your own GCP project name<service_account_name>
: Name of your GCP service account<service_account_private_key_json>
: Fully-qualified name of your Google Service Account's private key JSON file (e.g./home/myuser/my-sa.json
or./my-sa.json
)
Next, run the following script:
./setup.sh
This will replace the values you set above with your own GCP project's values in playbook.yml
and startup.sh
.
docker build -t docker-ansible:1.0.0 docker
Run the container instance. Here, we're mapping the ansible
folder on our host machine to the /workdir/ansible
folder in the container instance.
docker run -it --rm \
-v $(pwd)/ansible:/workdir/ansible \
-v $(pwd)/manifest:/workdir/manifest \
docker-ansible:1.0.0 /bin/bash
This playbook will create a GKE cluster and node pool, and update the container instance's kubeconfig
so that you can connect to the cluster.
From within the container instance run the following command:
./startup.sh && ansible-playbook -vv --extra-vars cluster_state=present ansible/playbook.yml
To delete the cluster and node pool, run the following command from within the container instance:
./startup.sh && ansible-playbook -vv --extra-vars cluster_state=absent ansible/playbook.yml
This will create a:
- Namespace
- Deployment
- Service
kubectl apply -f manifest/deployment.yml
Get the LoadBalancer IP:
LOAD_BALANCER_IP=$(kubectl get -n foo service service -o "go-template={{range .status.loadBalancer.ingress}}{{or .ip .hostname}}{{end}}")
Go to your browser and enter the following IP:
http://$LOAD_BALANCER_IP