1、概述:
Kubectl是管理k8s集群的命令行工具,通过生成的json格式传递给apiserver进行创建、查看、管理的操作。
[root@localhost 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 在集群中运行一个指定的镜像c+e
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 的版本信息
2、项目的生命周期:
创建–》发布–》更新–》回滚–》删除
1、创建资源:
[root@localhost bin]# kubectl run nginx-deployment --image=nginx --port=80 --replicas=3
1.1、监控创建过程
[root@localhost bin]# kubectl get pods -w
1.2、详细的ip信息
[root@master01 ~]# kubectl get pods,svc,rs -o wide (详细的ip信息)
1.3、查看所有信息:
[root@localhost bin]# kubectl get all
1.4、无状态化,控制器定义资源类型
[root@localhost bin]# kubectl get pods,deployment
1.5、副本集
[root@localhost bin]# kubectl get pods,deployment,replicaset(副本集)
1.4、删除delete两种方式!!!
[root@localhost bin]# kubectl delete deploy/nginx (命令创建的:kubectl delete pod +名字)
[root@localhost bin]# kubectl delete -f (创建时用到的文件名)
2:发布nginx service提供负载均衡的功能:
2.1、[root@localhost bin]# kubectl expose deployment nginx --port=80 --target-port=80 --name=nginx-service --type=NodePort
2.2、查看资源对象简写
[root@localhost bin]# kubectl api-resources
2.3、查看关联后端的节点
[root@localhost bin]# kubectl get endpoints
2.4、服务暴露的端口
[root@localhost bin]# kubectl get svc
//kubernetes里kube-proxy支持三种模式,在v1.8之前我们使用的是iptables 以及 userspace两种模式,在kubernetes 1.8之后引入了ipvs模式
[root@localhost ~]# yum install ipvsadm -y
[root@localhost ~]# ipvsadm -L -n
3、更新nginx 为1.14版本
3.1、获取修改模板、更新:
[root@localhost bin]# kubectl set image --help
[root@localhost bin]# kubectl set image deployment/nginx nginx=nginx:1.14
[root@localhost bin]# kubectl get pods -w 查看创建过程
4:回滚nginx:
[root@localhost bin]# kubectl rollout --help
4.1、查看历史版本
[root@localhost bin]# kubectl rollout history deployment/nginx
4.2、执行回滚
[root@localhost bin]# kubectl rollout undo deployment/nginx
4.3、检查回滚状态
[root@localhost bin]# kubectl rollout status deployment/nginx
5、//删除nginx
5.1、查看deployment
[root@localhost bin]# kubectl get deploy
5.2、删除
[root@localhost bin]# kubectl delete deployment/nginx
5.3、删除服务SVC
[root@localhost bin]# kubectl get svc
[root@localhost bin]# kubectl delete svc/nginx-service
5.4、查看具体资源的详细信息
获取资源码:
[root@localhost bin]# kubectl get pods
查看具体资源的详细信息
[root@localhost bin]# kubectl describe pod nginx-7697996758-75shs
查看deployment资源
[root@localhost bin]# kubectl describe deployment/nginx
进入pod
[root@localhost bin]# kubectl exec -it nginx-7697996758-75shs bash