本篇文章實現從本地端的 K8s 群集操作 GKE。先從 GKE 上的群集 .kube/config
進行複製並貼上至本地端主機 master
上,這邊將其複製到 gke1
檔案。
$ KUBECONFIG=gke1:~/.kube/config kubectl config view
# gke1 是 GKE 的 key
# DATA+OMITTED 會導致資訊不完全因此需用以下方式攤平
$ KUBECONFIG=gke1:~/.kube/config kubectl config view --flatten
$ KUBECONFIG=gke1:~/.kube/config kubectl config view --flatten > new
$ KUBECONFIG=new kubectl config get-contexts
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
* gke_sunny-catwalk-286908_us-central1-c_cluster-1-test gke_sunny-catwalk-286908_us-central1-c_cluster-1-test gke_sunny-catwalk-286908_us-central1-c_cluster-1-test
gke_sunny-catwalk-286908_us-central1-c_cluster-2-test gke_sunny-catwalk-286908_us-central1-c_cluster-2-test gke_sunny-catwalk-286908_us-central1-c_cluster-2-test
kubernetes-admin@kubernetes kubernetes kubernetes-admin
$ KUBECONFIG=new kubectl config use-context kubernetes-admin@kubernetes # 切換
Switched to context "kubernetes-admin@kubernetes".
$ KUBECONFIG=new kubectl config get-contexts
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
gke_sunny-catwalk-286908_us-central1-c_cluster-1-test gke_sunny-catwalk-286908_us-central1-c_cluster-1-test gke_sunny-catwalk-286908_us-central1-c_cluster-1-test
gke_sunny-catwalk-286908_us-central1-c_cluster-2-test gke_sunny-catwalk-286908_us-central1-c_cluster-2-test gke_sunny-catwalk-286908_us-central1-c_cluster-2-test
* kubernetes-admin@kubernetes kubernetes kubernetes-admin
$ KUBECONFIG=new kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready master 5d18h v1.18.8
node01 Ready <none> 5d17h v1.18.8
node02 Ready <none> 5d17h v1.18.8
切換至 GKE 叢集
cch@master:~/context$ KUBECONFIG=new kubectl config use-context gke_sunny-catwalk-286908_us-central1-c_cluster-1-test
Switched to context "gke_sunny-catwalk-286908_us-central1-c_cluster-1-test".
cch@master:~/context$ KUBECONFIG=new kubectl config current-context
gke_sunny-catwalk-286908_us-central1-c_cluster-1-test
cch@master:~/context$ KUBECONFIG=new kubectl get nodes
NAME STATUS ROLES AGE VERSION
gke-cluster-1-test-default-pool-255d7fb2-1f8l Ready <none> 4d12h v1.15.12-gke.2
gke-cluster-1-test-default-pool-255d7fb2-lbwc Ready <none> 4d12h v1.15.12-gke.2
gke-cluster-1-test-default-pool-255d7fb2-ppnm Ready <none> 4d12h v1.15.12-gke.2
cloud sdk 安裝,之後才能操作
$ echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" |
$ sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
$ sudo apt-get install apt-transport-https ca-certificates gnupg
$ curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key --keyring /usr/share/keyrings/cloud.google.gpg add -
$ sudo apt-get update && sudo apt-get install google-cloud-sdk
$ gcloud auth login # 它會引導認證