aokabin / rancher-practice

0 stars 0 forks source link

Rancherで構築したクラスタに、kubectlで接続する #4

Closed aokabin closed 5 years ago

aokabin commented 5 years ago

3 である程度クラスタが作れたので、今度は接続してアプリケーションを動かしてみたい

まずはサンプルでnginxとか動かしてみたい

aokabin commented 5 years ago

まずはkubectlで接続したいので、configファイルを参照する configファイルはclusterのページのここ 2019-01-27 23 51 56

これを~/.kube/configにいい感じに追記←

そして以下を実行

$ kubectl cluster-info
Kubernetes master is running at https://kabilab.st.ie.u-ryukyu.ac.jp/k8s/clusters/c-l5hc5
KubeDNS is running at https://kabilab.st.ie.u-ryukyu.ac.jp/k8s/clusters/c-l5hc5/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

$ kubectl version
Client Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.7", GitCommit:"0c38c362511b20a098d7cd855f1314dad92c2780", GitTreeState:"clean", BuildDate:"2018-08-20T10:09:03Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.6", GitCommit:"b1d75deca493a24a2f87eb1efde1a569e52fc8d9", GitTreeState:"clean", BuildDate:"2018-12-16T04:30:10Z", GoVersion:"go1.10.3", Compiler:"gc", Platform:"linux/amd64"}

おおー!めっちゃ簡単だ!!(クラスタ作るのが大変だっただけだ)

aokabin commented 5 years ago

ちゃんとNode情報も取得できる 💪

$ kubectl get nodes
NAME              STATUS    ROLES                      AGE       VERSION
anago             Ready     controlplane,etcd,worker   36m       v1.11.6
kabilab-centos    Ready     worker                     25m       v1.11.6
kabilab-centos2   Ready     worker                     24m       v1.11.6
kabilab-centos3   Ready     worker                     24m       v1.11.6
ubuntu            Ready     worker                     31m       v1.11.6
ubuntu2           Ready     worker                     30m       v1.11.6
ubuntu3           Ready     worker                     30m       v1.11.6
aokabin commented 5 years ago

サンプル、nginxじゃなくて、kube-dashboardというものがあったので、それを動かしてみる

$ kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml
secret "kubernetes-dashboard-certs" created
serviceaccount "kubernetes-dashboard" created
role.rbac.authorization.k8s.io "kubernetes-dashboard-minimal" created
rolebinding.rbac.authorization.k8s.io "kubernetes-dashboard-minimal" created
deployment.apps "kubernetes-dashboard" created
service "kubernetes-dashboard" created

一瞬で作れた

で、接続するためにproxyする

$ kubectl proxy

そしてURLにアクセス... http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/ できぬ 😭 rancherの画面で404になる...

調べていると、どうやらrancherではクラスタIDみたいなのを指定する必要があるみたい

上記にあるように、clusterIDを挿入してみたら接続できた!

そして、ログイン画面的なものが表示される 2019-01-28 0 19 57

今回は学内でしか使わないけど、調べてみたところいい記事があったので こちらを参考にしながらやってみる

aokabin commented 5 years ago
apiVersion: v1
kind: ServiceAccount
metadata:
  name: admin-user
  namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: admin-user
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: admin-user
  namespace: kube-system

こういうyamlを書いて...

$ kubectl apply -f admin-user.yaml
$ kubectl -n kube-system get secret
$ kubectl -n kube-system describe secret admin-user-token-なんか文字列

このtokenを突っ込めばOKだった、簡単...!

aokabin commented 5 years ago

kubectl get pods --all-namespaces これでdashboardのpodも表示されるよ