k8s安裝
2024-01-17 13:41 youxin 閱讀(246) 評(píng)論(0) 收藏 舉報(bào)kubectl安裝:
https://kubernetes.io/zh-cn/docs/tasks/tools/install-kubectl-linux/
啟動(dòng) kubectl 自動(dòng)補(bǔ)全功能
Bash
你現(xiàn)在需要確保一點(diǎn):kubectl 補(bǔ)全腳本已經(jīng)導(dǎo)入(sourced)到 Shell 會(huì)話中。 可以通過以下兩種方法進(jìn)行設(shè)置:
echo 'source <(kubectl completion bash)' >>~/.bashrc
如果 kubectl 有關(guān)聯(lián)的別名,你可以擴(kuò)展 Shell 補(bǔ)全來適配此別名:
echo 'alias k=kubectl' >>~/.bashrc
echo 'complete -o default -F __start_kubectl k' >>~/.bashrc
bash-completion 負(fù)責(zé)導(dǎo)入 /etc/bash_completion.d 目錄中的所有補(bǔ)全腳本。
兩種方式的效果相同。重新加載 Shell 后,kubectl 自動(dòng)補(bǔ)全功能即可生效。 若要在當(dāng)前 Shell 會(huì)話中啟用 Bash 補(bǔ)全功能,源引 ~/.bashrc 文件:
source ~/.bashrc
k8s解決kubectl -bash: _get_comp_words_by_ref: 未找到命令
1. yum -y install bash-completion
2. source /usr/share/bash-completion/bash_completion
3. echo 'source <(kubectl completion bash)' >> ~/.bashrc
kind安裝
kind安裝
curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.9.0/kind-linux-amd64 chmod +x ./kind mv ./kind /$PATH/kind
用go install命令安裝:
Installing with go install
When installing with Go please use the latest stable Go release. At least go1.16 or greater is required.
To install use: go install sigs.k8s.io/kind@v0.20.0.
If you are building from a local source clone, use go install . from the top-level directory of the clone.
go install will typically put the kind binary inside the bin directory under go env GOPATH, see Go’s “Compile and install packages and dependencies” for more on this. You may need to add that directory to your $PATH if you encounter the error kind: command not found after installation, you can find a guide for adding a directory to your PATH at https://gist.github.com/nex3/c395b2f8fd4b02068be37c961301caa7#file-path-md.
kind kind creates and manages local Kubernetes clusters using Docker container 'nodes' Usage: kind [command] Available Commands: build Build one of [node-image] completion Output shell completion code for the specified shell (bash, zsh or fish) create Creates one of [cluster] delete Deletes one of [cluster] export Exports one of [kubeconfig, logs] get Gets one of [clusters, nodes, kubeconfig] help Help about any command load Loads images into nodes version Prints the kind CLI version Flags: -h, --help help for kind --loglevel string DEPRECATED: see -v instead -q, --quiet silence all stderr output -v, --verbosity int32 info log verbosity, higher value produces more output --version version for kind Use "kind [command] --help" for more information about a command.
簡單說下幾個(gè)比較常用選項(xiàng)的含義:
build:用來從 Kubernetes 源代碼構(gòu)建一個(gè)新的鏡像。
create:創(chuàng)建一個(gè) Kubernetes 集群。
delete:刪除一個(gè) Kubernetes 集群。
get: 可用來查看當(dāng)前集群、節(jié)點(diǎn)信息以及 Kubectl 配置文件的地址。
load:從宿主機(jī)向 Kubernetes 節(jié)點(diǎn)內(nèi)導(dǎo)入鏡像。
Command Output: WARNING: Your kernel does not support cgroup namespaces. Cgroup namespace setting discarded.
9e339988d087173127bc4f291b34c6269e4b485cfdfb892789b38db8f0b3e664
docker: Error response from daemon: failed to create shim: OCI runtime create failed: cgroup namespaces aren't enabled in the kernel: unknown.
So unfortunately RHEL 7 is stuck on kernel 3.10, which means that the cgroupns=private feature cannot be used, even though we have a recent enough verison of docker to support it.
Even trying to create any container with that feature fails:
$ docker run -ti --rm --cgroupns=private alpine
WARNING: Your kernel does not support cgroup namespaces. Cgroup namespace setting discarded.
docker: Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: cgroup namespaces aren't enabled in the kernel: unknown.
On RHEL8, it uses kernel 4.18, which is sufficient for this feature, and that will also use cgroups v2.
While I understand the project want to use this feature, with good reason, would it be possible to have a flag to disable its use, for use on older environments?
解決辦法:降級(jí)kind go install sigs.k8s.io/kind@v0.19.0
K8s 創(chuàng)建好之后,我們可以用下列命令查看 K8s 集群
#查看集群
kubectl cluster-info --context kind-tsk8s
#查看node
kubectl get nodes
#查看kube-system空間內(nèi)運(yùn)行的pod
kubectl get pods -n kube-system
kubectl get node
NAME STATUS ROLES AGE VERSION
mykind1-control-plane Ready control-plane 4m46s v1.27.1
運(yùn)行結(jié)果
圖 11

