关于java:通过gcloud创建Google-Kubernetes-EngineGKE并通过kubectl访问

63次阅读

共计 4635 个字符,预计需要花费 12 分钟才能阅读完成。

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    <none>   59s   v1.20.10-gke.1600
gke-pkslow-k8s-default-pool-4743a88e-71l6   Ready    <none>   58s   v1.20.10-gke.1600
gke-pkslow-k8s-default-pool-4743a88e-9wn2   Ready    <none>   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 查看,更不便:

像 Deployment, DaemonSet 这些一样能够查看:

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

3 虚拟机

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

当然也能够能过命令 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

日志也是非常不便查看的:

4 删除集群

应用结束后,要记得删除集群,免得造成节约:

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

正文完
 0