前言
kubectl是運行kubernetes集羣命令的管理工具,通過命令行參數轉換爲對apiserver的REST API調用,並將調用結果輸出
kubectl的語法格式
kubectl [命令] [選項]
命令操作
1、查看幫助信息
[root@localhost ~]# 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
Advanced Commands:
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 轉換配置文件
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).
- 項目的生命週期:創建----發佈----更新----回滾----刪除
2、創建(kubectl run命令)
(1)創建一個名稱爲nginx-deployment的pods,鏡像使用nginx,端口爲80,創建副本集爲3
[root@localhost ~]# kubectl run nginx-deployment --image=nginx --port=80 --replicas=3
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
deployment.apps/nginx-deployment created
(2)查看創建的pod資源
[root@localhost ~]# kubectl get pods //可在末尾加上"-w"可以是查看到創建pod的資源動態
NAME READY STATUS RESTARTS AGE
nginx-deployment-5477945587-ntn6r 1/1 Running 0 2m27s
nginx-deployment-5477945587-rfn7d 1/1 Running 0 2m27s
nginx-deployment-5477945587-tj89b 1/1 Running 0 2m27s
(3)pod資源創建同時會創建deployment(控制器)和replicaset(副本集)資源
[root@localhost ~]# kubectl get pods,deployment,replicaset
NAME READY STATUS RESTARTS AGE
pod/nginx-deployment-5477945587-ntn6r 1/1 Running 0 4m18s
pod/nginx-deployment-5477945587-rfn7d 1/1 Running 0 4m18s
pod/nginx-deployment-5477945587-tj89b 1/1 Running 0 4m18s
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deployment.extensions/nginx-deployment 3 3 3 3 4m15s
NAME DESIRED CURRENT READY AGE
replicaset.extensions/nginx-deployment-5477945587 3 3 3 4m18s
3、發佈(kubectl expose命令),service提供負載均衡功能
[root@localhost ~]# kubectl expose deployment nginx-deployment \
> --port=80 --target-port=80 --name=nginx-service \
> --type=NodePort
service/nginx-service exposed
#查看pod及暴露的端口信息
[root@localhost ~]# kubectl get pods,svc
NAME READY STATUS RESTARTS AGE
pod/nginx-deployment-5477945587-ntn6r 1/1 Running 0 36m
pod/nginx-deployment-5477945587-rfn7d 1/1 Running 0 36m
pod/nginx-deployment-5477945587-tj89b 1/1 Running 0 36m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 10d
service/nginx-service NodePort 10.0.0.202 <none> 80:30916/TCP 42s
#查看網絡狀態詳細信息
[root@localhost ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE
nginx-deployment-5477945587-ntn6r 1/1 Running 0 45m 172.17.79.3 192.168.7.102 <none>
nginx-deployment-5477945587-rfn7d 1/1 Running 0 45m 172.17.23.4 192.168.7.103 <none>
nginx-deployment-5477945587-tj89b 1/1 Running 0 45m 172.17.23.3 192.168.7.103 <none>
#查看關聯後的節點
[root@localhost ~]# kubectl get endpoints
NAME ENDPOINTS AGE
kubernetes 192.168.7.100:6443,192.168.7.101:6443 10d
nginx-service 172.17.23.3:80,172.17.23.4:80,172.17.79.3:80 9m38s
- 完成發佈後,就可以使用本地瀏覽器訪問創建的pod資源了
#查看pod的訪問日誌
[root@localhost ~]# kubectl logs nginx-deployment-5477945587-ntn6r
172.17.79.1 - - [10/May/2020:05:44:36 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36" "-"
2020/05/10 05:44:36 [error] 6#6: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.79.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.7.102:30916", referrer: "http://192.168.7.102:30916/"
172.17.79.1 - - [10/May/2020:05:44:36 +0000] "GET /favicon.ico HTTP/1.1" 404 556 "http://192.168.7.102:30916/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36" "-"
[root@localhost ~]# kubectl logs nginx-deployment-5477945587-rfn7d
172.17.23.1 - - [10/May/2020:05:45:21 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36" "-"
2020/05/10 05:45:21 [error] 6#6: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.23.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.7.103:30916", referrer: "http://192.168.7.103:30916/"
172.17.23.1 - - [10/May/2020:05:45:21 +0000] "GET /favicon.ico HTTP/1.1" 404 556 "http://192.168.7.103:30916/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36" "-"
4、更新(kubectl set命令)
- 創建完成的pod中可以看到nginx的版本爲1.17版本
- 將此版本更改爲1.14版本
[root@localhost ~]# kubectl set image deployment/nginx-deployment nginx-deployment=nginx:1.14
deployment.extensions/nginx-deployment image updated
5、回滾(kubectl rollout命令)
#查看版本歷史
[root@localhost ~]# kubectl rollout history deployment/nginx-deployment
deployment.extensions/nginx-deployment
REVISION CHANGE-CAUSE
1 <none>
2 <none>
#執行回滾
[root@localhost ~]# kubectl rollout undo deployment/nginx-deployment
deployment.extensions/nginx-deployment
- 再次使用瀏覽器訪問即可查看到版本的信息變更爲1.17版本
6、刪除(kubectl delete命令)
[root@localhost ~]# kubectl delete deploy/nginx-deployment
deployment.extensions "nginx-deployment" deleted
[root@localhost ~]# kubectl get pods
No resources found.
#此時雖然刪除了pod資源,但是發佈pod資源的時候創建的nginx-service資源還在
[root@localhost ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 10d
nginx-service NodePort 10.0.0.202 <none> 80:30916/TCP 38m
#刪除nginx-service
[root@localhost ~]# kubectl delete svc/nginx-service
service "nginx-service" deleted
[root@localhost ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 10d