Kubectl是管理k8s集群的命令行工具,通过生成的json格式传递给apiserver进行创建、查看、管理的操作
注意:此处需要用到我们之前部署的K8s多节点的部署环境,如果还未部署的可以参考我的上篇文章: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 并暴露它作为一个 新的
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 的版本信息
接下来我们以项目的形式讲其中的命令
其步骤如下:创建一>发布一>更新一>回滚一>删除
1.创建nginx
#语法:kubectl run NAME --image=image [--env="key=value"参数] [--port=port端口] [--replicas=replicas副本集] [--dry-run=bool状态] [--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`
#最后三个就是我们新创建的副本集
2.发布nginx service提供负载均衡的功能
#语法:kubectl expose (-f FILENAME | TYPE NAME) [--port=port群集之间内部通信的端口] [--protocol=TCP|UDP|SCTP] [--target-port对外暴露的端口=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
`查看发布`
[root@master1 k8s]# kubectl get pods,svc #此处svc位service服务组件的缩写
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