天天看點

Kubernetes指令行管理工具 — Kubectl

附上:

喵了個咪的部落格:

w-blog.cn Kubernetes官方文檔: https://kubernetes.io/docs/reference/ Kubernetes官方Git位址: https://github.com/kubernetes/kubernetes

PS:本系列中使用 KubernetesV1.8 RancherV1.6.14

一、國内安裝Kubectl

官網提供的安裝指令是:

curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl           

國内直接安裝Kubectl會失敗主要是通路了 google 的位址,但是可以通過Github上來找到可以進行使用編譯好的二進制檔案

通路如下網址:

kubernetes/CHANGELOG.md at master · kubernetes/kubernetes · GitHub

找到你希望安裝的Kubectl版本點選進入

找到Client Binaries

更具系統版本選擇不同的安裝包位址

執行安裝指令

wget https://dl.k8s.io/v1.15.3/kubernetes-client-linux-amd64.tar.gz
tar -zxvf kubernetes-client-linux-amd64.tar.gz
cd kubernetes/client/bin
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl           

kubectl version 能夠正常看到輸出即可

二、配置檔案

光有Kubectl指令想操作K8S叢集還是不夠的,必須要把連結K8S的相關配置資訊填寫進去,預設kubectl會在 ~/.kube/config 尋找配置檔案

vim ~/.kube/config

apiVersion: v1
clusters:
- cluster:
    server: https://xxxxxxxxxx:443
    insecure-skip-tls-verify: true
  name: kubernetes
contexts:
- context:
    cluster: kubernetes
    user: "kubernetes-admin"
  name: kubernetes-admin-xxxxxx
current-context: kubernetes-admin-xxxxxxxx
kind: Config
preferences: {}
users:
- name: "kubernetes-admin"
  user: xxxxxxxx           

就可以通過指令行執行對K8S叢集的控制了

kubectl version
Client Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.5", GitCommit:"32ac1c9073b132b8ba18aa830f46b77dcceb0723", GitTreeState:"clean", BuildDate:"2018-06-21T11:46:00Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.5", GitCommit:"753b2dbc622f5cc417845f0ff8a77f539a4213ea", GitTreeState:"clean", BuildDate:"2018-11-26T14:31:35Z", GoVersion:"go1.10.3", Compiler:"gc", Platform:"linux/amd64"}           

當然也可以指定對應的檔案路徑作為配置檔案

kubectl  --kubeconfig=~/.kubu/config xxxxxxxxxx           

三、Kubectl 常用指令

kubectl apply – 通過檔案名或控制台輸入,對資源進行配置。
kubectl create – 通過檔案名或控制台輸入,建立資源。
kubectl delete – 通過檔案名、控制台輸入、資源名或者label selector删除資源。
kubectl edit – 編輯服務端的資源。
kubectl exec – 在容器内部執行指令。
kubectl get – 輸出一個/多個資源。
kubectl logs – 輸出pod中一個容器的日志。
kubectl namespace -(已停用)設定或檢視目前使用的namespace。
kubectl port-forward – 将本地端口轉發到Pod。
kubectl rolling-update – 對指定的replication controller執行滾動更新。
kubectl stop – (已停用)通過資源名或控制台輸入安全删除資源。
kubectl version – 輸出服務端和用戶端的版本資訊。           
Basic Commands (Beginner):
  create         Create a resource from a file or from stdin.
  expose         使用 replication controller, service, deployment 或者 pod 并暴露它作為一個 新的
Kubernetes Service
  run            在叢集中運作一個指定的鏡像
  set            為 objects 設定一個指定的特征
  run-container  在叢集中運作一個指定的鏡像. This command is deprecated, use "run" instead

Basic Commands (Intermediate):
  get            顯示一個或更多 resources
  explain        檢視資源的文檔
  edit           在伺服器上編輯一個資源
  delete         Delete resources by filenames, stdin, resources and names, or by resources and label selector

Deploy Commands:
  rollout        Manage the rollout of a resource
  rolling-update 完成指定的 ReplicationController 的滾動更新
  scale          為 Deployment, ReplicaSet, Replication Controller 或者 Job 設定一個新的副本數量
  autoscale      自動調整一個 Deployment, ReplicaSet, 或者 ReplicationController 的副本數量

Cluster Management Commands:
  certificate    修改 certificate 資源.
  cluster-info   顯示叢集資訊
  top            Display Resource (CPU/Memory/Storage) usage.
  cordon         标記 node 為 unschedulable
  uncordon       标記 node 為 schedulable
  drain          Drain node in preparation for maintenance
  taint          更新一個或者多個 node 上的 taints

Troubleshooting and Debugging Commands:
  describe       顯示一個指定 resource 或者 group 的 resources 詳情
  logs           輸出容器在 pod 中的日志
  attach         Attach 到一個運作中的 container
  exec           在一個 container 中執行一個指令
  port-forward   Forward one or more local ports to a pod
  proxy          運作一個 proxy 到 Kubernetes API server
  cp             複制 files 和 directories 到 containers 和從容器中複制 files 和 directories.
  auth           Inspect authorization

Advanced Commands:
  apply          通過檔案名或标準輸入流(stdin)對資源進行配置
  patch          使用 strategic merge patch 更新一個資源的 field(s)
  replace        通過 filename 或者 stdin替換一個資源
  convert        在不同的 API versions 轉換配置檔案

Settings Commands:
  label          更新在這個資源上的 labels
  annotate       更新一個資源的注解
  completion     Output shell completion code for the specified shell (bash or zsh)

Other Commands:
  api-versions   Print the supported API versions on the server, in the form of "group/version"
  config         修改 kubeconfig 檔案
  help           Help about any command
  plugin         Runs a command-line plugin
  version        輸出 client 和 server 的版本資訊