kubectl用于运行Kubernetes集群命令的管理工具。
1 |
kubectl [ command ] [TYPE] [NAME] [flags]
|
Operation |
Description |
Syntax |
annotate |
为一个或多个资源添加注释 |
kubectl annotate (-f FILENAME | TYPE NAME | TYPE/NAME) KEY_1=VAL_1 ... KEY_N=VAL_N [--overwrite] [--all] [--resource-version=version] [flags] |
api-versions |
列出支持的API版本。 |
kubectl api-versions [flags] |
apply |
对文件或stdin的资源进行配置更改。 |
kubectl apply -f FILENAME [flags] |
attach |
连接到一个运行的容器,既可以查看output stream,也可以与容器(stdin)进行交互。 |
kubectl attach POD -c CONTAINER [-i] [-t] [flags] |
autoscale |
自动扩容/缩容由replication controller管理的一组pod。 |
kubectl autoscale (-f FILENAME | TYPE NAME | TYPE/NAME) [--min=MINPODS] --max=MAXPODS [--cpu-percent=CPU] [flags] |
cluster-info |
显示有关集群中master和services的终端信息。 |
kubectl cluster-info [flags] |
config |
修改kubeconfig文件。有关详细信息,请参阅各个子命令。 |
kubectl config SUBCOMMAND [flags] |
create |
从file或stdin创建一个或多个资源。 |
kubectl create -f FILENAME [flags] |
delete |
从file,stdin或指定label 选择器,names,resource选择器或resources中删除resources。 |
kubectl delete (-f FILENAME | TYPE [NAME | /NAME | -l label | --all]) [flags] |
describe |
显示一个或多个resources的详细状态。 |
kubectl describe (-f FILENAME | TYPE [NAME_PREFIX | /NAME | -l label]) [flags] |
edit |
使用默认编辑器编辑和更新服务器上一个或多个定义的资源。 |
kubectl edit (-f FILENAME | TYPE NAME | TYPE/NAME) [flags] |
exec |
对pod中的容器执行命令。 |
kubectl exec POD [-c CONTAINER] [-i] [-t] [flags] [-- COMMAND [args...]] |
explain |
获取各种资源的文档。例如pod,node,services等 |
kubectl explain [--include-extended-apis=true] [--recursive=false] [flags] |
expose |
将 replication controller,service或pod作为一个新的Kubernetes service显示。 |
kubectl expose (-f FILENAME | TYPE NAME | TYPE/NAME) [--port=port] [--protocol=TCP|UDP] [--target-port=number-or-name] [--name=name] [----external-ip=external-ip-of-service] [--type=type] [flags] |
get |
列出一个或多个资源。 |
kubectl get (-f FILENAME | TYPE [NAME | /NAME | -l label]) [--watch] [--sort-by=FIELD] [[-o | --output]=OUTPUT_FORMAT] [flags] |
label |
添加或更新一个或多个资源的flags。 |
kubectl label (-f FILENAME | TYPE NAME | TYPE/NAME) KEY_1=VAL_1 ... KEY_N=VAL_N [--overwrite] [--all] [--resource-version=version] [flags] |
logs |
在pod中打印容器的日志。 |
kubectl logs POD [-c CONTAINER] [--follow] [flags] |
patch |
使用strategic merge 补丁程序更新资源的一个或多个字段。 |
kubectl patch (-f FILENAME | TYPE NAME | TYPE/NAME) --patch PATCH [flags] |
port-forward |
将一个或多个本地端口转发到pod。 |
kubectl port-forward POD [LOCAL_PORT:]REMOTE_PORT [...[LOCAL_PORT_N:]REMOTE_PORT_N] [flags] |
proxy |
在Kubernetes API服务器运行代理。 |
kubectl proxy [--port=PORT] [--www=static-dir] [--www-prefix=prefix] [--api-prefix=prefix] [flags] |
replace |
从file或stdin替换资源。 |
kubectl replace -f FILENAME |
rolling-update |
通过逐步替换指定的replication controller及其pod来执行滚动更新。 |
kubectl rolling-update OLD_CONTROLLER_NAME ([NEW_CONTROLLER_NAME] --image=NEW_CONTAINER_IMAGE | -f NEW_CONTROLLER_SPEC) [flags] |
run |
在集群上运行指定的镜像。 |
kubectl run NAME --image=image [--env="key=value"] [--port=port] [--replicas=replicas] [--dry-run=bool] [--overrides=inline-json] [flags] |
scale |
更新指定replication controller的大小。 |
kubectl scale (-f FILENAME | TYPE NAME | TYPE/NAME) --replicas=COUNT [--resource-version=version] [--current-replicas=count] [flags] |
stop |
已弃用 |
kubectl stop |
version |
显示客户端和服务器上运行的Kubernetes版本。 |
kubectl version [--client] [flags] |
(1)简单的查询操作
查询节点详细信息
1 2 3 4 5 |
[root@master ~] # kubectl describe node node1
Name: node1
Roles: <none>
Labels: beta.kubernetes.io /arch =amd64
... ...
|
查看版本
1 2 3 |
[root@master ~] # kubectl version
Client Version: version.Info{Major: "1" , Minor: "11" , GitVersion: "v1.11.1" , GitCommit: "bb9ffb1654d4a729bb4cec18ff088eacc153c239" , GitTreeState: "clean" , BuildDate: "2018-08-07T23:17:28Z" , GoVersion: "go1.10.3" , Compiler: "gc" , Platform: "linux/amd64" }
Server Version: version.Info{Major: "1" , Minor: "11" , GitVersion: "v1.11.1" , GitCommit: "b1b29978270dc22fecc592ac55d903350454310a" , GitTreeState: "clean" , BuildDate: "2018-07-17T18:43:26Z" , GoVersion: "go1.10.3" , Compiler: "gc" , Platform: "linux/amd64" }
|
查看集群信息
1 2 3 |
[root@master ~] # kubectl cluster-info
Kubernetes master is running at https: //192 .168.10.103:6443
KubeDNS is running at https: //192 .168.10.103:6443 /api/v1/namespaces/kube-system/services/kube-dns :dns /proxy
|
创建并运行一个或多个容器镜像。
1 |
$ kubectl run NAME --image=image [-- env = "key=value" ] [--port=port] [--replicas=replicas] [--dry-run=bool] [--overrides=inline-json] [-- command ] -- [COMMAND] [args...] [options]
|
(1)运行一个nginx的pod
1 2 |
[root@master ~] # kubectl run nginx-deploy --image=nginx:1.14-alpine --port=80 --replicas=1
deployment.apps /nginx-deploy created
|
(2)在master上查询验证
1 2 3 4 5 6 7 8 9 |
[root@master ~] # kubectl get deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx-deploy 1 1 1 1 44s
[root@master ~] # kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deploy-5b595999-6kw54 1 /1 Running 0 51s
[root@master ~] # kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
nginx-deploy-5b595999-6kw54 1 /1 Running 0 1m 10.244.2.2 node2
|
(3)在部署的node节点上查询
1 2 3 |
[root@node2 ~] # ifconfig |head -2
cni0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1450
inet 10.244.2.1 netmask 255.255.255.0 broadcast 0.0.0.0
|
(4)访问pod内的nginx服务
1 2 3 4 5 |
[root@node1 ~] # curl 10.244.2.2
<!DOCTYPE html>
<html>
< head >
<title>Welcome to nginx!< /title >
|
1 |
$ kubectl delete ([-f FILENAME] | TYPE [(NAME | -l label | --all)])
|
删除刚才的pod,立刻会生成一个新的pod,因为此pod是控制器管理的
1 2 3 4 5 6 7 8 |
[root@master ~] # kubectl delete pods nginx-deploy-5b595999-6kw54
pod "nginx-deploy-5b595999-6kw54" deleted
[root@master ~] # kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deploy-5b595999-jf5gm 0 /1 ContainerCreating 0 16s
[root@master ~] # kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
nginx-deploy-5b595999-jf5gm 1 /1 Running 0 47s 10.244.1.2 node1
|
1 |
$ kubectl expose (-f FILENAME | TYPE NAME) [--port=port] [--protocol=TCP|UDP] [--target-port=number-or-name] [--name=name] [--external-ip=external-ip-of-service] [-- type = type ]
|
(1)将nginx-deploy控制器中相关的Pod资源创建一个名为nginx的服务
1 2 3 4 5 6 7 8 9 10 |
[root@master ~] # kubectl expose deployment nginx-deploy --name=nginx --port=80 --target-port=80 --protocol=TCP
service /nginx exposed
[root@master ~] # kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443 /TCP 16h
nginx ClusterIP 10.96.212.79 <none> 80 /TCP 9s
[root@master ~] # kubectl get svc #简写
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443 /TCP 16h
nginx ClusterIP 10.96.212.79 <none> 80 /TCP 13s
|
(2)验证,在集群内部节点访问服务
1 2 3 4 5 |
[root@master ~] # curl 10.96.212.79
<!DOCTYPE html>
<html>
< head >
<title>Welcome to nginx!< /title >
|
(3)删除pod,立即生产新的pod,但是service还是不变,继续提供服务,只是指定到新的pod
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
---删除pod
[root@master ~] # kubectl delete pods nginx-deploy-5b595999-jf5gm
---立即生产新的pod,service继续提供服务
[root@master ~] # curl 10.96.212.79
<!DOCTYPE html>
<html>
< head >
<title>Welcome to nginx!< /title >
---指定到新的pod
[root@master ~] # kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE
nginx-deploy-5b595999-2z769 1 /1 Running 0 29m 10.244.1.2 node2
[root@master ~] # kubectl describe svc nginx
Name: nginx
Namespace: default
Labels: run=nginx-deploy
Annotations: <none>
Selector: run=nginx-deploy
Type: ClusterIP
IP: 10.96.212.79
Port: < unset > 80 /TCP
TargetPort: 80 /TCP
Endpoints: 10.244.1.2:80
Session Affinity: None
Events: <none>
|
1 |
$ kubectl scale [--resource-version=version] [--current-replicas=count] --replicas=COUNT (-f FILENAME | TYPE NAME)
|
(1)先运行名为myapp标签选择器的2个pod,镜像是我用于测试演示自己构建的
1 2 3 4 5 6 7 8 9 |
[root@master ~] # kubectl run myapp --image=ikubernetes/myapp:v1 --replicas=2
deployment.apps /myapp created
[root@master ~] # kubectl get deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
myapp 2 2 2 2 6s
[root@master ~] # kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
myapp-848b5b879b-58jcr 1 /1 Running 0 11s 10.244.2.53 node2
myapp-848b5b879b-wpx9v 1 /1 Running 0 11s 10.244.1.56 node1
|
(2)创建service,并验证
1 2 3 4 5 6 7 8 9 10 11 12 |
[root@master ~] # kubectl expose deployment myapp --name=myapp --port=80
service /myapp exposed
[root@master ~] # kubectl get svc myapp
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
myapp ClusterIP 10.104.205.158 <none> 80 /TCP 8s
[root@master ~] # curl 10.104.205.158
Hello MyApp | Version: v1 | <a href= "hostname.html" >Pod Name< /a >
---因为有2个pod,所以是随机调度
[root@master ~] # curl 10.104.205.158/hostname.html
myapp-848b5b879b-58jcr
[root@master ~] # curl 10.104.205.158/hostname.html
myapp-848b5b879b-wpx9v
|
(3)使用scale,将pod扩容到5个
1 2 3 4 5 6 7 8 9 |
[root@master ~] # kubectl scale --replicas=5 deployment myapp
deployment.extensions /myapp scaled
[root@master ~] # kubectl get pods
NAME READY STATUS RESTARTS AGE
myapp-848b5b879b-2sj8n 1 /1 Running 0 8s
myapp-848b5b879b-58jcr 1 /1 Running 0 4m
myapp-848b5b879b-hp7zs 1 /1 Running 0 8s
myapp-848b5b879b-sfq8r 1 /1 Running 0 8s
myapp-848b5b879b-wpx9v 1 /1 Running 0 4m
|
(4)使用scale,将pod缩容到3个
1 2 3 4 5 6 |
[root@master ~] # kubectl scale --replicas=3 deployment myapp
[root@master ~] # kubectl get pods
NAME READY STATUS RESTARTS AGE
myapp-848b5b879b-2sj8n 1 /1 Running 0 1m
myapp-848b5b879b-58jcr 1 /1 Running 0 6m
myapp-848b5b879b-wpx9v 1 /1 Running 0 6m
|
(1)set
(2)set image
1 |
$ kubectl set image (-f FILENAME | TYPE NAME) CONTAINER_NAME_1=CONTAINER_IMAGE_1 ... CONTAINER_NAME_N=CONTAINER_IMAGE_N
|
将镜像版本从v1升级到v2版
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[root@master ~] # kubectl set image deployment myapp myapp=ikubernetes/myapp:v2
deployment.extensions /myapp image updated
[root@master ~] # kubectl rollout status deployment myapp #动态显示版本升级过程
---因为版本升级,所以pod都是新建的,名字都改变了
[root@master ~] # kubectl get pods
NAME READY STATUS RESTARTS AGE
client 1 /1 Running 0 3h
myapp-74c94dcb8c-d5fgk 1 /1 Running 0 55s
myapp-74c94dcb8c-f2ntn 1 /1 Running 0 21s
myapp-74c94dcb8c-ht9v7 1 /1 Running 0 38s
---验证,版本升级成功
[root@master ~] # curl 10.104.205.158
Hello MyApp | Version: v2 | <a href= "hostname.html" >Pod Name< /a >
|
(1)rollout
(2)rollout undo
回滚pod到之前的版本。
1 |
$ kubectl rollout undo (TYPE NAME | TYPE /NAME ) [flags]
|
1 2 3 4 5 6 7 8 9 10 |
[root@master ~] # kubectl rollout undo deployment myapp
deployment.extensions /myapp
[root@master ~] # kubectl get pods
NAME READY STATUS RESTARTS AGE
myapp-848b5b879b-gg9lr 1 /1 Running 0 15s
myapp-848b5b879b-km4t6 1 /1 Running 0 16s
myapp-848b5b879b-nq98c 1 /1 Running 0 17s
---验证,回滚成功
[root@master ~] # curl 10.104.205.158
Hello MyApp | Version: v1 | <a href= "hostname.html" >Pod Name< /a >
|
1 |
$ kubectl edit (RESOURCE /NAME | -f FILENAME)
|
(1)修改service,将类型type: ClusterIP,修改为type: NodePort;修改后,可以在集群外访问到pod中的服务
1 2 3 4 5 6 7 8 9 10 |
[root@master ~] # kubectl edit svc myapp
... ...
spec:
... ...
type : NodePort
... ...
service /myapp edited
[root@master ~] # kubectl get svc myapp
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
myapp NodePort 10.104.205.158 <none> 80:31599 /TCP 23m
|
(2)在集群外的主机上访问了
[root@master manifests]# mkdir manifests # 创建一个目录 [root@master manifests]# cd manifests [root@master manifests]# cat develop-ns.yaml # 创建一个develop-ns.yaml的配置文件 apiVersion: v1 kind: Namespace # 类型是名称空间 metadata: name: develop # 定义develop名称空间
使用create开始创建develop名称空间,如果继续对此文件继续创建,就会提示报错信息
[root@master manifests]# kubectl create -f develop-ns.yaml # create是陈述式创建名称空间,从develop-ns.yaml文件中加载定义空间名称 namespace/develop created [root@master manifests]# kubectl get ns # 查看创建的名称空间 NAME STATUS AGE default Active 6h51m develop Active 57s kube-node-lease Active 6h51m kube-public Active 6h51m kube-system Active 6h51m
[root@master manifests]# cat prod-ns.yaml # 创建一个prod-ns.yaml声明式配置文件 apiVersion: v1 kind: Namespace metadata: name: prod
使用apply创建新的pod,如果pod名称发生改变,就会进行创建pod,如果未发生改变,就会源文件输出,不会报错
[root@master manifests]# kubectl apply -f prod-ns.yaml # 使用apply 执行此配置文件,实现声明式创建 namespace/prod created [root@master manifests]# kubectl get ns # 查看此时创建的名称空间 NAME STATUS AGE default Active 7h develop Active 9m19s kube-node-lease Active 7h kube-public Active 7h kube-system Active 7h prod Active 2m52s # 创建的新的Prod名称空间
apply:可以重复创建一个文件,如果文件内容相同,不发生改变,如果文件内容改变了,可以创建新的pod。
create:只能对相同文件名创建一次名称空间,如果继续对一个源文件进行修改后,就会提示报错信息。
containerPort: 暴露容器端口
1、定义多个容器名称
[root@master manifests]# cat pod-daem.yaml # 自定义一个pod-daem.yaml资源文件 apiVersion: v1 kind: Pod metadata: name: pod-demo namespace: prod spec: containers: - name: myapp # 第一个容器名称 image: ikubernetes/myapp:v1 # 指定了镜像路径,个人提交的镜像路径 - name: bbox # 第二个容器名称 image: busybox:latest imagePullPolicy: IfNotPresent command: ["/bin/sh","-c","sleep 86400"]
2、执行资源配置文件,并查看此时执行完成后的pod状态
[root@master manifests]# kubectl apply -f pod-daem.yaml # 创建并声明pod-daem.yaml配置文件 pod/pod-demo created [root@master manifests]# kubectl get pods -n prod # 查看此时的prod名称空间状态 NAME READY STATUS RESTARTS AGE pod-demo 2/2 Running 0 13s # pod-demo有两个容器已经运行
3、执行kubectl命令进入进入到pod-demo名称空间中,查看指定的bbox容器信息
[root@master ~]# kubectl exec pod-demo -c bbox -n prod -it -- /bin/sh # 其中pod-demo是名称空间 / # ls bin dev etc home proc root sys tmp usr var / # ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 3: eth0@if8: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1450 qdisc noqueue link/ether ae:4b:24:1c:2b:3f brd ff:ff:ff:ff:ff:ff inet 10.224.1.11/24 brd 10.224.1.255 scope global eth0 valid_lft forever preferred_lft forever / # netstat -nlt # 可以看出监听了80端口,实际是上面创建的myapp监听的端口,因为pod-demo名称空间中有两个容器 Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN / # wget -O - -q 127.0.0.1 # 此时访问myapp容器中的web页面也可以访问 Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
4、通过kubectl 命令,我们也可以查看到容器里边的日志信息:
[root@master ~]# kubectl logs pod-demo -n prod -c myapp # 查看pod-demo名称空间里边的myapp容器访问日志信息 127.0.0.1 - - [24/Jul/2020:14:22:05 +0000] "GET / HTTP/1.1" 200 65 "-" "Wget" "-" 127.0.0.1 - - [24/Jul/2020:14:26:51 +0000] "GET / HTTP/1.1" 200 65 "-" "Wget" "-" 127.0.0.1 - - [24/Jul/2020:14:26:52 +0000] "GET / HTTP/1.1" 200 65 "-" "Wget" "-" 127.0.0.1 - - [24/Jul/2020:14:26:53 +0000] "GET / HTTP/1.1" 200 65 "-" "Wget" "-"
5、我们也可以直接查看容器的IP地址之后,对IP地址进行访问,然后直接查看pod名称对应的日志即可
[root@master ~]# kubectl get pods -o wide # 查看pod名称 NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES myapp-5d587c4d45-dlz84 1/1 Running 1 5h48m 10.224.2.8 node2 <none> <none> myapp-5d587c4d45-mwvq5 1/1 Running 1 5h56m 10.224.1.10 node1 <none> <none> nginx-6dd57bccf7-mkrmx 1/1 Running 1 7h12m 10.224.1.9 node1 <none> <none> nginx-dep-84b6dfdcd5-gm9dd 1/1 Running 1 7h5m 10.224.2.10 node2 <none> <none> ngx-dep-5d855b5b54-98rc2 1/1 Running 1 6h42m 10.224.2.9 node2 <none> <none> [root@master ~]# curl 10.224.2.8 # 访问对应的IP地址 Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> [root@master ~]# curl 10.224.2.8 Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> [root@master ~]# curl 10.224.2.8 Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> [root@master ~]# kubectl logs myapp-5d587c4d45-dlz84 # 查看对应pod的访问日志 10.224.0.0 - - [24/Jul/2020:14:30:03 +0000] "GET / HTTP/1.1" 200 65 "-" "curl/7.29.0" "-" 10.224.0.0 - - [24/Jul/2020:14:30:05 +0000] "GET / HTTP/1.1" 200 65 "-" "curl/7.29.0" "-" 10.224.0.0 - - [24/Jul/2020:14:30:05 +0000] "GET / HTTP/1.1" 200 65 "-" "curl/7.29.0" "-"
6、创建一个可以暴露容器和宿主机端口的容器
[root@master manifests]# cat pod-daem1.yaml # 创建一个yaml文件 apiVersion: v1 kind: Pod metadata: name: mypod # Pod名称为mypod namespace: default spec: containers: - name: myapp # 容器名称为myapp image: ikubernetes/myapp:v1 ports: - protocol: TCP # 模式为TCP containerPort: 80 # 暴露端口是80 name: http # 命名为http hostPort: 8080 # 暴露宿主机端口号
此时我们可以看到随机分配到node2宿主机上了,
[root@master manifests]# kubectl apply -f pod-daem1.yaml # 通过yaml配置文件创建一个pod文件 pod/mypod created [root@master manifests]# kubectl get pods -o wide # 查看pod的详细信息,可以看到mypod在node2容器上运行中 NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES myapp-5d587c4d45-dlz84 1/1 Running 1 6h 10.224.2.8 node2 <none> <none> myapp-5d587c4d45-mwvq5 1/1 Running 1 6h8m 10.224.1.10 node1 <none> <none> mypod 1/1 Running 0 12s 10.224.2.11 node2 <none> <none> nginx-6dd57bccf7-mkrmx 1/1 Running 1 7h24m 10.224.1.9 node1 <none> <none> nginx-dep-84b6dfdcd5-gm9dd 1/1 Running 1 7h18m 10.224.2.10 node2 <none> <none> ngx-dep-5d855b5b54-98rc2 1/1 Running 1 6h54m 10.224.2.9 node2 <none> <none>
通过node2宿主机的IP地址和暴露的8080端口可以访问到指定的网页:
更新(增加、修改或删除)资源上的 label(标签)。
标签就是"键值"类型的数据,他们可于资源创建时直接指定,也可随时按需添加于活动对象,而后即可由标签选择器进行匹配度检查从而完成资源挑选。
一个对象可拥有不止一个标签,而同一个标签也可被添加至多个资源之上
实践中,可以为资源附加多个不同维度的标签以实现灵活的资源分组管理功能,例如版本标签、环境标签、分层架构标签等,用于交叉标识同一个资源所属的不同版本、环境及架构层级等
标签中的键名通常由键前缀和键名组成,其中键前缀可选,其格式形如"KEY_PREFIX/KEY_NAME"
键名之多能使用63个字符,可使用字母、数字、连接号(-),下划线(_)、点号(.)等字符,且只能以字母或数字开头
键前缀必须为DNS子域名格式,且不饿能超过253个字符。省略键前缀时,键将被视为用户的私有数据,不过由Kubernetes系统组件或第三方组件自动为用户资源添加的键必须使用键前缀,而"Kubernetes.io/"前缀预留给kubernetes的核心组件使用
标签中的兼职必须不能多余63个祖父,它要么为空。要么时以字母或数字开头及结尾,且中间仅使用了字母、数字、连接号(-),下划线(_)、点号(.)等字符的数据
标签选择器用于表达标签的查询条件或选择标准, Kubernetes API 目前支持两个选择 器:基于等值关系(equality-based)以及基于集合关系(set-based) 。 例如, env=production 和 env!=qa 是基于等值关系的选择器,而 tier in (fronte时, backend)则是基于集合关系的选择 器。 另外,使用标签选择器时还将遵循以下逻辑。
基于等值关系的标签选择器的可用操作符有“=”“==”和“!=” 三种,其中前两个意 义相同,都表示“等值” 关系, 最后一个表示“不等” 关系。 “kubectl get”命令的“”l ”选 项能够指定使用标签选择器,例如,显示键名 env 的值不为 qa 的所有 Pod 对象:
此外, Kubemetes 的诸多资源对象必须以标签选择器的方式关联到 Pod 资源对象,例如 Service、 Deployment 和 ReplicaSet 类型的资源等,它们在 spec 字段中嵌套使用嵌套的“selector” 字段, 通过“ matchLabels”来指定标签选择器,有的甚至还支持使用“mate阻xpressions”构造 复杂的标签选择机制。
matchLabels:通过直接给定键值对来指定标签选择器。
matchExpressions : 基于表达式指定的标签选择器列表,每个选择器都形如“{key: 阻Y_NAME, operator: OPERATOR, values: [VALUE!, VALUE2,…]}”,选择器列表 间为“逻辑与”关系 ;使用 In 或 Notln 操作符时, 其 values 不强制要求为非空的字 符串列表,而使用 Exists 或 DostNotExist 时,其 values 必须为空。
1 |
$ kubectl label [--overwrite] (-f FILENAME | TYPE NAME) KEY_1=VAL_1 ... KEY_N=VAL_N [--resource-version=version]
|
(1)运行一个标签为app=nginx的pod
1 2 |
[root@master ~] # kubectl run nginx --image=nginx:1.14-alpine --replicas=1 --labels="app=nginx"
deployment.apps /nginx created
|
(2)-l 指定标签,实现标签过滤
1 2 3 4 5 6 7 8 9 |
[root@master ~] # kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
myapp-848b5b879b-5blnx 1 /1 Running 0 4m pod-template- hash =4046164356,run=myapp
myapp-848b5b879b-6hgpp 1 /1 Running 0 4m pod-template- hash =4046164356,run=myapp
myapp-848b5b879b-hzj5g 1 /1 Running 0 4m pod-template- hash =4046164356,run=myapp
nginx-86d5b5ddb6-rwv2z 1 /1 Running 0 53s app=nginx,pod-template- hash =4281618862
[root@master ~] # kubectl get pods --show-labels -l app
NAME READY STATUS RESTARTS AGE LABELS
nginx-86d5b5ddb6-rwv2z 1 /1 Running 0 1m app=nginx,pod-template- hash =4281618862
|
(3)打一个新的标签
1 2 3 4 5 |
[root@master ~] # kubectl label pods nginx-86d5b5ddb6-rwv2z release=canary
pod /nginx-86d5b5ddb6-rwv2z labeled
[root@master ~] # kubectl get pods --show-labels -l release
NAME READY STATUS RESTARTS AGE LABELS
nginx-86d5b5ddb6-rwv2z 1 /1 Running 0 4m app=nginx,pod-template- hash =4281618862,release=canary
|
(4)修改标签
1 2 3 4 5 |
[root@master ~] # kubectl label pods nginx-86d5b5ddb6-rwv2z release=stable --overwrite
pod /nginx-86d5b5ddb6-rwv2z labeled
[root@master ~] # kubectl get pods --show-labels -l release
NAME READY STATUS RESTARTS AGE LABELS
nginx-86d5b5ddb6-rwv2z 1 /1 Running 0 5m app=nginx,pod-template- hash =4281618862,release=stable
|
(1)等值关系:=,==,!=
1 2 3 4 5 6 7 8 9 10 11 |
[root@master ~] # kubectl get pods -l release --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-86d5b5ddb6-rwv2z 1 /1 Running 0 6m app=nginx,pod-template- hash =4281618862,release=stable
[root@master ~] # kubectl get pods -l release=stable --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-86d5b5ddb6-rwv2z 1 /1 Running 0 6m app=nginx,pod-template- hash =4281618862,release=stable
[root@master ~] # kubectl get pods -l release!=stable --show-labels
NAME READY STATUS RESTARTS AGE LABELS
myapp-848b5b879b-5blnx 1 /1 Running 0 10m pod-template- hash =4046164356,run=myapp
myapp-848b5b879b-6hgpp 1 /1 Running 0 10m pod-template- hash =4046164356,run=myapp
myapp-848b5b879b-hzj5g 1 /1 Running 0 10m pod-template- hash =4046164356,run=myapp
|
(2)集合关系
[root@master ~]# kubectl get pods -l "release in (stable,beta,alpha)" NAME READY STATUS RESTARTS AGE nginx-86d5b5ddb6-rwv2z 1/1 Running 0 9m [root@master ~]# kubectl get pods -l "release notin (stable,beta,alpha)" NAME READY STATUS RESTARTS AGE myapp-848b5b879b-5blnx 1/1 Running 0 12m myapp-848b5b879b-6hgpp 1/1 Running 0 12m myapp-848b5b879b-hzj5g 1/1 Running 0 12m
1、查看pods的所有标签:
[root@master ~]# kubectl get pods --show-labels # 查看此时的pods标签 NAME READY STATUS RESTARTS AGE LABELS myapp-5d587c4d45-dlz84 0/1 Completed 1 17h app=myapp,pod-template-hash=5d587c4d45 myapp-5d587c4d45-mwvq5 0/1 Completed 1 17h app=myapp,pod-template-hash=5d587c4d45 mypod 0/1 Completed 0 11h <none> nginx-6dd57bccf7-mkrmx 0/1 Completed 1 18h app=nginx,pod-template-hash=6dd57bccf7 nginx-dep-84b6dfdcd5-gm9dd 0/1 Completed 1 18h app=nginx-dep,pod-template-hash=84b6dfdcd5 ngx-dep-5d855b5b54-98rc2 0/1 Completed 1 18h app=ngx-dep,pod-template-hash=5d855b5b54
2、自定义一个标签资源文件
[root@master manifests]# cat pod-daem.yaml apiVersion: v1 kind: Pod metadata: name: pod-demo namespace: prod labels: # 定义多个标签 app: pod-demo # 标签名称为pod-demo rel: stable 第二个标签,试出版本是stable spec: containers: - name: myapp image: ikubernetes/myapp:v1 - name: bbox image: busybox:latest imagePullPolicy: IfNotPresent command: ["/bin/sh","-c","sleep 86400"
执行pod-demo.yaml文件,创建附带标签的pod,并查看此时的标签名称。
[root@master manifests]# kubectl apply -f pod-daem.yaml # 重新创建pod pod/pod-demo configured [root@master manifests]# kubectl get pods -n prod --show-labels # 查看此时添加的标签名称 NAME READY STATUS RESTARTS AGE LABELS pod-demo 2/2 Running 2 11h app=pod-demo,rel=stable # 可以看到此时就有两个标签名称
我们也可以使用label指令指明要创建的标签,需要指定pod名称为pod-demo和指定的namespace名称空间信息为prod
[root@master manifests]# kubectl label pods pod-demo -n prod tier=frontend # 创建一个frontend的标签名称 pod/pod-demo labeled [root@master manifests]# kubectl get pods -n prod --show-labels # 查看此时新创建的标签名frontend NAME READY STATUS RESTARTS AGE LABELS pod-demo 2/2 Running 2 11h app=pod-demo,rel=stable,tier=frontend # 查看此时定义的标签名
修改此时的标签的键,即:将app改为myapp,使用--overwrite选项即可
[root@master manifests]# kubectl label pods pod-demo -n prod --overwrite app=myapp #将app修改为myapp pod/pod-demo labeled [root@master manifests]# kubectl get pods -n prod --show-labels NAME READY STATUS RESTARTS AGE LABELS pod-demo 2/2 Running 2 11h app=myapp,rel=stable,tier=frontend
如果我们想删除一个标签怎么办?直接在标签的键后面加一个减号即可,如下所示,想删除rel的标签,直接在键后面加减号即
[root@master manifests]# kubectl label pods pod-demo -n prod rel- # 删除rel标签,此时在rel后面加-即可 pod/pod-demo labeled [root@master manifests]# kubectl get pods -n prod --show-labels # 查看此时的标签已经被删除 NAME READY STATUS RESTARTS AGE LABELS pod-demo 2/2 Running 2 12h app=myapp,tier=frontend
-l选项是指定标签,然后实现过滤信息:
[root@master manifests]# kubectl get pods --show-labels -l app=myapp # 查看标签中app=myapp的pod NAME READY STATUS RESTARTS AGE LABELS myapp-5d587c4d45-dlz84 1/1 Running 2 17h app=myapp,pod-template-hash=5d587c4d45 myapp-5d587c4d45-mwvq5 1/1 Running 2 17h app=myapp,pod-template-hash=5d587c4d45 [root@master manifests]# kubectl get pods --show-labels -l app!=myapp # 查看标签中app不等于myapp的pod NAME READY STATUS RESTARTS AGE LABELS mypod 1/1 Running 1 11h <none> nginx-6dd57bccf7-mkrmx 1/1 Running 2 18h app=nginx,pod-template-hash=6dd57bccf7 nginx-dep-84b6dfdcd5-gm9dd 1/1 Running 2 18h app=nginx-dep,pod-template-hash=84b6dfdcd5 ngx-dep-5d855b5b54-98rc2 1/1 Running 2 18h app=ngx-dep,pod-template-hash=5d855b5b54
查看指定app标签中的信息
[root@master manifests]# kubectl get pods --show-labels -l "app in (myapp,ngx-dep)" # 查看与app关联的pod NAME READY STATUS RESTARTS AGE LABELS myapp-5d587c4d45-dlz84 1/1 Running 2 17h app=myapp,pod-template-hash=5d587c4d45 myapp-5d587c4d45-mwvq5 1/1 Running 2 17h app=myapp,pod-template-hash=5d587c4d45 ngx-dep-5d855b5b54-98rc2 1/1 Running 2 18h app=ngx-dep,pod-template-hash=5d855b5b54 [root@master manifests]# kubectl get pods -l "app in (myapp,ngx-dep)" -L app # -L选项是只看app的键值对中的值 NAME READY STATUS RESTARTS AGE APP myapp-5d587c4d45-dlz84 1/1 Running 2 17h myapp myapp-5d587c4d45-mwvq5 1/1 Running 2 17h myapp ngx-dep-5d855b5b54-98rc2 1/1 Running 2 18h ngx-dep
转载:https://www.cnblogs.com/along21/p/10304362.html