Kubernetes常用基础命令

本文介绍了Kubernetes常用基础命令,包括创建、暴露、获取、删除等操作,以及run、edit、explain等高级命令的使用。通过实例演示了命令组合使用的场景,如自动扩展、端口映射和访问web页面。
摘要由CSDN通过智能技术生成

一、如何查看帮助文档

// 查看 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 more local 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 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 的版本信息

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 ~]# 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
test                     1/1     Running   0          54m

# deployment
[root@master ~]# kubectl get deployments
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   1/1     1            1           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
3.13 port:指定端口
[root@master ~]# kubectl create deployment test --image nginx --port 80
deployment.apps/test created


[root@master ~]# kubectl get pods
NAME                     READY   STATUS             RESTARTS      AGE
b2-866876dc6f-xznl9      0/1     CrashLoopBackOff   5 (45s ago)   4m21s
myapp-d56b9c6b9-vzcq2    1/1     Running            0             3m33s
myapp-d56b9c6b9-wfh6z    1/1     Running            0             3m33s
myapp-d56b9c6b9-wt928    1/1     Running            0             3m33s
nginx-85b98978db-2dj8t   1/1     Running            1 (18h ago)   19h
test-7c68854699-bph9c    1/1     Running            0             23s

// 查看详细信息
[root@master ~]# kubectl get pods -o wide
NAME                     READY   STATUS             RESTARTS      AGE     IP           NODE                NOMINATED NODE   READINESS GATES
b2-866876dc6f-xznl9      0/1     CrashLoopBackOff   5 (53s ago)   4m29s   10.244.2.4   node2.example.com   <none>           <none>
myapp-d56b9c6b9-vzcq2    1/1     Running            0             3m41s   10.244.1.7   node1.example.com   <none>           <none>
myapp-d56b9c6b9-wfh6z    1/1     Running            0             3m41s   10.244.1.6   node1.example.com   <none>           <none>
myapp-d56b9c6b9-wt928    1/1     Running            0             3m41s   10.244.2.5   node2.example.com   <none>           <none>
nginx-85b98978db-2dj8t   1/1     Running            1 (18h ago)   19h     10.244.2.3   node2.example.com   <none>           <none>
test-7c68854699-bph9c    1/1     Running            0             31s     10.244.1.8   node1.example.com   <none>           <none>
[root@master ~]# kubectl get deployment
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
b2      0/1     1            0           7m6s
myapp   3/3     3            3           6m18s
nginx   1/1     1            1           19h
test    1/1     1            1           3m8s

3.2 expose:暴露

3.21 映射端口
// 暴露8080端口,映射到容器里面的80端口
[root@master ~]# kubectl get deployment
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
b2      0/1     1            0           7m6s
myapp   3/3     3            3           6m18s
nginx   1/1     1            1           19h

test    1/1     1            1           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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值