前言
雲原生勢不可擋,了解雲原生肯定繞不開Kubernetes,這裡不對雲原生、Kubernetes概念做過多描述,直接開幹,在本地搭建Kubernetes環境并了解基礎操作。
參考文檔:
操作環境:
- macOS 10.15.3
約定:
- 後續文章中統一使用Kubernetes的簡稱:k8s
kubectl
kubectl是k8s叢集的指令行管理工具,可以使用kubectl對k8s叢集進行管理、進行容器化應用的安裝部署等。
安裝
brew install kubectl
mac下直接使用brew進行安裝,其它環境或安裝方式可見
官方安裝文檔使用
kubectl -h
使用-h參數列出支援指令,有以下輸出說明已安裝成功
kubectl controls the Kubernetes cluster manager.
Find more information at:
https://kubernetes.io/docs/reference/kubectl/overview/
Basic Commands (Beginner):
create Create a resource from a file or from stdin.
expose 使用 replication controller, service, deployment 或者 pod
并暴露它作為一個 新的 Kubernetes Service
run 在叢集中運作一個指定的鏡像
set 為 objects 設定一個指定的特征
Basic Commands (Intermediate):
explain 檢視資源的文檔
get 顯示一個或更多 resources
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
scale Set a new size for a Deployment, ReplicaSet or Replication
Controller
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:
diff Diff live version against would-be applied version
apply 通過檔案名或标準輸入流(stdin)對資源進行配置
patch 使用 strategic merge patch 更新一個資源的 field(s)
replace 通過 filename 或者 stdin替換一個資源
wait Experimental: Wait for a specific condition on one or many
resources.
convert 在不同的 API versions 轉換配置檔案
kustomize Build a kustomization target from a directory or a remote url.
Settings Commands:
label 更新在這個資源上的 labels
annotate 更新一個資源的注解
completion Output shell completion code for the specified shell (bash or
zsh)
Other Commands:
alpha Commands for features in alpha
api-resources Print the supported API resources on the server
api-versions Print the supported API versions on the server, in the form of
"group/version"
config 修改 kubeconfig 檔案
plugin Provides utilities for interacting with plugins.
version 輸出 client 和 server 的版本資訊
Usage:
kubectl [flags] [options]
Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all
commands).
指令的使用方式,在後續使用時會具體介紹。
Minikube
準備
先執行以下指令檢查你的系統是否支援虛拟化。
sysctl -a | grep -E --color 'machdep.cpu.features|VMX'

如果看到輸出中有 VMX 則說明支援。
brew install minikube
直接使用brew安裝,其它環境或安裝方式可見
啟動
minikube start --driver=docker
--driver 參數可以指定minikube的啟動方式,有:virtualbox、hyperkit、docker、vmware等。
參考文檔這裡我們選擇docker的方式
然後就是漫長的等待。。。
啟動完成
檢視狀态
使用status檢查叢集狀态
minikube status
出現下列輸出,說明叢集啟動正常
m01
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured
關閉
minikube stop
關閉後再運作
minikube status
輸出
m01
host: Stopped
kubelet: Stopped
apiserver: Stopped
kubeconfig: Stopped
dashboard
dashboard是k8s提供的一個web操作控制台,可以檢視叢集運作情況并進行管理。
minikube dashboard
執行後,會自動在預設浏覽器中打開
http://127.0.0.1:50281/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/#/overview?namespace=default總結
至此,一個使用Minikube搭建的k8s叢集就已經ready了。下文中,我們将繼續學習如何在叢集中部署一個簡單的應用。
其實可以看到,最基礎的入門操作是比較簡單的,官方文檔也十分詳細,隻要跟着操作一遍,基本就能了解。
之前嘗試過幾次k8s的入門學習,都止步于基礎概念,看了忘、忘了看,卻沒有真正的操作一遍。從實踐入手,是亘古不變的學習法門,一定不能手懶。
更多文章
見我的部落格:
https://nc2era.comwritten by
AloofJr,轉載請注明出處