說明:
可以看到現(xiàn)在只有一個(gè) node。上一篇文章中介紹的 control-plance 和工作 node 都在這個(gè) node 上。
圖 12

說明:
從這個(gè)圖片可以看到,上一篇文章中提到到 apiserver,dns,scheduler 等組件均是以 pod 的形式(容器)在運(yùn)行。
這里 K8s 作為管理容器的服務(wù),本身也是運(yùn)行容器來實(shí)現(xiàn)其功能。
我們用 docker 命令再觀察一下現(xiàn)在運(yùn)行的容器
docker ps
圖 13

在 Docker Desktop 中查看容器
圖 14

分析:
這里我們看到只有一個(gè)運(yùn)行的容器“tsk8s-control-plane”,但是我們用“kubectl get pods”查看 pod 時(shí),發(fā)現(xiàn)有許多 pod 在運(yùn)行。
這是怎么回事?
原因在于 kind 創(chuàng)建 K8s 時(shí),實(shí)際上是利用鏡像啟了一個(gè)容器而 K8s 的整個(gè)服務(wù)都運(yùn)行在這個(gè)容器內(nèi)部。
我們進(jìn)入這個(gè)容器的內(nèi)部進(jìn)行觀察,運(yùn)行命令
#進(jìn)入容器內(nèi)部
docker exec -it tsk8s-control-plane bash
#查看運(yùn)行的容器
crictl ps
(
crictl 是Kubelet容器接口(CRI)的CLI和驗(yàn)證工具。。 你可以使用它來檢查和調(diào)試 Kubernetes 節(jié)點(diǎn)上的容器運(yùn)行時(shí)和應(yīng)用程序。 crictl 和它的源代碼在 cri-tools 代碼庫。
)
圖 15

