kubectl命令

kubernetes基本概念

Pod:k8s最小部署单元,一组容器的集合

Deployment:最常见的控制器,用于更高级别部署和管理Pod

Service:为一组Pod提供负载均衡,对外提供统一访问入口

Label:标签,附加到某个资源上,用于关联对象、查询和筛选

Namespaces∶命名空间。将对象逻辑上隔离,也利于权限控制

edit(编辑资源)

//运行一个pod类型的nginx,名字叫nginx,定义的标签是app=nginx
[root@master ~]# kubectl run nginx --image=nginx --labels="app=nginx"
pod/nginx created
[root@master ~]# kubectl get pod
NAME                     READY   STATUS    RESTARTS   AGE
nginx                    1/1     Running   0          44s
// 使用edit命令编辑
[root@master ~]# kubectl edit pods/nginx
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: "2021-12-20T09:26:53Z"
  labels:
    app: test  # 把原本app:nginx改为app:test
  name: nginx
  namespace: default
[root@master ~]# kubectl edit pods/nginx
pod/nginx edited
// 查看是否更改成功
[root@master ~]# kubectl describe pods/nginx
Name:         nginx
Namespace:    default
Priority:     0
Node:         node1.example.com/192.168.47.164
Start Time:   Mon, 20 Dec 2021 19:22:07 +0800
Labels:       app=test
Annotations:  <none>
Status:       Running
IP:           10.244.1.2

scale(动态扩展)

#创建一个deployment类型的nginx2的pod
[root@master ~]# kubectl create deployment nginx2 --image=nginx
deployment.apps/nginx2 created
[root@master ~]# kubectl get pods
NAME                      READY   STATUS    RESTARTS   AGE
nginx                     1/1     Running   0          7m38s
nginx2-85bf7b8976-n9vcl   1/1     Running   0          13s

# 使用scale扩展
[root@master ~]# kubectl scale --replicas 4 deployment/nginx2
deployment.apps/nginx2 scaled
# 扩展后查看多了几个相同类型的pod
[root@master ~]# kubectl get pods
NAME                      READY   STATUS              RESTARTS   AGE
nginx                     1/1     Running             0          8m55s
nginx2-85bf7b8976-bmcq4   0/1     ContainerCreating   0          11s
nginx2-85bf7b8976-n9vcl   1/1     Running             0          90s
nginx2-85bf7b8976-qzfhz   0/1     ContainerCreating   0          11s
nginx2-85bf7b8976-xk594   0/1     ContainerCreating   0          11s

# 如果只需要2个deployment类型的nginx的pod
[root@master ~]#  kubectl scale --replicas 2 deployment/nginx2
deployment.apps/nginx2 scaled
[root@master ~]#  kubectl get pods
NAME                      READY   STATUS    RESTARTS   AGE
nginx2-85bf7b8976-4dm2h   1/1     Running   0          39s
nginx2-85bf7b8976-fwj55   1/1     Running   0          49s

autoscale(自动扩展)

自动扩展,给定一个范围,自动根据业务的访问量增加或减少

[root@master ~]# kubectl autoscale --min=2 --max=7 --cpu-percent=60 deployment/nginx2
horizontalpodautoscaler.autoscaling/nginx2 autoscaled
[root@master ~]# kubectl get hpa
NAME     REFERENCE           TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
nginx    Deployment/nginx    <unknown>/80%   1         3         3          29m
nginx2   Deployment/nginx2   <unknown>/60%   2         7         0          10s

cluster-info(显示集群信息)

[root@master ~]# kubectl cluster-info
Kubernetes control plane is running at https://192.168.47.163:6443
KubeDNS is running at https://192.168.47.163:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

describe(显示指定pod的详细信息)

[root@master ~]# kubectl describe pod nginx
Name:         nginx-6799fc88d8-5kshn
Namespace:    default
Priority:     

你可能感兴趣的:(kubernetes,docker,容器)