目錄
K8S模拟項目
一、項目的生命周期
二、建立kubectl run指令
使用run報錯了
三、釋出kubectl expose指令
1.service的作用
2.Service的類型
3.檢視Pod網絡狀态詳細資訊和Service暴露端口
4.檢視關聯後端的節點
5.檢視service的較長的描述資訊
6.通路内部IP檢視
7.檢視通路日志
四、更新kubectl set
1.擷取修改模闆
2.檢視目前nginx的版本号
3.将nginx版本更新為1.15
4.監聽pod狀态
5.檢視pod的IP變化
五、復原kubectl rollout
1.檢視曆史版本
2.執行復原到上一個版本
3.執行復原到執行版本
檢視曆史版本
回到revision2,即1.15版本
4.檢查復原狀态
六、删除kubectl delete
1.删除副本控制器
2.删除service
K8S模拟項目
Kubectl是管理k8s叢集的指令行工具,通過生成的json格式傳遞給apiserver進行建立、檢視、管理的操作。
//幫助資訊
[[email protected] bin]# kubectl --help
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 為 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
一、項目的生命周期
建立–>釋出–>更新–>復原–>删除
二、建立kubectl run指令
- 建立并允許一個或多個容器鏡像
- 建立一個seployment或job來管理容器
- kubectl run --help檢視使用幫助
啟動nginx執行個體,暴露容器端口80,設定副本數3
kubectl run nginx --image=nginx --port=80 --replicas=3
使用run報錯了
k8sv1.18.0以後的版本, --replicas以後棄用該指令,推薦使用deployment建立pods
我這裡用的是1.21.3版本
- 想建立多個執行個體時可以使用:kubectl create deployment nginx --images=nginx --port=80 --replicas=3來進行建立
- 檢視pod: kubectl get pod,用來檢視使用指令建立的所有執行個體
- 檢視deploy:kubectl get deploy,用來檢視執行個體所建立的數量;
- 高于1.17版本的建議以後直接使用create deployment建立pod管理器方式建立pod;
kubectl create deployment nginx1 --image=nginx --port=80 --replicas=3
三、釋出kubectl expose指令
将資源暴露為新的Service
将Deployment的nginx建立Service,并通過Service的80端口轉發至容器的80的端口上,Service的名稱為nginx,類型為NodePort
kubectl expose deployment nginx --port=80 --target-port=80 --name=nginx --type=NodePort
1.service的作用
- Kubernetes之是以需要Service,一方面是因為Pod的IP不是固定的(Pod可能會重建),另一方面是因為一組Pod執行個體之間總會有負載均衡的需求
- Service通過Label Selector實作對一組Pod進行通路
- 對于容器應用而言,Kubernetes提供了基于VIP(虛拟IP)的網橋的方式通路Service,再由Service重定向到相應的Pod
2.Service的類型
- ClusterIP:提供一個叢集内部的虛拟IP以供Pod通路(Service預設類型)
- NodePort:在每個Node上打開一個端口以供外部通路,Kubernetes将會在每個Node上打開一個端口并且每個Node的端口都是一樣的,通過NodeIP:NodePort的方式
- LoadBalancer:通過外部的負載均衡器來通路,通常在雲平台部署LoadBalancer還需要額外的費用。
3.檢視Pod網絡狀态詳細資訊和Service暴露端口
kubectl get pods,svc -o wide
4.檢視關聯後端的節點
kubectl get endpoints
5.檢視service的較長的描述資訊
kubectl describe svc nginx
6.通路内部IP檢視
kubectl describe svc nginx | grep NodePort
curl 192.168.131.10:32395
7.檢視通路日志
kubectl logs []
四、更新kubectl set
- 更改現有應用資源一些資訊。
kubectl set --help檢視使用幫助
1.擷取修改模闆
kubectl set image --help擷取
2.檢視目前nginx的版本号
curl -I 192.168.131.10:32395
3.将nginx版本更新為1.15
kubectl set image deployment/nginx1 nginx=nginx:1.15
4.監聽pod狀态
處于動态監聽pod狀态,由于使用的是滾動更新方式,是以會先生成一個新的pod,然後删除一個舊的pod,往後以此類推
kubectl get pods -w
注:更新規則可通過“kubetl describe deployment nginx1”的“RollingUpdateStrategy”檢視,預設配置為“25% max unavailable, 25% max surge”,即按照25%的比例進行滾動更新。
5.檢視pod的IP變化
kubectl get pod -o wide
五、復原kubectl rollout
- 對資源進行復原管理
kubectl rollout --help檢視使用幫助
1.檢視曆史版本
kubectl rollout history deployment/nginx1
2.執行復原到上一個版本
kubectl rollout undo deployment/nginx
kubectl get pods -o wide
3.執行復原到執行版本
檢視曆史版本
回到revision2,即1.15版本
kubectl rollout undo deployment/nginx-service --to-revision=2
4.檢查復原狀态
kubectl rollout status deployment/nginx-service
六、删除kubectl delete
1.删除副本控制器
kubectl delete deployment/nginx