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 --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 443/TCP 10d
service/nginx-service NodePort 10.0.0.202 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
nginx-deployment-5477945587-rfn7d 1/1 Running 0 45m 172.17.23.4 192.168.7.103
nginx-deployment-5477945587-tj89b 1/1 Running 0 45m 172.17.23.3 192.168.7.103
#查看关联后的节点
[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的访问日志
[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命令)
[root@localhost ~]# kubectl set image deployment/nginx-deployment nginx-deployment=nginx:1.14
deployment.extensions/nginx-deployment image updated
#查看版本历史
[root@localhost ~]# kubectl rollout history deployment/nginx-deployment
deployment.extensions/nginx-deployment
REVISION CHANGE-CAUSE
1
2
#执行回滚
[root@localhost ~]# kubectl rollout undo deployment/nginx-deployment
deployment.extensions/nginx-deployment
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 443/TCP 10d
nginx-service NodePort 10.0.0.202 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 443/TCP 10d