Kubernetes常用基础命令
- 1. 查看帮助文档
- 2. 类型介绍
- 3. 常用基础命令
- 3.1 create
- 3.2 replicas
- 3.3 port
-
1. 查看帮助文档
[root@master ~]
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 Set a new size for a Deployment, ReplicaSet or Replication Controller
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
debug Create debugging sessions for troubleshooting workloads and nodes
Advanced Commands:
diff Diff live version against would-be applied version
apply 通过文件名或标准输入流(stdin)对资源进行配置
patch Update field(s) of a resource
replace 通过 filename 或者 stdin替换一个资源
wait Experimental: Wait for a specific condition on one or many resources.
kustomize Build a kustomization target from a directory or a remote url.
Settings Commands:
label 更新在这个资源上的 labels
annotate 更新一个资源的注解
completion Output shell completion code for the specified shell (bash or zsh)
Other Commands:
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).
[root@master ~]
2. 类型介绍
- Pod:K8s最小部署单元,一组容器的集合
- Deployment:最常见的控制器,用于更高级别部署和管理Pod
- Service:为一组Pod提供负载均衡,对外提供一访问入口,可使用缩写 “svc”
- Label:标签,附加到某个资源上,用于关联对象、查询和筛
- Namespaces:命令空间,将对象逻辑上隔离,也利于权限控制
3. 常用基础命令
![Kubernetes常用基础命令_第1张图片](http://img.e-com-net.com/image/info8/8c45a50df9744227839dbc281958bc45.jpg)
3.1 create
[root@master ~]
deployment.apps/test created
[root@master ~]
NAME READY STATUS RESTARTS AGE
nginx-6799fc88d8-6s4hh 1/1 Terminating 0 3h32m
nginx-6799fc88d8-7nr4l 1/1 Running 0 28m
test-659fb5c67-xs7h2 0/1 RunContainerError 0 9s
[root@master ~]
3.2 replicas
[root@master ~]
deployment.apps/myapp created
[root@master ~]
NAME READY STATUS RESTARTS AGE
myapp-6d8d776547-2m9lk 0/1 ContainerCreating 0 3s
myapp-6d8d776547-v9lcd 0/1 ContainerCreating 0 3s
myapp-6d8d776547-x2drr 0/1 ContainerCreating 0 3s
nginx-6799fc88d8-6s4hh 1/1 Terminating 0 3h34m
nginx-6799fc88d8-7nr4l 1/1 Running 0 30m
test-659fb5c67-xs7h2 0/1 CrashLoopBackOff 3 2m4s
3.3 port
[root@master ~]
deployment.apps/test1 created
[root@master ~]
NAME READY STATUS RESTARTS AGE
myapp-6d8d776547-2m9lk 1/1 Running 0 72s
myapp-6d8d776547-v9lcd 1/1 Running 0 72s
myapp-6d8d776547-x2drr 1/1 Running 0 72s
nginx-6799fc88d8-6s4hh 1/1 Terminating 0 3h35m
nginx-6799fc88d8-7nr4l 1/1 Running 0 31m
test-659fb5c67-xs7h2 0/1 CrashLoopBackOff 4 3m13s
test1-7cbbd465d8-qgx69 0/1 ContainerCreating 0 8s
//查看详细信息
[root@master ~]
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp-6d8d776547-2m9lk 1/1 Running 0 97s 10.244.1.6 node1.example.com <none> <none>
myapp-6d8d776547-v9lcd 1/1 Running 0 97s 10.244.1.7 node1.example.com <none> <none>
myapp-6d8d776547-x2drr 1/1 Running 0 97s 10.244.1.5 node1.example.com <none> <none>
nginx-6799fc88d8-6s4hh 1/1 Terminating 0 3h35m 10.244.2.2 node2.example.com <none> <none>
nginx-6799fc88d8-7nr4l 1/1 Running 0 32m 10.244.1.3 node1.example.com <none> <none>
test-659fb5c67-xs7h2 0/1 CrashLoopBackOff 4 3m38s 10.244.1.4 node1.example.com <none> <none>
test1-7cbbd465d8-qgx69 1/1 Running 0 33s 10.244.1.8 node1.example.com <none> <none>
[root@master ~]
[root@master ~]
NAME READY UP-TO-DATE AVAILABLE AGE
myapp 3/3 3 3 2m16s
nginx 1/1 1 1 47h
test 0/1 1 0 4m17s
test1 1/1 1 1 72s
[root@master ~]
3.4 expose
//暴露8080端口,映射到容器里面的80端口
[root@master ~]
NAME READY UP-TO-DATE AVAILABLE AGE
myapp 3/3 3 3 2m16s
nginx 1/1 1 1 47h
test 0/1 1 0 4m17s
test1 1/1 1 1 72s
[root@master ~]
[root@master ~]
NAME READY UP-TO-DATE AVAILABLE AGE
myapp 3/3 3 3 2m57s
nginx 1/1 1 1 47h
test 0/1 1 0 4m58s
test1 1/1 1 1 113s
[root@master ~]
service/myapp exposed
[root@master ~]
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 47h
myapp ClusterIP 10.102.192.74 <none> 8080/TCP 10s
nginx NodePort 10.104.160.232 <none> 80:31085/TCP 47h
[root@master ~]
curl: (7) Failed to connect to 10.102.192.74 port 80: 拒绝连接
[root@master ~]
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@master ~]
//暴露nginx端口
[root@master ~]
Error from server (AlreadyExists): services "nginx" already exists
[root@master ~]
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 47h
myapp ClusterIP 10.102.192.74 <none> 8080/TCP 2m52s
nginx NodePort 10.104.160.232 <none> 80:31085/TCP 47h
[root@master ~]
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@master ~]
![Kubernetes常用基础命令_第2张图片](http://img.e-com-net.com/image/info8/b5fd775fcf7f41b1a14373bdf12a0460.jpg)
3.5 get
[root@master ~]
NAME STATUS AGE
default Active 2d
kube-node-lease Active 2d
kube-public Active 2d
kube-system Active 2d
[root@master ~]
NAME STATUS ROLES AGE VERSION
master.example.com Ready control-plane,master 2d v1.20.0
node1.example.com Ready <none> 3h22m v1.20.0
node2.example.com Ready <none> 47h v1.20.0
[root@master ~]
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d
myapp ClusterIP 10.102.192.74 <none> 8080/TCP 30m
nginx NodePort 10.104.160.232 <none> 80:31085/TCP 47h
[root@master ~]
NAME READY STATUS RESTARTS AGE
myapp-6d8d776547-2m9lk 1/1 Running 0 33m
myapp-6d8d776547-v9lcd 1/1 Running 0 33m
myapp-6d8d776547-x2drr 1/1 Running 0 33m
nginx-6799fc88d8-7nr4l 1/1 Running 0 64m
test-659fb5c67-xs7h2 0/1 CrashLoopBackOff 11 35m
test1-7cbbd465d8-qgx69 1/1 Running 0 32m
[root@master ~]