k8s中kubectl命令的使用
kubernetes允许对资源进行多种操作,可以通过--help查看详细的操作命令。暴露端口号,–target-port表示暴露目标端口号。自动扩展,指定一个范围,根据访问量自动增加或删除。获取node节点、pod、service信息。本机和容器之间传输文件或目录。进到容器内执行一个命令。列举一些命令进行演示。
·
资源管理方式
- 命令式对象管理:直接使用命令去操作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的数量 | |
高级命令 | apply | rc | 通过文件对资源进行配置 |
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"}
更多推荐
已为社区贡献1条内容
所有评论(0)