Kubectl是管理k8s集群的命令行工具,通過生成的json格式傳遞給apiserver進(jìn)行創(chuàng)建、查看、管理的操作
10年積累的成都網(wǎng)站制作、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有庫(kù)爾勒免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
注意:此處需要用到我們之前部署的K8s多節(jié)點(diǎn)的部署環(huán)境,如果還未部署的可以參考我的上篇文章:https://blog.csdn.net/JarryZho/article/details/104212822
`查看幫助命令`
[root@master1 ~]# 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 并暴露它作為一個(gè) 新的
Kubernetes Service
run 在集群中運(yùn)行一個(gè)指定的鏡像
set 為 objects 設(shè)置一個(gè)指定的特征
Basic Commands (Intermediate):
explain 查看資源的文檔
get 顯示一個(gè)或更多 resources
edit 在服務(wù)器上編輯一個(gè)資源
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 設(shè)置一個(gè)新的副本數(shù)量
autoscale 自動(dòng)調(diào)整一個(gè) Deployment, ReplicaSet, 或者 ReplicationController 的副本數(shù)量
Cluster Management Commands:
certificate 修改 certificate 資源.
cluster-info 顯示集群信息
top Display Resource (CPU/Memory/Storage) usage.
cordon 標(biāo)記 node 為 unschedulable
uncordon 標(biāo)記 node 為 schedulable
drain Drain node in preparation for maintenance
taint 更新一個(gè)或者多個(gè) node 上的 taints
Troubleshooting and Debugging Commands:
describe 顯示一個(gè)指定 resource 或者 group 的 resources 詳情
logs 輸出容器在 pod 中的日志
attach Attach 到一個(gè)運(yùn)行中的 container
exec 在一個(gè) container 中執(zhí)行一個(gè)命令
port-forward Forward one or more local ports to a pod
proxy 運(yùn)行一個(gè) proxy 到 Kubernetes API server
cp 復(fù)制 files 和 directories 到 containers 和從容器中復(fù)制 files 和 directories.
auth Inspect authorization
Advanced Commands:
apply 通過文件名或標(biāo)準(zhǔn)輸入流(stdin)對(duì)資源進(jìn)行配置
patch 使用 strategic merge patch 更新一個(gè)資源的 field(s)
replace 通過 filename 或者 stdin替換一個(gè)資源
wait Experimental: Wait for a specific condition on one or many resources.
convert 在不同的 API versions 轉(zhuǎn)換配置文件
Settings Commands:
label 更新在這個(gè)資源上的 labels
annotate 更新一個(gè)資源的注解
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 的版本信息
其步驟如下:創(chuàng)建一>發(fā)布一>更新一>回滾一>刪除
#語(yǔ)法:kubectl run NAME --image=image [--env="key=value"參數(shù)] [--port=port端口] [--replicas=replicas副本集] [--dry-run=bool狀態(tài)] [--overrides=inline-json] [--command命令] -- [COMMAND] [args...] [options]`
`示例:`
[root@master1 k8s]# 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
[root@master1 k8s]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-dbddb74b8-7hdfj 1/1 Running 0 4d18h
`nginx-deployment-5477945587-2dljt 1/1 Running 0 68s`
`nginx-deployment-5477945587-tt8vx 1/1 Running 0 68s`
`nginx-deployment-5477945587-wsb69 1/1 Running 0 68s`
#最后三個(gè)就是我們新創(chuàng)建的副本集
#語(yǔ)法:kubectl expose (-f FILENAME | TYPE NAME) [--port=port群集之間內(nèi)部通信的端口] [--protocol=TCP|UDP|SCTP] [--target-port對(duì)外暴露的端口=number-or-name] [--name=name指定名稱] [--external-ip=external-ip-of-service] [--type=type指定類型] [options]
`示例:`
[root@master1 k8s]# kubectl expose deployment nginx-deployment --port=80 --target-port=80 --name=nginx-deployment-service --type=NodePort
service/nginx-deployment-service exposed
`查看發(fā)布`
[root@master1 k8s]# kubectl get pods,svc #此處svc位service服務(wù)組件的縮寫
NAME READY STATUS RESTARTS AGE
pod/nginx-dbddb74b8-7hdfj 1/1 Running 0 4d19h
pod/nginx-deployment-5477945587-2dljt 1/1 Running 0 13m
pod/nginx-deployment-5477945587-tt8vx 1/1 Running 0 13m
pod/nginx-deployment-5477945587-wsb69 1/1 Running 0 13m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.0.0.1 443/TCP 6d19h
service/nginx-service NodePort 10.0.0.242 80:40422/TCP 111s
#此時(shí)對(duì)外暴露的端口為40422
`查看資源對(duì)象簡(jiǎn)寫`
[root@master1 k8s]# kubectl api-resources
NAME SHORTNAMES APIGROUP NAMESPACED
componentstatuses cs false
configmaps cm true
endpoints ep true
events ev true
limitranges limits true
namespaces ns false
nodes no false
persistentvolumeclaims pvc true
persistentvolumes pv false
pods po true
replicationcontrollers rc true
resourcequotas quota true
serviceaccounts sa true
services svc true
customresourcedefinitions crd,crds apiextensions.k8s.io false
daemonsets ds apps true
deployments deploy apps true
replicasets rs apps true
statefulsets sts apps true
horizontalpodautoscalers hpa autoscaling true
cronjobs cj batch true
certificatesigningrequests csr certificates.k8s.io false
events ev events.k8s.io true
daemonsets ds extensions true
deployments deploy extensions true
ingresses ing extensions true
networkpolicies netpol extensions true
podsecuritypolicies psp extensions false
replicasets rs extensions true
networkpolicies netpol networking.k8s.io true
poddisruptionbudgets pdb policy true
podsecuritypolicies psp policy false
priorityclasses pc scheduling.k8s.io false
storageclasses sc storage.k8s.io false
`查看關(guān)聯(lián)后端的節(jié)點(diǎn)`
[root@master1 k8s]# kubectl get endpoints
NAME ENDPOINTS AGE
kubernetes 192.168.18.128:6443,192.168.18.132:6443 7d4h
nginx-deployment-service 172.17.32.4:80,172.17.40.2:80,172.17.40.3:80 17s
`網(wǎng)絡(luò)狀態(tài)詳細(xì)信息`
[root@master1 ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE
nginx-dbddb74b8-7hdfj 1/1 Running 0 5d5h 172.17.32.2 192.168.18.148
nginx-deployment-5477945587-2dljt 1/1 Running 0 10h 172.17.40.3 192.168.18.145
nginx-deployment-5477945587-tt8vx 1/1 Running 0 10h 172.17.40.2 192.168.18.145
nginx-deployment-5477945587-wsb69 1/1 Running 0 10h 172.17.32.4 192.168.18.148
`服務(wù)暴露的端口`
[root@master1 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 443/TCP 7d5h
nginx-deployment-service NodePort 10.0.0.50 80:48330/TCP 70m
`在node1操作,查看負(fù)載均衡端口48330`
`k8s里kube-proxy支持三種模式,在v1.8之前我們使用的是iptables以及userspace兩種模式,在k8s1.8之后引入了ipvs模式`
[root@node1 ~]# yum install ipvsadm -y
[root@node1 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.18.148:48330 rr
-> 172.17.32.4:80 Masq 1 0 0
-> 172.17.40.2:80 Masq 1 0 0
-> 172.17.40.3:80 Masq 1 0 0
#對(duì)外提供端口48330,調(diào)度算法為rr輪詢
`在node2操作 同樣安裝ipvsadmin工具查看`
[root@node2 ~]# yum install ipvsadm -y
[root@node2 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.18.145:48330 rr
-> 172.17.32.4:80 Masq 1 0 0
-> 172.17.40.2:80 Masq 1 0 0
-> 172.17.40.3:80 Masq 1 0 0
`在master1操作查看訪問日志(注意:如果訪問其他node無(wú)法訪問檢查proxy組件)`
[root@master1 ~]# kubectl get pods #此時(shí)會(huì)有三個(gè)副本集
NAME READY STATUS RESTARTS AGE
nginx-dbddb74b8-7hdfj 1/1 Running 0 5d5h
nginx-deployment-5477945587-2dljt 1/1 Running 0 10h
nginx-deployment-5477945587-tt8vx 1/1 Running 0 10h
nginx-deployment-5477945587-wsb69 1/1 Running 0 10h
`此時(shí)回頭查看訪問日志:`
[root@master1 ~]# kubectl logs nginx-deployment-5477945587-2dljt
172.17.40.1 - - [12/Feb/2020:11:44:46 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.131 Safari/537.36" "-"
172.17.40.1 - - [12/Feb/2020:11:44:46 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://192.168.18.145:48330/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.131 Safari/537.36" "-"
[root@master1 ~]# kubectl logs nginx-deployment-5477945587-tt8vx
[root@master1 ~]# kubectl logs nginx-deployment-5477945587-wsb69
172.17.32.1 - - [12/Feb/2020:11:47:07 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.131 Safari/537.36" "-"
172.17.32.1 - - [12/Feb/2020:11:47:07 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://192.168.18.148:48330/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.131 Safari/537.36" "-"
#此時(shí)nginx-deployment-5477945587-wsb69和nginx-deployment-5477945587-2dljt分別被訪問了nginx的主頁(yè),說(shuō)明業(yè)務(wù)已經(jīng)成功發(fā)布,而且對(duì)方用的是輪詢的方式訪問多個(gè)Pod資源
查看nginx版本步驟:F12打開開發(fā)者選項(xiàng)--》F5刷新訪問---》找到network---》點(diǎn)擊name---》找到headers頭部信息
[root@master1 ~]# kubectl set image deployment/nginx-deployment nginx-deployment=nginx:1.14
deployment.extensions/nginx-deployment image updated
`處于動(dòng)態(tài)監(jiān)聽狀態(tài),此時(shí)可以查看監(jiān)控狀態(tài)`
[root@master1 ~]# kubectl get pods -w
NAME READY STATUS RESTARTS AGE
nginx-6c94d899fd-8pf48 1/1 Running 0 3m54s
nginx-deployment-5477945587-2dljt 1/1 Running 0 10h
nginx-deployment-5477945587-tt8vx 1/1 Running 0 10h
nginx-deployment-5477945587-wsb69 1/1 Running 0 10h
nginx-deployment-8f66bcd89-jncdr 0/1 ContainerCreating 0 7s
#此時(shí)哦我們可以按Ctrl+c中斷監(jiān)聽,這樣更新速度快
`查看歷史版本`
[root@master1 ~]# kubectl rollout history deployment/nginx-deployment
deployment.extensions/nginx-deployment
REVISION CHANGE-CAUSE
1 #1.17版本
2 #1.14版本
`執(zhí)行回滾到上一次`
[root@master1 ~]# kubectl rollout undo deployment/nginx-deployment
deployment.extensions/nginx-deployment
`檢查回滾狀態(tài)`
[root@master1 ~]# kubectl rollout status deployment/nginx-deployment
Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
deployment "nginx-deployment" successfully rolled out
#最終顯示成功回滾
`查看deployment`
[root@master1 ~]# kubectl get deploy
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx-deployment 3 3 3 3 11h
[root@master1 ~]# kubectl delete deployment/nginx-deployment
deployment.extensions "nginx-deployment" deleted
[root@master1 ~]# kubectl get deploy
No resources found.
[root@master1 ~]# kubectl get pods
No resources found.
`刪除服務(wù)SVC`
[root@master1 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 443/TCP 7d6h
nginx-deployment-service NodePort 10.0.0.50 80:48330/TCP 142m
[root@master1 ~]# kubectl delete svc/nginx-deployment-service
service "nginx-deployment-service" deleted #此步驟為刪除步驟
[root@master1 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 443/TCP 7d7h
`查看具體資源的詳細(xì)信息`
[root@master1 ~]# 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
[root@master1 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-6c94d899fd-8pf48 1/1 Running 0 54m
nginx-deployment-5477945587-f5dsm 1/1 Running 0 15s
nginx-deployment-5477945587-hmgd2 1/1 Running 0 15s
nginx-deployment-5477945587-pl2hn 1/1 Running 0 15s
[root@master1 ~]# kubectl describe pod nginx-deployment-5477945587-f5dsm
Name: nginx-deployment-5477945587-f5dsm
Namespace: default
Priority: 0
PriorityClassName:
Node: 192.168.18.145/192.168.18.145
Start Time: Wed, 12 Feb 2020 21:04:40 +0800
Labels: pod-template-hash=5477945587
run=nginx-deployment
Annotations:
Status: Running
IP: 172.17.40.2
Controlled By: ReplicaSet/nginx-deployment-5477945587
Containers:
nginx-deployment:
Container ID: docker://670cb7230f200279b2accb344766e621ab97d279c8585cc27bd4e519dac9e677
Image: nginx
Image ID: docker-pullable://nginx@sha256:ad5552c786f128e389a0263104ae39f3d3c7895579d45ae716f528185b36bc6f
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Wed, 12 Feb 2020 21:04:44 +0800
Ready: True
Restart Count: 0
Environment:
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-pbr9p (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-pbr9p:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-pbr9p
Optional: false
QoS Class: BestEffort
Node-Selectors:
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 19m default-scheduler Successfully assigned default/nginx-deployment-5477945587-f5dsm to 192.168.18.145
Normal Pulling 19m kubelet, 192.168.18.145 pulling image "nginx"
Normal Pulled 19m kubelet, 192.168.18.145 Successfully pulled image "nginx"
Normal Created 19m kubelet, 192.168.18.145 Created container
Normal Started 19m kubelet, 192.168.18.145 Started container
`查看deployment資源`
[root@master1 ~]# kubectl describe deployment/nginx-deployment
Name: nginx-deployment
Namespace: default
CreationTimestamp: Wed, 12 Feb 2020 21:04:40 +0800
Labels: run=nginx-deployment
Annotations: deployment.kubernetes.io/revision: 1
Selector: run=nginx-deployment
Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: run=nginx-deployment
Containers:
nginx-deployment:
Image: nginx
Port: 80/TCP
Host Port: 0/TCP
Environment:
Mounts:
Volumes:
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets:
NewReplicaSet: nginx-deployment-5477945587 (3/3 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 21m deployment-controller Scaled up replica set nginx-deployment-5477945587 to 3
`進(jìn)入pod`
[root@master1 ~]# kubectl exec -it nginx-deployment-5477945587-f5dsm bash
root@nginx-deployment-5477945587-f5dsm:/#
`1:狀態(tài)為NotReady`
[root@localhost bin]# kubectl get node
NAME STATUS ROLES AGE VERSION
192.168.195.150 NotReady 6d19h v1.12.3
192.168.195.151 NotReady 6d18h v1.12.3
#原因: node 節(jié)點(diǎn)無(wú)法連接apiserver
1)單節(jié)點(diǎn):master節(jié)點(diǎn) apiserver服務(wù)是否正常運(yùn)行
systemctl restart kube-apiserver 失敗
cat /var/log/messages 日志(第一次部署:檢查證書)
2)多節(jié)點(diǎn):如果api-server運(yùn)行正常
檢查:負(fù)載均衡中的VIP,檢查nginx調(diào)度 四層轉(zhuǎn)發(fā)模塊配置
`2:kubectl get pods 查詢podes資源狀態(tài)`
NAME READY STATUS RESTARTS AGE
nginx-7697996758-jg47q 0/1 Pending 0 93s
nginx-7697996758-k967k 0/1 Pending 0 93s
nginx-7697996758-p5n8m 0/1 Pending 0 93s
無(wú)法配置資源到node節(jié)點(diǎn)中進(jìn)行創(chuàng)建(處于pending狀態(tài))
檢查node節(jié)點(diǎn)中kubelet服務(wù)組件,kubeconfig配置
`3:客戶無(wú)法訪問pods發(fā)布的服務(wù)`
ipvsadm -L -n 負(fù)載均衡 對(duì)外提供的端口44888
#正常情況每個(gè)node應(yīng)該是以下狀態(tài):
TCP 192.168.195.151:44888 rr
-> 172.17.68.2:80 Masq 1 0 0
-> 172.17.68.3:80 Masq 1 0 0
-> 172.17.86.2:80 Masq 1 0 0
#原因:kube-proxy組件服務(wù)