Kubernetes命令行管理工具—Kubectl

Kubernetes命令行管理工具—Kubectl

附上:

喵了個咪的博客:w-blog.cnKubernetes官方文檔:https://kubernetes.io/docs/reference/Kubernetes官方Git地址:https://github.com/kubernetes/kubernetes

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

一、國內安裝Kubectl

官網提供的安裝命令是:

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

國內直接安裝Kubectl會失敗主要是訪問了 google 的地址,但是可以通過Github上來找到可以進行使用編譯好的二進制文件

訪問如下網址:https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG.md#client-binaries-1

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

Kubernetes命令行管理工具—Kubectl

找到Client Binaries

Kubernetes命令行管理工具—Kubectl

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

Kubernetes命令行管理工具—Kubectl

執行安裝命令

<code>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/<code>

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

二、配置文件

光有Kubectl命令想操作K8S集群還是不夠的,必須要把鏈接K8S的相關配置信息填寫進去,默認kubectl會在 ~/.kube/config 尋找配置文件

<code>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/<code>

就可以通過命令行執行對K8S集群的控制了

<code>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"}/<code>

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

<code>kubectl  --kubeconfig=~/.kubu/config xxxxxxxxxx/<code>


三、Kubectl 常用命令


<code>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 – 輸出服務端和客戶端的版本信息。/<code>


<code>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 的版本信息
/<code>


分享到:


相關文章: