iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 28
0

本篇文章實現從本地端的 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 # 它會引導認證

上一篇
etcd 元件淺解
下一篇
管理 Deployments
系列文
我真的覺得認為 K8s 到底在火什麼30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言