// 查看 kubectl 能使用哪些命令
[root@master ~]# kubectl --help
kubectl controls the Kubernetes cluster manager.
Find more information at: https://k8s.io/docs/reference/kubectl/overview/
Basic Commands (Beginner):
create Create a resource from a file or from stdin
expose Take a replication controller, service, deployment or pod and expose it as a new Kubernetes service
run 在集群中运行一个指定的镜像
set 为 objects 设置一个指定的特征
Basic Commands (Intermediate):
explain Get documentation for a resource
get 显示一个或更多 resources
edit 在服务器上编辑一个资源
delete Delete resources by file names, 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, replica set, or replication controller
autoscale Auto-scale a deployment, replica set, stateful set, or replication controller
Cluster Management Commands:
certificate 修改 certificate 资源.
cluster-info Display cluster information
top Display resource (CPU/memory) 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 morelocal ports to a pod
proxy 运行一个 proxy 到 Kubernetes API server
cp Copy files and directories to and from containers
auth Inspect authorization
debug Create debugging sessions for troubleshooting workloads and nodes
Advanced Commands:
diff Diff the live version against a would-be applied version
apply Apply a configuration to a resource by file name or stdin
patch Update fields of a resource
replace Replace a resource by file name or stdin
wait Experimental: Wait for a specific condition on one or many resources
kustomize Build a kustomization target from a directory or URL.
Settings Commands:
label 更新在这个资源上的 labels
annotate 更新一个资源的注解
completion Output shell completion code for the specified shell (bash, zsh or fish)
Other Commands:
alpha Commands forfeaturesin 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 <command> --help"formore information about a given command.
Use "kubectl options"for a list of global command-line options (applies to all commands).
// 查看单独命令的使用方法
[root@master ~]# kubectl delete --help // 举个例子
二、类型介绍
Pod:K8s最小部署单元,一组容器的集合
Deployment:最常见的控制器,用于更高级别部署和管理Pod
Service:为一组Pod提供负载均衡,对外提供一访问入口,可使用缩写 “svc”
Label:标签,附加到某个资源上,用于关联对象、查询和筛
Namespaces:命令空间,将对象逻辑上隔离,也利于权限控制
# pods[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-85b98978db-mns5l 1/1 Running 0 10m
test1/1 Running 0 54m
# deployment[root@master ~]# kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 1/1 11 10m
# svc[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none>443/TCP 46h
nginx ClusterIP 10.108.208.249 <none>80/TCP 62m
三、常用基础命令
3.1 create:创建
3.11 sleep:延迟
[root@master ~]# kubectl create deployment b2 --image busybox -- slepp 6000
deployment.apps/b2 created
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
b2-866876dc6f-xznl9 0/1 ContainerCreating 0 9s
nginx-85b98978db-2dj8t 1/1 Running 1(18h ago) 19h
3.12 replicas:复制
[root@master ~]# kubectl create deployment myapp --image nginx --replicas 3
deployment.apps/myapp created
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
b2-866876dc6f-xznl9 0/1 CrashLoopBackOff 3(23s ago) 98s
myapp-d56b9c6b9-vzcq2 1/1 Running 0 50s
myapp-d56b9c6b9-wfh6z 1/1 Running 0 50s
myapp-d56b9c6b9-wt928 1/1 Running 0 50s
nginx-85b98978db-2dj8t 1/1 Running 1(18h ago) 19h
[root@master ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
b2 0/1 10 7m6s
myapp 3/3 33 6m18s
nginx 1/1 11 19h
test1/1 11 3m8s
3.2 expose:暴露
3.21 映射端口
// 暴露8080端口,映射到容器里面的80端口
[root@master ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
b2 0/1 10 7m6s
myapp 3/3 33 6m18s
nginx 1/1 11 19h
test1/1 11 3m8s
[root@master ~]# kubectl expose deployment myapp --port 8080 --target-port 80
service/myapp exposed
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
k8s ClusterIP 10.96.0.1 <none>443/TCP 23h
myapp ClusterIP 10.111.35.32 <none>8080/TCP 36s
nginx NodePort 10.99.134.85 <none>80:31766/TCP 19h
// 访问测试
[root@master ~]# curl http://10.111.35.32:8080<!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>