Getting Started with kops on GCE

Make sure you have installed kops and installed kubectl, and installedthe gcloud tools.

You'll need a Google Cloud account, and make sure that gcloud is logged in to your account using gcloud init.

You should confirm that basic commands like gcloud compute zones list are working.

You'll also need to configure default credentials, using gcloud auth application-default login.

Creating a state store

kops needs a state store, to hold the configuration for your clusters. The simplest configurationfor Google Cloud is to store it in a Google Cloud Storage bucket in the same account, so that's how we'llstart.

So, just create an empty bucket - you can use any (available) name - e.g. gsutil mb gs://kubernetes-clusters/

Further, rather than typing the —state argument every time, it's much easier to export the KOPS_STATE_STOREenvironment variable:

  1. export KOPS_STATE_STORE=gs://kubernetes-clusters/

You can also put this in your ~/.bashrc or similar.

Creating our first cluster

kops create cluster creates the Cluster object and InstanceGroup object you'll be working with in kops:

  1. PROJECT=`gcloud config get-value project`
  2. export KOPS_FEATURE_FLAGS=AlphaAllowGCE # to unlock the GCE features
  3. kops create cluster simple.k8s.local --zones us-central1-a --state ${KOPS_STATE_STORE}/ --project=${PROJECT}

You can now list the Cluster objects in your kops state store (the GCS bucketwe created).

  1. > kops get cluster --state ${KOPS_STATE_STORE}
  2.  
  3. NAME CLOUD ZONES
  4. simple.k8s.local gce us-central1-a

This shows that you have one Cluster object configured, named simple.k8s.local. The cluster holds the cluster-wide configuration fora kubernetes cluster - things like the kubernetes version, and the authorization policy in use.

The kops tool should feel a lot like kubectl - kops uses the same API machinery as kubernetes,so it should behave similarly, although now you are managing kubernetes clusters, instead of managingobjects on a kubernetes cluster.

You can see the details of your Cluster object by doing:

  1. > kops get cluster --state ${KOPS_STATE_STORE}/ simple.k8s.local -oyaml
  2.  
  3. apiVersion: kops.k8s.io/v1alpha2
  4. kind: Cluster
  5. metadata:
  6. creationTimestamp: 2017-10-03T05:07:27Z
  7. name: simple.k8s.local
  8. spec:
  9. api:
  10. loadBalancer:
  11. type: Public
  12. authorization:
  13. alwaysAllow: {}
  14. channel: stable
  15. cloudProvider: gce
  16. configBase: gs://kubernetes-clusters/simple.k8s.local
  17. etcdClusters:
  18. - etcdMembers:
  19. - instanceGroup: master-us-central1-a
  20. name: a
  21. name: main
  22. - etcdMembers:
  23. - instanceGroup: master-us-central1-a
  24. name: a
  25. name: events
  26. iam:
  27. legacy: false
  28. kubernetesApiAccess:
  29. - 0.0.0.0/0
  30. kubernetesVersion: 1.7.2
  31. masterPublicName: api.simple.k8s.local
  32. networking:
  33. kubenet: {}
  34. nonMasqueradeCIDR: 100.64.0.0/10
  35. project: my-gce-project
  36. sshAccess:
  37. - 0.0.0.0/0
  38. subnets:
  39. - name: us-central1
  40. region: us-central1
  41. type: Public
  42. topology:
  43. dns:
  44. type: Public
  45. masters: public
  46. nodes: public

Similarly, you can also see your InstanceGroups using:

  1. > kops get instancegroup --state ${KOPS_STATE_STORE}/ --name simple.k8s.local
  2.  
  3. NAME ROLE MACHINETYPE MIN MAX SUBNETS
  4. master-us-central1-a Master n1-standard-1 1 1 us-central1
  5. nodes Node n1-standard-2 2 2 us-central1

InstanceGroups are the other main kops object - an InstanceGroup manages a set of cloud instances,which then are registered in kubernetes as Nodes. You have multiple InstanceGroups for different typesof instances / Nodes - in our simple example we have one for our master (which only has a single member),and one for our nodes (and we have two nodes configured).

We'll see a lot more of Cluster objects and InstanceGroups as we use kops to reconfigure clusters. But let's geton with our first cluster.

Creating a cluster

kops create cluster created the Cluster object & InstanceGroup object in our state store,but didn't actually create any instances or other cloud objects in GCE. To do that, we'll usekops update cluster.

kops update cluster without —yes will show you a preview of all the changes will be made;it is very useful to see what kops is about to do, before actually making the changes.

Run kops update cluster simple.k8s.local and peruse the changes.

We're now finally ready to create the object: kops update cluster simple.k8s.local —yes

(If you haven't created an SSH key, you'll have to ssh-keygen -t rsa) Your cluster is created in the background - kops actually creates GCE Managed Instance Groupsthat run the instances; this ensures that even if instances are terminated, they will automaticallybe relaunched by GCE and your cluster will self-heal.

After a few minutes, you should be able to do kubectl get nodes and your first cluster should be ready!

Enjoy

At this point you have a kubernetes cluster - the core commands to do so are as simple as kops create clusterand kops update cluster. There's a lot more power in kops, and even more power in kubernetes itself, so we'veput a few jumping off places here. But when you're done, don't forget to delete your cluster.

Deleting the cluster

When you're done using the cluster, you should delete it to release the cloud resources. kops delete cluster isthe command. When run without —yes it shows a preview of the objects it will delete:

  1. > kops delete cluster simple.k8s.local
  2. TYPE NAME ID
  3. Address api-simple-k8s-local api-simple-k8s-local
  4. Disk a-etcd-events-simple-k8s-local a-etcd-events-simple-k8s-local
  5. Disk a-etcd-main-simple-k8s-local a-etcd-main-simple-k8s-local
  6. ForwardingRule api-simple-k8s-local api-simple-k8s-local
  7. Instance master-us-central1-a-9847 us-central1-a/master-us-central1-a-9847
  8. Instance nodes-0s0w us-central1-a/nodes-0s0w
  9. Instance nodes-dvlq us-central1-a/nodes-dvlq
  10. InstanceGroupManager a-master-us-central1-a-simple-k8s-local us-central1-a/a-master-us-central1-a-simple-k8s-local
  11. InstanceGroupManager a-nodes-simple-k8s-local us-central1-a/a-nodes-simple-k8s-local
  12. InstanceTemplate master-us-central1-a-simple-k8s-local-1507008700 master-us-central1-a-simple-k8s-local-1507008700
  13. InstanceTemplate nodes-simple-k8s-local-1507008700 nodes-simple-k8s-local-1507008700
  14. Route simple-k8s-local-715bb0c7-a7fc-11e7-93d7-42010a800002 simple-k8s-local-715bb0c7-a7fc-11e7-93d7-42010a800002
  15. Route simple-k8s-local-9a2a08e8-a7fc-11e7-93d7-42010a800002 simple-k8s-local-9a2a08e8-a7fc-11e7-93d7-42010a800002
  16. Route simple-k8s-local-9c17a4e6-a7fc-11e7-93d7-42010a800002 simple-k8s-local-9c17a4e6-a7fc-11e7-93d7-42010a800002
  17. TargetPool api-simple-k8s-local api-simple-k8s-local
  18.  
  19. Must specify --yes to delete cluster

After you've double-checked you're deleting exactly what you want to delete, run kops delete cluster simple.k8s.local —yes.