1. 什么是pod?
- pod相当于逻辑主机,每个pod都有自己的ip地址
- pod内的容器共享相同的ip和端口空间
- 默认情况下,每个容器的文件系统与其他容器完全隔离
- 可以理解为:容器组,同时pod相当于逻辑主机,进入pod后仿佛进入一个linux主机,命令都可用(linux系统下),该“主机”内又有很多容器,进入后又仿佛是又进了一个linux主机。
1.1 pod的分类
pod分为两种类型:
- 自主式pod
- 控制器管理的pod
1,自我管理的pod,创建以后仍然需要提交给apiserver,由apiserver接收以后借助于调度器将其调度至指定的node节点,由node启动此pod,如果此pod出现故障,需要重启容器则由kubelet来完成;如果node节点故障了,那么此pod将会消失。其无法实现全局调度。所以不推荐使用此种pod。
2,Pod控制器是管理pod的中间层,使用Pod控制器之后,只需要告诉Pod控制器,想要多少个什么样的Pod就可以了,它会创建出满足条件的Pod并确保每一个Pod资源处于用户期望的目标状态。如果Pod资源在运行中出现故障,它会基于指定策略重新编排Pod
1.2 常见的pod控制器
replicationController:当启动一个pod时,这个pod如果不够用可以再启一个副本,而后由控制器来管理同一类pod的各种副本与对象。一旦副本少了就会自动增加。采取多退少补的规则,精确符合我们所定义的期望;支持滚动更新。(比较原始的pod控制器,已经被废弃,由ReplicaSet替代)
-
replicaset:由一个名叫Deployment的声明式更新的控制器来管理
-
Deployment:Deployment只能管理无状态的应用
-
StateFulSet:有状态副本集,可以管理有状态的应用
-
DaemonSet:如果需要在每个node上运行一个副本的时候可以用DaemonSet
-
Job:它创建出来的pod只要完成任务就立即退出,不需要重启或重建,用于执行一次性任务
-
Cronjob:它创建的Pod负责周期性任务控制,不需要持续后台运行
以上所有控制器都是用来实现一种特定的应用管理的。
2. kubernetes的核心组件
2.1 HPA
Deployment还支持二级控制器HPA(HorizontalPodAutoscaler,水平pod自动伸缩控制器)一般情况下我们可以确保一个node上有2个pod运行,万一用户访问流量增加,2个pod不足以承受这么多的流量怎么办?此时我们就应该要增加pod资源,那么到底应该加几个?HPA控制器可自动监控pod、自动进行扩展。
2.2 service
假如有2个pod,pod有其生命周期,万一pod所在的节点宕机了,那么此pod将应该要在其他的节点上重建,而重建完的pod与原来pod已经不是同一个pod了,只是两者都是运行的同一服务而已。且每个容器都有其IP地址,重建的pod中的容器其IP地址与之前的pod中容器的IP地址是不一样的,如此一来就会存在一个问题,客户端如何访问这些pod中的容器呢?
措施:服务发现:就比如集贸市场的注册摊位和声明地址,注册摊位就是买东西的摊位,有一天这个摊位的商贩换地方了,就会在原来的摊位上留下一个摊位声明告诉顾客自己换地方了,但是它出售的商品还是一样的。只是换个地方买而已,这就是服务发现。
pod是有生命周期的,一个pod随时都有可能离去,随时都有可能会有其他pod加入进来,假如它们提供的同一种服务,客户端是无法通过固定的手段来访问这些pod的,因为pod本身是不固定的,它们随时可能被替换掉,无论使用主机名还是IP地址,都随时会被替换掉。为了尽可能的降低客户端与pod间协调的复杂度,k8s为每一组提供同类服务的pod和其客户端之间添加了一个中间层,这个中间层是固定的,这个中间层就叫service。
service只要不被删除,其地址与名称皆是固定的,当客户端需要在其配置文件中写上访问某个服务时,它不再需要自动发现,只需要在配置文件中写明service的名称即可,而这个service是个调度器,其不但能够提供一个稳定的访问入口,还可以做反向代理,当service接收到客户端的请求后,会将其代理到后端的pod之上,一旦pod宕机了会立即新建一个pod,这个新建的pod会立即被service关联上,作为service后端的可用pod之一。
客户端程序访问服务都是通过IP+端口号或主机名+端口的方式来实现的。而service关联后端的pod不是靠它的IP和主机名。而是靠pod的标签选择器。只要其在service所识别。如此一来,只要pod属于标签选择器,只要其在service的管理范围之内,则其就会被关联到service中,当这个动态的pod关联到service中,当这个动态的pod关联到service中之后,再进行动态的探测此pod的IP地址、端口号,再将其作为自己后端可调度的可用服务器主机对象。因此,客户端的请求发送到service,然后service代理到后端真实的pod中的容器进行响应。
service不是一个程序,也不是一个组件,它只是一个iptables的dnat规则,service作为k8s的对象,有其自身的名称,而service的名称相当于服务的名称,而这个名称可以被解析。
3. kubernetes网络模型
- 节点网络
- service网络
- pod网络
k8s的三种网络模型分属于三个网段,由此延伸出来三个问题
同一个pod内的多个容器间如何通信?
lo网卡进行通信,因为同一个pod内的容器共用一个网路名称空间,而且在同一个网段。
各pod间如何通信?
- 物理桥接,但是在一定规模下容易产生网络风暴,不建议使用
- Overlay Network 通过隧道的方式转发报文
- pod与service间如何通信?首先各节点之间是相互通信的,节点也就是真机之间的通信,因为service网络是一个iptables规则,且与真机是相连的,而pod和service是我们初始化的时候通过flannel网络进行互联,且属于同一网段。
4. kubectl命令使用
kubectl命令官方文档:https://kubernetes.io/zh/docs/reference/kubectl/.
4.1 create命令
//语法
kubectl create deployment NAME --image=image -- [COMMAND] [args...]
kubectl create deployment NAME --image=image -- [COMMAND] [args...] //格式
[root@master ~]# kubectl create deployment test1 --image busybox
deployment.apps/test1 created //使用busybox镜像创建一个test1的pod
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-6799fc88d8-thr6q 1/1 Running 0 3h28m
test1-78d64fd9b9-8gmbc 0/1 CrashLoopBackOff 3 114s //可以看到处于退出状态,因为busybox使用的是sh,没有任务就会退出
[root@master ~]# kubectl create deployment test2 --image busybox -- sleep 60
deployment.apps/test2 created
test2-7c95bf5bcb-tqgn5 1/1 Running 0 17s //正在运行
[root@master ~]# kubectl create deployment web --image nginx --replicas 3 // 创建使用nginx镜像创建三个pod,名字为web
deployment.apps/web created
[root@master ~]# kubectl get pod
web-96d5df5c8-2kqfx 1/1 Running 0 57s
web-96d5df5c8-ld842 1/1 Running 0 57s
web-96d5df5c8-vtwks 1/1 Running 0 57s
[root@master ~]# kubectl get pods -o wide //查看pod运行的节点位置
web-96d5df5c8-2kqfx 1/1 Running 0 2m2s 10.244.2.3 node2.example.com <none> <none>
web-96d5df5c8-ld842 1/1 Running 0 2m2s 10.244.1.6 node1.example.com <none> <none>
web-96d5df5c8-vtwks 1/1 Running 0 2m2s 10.244.2.4 node2.example.com <none> <none>
[root@master ~]# kubectl create deployment web01 --image nginx --port=80 //暴露80端口号
4.2 run命令
// 启动一个 nginx pod
[root@master ~]# kubectl run nginx --image nginx
pod/nginx created
[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx 1/1 Running 0 36s 10.244.1.7 node1.example.com <none> <none>
[root@master ~]# kubectl delete pods nginx //删除nginx的pod
pod "nginx" deleted
[root@master ~]# kubectl run nginx --images nginx --port 80 // 暴露容器的80端口号
// 在容器中设置标签“app=nginx”和“env=prod”
[root@master ~]# kubectl run nginx --image nginx --labels "aap=nginx,env=prod"
pod/nginx created
[root@master ~]# kubectl describe pod nginx //描述nginx信息
// 测试运行
[root@master ~]# kubectl run nginx --image nginx --dry-run server //不会真正运行
W1219 01:41:21.786495 157488 helpers.go:553] --dry-run is deprecated and can be replaced with --dry-run=client.
pod/nginx created (dry run)
4.3 delete命令
[root@master ~]# kubectl delete deployment test1 // 删除test1,使用deployment类型,因为我们当时创建的时候使用的是deployment类型
deployment.apps "test1" deleted
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-6799fc88d8-thr6q 1/1 Running 0 4h2m
test2-7c95bf5bcb-tqgn5 0/1 Terminating 8 32m
web-96d5df5c8-2kqfx 1/1 Running 0 30m
web-96d5df5c8-ld842 1/1 Running 0 30m
web-96d5df5c8-vtwks 1/1 Running 0 30m
[root@master ~]# kubectl delete deployment test2
deployment.apps "test2" deleted
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-6799fc88d8-thr6q 1/1 Running 0 4h2m
web-96d5df5c8-2kqfx 1/1 Running 0 31m
web-96d5df5c8-ld842 1/1 Running 0 31m
web-96d5df5c8-vtwks 1/1 Running 0 31m
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 29h
nginx NodePort 10.99.74.11 <none> 80:31173/TCP 28h
web ClusterIP 10.109.101.12 <none> 8080/TCP 12m
// 删除service类型的pod
[root@master ~]# kubectl delete svc nginx
'service "nginx" deleted
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 29h
web ClusterIP 10.109.101.12 <none> 8080/TCP 15m
[root@master ~]# kubectl delete pods --all //删除所有pod
[root@master ~]# kubectl delete pod foo --force //强制删除pod节点
4.4 expose命令
[root@master ~]# kubectl expose deployment web --port 8080 --target-port 80
service/web exposed //将pod中的80暴露到宿主机上的8080
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 29h
nginx NodePort 10.99.74.11 <none> 80:31173/TCP 27h
web ClusterIP 10.109.101.12 <none> 8080/TCP 22s
[root@master ~]# curl 10.109.101.12:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
4.5 get命令
[root@master ~]# kubectl get pods //列出所有的pod
NAME READY STATUS RESTARTS AGE
nginx-6799fc88d8-thr6q 1/1 Running 0 3h41m
test1-78d64fd9b9-8gmbc 0/1 CrashLoopBackOff 7 15m
test2-7c95bf5bcb-tqgn5 0/1 CrashLoopBackOff 5 11m
web-96d5df5c8-2kqfx 1/1 Running 0 9m48s
web-96d5df5c8-ld842 1/1 Running 0 9m48s
web-96d5df5c8-vtwks 1/1 Running 0 9m48s
[root@master ~]# kubectl get pods -o wide //显示所有pod的详细信息
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-6799fc88d8-thr6q 1/1 Running 0 3h41m 10.244.2.2 node2.example.com <none> <none>
test1-78d64fd9b9-8gmbc 0/1 CrashLoopBackOff 7 15m 10.244.1.4 node1.example.com <none> <none>
test2-7c95bf5bcb-tqgn5 0/1 CrashLoopBackOff 5 12m 10.244.1.5 node1.example.com <none> <none>
web-96d5df5c8-2kqfx 1/1 Running 0 10m 10.244.2.3 node2.example.com <none> <none>
web-96d5df5c8-ld842 1/1 Running 0 10m 10.244.1.6 node1.example.com <none> <none>
web-96d5df5c8-vtwks 1/1 Running 0 10m 10.244.2.4 node2.example.com <none> <none>
[root@master ~]# kubectl get deployment web //查看你指定类型的pod,类型加pod名
NAME READY UP-TO-DATE AVAILABLE AGE
web 3/3 3 3 13m
[root@master ~]# kubectl get svc //列出所有服务
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 29h
nginx NodePort 10.99.74.11 <none> 80:31173/TCP 27h
4.6 edit 编辑
[root@master ~]# kubectl run nginx --image nginx --labels "app=ppp"
pod/nginx created
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myapp-6d8d776547-2m9lk 1/1 Running 0 42m
myapp-6d8d776547-v9lcd 1/1 Running 0 42m
myapp-6d8d776547-x2drr 1/1 Running 0 42m
nginx 0/1 ContainerCreating 0 6s
nginx-6799fc88d8-7nr4l 1/1 Running 0 73m
test-659fb5c67-xs7h2 0/1 RunContainerError 13 44m
test1-7cbbd465d8-qgx69 1/1 Running 0 41m
//编辑标签
[root@master ~]# kubectl edit pods/nginx
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: "2021-12-20T06:38:09Z"
labels:
app: lry //修改此行
name: nginx
//查看nginx描述
[root@master ~]# kubectl describe pod nginx
Name: nginx
Namespace: default
Priority: 0
Node: node2.example.com/192.168.35.135
Start Time: Mon, 20 Dec 2021 14:38:09 +0800
Labels: app=lry //标签修改成功
Annotations: <none>
Status: Running
IP: 10.244.2.3
4.7 explain 解释
//查看定义文件
[root@master ~]# kubectl explain deployment
KIND: Deployment
VERSION: apps/v1
DESCRIPTION:
Deployment enables declarative updates for Pods and ReplicaSets.
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 metadata.
spec <Object>
Specification of the desired behavior of the Deployment.
status <Object>
Most recently observed status of the Deployment.
[root@master ~]#
4.8 rollout 回滚
有效的资源类型的:
- deployments
- daemonsets
- statefulsets
[root@master ~]# kubectl create deployment nginx --image nginx
deployment.apps/nginx created
// 回本成功
[root@master ~]# kubectl rollout status deployments/nginx
deployment "nginx" successfully rolled out
更新版本之后才能看见回本效果
注意:rollout不能回滚 pod 和 svc 类型
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
test 1/1 Running 0 61m
[root@master ~]# kubectl rollout status pod/test
error: no status viewer has been implemented for Pod
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 46h
nginx ClusterIP 10.108.208.249 <none> 80/TCP 68m
[root@master ~]# kubectl rollout status svc/nginx
error: no status viewer has been implemented for Service
4.9 scale 扩展
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-85b98978db-h2zvv 1/1 Running 0 3m8s
// 动态扩展:之前只有一个,现在变成五个
[root@master ~]# kubectl scale --replicas 5 deployment/nginx
deployment.apps/nginx scaled
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-85b98978db-7jdqg 1/1 Running 0 40s
nginx-85b98978db-h2zvv 1/1 Running 0 4m47s
nginx-85b98978db-k4twc 1/1 Running 0 40s
nginx-85b98978db-lvxq8 1/1 Running 0 40s
nginx-85b98978db-zltqr 1/1 Running 0 40s
// 当你不需要那么多的 deployment/nginx 时候,你可以指定你想留下几个,他会随机删除不需要的
[root@master ~]# kubectl scale --replicas 3 deployment/nginx
deployment.apps/nginx scaled
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-85b98978db-7jdqg 1/1 Running 0 77s
nginx-85b98978db-h2zvv 1/1 Running 0 5m24s
nginx-85b98978db-k4twc 1/1 Running 0 77s
4.10 autoscale 自动扩展
//最少1个,最多5个,cpu占比50%
[root@master ~]# kubectl autoscale --min 1 --max 5 --cpu-percent 50 deployment/nginx
horizontalpodautoscaler.autoscaling/nginx autoscaled
//查看运行情况
[root@master ~]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
nginx Deployment/nginx <unknown>/50% 1 5 0 7s
//自动扩展成功
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myapp-6d8d776547-2m9lk 1/1 Running 0 57m
myapp-6d8d776547-v9lcd 1/1 Running 0 57m
myapp-6d8d776547-x2drr 1/1 Running 0 57m
nginx 1/1 Running 0 14m
nginx-6799fc88d8-7nr4l 1/1 Running 0 88m
test-659fb5c67-xs7h2 0/1 CrashLoopBackOff 15 59m
test1-7cbbd465d8-qgx69 1/1 Running 0 56m
//删除所有 deployment类型的 nginx
[root@master ~]# kubectl delete deployment/nginx
deployment.apps "nginx" deleted
4.11 top 查看资源使用率
top Display resource (CPU/memory) usage
// 运行一个nginx容器,默认为pod类型,名字是test
[root@master ~]# kubectl run test --image nginx
pod/test created
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
test 1/1 Running 0 48s
4.12 describe 描述
//查看test容器的描述
[root@master ~]# kubectl describe pod/test1
Error from server (NotFound): pods "test1" not found
[root@master ~]#
[root@master ~]# kubectl describe pod/nginx
Name: nginx
Namespace: default
Priority: 0
Node: node2.example.com/192.168.35.135
Start Time: Mon, 20 Dec 2021 14:38:09 +0800
Labels: app=lry
Annotations: <none>
Status: Running
IP: 10.244.2.3
......以下省略N行
4.13 logs 查看日志
[root@master ~]# kubectl logs 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
2021/12/20 06:38:26 [notice] 1#1: using the "epoll" event method
2021/12/20 06:38:26 [notice] 1#1: nginx/1.21.4
2021/12/20 06:38:26 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2021/12/20 06:38:26 [notice] 1#1: OS: Linux 3.10.0-862.el7.x86_64
2021/12/20 06:38:26 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/12/20 06:38:26 [notice] 1#1: start worker processes
2021/12/20 06:38:26 [notice] 1#1: start worker process 31
2021/12/20 06:38:26 [notice] 1#1: start worker process 32
2021/12/20 06:38:26 [notice] 1#1: start worker process 33
2021/12/20 06:38:26 [notice] 1#1: start worker process 34
[root@master ~]#
4.14 exec
-it:交互模式
//pod类型查看日期
[root@master ~]# kubectl exec nginx -- date
Mon Dec 20 07:01:32 UTC 2021
//使用交互模式进入
[root@master ~]# kubectl exec -it nginx -- /bin/bash
root@nginx:/# ls
bin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var
boot docker-entrypoint.d etc lib media opt root sbin sys usr
root@nginx:/# exit
exit
command terminated with exit code 127
[root@master ~]#
// 创建一个deployment类型的nginx容器
[root@master ~]# kubectl create deployment nginx --image nginx
deployment.apps/nginx created
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-85b98978db-mns5l 1/1 Running 0 28s
test 1/1 Running 0 45m
// deployment类型查看日期
[root@master ~]# kubectl exec nginx-85b98978db-mns5l -- date
Mon Dec 20 02:25:19 UTC 2021
[root@master ~]# kubectl exec -it nginx-85b98978db-mns5l /bin/bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead. // 未来会删除这个功能
root@nginx-85b98978db-mns5l:/# 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
# svc
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 46h
nginx ClusterIP 10.108.208.249 <none> 80/TCP 56m
// svc类型查看日期
[root@master ~]# kubectl exec svc/nginx -- dateMon Dec 20 02:31:08 UTC 2021
[root@master ~]# kubectl exec -it svc/nginx /bin/sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead. // 未来会删除这个功能
# 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
4.15 转发端口
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myapp-6d8d776547-2m9lk 1/1 Running 0 71m
myapp-6d8d776547-v9lcd 1/1 Running 0 71m
myapp-6d8d776547-x2drr 1/1 Running 0 71m
nginx 1/1 Running 0 28m
nginx-6799fc88d8-8sn8m 1/1 Running 0 3m22s
test-659fb5c67-xs7h2 0/1 CrashLoopBackOff 18 73m
test1-7cbbd465d8-qgx69 1/1 Running 0 70m
[root@master ~]# kubectl port-forward deployment/nginx 80
Forwarding from 127.0.0.1:80 -> 80
Forwarding from [::1]:80 -> 80
// 访问测试
[root@master ~]# curl 127.0.0.1:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@master ~]# kubectl port-forward deployment/nginx :80 // 本机随机端口,容器80端口
Forwarding from 127.0.0.1:34767 -> 80
Forwarding from [::1]:34767 -> 80
Handling connection for 34767
// 访问测试
[root@master ~]# curl 127.0.0.1:34767
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
// 隐藏IP地址
[root@master ~]# kubectl port-forward --address 0.0.0.0 deployment/nginx :80
Forwarding from 0.0.0.0:32841 -> 80
4.16 cp 复制
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
busybox 0/1 Completed 0 17m
nginx-85b98978db-mns5l 1/1 Running 0 31m
test 1/1 Running 0 75m
// 现在 test容器里面的tmp目录没有文件
[root@master ~]# kubectl exec test -- ls /tmp/
[root@master ~]# ls
anaconda-ks.cfg init kube-flannel.yml
// 我把本机家目录下的 anaconda-ks.cfg文件复制到 test容器的 tmp目录下
[root@master ~]# kubectl cp /root/anaconda-ks.cfg test:/tmp/
[root@master ~]# kubectl exec test -- ls /tmp/
anaconda-ks.cfg
4.17 overwrite 覆盖
// 运行一个pod类型的nginx容器,标签设置为app=nginx
[root@master ~]# kubectl run nginx --image nginx --labels "app=test"
pod/nginx created
// 查看描述情况
[root@master ~]# kubectl describe pod nginx
Name: nginx
Namespace: default
Priority: 0
Node: node1.example.com/192.168.35.137
Start Time: Mon, 20 Dec 2021 10:57:33 +0800
Labels: app=test // 标签设置成功
Annotations: <none>
Status: Running
IP: 10.244.1.19
......以下省略
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
busybox 0/1 Completed 0 19m
nginx 1/1 Running 0 21s
nginx-85b98978db-mns5l 1/1 Running 0 33m
test 1/1 Running 0 78m
// 使用 app=test 覆盖掉原有的 app=nginx 标签
[root@master ~]# kubectl label pod nginx --overwrite app=lry
pod/nginx unlabeled
// 查看描述情况
[root@master ~]# kubectl describe pod nginx
Name: nginx
Namespace: default
Priority: 0
Node: node1.example.com/192.168.35.137
Start Time: Mon, 20 Dec 2021 15:29:33 +0800
Labels: app=lry // 标签覆盖成功
Annotations: <none>
Status: Running
IP: 10.244.1.19
......以下省略
// 添加标签
[root@master ~]# kubectl label pod nginx test1=yy
pod/nginx labeled
// 查看描述情况
[root@master ~]# kubectl describe pod nginx
Name: nginx
Namespace: default
Priority: 0
Node: node1.example.com/192.168.35.136
Start Time: Mon, 20 Dec 2021 15:57:33 +0800
Labels: app=lry
test1=yy // 标签添加成功,这里不是覆盖了
4.18 api-resources 查看api资源详细信息
[root@master ~]# kubectl api-resources
NAME SHORTNAMES APIVERSION NAMESPACED KIND
bindings v1 true Binding
componentstatuses cs v1 false ComponentStatus
configmaps cm v1 true ConfigMap
endpoints ep v1 true Endpoints
events ev v1 true Event
limitranges limits v1 true LimitRange
namespaces ns v1 false Namespace
nodes no v1 false Node
persistentvolumeclaims pvc v1 true PersistentVolumeClaim
persistentvolumes pv v1 false PersistentVolume
pods po v1 true Pod
......以下省略N行
4.19 api-versions
[root@master ~]# kubectl api-versions
admissionregistration.k8s.io/v1
apiextensions.k8s.io/v1
apiregistration.k8s.io/v1
apps/v1
authentication.k8s.io/v1
authorization.k8s.io/v1
autoscaling/v1
autoscaling/v2
autoscaling/v2beta1
autoscaling/v2beta2
batch/v1
batch/v1beta1
certificates.k8s.io/v1
coordination.k8s.io/v1
discovery.k8s.io/v1
discovery.k8s.io/v1beta1
events.k8s.io/v1
events.k8s.io/v1beta1
flowcontrol.apiserver.k8s.io/v1beta1
flowcontrol.apiserver.k8s.io/v1beta2
networking.k8s.io/v1
node.k8s.io/v1
node.k8s.io/v1beta1
policy/v1
policy/v1beta1
rbac.authorization.k8s.io/v1
scheduling.k8s.io/v1
storage.k8s.io/v1
storage.k8s.io/v1beta1
v1