什麼是資源
在K8s叢集中建立的任何東西都可以被視為資源:deployment、Pod、Service等等
資源的管理方式
1、指令式對象管理
直接使用指令去操作k8s的資源,操作簡單,隻能操作活動對象,無法審計和跟蹤,是以通常用于測試
指令文法:kubectl command [type] [name] [flags]
command:指定要對資源執行的操作,create、apply、run、get、delete
type:指定資源的類型,deployment、pod、service、ingress
name:指定資源的名稱(名稱大小寫敏感)
flags:指定額外的可選參數
command
-
基礎指令
create【建立資源】
edit【編輯資源】
set【為對象設定指定特性】
get【顯示資源】
delete【删除資源】
-
運作、調試指令
run【在叢集上運作特定鏡像】
expose【暴露資源為Service】
describe【 顯示特定資源或資源組的詳細資訊】
explain【顯示資源文檔】
logs【列印 Pod 中容器的日志】
attach【挂接到一個運作中的容器】
exec【在某個容器中執行一個指令】
port-forward【将一個或多個本地端口轉發到某個 Pod】
proxy【運作一個指向 Kubernetes API 伺服器的代理】
cp【在Pod内、外複制檔案】
auth【檢查授權】
debug【建立調試會話以排除工作負載和節點故障】
events【列出事件】
-
進階指令
apply【根據檔案名或stdin建立資源】
replace【根據檔案名或stdin替換資源】
patch【更新資源】
diff【區分實時版本與潛在應用版本】
wait【Experimental: Wait for a specific condition on one or many resources】
kustomize【Build a kustomization target from a directory or URL】
-
設定指令
label【更新某資源上的标簽】
annotate【更新一個資源的注解】
completion【輸出指定shell(bash、zsh、fish或powershell)的shell完成代碼】
-
部署指令
rollout【Manage the rollout of a resource】
scale【Set a new size for a deployment, replica set, or replication controller】
autoscale【Auto-scale a deployment, replica set, stateful set, or replication controller】
-
叢集管理指令
cluster-info【展示叢集資訊】
top【Display resource (CPU/memory) usage】
cordon【标記節點為不可排程】
uncordon【标記節點為可排程】
drain【 清空節點以準備維護】
taint【更新一個或者多個節點上的污點】
certificate【修改證書資源】
-
其它指令
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【輸出用戶端和服務端的版本資訊】
type
-
叢集級别資源
namespaces【命名空間,隔離Pod】
nodes【叢集組成部分】
-
Pod資源
pods【裝載容器】
-
Pod資源控制器
deployments【Pod控制器】
replicationcontrollers
replicasets
daemonsets
statefulsets
controllerrevisions
jobs
cronjobs
horizontalpodautoscalers
-
服務發現資源
services【統一Pod對外接口】
ingresses【統一Pod對外接口】
ingressclasses
networkpolicies
-
存儲資源
csidrivers
csinodes
csistoragecapacities
storageclasses
volumeattachments
-
配置資源
configmaps
secrets
-
其它
bindings
componentstatuses
endpoints
events
limitranges
persistentvolumeclaims
persistentvolumes
podtemplates
resourcequotas
serviceaccounts
localsubjectaccessreviews
selfsubjectaccessreviews
selfsubjectrulesreviews
subjectaccessreviews
clusterrolebindings
clusterroles
rolebindings
roles
mutatingwebhookconfigurations
validatingwebhookconfigurations
2、指令式對象配置
通過create指令和配置檔案去操作kubernetes的資源,如果存在則會報錯
- nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
namespace: dev
labels:
env: "dev"
version: "1.0"
spec:
containers:
- image: nginx:1.17.1
imagePullPolicy: IfNotPresent
name: nginx-container
ports:
- name: nginx-port
containerPort: 80
protocol: TCP
3、聲明式對象配置
通過apply指令和配置檔案去操作kubernetes的資源。可以同時操作一個目錄下的多個配置檔案,出現錯誤難調試。
apply的資源不存在則建立,存在則更新,相當于create和patch的組合