說明:
在“tsk8s-control-plane”容器內(nèi)部,我們看到了 K8s 集群正在運(yùn)行的容器
在這里我們用的是“crictl ps”來查看容器,因?yàn)樵凇皌sk8s-control-plane”容器中的容器運(yùn)行環(huán)境(runtime)不是 Docker 而是 Containerd。
Containerd 使用的命令與 docker 相似,把 docker 命令中 docker 換成 crictl 即可。
提示:未來 K8s 新版本將不再支持 docker 做為運(yùn)行環(huán)境
對(duì)于 kind 創(chuàng)建的 K8s 集群,我們可以簡單理解上面用 docker ps 看到的“tsk8s-control-plane”容器就是一臺(tái)服務(wù)器,而我們把 K8s 服務(wù)(Control plane + 工作 node)都搭在了這臺(tái)服務(wù)器上。
我們的 WSL 就相當(dāng)于一個(gè)裝有 kubectl 命令行的客戶端,我們用這個(gè)客戶端連接 K8s 集群并操作。
5. 使用 Dashboard
我們除了可以用 kubectl 命令行來控制 K8s 外,我們還可以利用 Dashboard 圖形界面來進(jìn)行一些操作。
我們可以利用 Dashboard 監(jiān)控 K8s,部署應(yīng)用,創(chuàng)建及修改各種資源。
Dashboard 非默認(rèn)安裝,所以我們需要手工部署。
https://github.com/kubernetes/dashboard?tab=readme-ov-file
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v3.0.0-alpha0/charts/kubernetes-dashboard.yaml
cert-manager
3.0需要依賴cert-manager,我們先要這個(gè)這個(gè)
Kubeflow 組件使用 cert-manager 來為準(zhǔn)入 Webhooks 提供證書。
Install cert-manager:
kustomize build common/cert-manager/cert-manager/base | kubectl apply -f -
kubectl wait --for=condition=ready pod -l 'app in (cert-manager,webhook)' --timeout=180s -n cert-manager
kustomize build common/cert-manager/kubeflow-issuer/base | kubectl apply -f -
如果出現(xiàn)如下錯(cuò)誤:
Error from server (InternalError): error when creating "STDIN": Internal error occurred: failed calling webhook "webhook.cert-manager.io": failed to call webhook: Post "https://cert-manager-webhook.cert-manager.svc:443/mutate?timeout=10s": dial tcp 10.96.202.64:443: connect: connection refused
這是因?yàn)?webhook 尚未準(zhǔn)備好接收請(qǐng)求。等待幾秒鐘,然后重試應(yīng)用清單
然后就安裝成功了
Kubernetes dashboardv2.7.0安裝指南:從零開始搭建可視化界面
https://zhuanlan.zhihu.com/p/655525723
命令行代理
你可以使用 kubectl 命令行工具來啟用 Dashboard 訪問,命令如下:
kubectl proxy
kubectl 會(huì)使得 Dashboard 可以通過 http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/ 訪問。
UI 只能 通過執(zhí)行這條命令的機(jī)器進(jìn)行訪問。更多選項(xiàng)參見 kubectl proxy --help。
Kubeconfig 身份驗(yàn)證方法不支持外部身份提供程序或基于 x509 證書的身份驗(yàn)證。
如果不是localhost訪問需要執(zhí)行:kubectl proxy --address=0.0.0.0
grep 'client-certificate-data' /etc/kubernetes/admin.conf | head -n 1 | awk '{print $2}' | base64 -d >> kubecfg.crt
grep 'client-key-data' /etc/kubernetes/admin.conf | head -n 1 | awk '{print $2}' | base64 -d >> kubecfg.key
openssl pkcs12 -export -clcerts -inkey kubecfg.key -in kubecfg.crt -out kubecfg.p12 -name "kubernetes-client"
導(dǎo)入p12證書。
k8s api訪問證書
$ cp /etc/kubernetes/admin.conf $HOME/.kube/config
$ grep 'client-certificate-data' ~/.kube/config | head -n 1 | awk '{print $2}' | base64 -d >> kubecfg.crt
$ grep 'client-key-data' ~/.kube/config | head -n 1 | awk '{print $2}' | base64 -d >> kubecfg.key
$ openssl pkcs12 -export -clcerts -inkey kubecfg.key -in kubecfg.crt -out kubecfg.p12 -name "kubernetes-client"
證書導(dǎo)入后還是訪問不了,看了一篇文章:
方法一: kubectl port-forward
注意: 這種方法適合有正式ssl證書的情況. 如果沒有, 先用方法二進(jìn)行訪問.
In an environment where one needs to access the dashboard from another host than thekubectl proxy's localhost, one can run:kubectl port-forward --namespace kubernetes-dashboard service/kubernetes-dashboard 10443:443 --address 0.0.0.0to access the dashboard through HTTPS
然后訪問: https://ttg12:10443.
不過因?yàn)闇y試環(huán)境沒有正式的ssl證書, 所以瀏覽器會(huì)拒絕訪問. 需要采用方法二.

終于可以看到這個(gè)界面了
創(chuàng)建token:
https://github.com/kubernetes/dashboard/blob/master/docs/user/access-control/creating-sample-user.md
v3.0.0的配置文件已經(jīng)給我們配置好了用戶名和role。我們只需要執(zhí)行:
kubectl -n kubernetes-dashboard create token kubernetes-dashboard
用上面生成的token進(jìn)去后看到的都是空數(shù)據(jù),而且右上角通知欄還有提升,沒有權(quán)限,我們按如下操作:
1、token沒有生成
1) 創(chuàng)建serviceaccount
kubectl create serviceaccount myadmin -n kubernetes-dashboard

2)查看賬戶信息
kubectl get serviceaccount myadmin -o yaml -n kubernetes-dashboard

3)綁定角色
kubectl create clusterrolebinding myadmin --clusterrole=cluster-admin --serviceaccount=kubernetes-dashboard:myadmin -n kubernetes-dashboard

4)生成token
kubectl create token myadmin -n kubernetes-dashboard

浙公網(wǎng)安備 33010602011771號(hào)