建立自己的GKE Cluster

承上,Google Container Engine (GKE)是Google所推出的Container Host Cluster服務,也就是說可以讓執行Container(Docker)的環境可以Cluster起來。一方面允許擴展可以使用的資源,另一方面則可以讓執行時候可以有容錯的可能性(避免單點錯誤的狀況發生)。

安裝kubectl指令與相關設定

在gcloud工具已經安裝好的前提下,設定GKE的工具以及預設的ZONE只要透過下面步驟即可:

$ gcloud components update kubectl
$ gcloud config set compute/zone asia-east1-a

安裝gcloud與kubectl無誤後,可以透過clusters list可以確認目前專案中有哪些Cluster

$ gcloud container clusters list
NAME  ZONE  MASTER_VERSION  MASTER_IP  MACHINE_TYPE  NUM_NODES  STATUS
Listed 0 items.

以及kubectl get確認目前狀態

$ kubectl get cluster-info
the server doesn't have a resource type "cluster-info"

建立GKE Cluster

透過clusters create可已建立自己的GKE cluster...

$ gcloud container clusters create mygke
Creating cluster mygke...done.
Created [https://container.googleapis.com/v1/projects/my-gcp-project/zones/asia-east1-a/clusters/mygke].
kubeconfig entry generated for mygke.
NAME   ZONE          MASTER_VERSION  MASTER_IP        MACHINE_TYPE   NUM_NODES  STATUS
mygke  asia-east1-a  1.1.2           104.199.140.230  n1-standard-1  3          RUNNING

然後執行在環境中設定預設的cluster為所建立的名稱

$ gcloud config set container/cluster mygke

設定環境使用

$ gcloud container clusters get-credentials mygke \
  --zone asia-east1-a \
  --project your-project-id
Fetching cluster endpoint and auth data.
kubeconfig entry generated for mygke.

如果想要了解一些細部的資訊,可以開啟Debug mode來看看指令做了些什麼...

$ gcloud container clusters get-credentials mygke --verbosity debug
DEBUG: Running gcloud.container.clusters.get-credentials with Namespace(__calliope_internal_deepest_parser=ArgumentParser(prog='gcloud.container.clusters.get-credentials', usage=None, description='See https://cloud.google.com/container-engine/docs/kubectl for\nkubectl documentation.', version=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=False), account=None, api_version=None, authority_selector=None, authorization_token_file=None, cmd_func=<bound method Command.Run of <googlecloudsdk.calliope.backend.Command object at 0x1054c1590>>, command_path=['gcloud', 'container', 'clusters', 'get-credentials'], configuration=None, document=None, format=None, h=None, help=None, http_timeout=None, log_http=None, name='mygke', project=None, quiet=None, trace_email=None, trace_log=False, trace_token=None, user_output_enabled=None, verbosity='debug', version=None, zone=None).
Fetching cluster endpoint and auth data.
DEBUG: Saved kubeconfig to /Users/peihsinsu/.kube/config
kubeconfig entry generated for mygke.
INFO: Display format "default".

調整 GKE group 大小

檢查目前的Cluster Size

$ gcloud compute instance-groups list
NAME                     ZONE         NETWORK MANAGED INSTANCES
gke-mygke-b0b86220-group asia-east1-a default Yes     3

執行調整指令,調整instance group大小...

$ gcloud compute instance-groups managed resize gke-mygke-b0b86220-group --zone asia-east1-a --size 1
Updated [https://www.googleapis.com/compute/v1/projects/my-gcp-project/zones/asia-east1-a/instanceGroupManagers/gke-mygke-b0b86220-group].
---
baseInstanceName: gke-mygke-b0b86220-node
creationTimestamp: '2015-11-23T18:47:48.119-08:00'
currentActions:
  abandoning: 0
  creating: 0
  deleting: 2
  none: 1
  recreating: 0
  refreshing: 0
  restarting: 0
fingerprint: 42WmSpB8rSM=
id: '8414696411043361083'
instanceGroup: gke-mygke-b0b86220-group
instanceTemplate: gke-mygke-b0b86220-1-1-2
kind: compute#instanceGroupManager
name: gke-mygke-b0b86220-group
selfLink: https://www.googleapis.com/compute/v1/projects/my-gcp-project/zones/asia-east1-a/instanceGroupManagers/gke-mygke-b0b86220-group
targetSize: 1
zone: asia-east1-a

kubectl操作指令與K8S API

K8S本身具備完整的環境管理、操控API,也提供kubectl這個指令讓使用者可以透過Command Line的方式管理Cluster,下面稍微介紹使用API與kubectl的方式...

K8S所提供的APIS

我們可以透過kubectl的Cluster info可以知道K8S環境上的一些資訊,其中包含API與K8S Web Console的位置資訊。

$ kubectl cluster-info
Kubernetes master is running at https://104.155.231.49
GLBCDefaultBackend is running at https://104.155.231.49/api/v1/proxy/namespaces/kube-system/services/default-http-backend
Heapster is running at https://104.155.231.49/api/v1/proxy/namespaces/kube-system/services/heapster
KubeDNS is running at https://104.155.231.49/api/v1/proxy/namespaces/kube-system/services/kube-dns
KubeUI is running at https://104.155.231.49/api/v1/proxy/namespaces/kube-system/services/kube-ui

如果想要使用上述的API與Web Console,可以使用下面指令,取出環境的存取帳號密碼:

$ gcloud container clusters describe mygke
clusterIpv4Cidr: 10.172.0.0/14
createTime: '2015-12-20T16:01:52+00:00'
currentMasterVersion: 1.1.3
currentNodeCount: 1
currentNodeVersion: 1.1.3
endpoint: 104.155.231.49
initialClusterVersion: 1.1.3
initialNodeCount: 3
instanceGroupUrls:
- https://www.googleapis.com/replicapool/v1beta2/projects/my-project/zones/asia-east1-a/instanceGroupManagers/gke-mygke-76b971b7-group
loggingService: logging.googleapis.com
masterAuth:
  clientCertificate: LS0tLS1CRUdJTiBDR...5EIENFUlRJRklDQVRFLS0tLS0K
  clientKey: LS0tLS1CRUdJT...RCBSU0EgUFJJVkFURSBLRVktLS0tLQo=
  clusterCaCertificate: LS0tLS1C...tCi0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K
  password: yFE...hAC
  username: admin
monitoringService: monitoring.googleapis.com
name: mygke
network: default
nodeConfig:
  diskSizeGb: 100
  machineType: n1-standard-1
  oauthScopes:
  - https://www.googleapis.com/auth/compute
  - https://www.googleapis.com/auth/devstorage.read_only
  - https://www.googleapis.com/auth/logging.write
  - https://www.googleapis.com/auth/monitoring
nodeIpv4CidrSize: 24
selfLink: https://container.googleapis.com/v1/projects/my-project/zones/asia-east1-a/clusters/mygke
servicesIpv4Cidr: 10.175.240.0/20
status: RUNNING
zone: asia-east1-a

在GKE的環境中,我們也可以透過web console中的cluster detail頁面來找到連線API的資訊:

Request example:

$ curl -k -u admin:yFEvP...WhAC https://104.155.231.49/api/v1/nodes

Result:

{
  "kind": "NodeList",
  "apiVersion": "v1",
  "metadata": {
    "selfLink": "/api/v1/nodes",
    "resourceVersion": "53696"
  },
  "items": [
    {
      "metadata": {
        "name": "gke-mygke-76b971b7-node-r1ph",
        "selfLink": "/api/v1/nodes/gke-mygke-76b971b7-node-r1ph",
        "uid": "5b740b74...2010af000f9",
        "resourceVersion": "53695",
        "creationTimestamp": "2015-12-24T10:44:49Z",
        "labels": {
          "kubernetes.io/hostname": "gke-mygke-76b971b7-node-r1ph"
        }
      },
      ...(skip),
        "nodeInfo": {
          "machineID": "",
          "systemUUID": "3C9751...9514B0E",
          "bootID": "16d8452...1da281ec",
          "kernelVersion": "3.16.0-0.bpo.4-amd64",
          "osImage": "Debian GNU/Linux 7 (wheezy)",
          "containerRuntimeVersion": "docker://1.8.3",
          "kubeletVersion": "v1.1.3",
          "kubeProxyVersion": "v1.1.3"
        }
      }
    }
  ]
}

Reference

Last updated