kubectl 是Kubernetes管理员最常用的集群管理命令行工具(CLI)。kubectl 提供了大量的子命令,方便管理 Kubernetes 集群中的各种功能。其最基本的语法格式为 kubectl command <resource> [flags]
,其中各部分的简要说明如下:
- command:对资源执行相应操作的子命令,例如get、create、delete、run等;常用的核心子命令见下面。
- resource:要操作的资源类型和对象名称,格式为:
[TYPE] [NAME]
也可以是TYPE/NAME
,其中:- TYPE:要操作的资源类型,例如pods、services等;类型名称大小写敏感,但支持使用单数、复数或简写格式。可以省略。
- NAME:要操作的资源对象名称,大小写敏感;省略时,则表示指定TYPE的所有资源对象;同一类型的资源名称可于TYPE后同时给出多个,也可以直接使用TYPE/NAME的格式来为每个资源对象分别指定类型。
- flags:命令的标志,常用的标志(flags)见下面。
一、常用的标志(flags)
- -o <format>:指定输出格式,format常用的有wide、json、yaml,其中wide表示多一点的简约信息(还是一行)、json和yaml表示将详细信息以json和yaml的形式输出(多行);
- -f <filename>:根据配置文件或者配置文件的目录来操作,配置
- -n <namespace>:指定的命名空间;
- --all-namespaces:表示所有命令空间;
- --dry-run:表示仅用于测试命令是否正确,并不会真正操作;
二、基础命令
- 创建并运行一个或多个容器镜像。
kubectl run NAME --image=image [--env="key=value"] [--port=port] [--replicas=replicas] [--dry-run=bool] [--overrides=inline-json] [--command] -- [COMMAND] [args...]
- 查看基本信息
kubectl get <resource> [-o wide | json | yaml] [-n namespace]
其中的 resource 可以是 pod、sevice、deployment甚至是namespace等类型,也可以是特定的资源对象名称。
$ kubectl get namespace
NAME STATUS AGE
default Active 4d18h
istio-demo Active 4d4h
istio-system Active 27h
kube-node-lease Active 4d18h
kube-public Active 4d18h
kube-system Active 4d18h
$ kubectl get namespace/istio-demo
NAME STATUS AGE
istio-demo Active 4d4h
- 查看某个资源的详细信息
kubectl describe <resource> [-o wide | json | yaml] [-n namespace]
每个资源对象都有用户期望的状态(Spec)和现有的实际状态(Status)两种状态信息,get命令可以查看用户期望的状态,而describe命令可以查看某个资源的两种状态信息。
$ kubectl describe namespace/istio-demo
Name: istio-demo
Labels: istio-injection=enabled
kubernetes.io/metadata.name=istio-demo
Annotations: <none>
Status: Active
No resource quota.
No LimitRange resource.
- 打印容器中的日志信息
kubectl logs [-f] [-p] (POD | TYPE/NAME) [-c CONTAINER] [options]
$ kubectl logs pod/nginx-deployment-f7dcc7c6f-pwtfr
127.0.0.1 - - [20/Apr/2022:16:48:55 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36" "-"
2022/04/20 16:48:55 [error] 8#8: *2 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/"
127.0.0.1 - - [20/Apr/2022:16:48:55 +0000] "GET /favicon.ico HTTP/1.1" 404 571 "http://127.0.0.1:8080/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36" "-"
127.0.0.1 - - [20/Apr/2022:16:50:02 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36" "-"
- 在容器中执行命令
kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...] [options]
$ kubectl exec pod/nginx-deployment-f7dcc7c6f-pwtfr -- ls
bin
boot
dev
etc
home
lib
...
- 删除资源对象
kubectl delete ([-f FILENAME] | [-k DIRECTORY] | TYPE [(NAME | -l label | --all)]) [options]
三、命令式使用实例
- 创建一个名字空间test-namespace,用来测试
kubectl create namespace test-namespace
- 在名字空间test-namespace中,运行一个容器镜像dnstool
kubectl run nginx --image=nginx:alpine --port=80 -n test-namespace
- 查看容器nginx的基本信息
kubectl get pod -n test-namespace
- 查看容器nginx的log日志
kubectl logs nginx -n test-namespace
- 在容器nginx中执行命令
kubectl exec nginx -n test-namespace -- ls
- 以交互的形式在容器nginx中执行命令(exit命令退出)
kubectl exec -it nginx -n test-namespace -- /bin/sh
- 删除容器nginx
>kubectl delete pod dnstool -n test-namespace
- 删除名字空间test-namespace
kubectl delete namespace test-namespace
四、声明式使用实例
- 将下面的内容保存为
nginx-deployment.yaml
,我们准备用来创建一个nginx的deployment。
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-1
labels:
app: nginx-1
spec:
replicas: 1
selector:
matchLabels:
app: nginx-1
template:
metadata:
labels:
app: nginx-1
spec:
containers:
- name: nginx-1
image: ccr.ccs.tencentyun.com/qcloud/nginx:1.9
ports:
- containerPort: 80
- 使用下面的创建一个nginx的deployment:
kubectl create -f k8s/nginx-deployment.yaml -n test-namespace
- 将下面的内容保存为
nginx-service.yaml
,我们准备用来创建一个nginx的service。
apiVersion: v1
kind: Service
metadata:
name: nginx-1
spec:
type: NodePort
selector:
app: nginx-1
ports:
- name: http
protocol: TCP
port: 80
targetPort: 80
nodePort: 32080
- 使用下面的创建一个nginx的service:
kubectl create -f k8s/nginx-service.yaml -n test-namespace
- 将下面的内容保存为
nginx-ingress.yaml
,我们准备用来创建一个nginx的ingress。
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$1
name: nginx-router
namespace: game
spec:
ingressClassName: ingress-nginx-controller
rules:
- host: demo.test.com
http:
paths:
- path: /nginx1$
pathType: Exact
backend:
service:
name: nginx-1
port:
number: 80
- path: /nginx1/(.*)
pathType: Prefix
backend:
service:
name: nginx-1
port:
number: 80
- 使用下面的创建一个nginx的ingress:
kubectl create -f k8s/nginx-ingress.yaml -n test-namespace
- 使用下面的创建一个nginx的ingress:
找到ingress-nginx-controller的pod名字,使用kubectl port-forward
命令将本机8000端口转发到pod的nodeport 33080端口。
kubectl port-forward ingress-nginx-controller-ingress-nginx-controller-3cc75bff7psvp 8000:33080 -n ingress-nginx