资源管理方式

  • 命令式对象管理:直接使用命令去操作kubernetes资源
[root@master ~]# kubectl run nginx-pod --image=nginx:1.17.1 --port=80
pod/nginx-pod created
//自主式pod
 
 
[root@master ~]# kubectl get pod,svc
NAME                        READY   STATUS              RESTARTS   AGE
pod/nginx-76d6c9b8c-77phw   1/1     Running             0          6m15s
pod/nginx-pod               0/1     ContainerCreating   0          14s
//正在拉取镜像
 
[root@master ~]# kubectl delete deployment nginx
deployment.apps "nginx" deleted
[root@master ~]# kubectl get pod
NAME        READY   STATUS    RESTARTS   AGE
nginx-pod   1/1     Running   0          18m
//删除deployment类型的pod
  • 命令式对象配置:通过命令配置和配置文件去操作kubernetes资源
kubectl create/patch -f nginx-pod.yaml
  • 声明式对象配置:通过apply命令和配置文件去操作kubernetes资源
kubectl apply -f nginx-pod.yaml
类型操作对象适用环境优点缺点
命令式对象管理对象测试简单只能操作活动对象,无法审计、跟踪
命令式对象配置文件开发可以审计、跟踪项目大时,配置文件多,操作麻烦
声明式对象配置目录开发支持目录操作意外情况下难以调试

命令式对象管理

kubectl命令

kubectl是kubernetes集群的命令行工具,通过它能够对集群本身进行管理,并能够在集群上进行容器化应用的安装部署。kubectl命令的语法如下

kubectl [command] [type] [name] [flags]

comand:指定要对资源执行的操作,例如create、get、delete

type:指定资源类型,比如deployment、pod、service

name:指定资源的名称,名称大小写敏感

flags:指定额外的可选参数

kubernetes允许对资源进行多种操作,可以通过--help查看详细的操作命令

kubectl --help

经常使用的操作有下面这些:

命令分类命令翻译命令作用
基本命令create创建创建一个资源
edit编辑编辑一个资源
get获取获取一个资源
patch更新更新一个资源
delete删除删除一个资源
explain解释展示资源文档
运行和调试run运行在集群中运行一个指定的镜像
expose暴露暴露资源为Service
describe描述显示资源内部信息
logs日志输出容器在 pod 中的日志输出容器在 pod 中的日志
attach缠绕进入运行中的容器进入运行中的容器
exec执行容器中的一个命令执行容器中的一个命令
cp复制在Pod内外复制文件
rollout首次展示管理资源的发布
scale规模扩(缩)容Pod的数量
autoscale自动调整自动调整Pod的数量
高级命令applyrc通过文件对资源进行配置
label标签更新资源上的标签
其他命令cluster-info集群信息显示集群信息
version版本显示当前Server和Client的版本

列举一些命令进行演示

run

//启动一个pod
[root@master ~]# kubectl run nginx --image nginx
podinx created
[root@master ~]# kubectl get pods
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          2m52s
[root@master ~]# kubectl get pods -o wide
NAME    READY   STATUS    RESTARTS   AGE     IP            NODE                NOMINATED NODE   READINESS GATES
nginx   1/1     Running   0          3m43s   10.244.2.11   node2.example.com   <none>           <none>

//暴露端口号
[root@master ~]# kubectl run nginx --image nginx --port 80
podinx created


get

获取node节点、pod、service信息

# 查看创建的pod
[root@master ~]# kubectl get pods
NAME                    READY   STATUS    RESTARTS   AGE
web1-5756d98cd9-fh588   1/1     Running   0          2m1s

