通过gcloud创建Google Kubernetes Engine(GKE)并通过kubectl访问

1 简介

GKE(Google Kubernetes Engine)是一个K8s平台, 我们可以使用gcloud来创建GKE集群。在开始之前,可以查看:《初始化一个GCP项目并用gcloud访问操作》。

2 创建GKE集群

2.1 打开API

在创建集群之前,需要打开Google API,不然无法操作:

$ gcloud services enable compute.googleapis.com
$ gcloud services enable container.googleapis.com

2.2 创建

K8s集群的节点机器类型多种多样,可以查看一下某个区都有哪些机器类型:

$ gcloud compute machine-types list | grep us-west

这里我选择一个便宜的:n1-standard-1

使用 gcloud container clusters create 来创建:

$ gcloud container clusters create pkslow-k8s \
--zone us-west1-a \
--cluster-version 1.20.10-gke.1600 \
--machine-type n1-standard-1

WARNING: Currently VPC-native is not the default mode during cluster creation. In the future, this will become the default mode and can be disabled using `--no-enable-ip-alias` flag. Use `--[no-]enable-ip-alias` flag to suppress this warning.
WARNING: Starting with version 1.18, clusters will have shielded GKE nodes by default.
WARNING: Your Pod address range (`--cluster-ipv4-cidr`) can accommodate at most 1008 node(s). 
WARNING: Starting with version 1.19, newly created clusters and node-pools will have COS_CONTAINERD as the default node image when no image type is specified.
Creating cluster pkslow-k8s in us-west1-a...done.                                                                                                                
Created [https://container.googleapis.com/v1/projects/pkslow/zones/us-west1-a/clusters/pkslow-k8s].
To inspect the contents of your cluster, go to: https://console.cloud.google.com/kubernetes/workload_/gcloud/us-west1-a/pkslow-k8s?project=pkslow
kubeconfig entry generated for pkslow-k8s.
NAME        LOCATION    MASTER_VERSION    MASTER_IP    MACHINE_TYPE   NODE_VERSION      NUM_NODES  STATUS
pkslow-k8s  us-west1-a  1.20.10-gke.1600  34.82.42.67  n1-standard-1  1.20.10-gke.1600  3          RUNNING

看日志,提示创建成功。

2.3 通过命令行访问

创建成功后,查看对应的版本号和节点:

$ kubectl version
Client Version: version.Info{Major:"1", Minor:"19", GitVersion:"v1.19.3", GitCommit:"1e11e4a2108024935ecfcb2912226cedeafd99df", GitTreeState:"clean", BuildDate:"2020-10-14T12:50:19Z", GoVersion:"go1.15.2", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"20+", GitVersion:"v1.20.10-gke.1600", GitCommit:"ef8e9f64449d73f9824ff5838cea80e21ec6c127", GitTreeState:"clean", BuildDate:"2021-09-06T09:24:20Z", GoVersion:"go1.15.15b5", Compiler:"gc", Platform:"linux/amd64"}

$ kubectl get node
NAME                                        STATUS   ROLES    AGE   VERSION
gke-pkslow-k8s-default-pool-4743a88e-175c   Ready       59s   v1.20.10-gke.1600
gke-pkslow-k8s-default-pool-4743a88e-71l6   Ready       58s   v1.20.10-gke.1600
gke-pkslow-k8s-default-pool-4743a88e-9wn2   Ready       59s   v1.20.10-gke.1600

SDK已经自动帮我们创建了k8s上下文,也下载了cert,所以可以直接访问:

$ kubectl config get-contexts
CURRENT   NAME                               CLUSTER                            AUTHINFO                           NAMESPACE
          docker-desktop                     docker-desktop                     docker-desktop                     
*         gke_pkslow_us-west1-a_pkslow-k8s   gke_pkslow_us-west1-a_pkslow-k8s   gke_pkslow_us-west1-a_pkslow-k8s 

如果没有对应的权限,可以重新获取:

$ gcloud container clusters get-credentials pkslow-k8s --zone=us-west1-a

查看所有的 Pods:

$ kubectl get pod -A
NAMESPACE     NAME                                                   READY   STATUS    RESTARTS   AGE
kube-system   event-exporter-gke-67986489c8-2lvtj                    2/2     Running   0          4m14s
kube-system   fluentbit-gke-6xdgs                                    2/2     Running   0          3m59s
kube-system   fluentbit-gke-92swm                                    2/2     Running   0          3m58s
kube-system   fluentbit-gke-xrj9q                                    2/2     Running   0          3m59s
kube-system   gke-metrics-agent-9z2m2                                1/1     Running   0          3m59s
kube-system   gke-metrics-agent-f5kj2                                1/1     Running   0          3m59s
kube-system   gke-metrics-agent-jwsnk                                1/1     Running   0          3m58s
kube-system   konnectivity-agent-686dbcf84c-852jd                    1/1     Running   0          3m28s
kube-system   konnectivity-agent-686dbcf84c-sx8rk                    1/1     Running   0          4m9s
kube-system   konnectivity-agent-686dbcf84c-vz68j                    1/1     Running   0          3m29s
kube-system   konnectivity-agent-autoscaler-6cb774c9cc-j6s2t         1/1     Running   0          4m9s
kube-system   kube-dns-autoscaler-844c9d9448-t545t                   1/1     Running   0          4m9s
kube-system   kube-dns-b4f5c58c7-psxrv                               4/4     Running   0          4m10s
kube-system   kube-dns-b4f5c58c7-v7phb                               4/4     Running   0          3m34s
kube-system   kube-proxy-gke-pkslow-k8s-default-pool-4743a88e-175c   1/1     Running   0          2m34s
kube-system   kube-proxy-gke-pkslow-k8s-default-pool-4743a88e-71l6   1/1     Running   0          3m53s
kube-system   kube-proxy-gke-pkslow-k8s-default-pool-4743a88e-9wn2   1/1     Running   0          2m56s
kube-system   l7-default-backend-56cb9644f6-5qzsr                    1/1     Running   0          4m15s
kube-system   metrics-server-v0.3.6-9c5bbf784-xrksq                  2/2     Running   0          3m19s
kube-system   pdcsi-node-7d7gp                                       2/2     Running   0          3m58s
kube-system   pdcsi-node-cx4dp                                       2/2     Running   0          3m59s
kube-system   pdcsi-node-zz2hv                                       2/2     Running   0          3m59s

2.4 通过Console访问

我们可以在GCP console查看,更方便:

通过gcloud创建Google Kubernetes Engine(GKE)并通过kubectl访问_第1张图片

像Deployment, DaemonSet这些一样可以查看:

通过gcloud创建Google Kubernetes Engine(GKE)并通过kubectl访问_第2张图片

GCP提供了Metrics,可以查看CPU、内存、硬盘等信息,当然还有日志:

通过gcloud创建Google Kubernetes Engine(GKE)并通过kubectl访问_第3张图片

3 虚拟机

当创建GKE集群的时候,每个节点会对应一个虚拟机,可以在Compute Engine上查看:

通过gcloud创建Google Kubernetes Engine(GKE)并通过kubectl访问_第4张图片

当然也可以能过命令gcloud查看:

$ gcloud compute instances list
NAME                                       ZONE        MACHINE_TYPE   PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP    STATUS
gke-pkslow-k8s-default-pool-4743a88e-175c  us-west1-a  n1-standard-1               10.138.0.3   34.105.xx.xxx  RUNNING
gke-pkslow-k8s-default-pool-4743a88e-71l6  us-west1-a  n1-standard-1               10.138.0.2   35.247.xx.xxx  RUNNING
gke-pkslow-k8s-default-pool-4743a88e-9wn2  us-west1-a  n1-standard-1               10.138.0.4   35.199.xx.xxx  RUNNING

如果要进入机器查问题,可以直接ssh进去:

$ gcloud compute ssh gke-pkslow-k8s-default-pool-4743a88e-175c --zone=us-west1-a

通过gcloud创建Google Kubernetes Engine(GKE)并通过kubectl访问_第5张图片

日志也是十分方便查看的:

通过gcloud创建Google Kubernetes Engine(GKE)并通过kubectl访问_第6张图片

4 删除集群

使用完毕后,要记得删除集群,以免造成浪费:

$ gcloud container clusters delete pkslow-k8s --zone us-west1-a

你可能感兴趣的:(java)