Skip to content

graziergeek/k8s-statefulset

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Working with StatefulSets in K8s

This experimentation is a WIP...

Some things are GKE-specific.

How to Create a K8s Cluster

Prerequisites

Ensure the following dependencies are already fulfilled on your host Linux/Windows/Mac Workstation/Laptop:

  1. An account has been registered with the Google Compute Platform (GCP). You can sign up to a free trial for GCP. Note: The free trial places some restrictions on account resource quotas, in particular restricting storage to a maximum of 100GB.

  2. GCP’s client command line tool gcloud has been installed.

  3. Your local workstation has been initialised to:

    1. use your GCP account,
    2. install the Kubernetes command tool (“kubectl”),
    3. configure authentication credentials, and
    4. set the default GCP zone to be deployed to:
    $ gcloud init
    $ gcloud components install kubectl
    $ gcloud auth application-default login
    $ gcloud config set compute/zone europe-west1-b
    

Note: To specify an alternative zone to deploy to, in the above command, you can first view the list of available zones by running the command: $ gcloud compute zones list

Create Cluster

  1. Make sure Kubernetes API is enabled in Google cloud console
  2. To create a 3-node, auto-scaling (9 max) cluster, run:
    gcloud container clusters create css-sandbox \
      --zone=us-central1-a \
      --enable-autorepair \
      --enable-autoupgrade \
      --machine-type=n1-standard-2 \
      --enable-autoscaling --min-nodes=3 --max-nodes=9
    

Deploy Applications

  1. Deploy MongoDB, Elasticsearch, Redis HA, ...
    1. See Helm manifests
  2. Deploy App (this is for test app but steps are pretty much the same for any)
    1. In the grails-crud test app...
    2. ./gradlew build
    3. ./gradlew prepareDocker
    4. docker build -t grails-crud ./build/docker
    5. docker tag grails-crud gcr.io/<GCP-projectID>/grails-crud:1.4 (change the image name:version in k8s/grails-crud.yaml to match)
    6. docker push gcr.io/<GCP-projectID>/grails-crud:1.4 (push image to GCR)
    7. kubectl apply -f k8s/grails-crud.yaml (deploy. pulls image from GCR.)
    8. kubectl get pods (should see something like):
    NAME                           READY     STATUS        RESTARTS   AGE
    grails-crud-666b6794c5-mgl8m   1/1       Running       0          6s
    mongo-0                        1/1       Running       0          1h
    mongo-1                        1/1       Running       0          1h
    mongo-2                        1/1       Running       0          1h
    
    1. See grails-crud for K8s testing.

Creating a GCP Account Token (Optional)

This step is not needed for this example.

Whenever downloading a private key, put it in a safe place. By all means do NOT add it to source control.

To create and download a gcloud private key file if you DON'T already have a service account:

  1. Go to the Google Cloud Platform / IAM & admin / Service Accounts page.

  2. Choose + Create Service Account

  3. In the resulting dialog, give it a name (for example, <project-name>-admin-service-account)

  4. Leave the Service account ID

  5. Select the required roles for creating and modifying a Kubernetes cluster:

    1. Compute Engine / Compute Admin
    2. Kubernetes Engine / Kubernetes Engine Admin
    3. Storage / Storage Admin
    4. Service Accounts / Service Account User
  6. Check the box that says Furnish a new private key

  7. Select Create

  8. The key is downloaded to your computer.

  9. Store the key file in a safe and accessible place. Do not check it in to source control.

To create and download a gcloud private key file if you DO already have a service account:

  1. Go to the Google Cloud Platform / IAM & admin / Service Accounts page.

  2. Choose your service account by selecting the ellipses (three dots) on its right.

  3. Select Create Key

  4. Leave Key type as JSON

  5. Select Create

  6. The key is downloaded to your computer.

  7. Store the key file in a safe and accessible place. Do not check it in to source control.

    1. e.g., ${HOME}/.k8sconfig/<name>.json.