# 查看所有的service
[root@master ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        25h
my-dep       NodePort    10.97.89.122     <none>        80:32213/TCP   5m17s
nginx        ClusterIP   10.106.246.141   <none>        8000/TCP       15m

查看多个信息,用","隔开
[root@master ~]# kubectl get pod,svc
NAME                          READY   STATUS    RESTARTS   AGE
pod/my-dep-6fcbf46469-drs28   1/1     Running   0          36m
pod/nginx-85b98978db-9c9xs    1/1     Running   0          42m

查看指定类型的pod
[root@master ~]# kubectl get deployment
NAME     READY   UP-TO-DATE   AVAILABLE   AGE
my-dep   1/1     1            1           29m

查看名称空间
[root@master ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   25h
kube-node-lease   Active   25h
kube-public       Active   25h
kube-system       Active   25h

查看更详细的信息
[root@master ~]# kubectl get pod -o wide
NAME                      READY   STATUS    RESTARTS   AGE   IP           NODE                NOMINATED NODE   READINESS GATES
my-dep-6f466859d9-drs28   1/1     Running   0          30m   10.244.2.7   node2.example.com   <none>           <none>
nginx-878b94fcbb-9c9xs    1/1     Running   0          36m   10.244.1.4   node1.example.com   <none>           <none>

expose

暴露端口号,–target-port表示暴露目标端口号

暴露nginx容器里面的80端口到集群的8000端口
[root@master ~]# kubectl expose deployment nginx --port=80 --target-port=8000
service/nginx exposed
# 查看
[root@master ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP    25h
nginx        ClusterIP   10.106.246.141   <none>        8000/TCP   29s
# 访问
[root@master ~]# curl 10.106.246.141:8000
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
........

explain

查看参数信息

[root@master ~]# kubectl explain pod
KIND:     Pod
VERSION:  v1

DESCRIPTION:
     Pod is a collection of containers that can run on a host. This resource is
     created by clients and scheduled onto hosts.

FIELDS:
   apiVersion   <string>
     APIVersion defines the versioned schema of this representation of an
     object. Servers should convert recognized schemas to the latest internal
     value, and may reject unrecognized values. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources

   kind <string>
     Kind is a string value representing the REST resource this object
     represents. Servers may infer this from the endpoint the client submits
     requests to. Cannot be updated. In CamelCase. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds

   metadata     <Object>
     Standard object's metadata. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata

   spec <Object>
     Specification of the desired behavior of the pod. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

   status       <Object>
     Most recently observed status of the pod. This data may not be up to date.
     Populated by the system. Read-only. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

autoscale

自动扩展,指定一个范围,根据访问量自动增加或删除

//自动扩展deploy类型的pod最少两个,最多五个
[root@master ~]# kubectl autoscale deploy nginx --min 2 --max=5 
horizontalpodautoscaler.autoscaling/nginx autoscaled

[root@master ~]# kubectl get pod
NAME                     READY   STATUS    RESTARTS   AGE
nginx-6799fc88d8-s8mjh   1/1     Running   0          42m
nginx-6799fc88d8-t54s5   1/1     Running   0          12m

//扩展pod数量为6个
[root@master ~]# kubectl scale deploy nginx --replicas 6
deployment.apps/nginx scaled  

//因为设置了最大的扩展数为5个,而我们扩展了6个超过了最大的扩展数,那么他会自动删除一个。
[root@master ~]# kubectl get pod  
NAME                     READY   STATUS        RESTARTS   AGE
nginx-6799fc88d8-d7rfs   0/1     Terminating   0          59s
nginx-6799fc88d8-f7nt7   1/1     Running       0          59s
nginx-6799fc88d8-gbp6l   1/1     Running       0          59s
nginx-6799fc88d8-l89vq   1/1     Running       0          59s
nginx-6799fc88d8-s8mjh   1/1     Running       0          44m
nginx-6799fc88d8-t54s5   1/1     Running       0          15m

cluster-info

显示集群信息


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

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

logs

查看日志

//查看service下的nginx的日志信息
[root@master ~]# kubectl logs service/nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2022/09/07 04:30:25 [notice] 1#1: using the "epoll" event method
2022/09/07 04:30:25 [notice] 1#1: nginx/1.23.1
2022/09/07 04:30:25 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2022/09/07 04:30:25 [notice] 1#1: OS: Linux 4.18.0-348.el8.x86_64
2022/09/07 04:30:25 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/09/07 04:30:25 [notice] 1#1: start worker processes
2022/09/07 04:30:25 [notice] 1#1: start worker process 31
10.244.0.0 - - [07/Sep/2022:04:33:15 +0000] "GET / HTTP/1.1" 200 615 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36" "-"
2022/09/07 04:33:15 [error] 31#31: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 10.244.0.0, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.80.128:30092", referrer: "http://192.168.80.128:30092/"
10.244.0.0 - - [07/Sep/2022:04:33:15 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://192.168.80.128:30092/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36" "-"
10.244.0.0 - - [07/Sep/2022:04:35:19 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.61.1" "-"

exec

进到容器内执行一个命令

[root@master ~]# kubectl get pod
NAME                    READY   STATUS    RESTARTS   AGE
nginx-76d6c9b8c-dhfrs   1/1     Running   0          47m
[root@master ~]# kubectl exec pods/nginx-76d6c9b8c-dhfrs -- ls
bin
boot
dev
docker-entrypoint.d
docker-entrypoint.sh
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var


进到容器执行命令
[root@master ~]# kubectl exec -it pods/nginx-76d6c9b8c-dhfrs -- /bin/sh
# ls
bin   docker-entrypoint.d   home   media  proc  sbin  tmp
boot  docker-entrypoint.sh  lib    mnt    root  srv   usr
dev   etc                   lib64  opt    run   sys   var
# exit

cp

本机和容器之间传输文件或目录

[root@master ~]# kubectl cp anaconda-ks.cfg nginx-76d6c9b8c-dhfrs:/tmp
[root@master ~]# kubectl exec pod/nginx-76d6c9b8c-dhfrs -- ls -l /tmp
total 4
-rw------- 1 root root 1175 Sep  7 05:13 anaconda-ks.cfg

version

[root@master ~]# kubectl version
WARNING: This version information is deprecated and will be replaced with the output from kubectl version --short.  Use --output=yaml|json to get the full version.
Client Version: version.Info{Major:"1", Minor:"25", GitVersion:"v1.25.0", GitCommit:"a866cbe2e5bbaa01cfd5e969aa3e033f3282a8a2", GitTreeState:"clean", BuildDate:"2022-08-23T17:44:59Z", GoVersion:"go1.19", Compiler:"gc", Platform:"linux/amd64"}
Kustomize Version: v4.5.7
Server Version: version.Info{Major:"1", Minor:"25", GitVersion:"v1.25.0", GitCommit:"a866cbe2e5bbaa01cfd5e969aa3e033f3282a8a2", GitTreeState:"clean", BuildDate:"2022-08-23T17:38:15Z", GoVersion:"go1.19", Compiler:"gc", Platform:"linux/amd64"}

Logo

开源、云原生的融合云平台

更多推荐