kubectl logs [-f] [-p] (POD | TYPE/NAME) [-c CONTAINER] [options]
使用kubectl logs可以查看pod中的日志。如果pod中有多个容器,可以使用-c <container>指定容器。比如:
kubectl port-forward TYPE/NAME [options] [LOCAL_PORT:]REMOTE_PORT [...[LOCAL_PORT_N:]REMOTE_PORT_N]
pod已经运行了,那如何向集群中的pod发出请求呢。我们通过kubectl port-forward命令,将本地端口映射到指定pod的端口上。
窗口1:运行kubectl port-forward
-> [root@kube0.vm] [~] k port-forward nginx 8000:80 Forwarding from 127.0.0.1:8000 -> 80 Forwarding from [::1]:8000 -> 80 Handling connection for 8000 # curl请求发出后出现窗口2:运行kubectl logs
-> [root@kube0.vm] [~] k logs nginx -f 127.0.0.1 - - [20/May/2020:03:19:50 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-" # curl请求发出后出现窗口3:curl发出请求
-> [root@kube0.vm] [~] curl :8000 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> ........ 标签通俗的讲,标签就是用于将Pod和其他Kubernetes资源进行分类,每个资源都可以有多个标签
标签是可以附加到资源上的键值对,用以选择具有该标签的资源(通过标签选择器完成)。
创建一个带有标签的描述文件
-> [root@kube0.vm] [~] cat nginx-labels.yaml apiVersion: v1 kind: Pod metadata: name: nginx-labels labels: # 添加了两个标签 env: prod app: order spec: containers: - image: nginx name: nginx ports: - containerPort: 80 protocol: TCP 查看标签使用--show-labels查看标签,po是pod的简写
-> [root@kube0.vm] [~] k create -f nginx-labels.yaml pod/nginx-labels created -> [root@kube0.vm] [~] k get po --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx-labels 0/1 ContainerCreating 0 3s app=order,env=prod使用-L查看指定标签,并且单独成列。
-> [root@kube0.vm] [~] k get po -L app,env NAME READY STATUS RESTARTS AGE APP ENV nginx-labels 1/1 Running 0 4m13s order prod kubectl label多个标签以空格分隔
添加 -> [root@kube0.vm] [~] k label po nginx-labels test=123 pod/nginx-labels labeled -> [root@kube0.vm] [~] k get pod --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx-labels 1/1 Running 0 6m23s app=order,env=prod,test=123 修改修改已存在的标签,需要指定--overwrite选项
-> [root@kube0.vm] [~] k label po nginx-labels test=456 --overwrite pod/nginx-labels labeled -> [root@kube0.vm] [~] k get pod --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx-labels 1/1 Running 0 7m24s app=order,env=prod,test=456 删除 -> [root@kube0.vm] [~] k label po nginx-labels test- pod/nginx-labels labeled -> [root@kube0.vm] [~] k get pod --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx-labels 1/1 Running 0 8m8s app=order,env=prod 标签选择器标签选择器可以使我们获取具有特定标签的pod子集。规则通过-l选项指定,多个用逗号分隔。
选择规则key:选择存在标签key的
!key:选择不存在标签key的
key=value:key存在并且值等于value
key!= value:选择"key=value"的补集。也就存在key并且不等value的,或者不存在key的。
key in (value1,value2):key存在且值为value1或者value2
key notin (value1,value2):key存在且值不为value1和value2
实操准备几个不同label的pod:
-> [root@kube0.vm] [~] k get po --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx-labels 1/1 Running 0 40m app=order,env=prod nginx-labels-1 1/1 Running 0 9m42s env=debug nginx-labels-2 1/1 Running 0 8m8s app=user,env=devapp
-> [root@kube0.vm] [~] k get po --show-labels -l app NAME READY STATUS RESTARTS AGE LABELS nginx-labels 1/1 Running 0 50m app=order,env=prod nginx-labels-2 1/1 Running 0 18m app=user,env=dev!app
-> [root@kube0.vm] [~] k get po --show-labels -l '!app' NAME READY STATUS RESTARTS AGE LABELS nginx-labels-1 1/1 Running 0 20m env=debugapp=order
-> [root@kube0.vm] [~] k get po --show-labels -l app=order NAME READY STATUS RESTARTS AGE LABELS nginx-labels 1/1 Running 0 77m app=order,env=prodapp!=order
-> [root@kube0.vm] [~] k get po --show-labels -l app!=order NAME READY STATUS RESTARTS AGE LABELS nginx-labels-1 1/1 Running 0 46m env=debug nginx-labels-2 1/1 Running 0 45m app=user,env=devenv in (dev,prod)
-> [root@kube0.vm] [~] k get po --show-labels -l 'env in (dev,prod)' NAME READY STATUS RESTARTS AGE LABELS nginx-labels 1/1 Running 0 78m app=order,env=prod nginx-labels-2 1/1 Running 0 46m app=user,env=dev