Kubernetes common basic commands

1. View the help documentation

[root@master ~]# 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         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 <command> --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. Type introduction

  • Pod: The smallest deployment unit of K8s, a collection of a set of containers
  • Deployment: The most common controller for higher-level deployment and management of Pods
  • Service: Provides load balancing for a group of Pods and provides an access portal to the outside world. The abbreviation "svc" can be used
  • Label: label, attached to a resource, used to associate objects, query and filter
  • Namespaces: Command space, which logically isolates objects and is also conducive to permission control

3. Common basic commands

insert image description here

3.1 create

[root@master ~]# kubectl create deployment test --image busybox -- slepp 6000
deployment.apps/test created
[root@master ~]# kubectl get pods
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 ~]# kubectl create deployment myapp --image nginx --replicas 3
deployment.apps/myapp created
[root@master ~]# kubectl get pods
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 ~]# kubectl create deployment test1 --image nginx --port 80
deployment.apps/test1 created
[root@master ~]# kubectl get pods
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 ~]# kubectl get pods -o wide
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 ~]# kubectl get deployment
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 ~]# kubectl get deployment
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 ~]# kubectl get deployment
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 ~]# 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
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 10.102.192.74 
curl: (7) Failed to connect to 10.102.192.74 port 80: 拒绝连接
[root@master ~]# curl 10.102.192.74: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>
</html>
[root@master ~]# 

//暴露nginx端口
[root@master ~]# kubectl expose deployment nginx --port 80 --type NodePort
Error from server (AlreadyExists): services "nginx" already exists
[root@master ~]#  kubectl get svc
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 ~]# curl 10.104.160.232 
<!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 ~]# 

insert image description here

3.5 get

[root@master ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   2d
kube-node-lease   Active   2d
kube-public       Active   2d
kube-system       Active   2d
[root@master ~]# kubectl get nodes
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 ~]# kubectl get svc
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 ~]# kubectl get pods
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 ~]# 

Guess you like

Origin blog.csdn.net/qq_49530779/article/details/122039471