pod管理
应用部署
上传测试镜像
[root@k8s1 docker]# docker push reg.westos.org/library/myapp:v1
[root@k8s1 docker]# docker push reg.westos.org/library/myapp:v2
创建自助式pod(生产不推荐)
[root@k8s2 ~]# kubectl run demo --image=myapp:v1
[root@k8s2 ~]# kubectl get pod -o wide //使用 wide 输出格式,可以展示更多的列,包括 Node(Pod 所在的节点)、IP、状态、重启次数、版本等信息
查看pod详情
[root@k8s2 ~]# kubectl describe pod demo //获取指定 Pod 的各项详细信息,包括容器的状态、日志、事件等

删除pod
[root@k8s2 ~]# kubectl delete pod demo
创建控制器(推荐)
[root@k8s2 ~]# kubectl create deployment myapp --image=myapp:v1 --replicas=3
//创建一个 Deployment 对象的命令,用于管理 Pod,管理的pod副本数为3
控制器自动维护pod副本数
[root@k8s2 ~]# kubectl get pod
[root@k8s2 ~]# kubectl get deployments.apps //返回所有 Deployments 的名称、可用副本数、所需副本数、更新策略等详细信息

[root@k8s2 ~]# kubectl delete pod myapp-67984c8646-4j6fs
[root@k8s2 ~]# kubectl get deployments.apps
[root@k8s2 ~]# kubectl get pod
在远程pod中执行命令
[root@k8s2 ~]# kubectl exec myapp-67984c8646-nwtl9 -- ls /usr/share/nginx/html
//在 Pod myapp-678fcbc488-gqdgk 中执行命令 ls /usr/share/nginx/html
扩容pod数量
[root@k8s2 ~]# kubectl scale deployment myapp --replicas=6
[root@k8s2 ~]# kubectl get pod

缩容pod数量
[root@k8s2 ~]# kubectl scale deployment myapp --replicas=3
[root@k8s2 ~]# kubectl get pod

通过service暴露pod
[root@k8s2 ~]# kubectl expose deployment myapp --port=80 --target-port=80
//kubectl expose deployment myapp 命令创建了一个 Service,并将 Deployment myapp 暴露在集群内部的网络中。
--port 参数指定了 Service 监听的端口号,这里为 80;
--target-port 参数指定了 Service 转发流量到 Deployment 中的 Pod 时所使用的端口号,这里同样为 80。
查看svc详情
[root@k8s2 ~]# kubectl describe svc myapp
//获取了名为 myapp 的 Service 的详细信息。该命令输出的信息包括:
Service 的名称、命名空间、标签等基本信息;
Service 的类型(ClusterIP、NodePort、LoadBalancer 等);
Service 的端口信息,包括监听的端口号、协议(TCP/UDP)等;
Service 关联的 Pod 信息,包括选择器、关联的 Pod 的 IP 地址、副本数等;
Service 的 Endpoints 信息,包括关联的 Pod 的 IP 地址和端口号。
[root@k8s2 ~]# curl 10.96.20.8
[root@k8s2 ~]# curl 10.96.20.8/hostname.html
service自动发现pod扩容与缩容,自动更新endpoints,实现对应用的负载均衡
service默认使用clusterip类型,只能在集群中访问
nodeport类型,可以在集群外部访问
[root@k8s2 ~]# kubectl edit svc myapp

[root@k8s2 ~]# kubectl get svc
访问集群任意节点+端口

更新应用版本
[root@k8s2 ~]# kubectl set image deployment/myapp myapp=myapp:v2 //将 myapp 容器的镜像更新为 myapp:v2
[root@k8s1 docker]# curl 192.168.81.11:31902


查看应用历史版本
[root@k8s2 ~]# kubectl rollout history deployment myapp //用于查看 Deployment myapp中所有副本集的升级历史记录
回滚
[root@k8s2 ~]# kubectl rollout undo deployment myapp --to-revision=1

删除应用
[root@k8s2 ~]# kubectl delete deployments.apps myapp
[root@k8s2 ~]# kubectl delete svc myapp
[root@k8s2 ~]# kubectl get ns
集群通过namespace来做资源隔离,默认操作的资源都指向default

编写yaml文件
获取帮助
[root@k8s2 pod]# kubectl explain pod.spec.containers
获取yaml模版
[root@k8s2 pod]# kubectl run demo --image nginx --dry-run=client -o yaml > pod.yaml
//--dry-run=client:使用客户端模式,即不会真正创建 Pod,仅会返回 YAML 格式的定义文件
-o yaml:以 YAML 格式输出定义文件
> pod.yaml:将输出的 YAML 定义文件保存到当前目录下的 pod.yaml 文件中
执行该命令会生成一个名为 pod.yaml 的文件,其中包含了一个定义 nginx 镜像 Pod 的 YAML 文件
[root@k8s2 pod]# vim pod.yaml
apiVersion: v1 //Kubernetes API的版本
kind: Pod //资源类型,这里是pod
metadata: //元数据对象,用于述 Pod 的基本信息,例如标签,名称等。
labels:
run: demo
name: demo
spec: //制定了pod的具体配置
containers: //定义了 Pod 中的容器
- image: nginx // 指定容器使用的镜像,这里使用的是nginx。
name: demo
imagePullPolicy: IfNotPresent //指定镜像拉取策略为IfNotPresent,表示如果本地已经存在相同版本的镜像,则不会从镜像仓库中拉取

创建pod
[root@k8s2 pod]# kubectl create -f pod.yaml //根据pod.yaml文件中的定义来创建一个新的 Kubernetes Pod对象
查看详情
[root@k8s2 pod]# kubectl get pod -o wide
[root@k8s2 pod]# kubectl describe pod demo
[root@k8s2 pod]# kubectl get pod demo -o yaml
//kubectl get pod -o wide命令会以表格形式显示所有的 Pod 对象,其中包括名称、状态、所在节点、IP地址、版本等信息。-o wide选项可以扩展输出内容,显示更多信息。
kubectl describe pod demo命令会以文本形式展示指定名称为 demo 的 Pod 对象的详细信息,包括 Pod 的元数据、各容器的信息、生命周期事件、存储和网络配置等。
kubectl get pod demo -o yaml命令会以 YAML 格式展示指定名称为 demo 的 Pod 对象的详细信息,包括 Pod 的元数据、各容器的信息、存储和网络配置等。这种格式的输出可以用于创建或修复 Pod 对象。
示例
1、

2、


3、


Guaranteed
,保证级别:Pod 中的容器都设置了 CPU 和 Memory 的 requests 和 limits,并且这些值相等。此时,如果资源满足所有 Guaranteed 类别的 Pod 的需求,则这些 Pod 会被同时调度在集群中,保证它们的运行。Burstable
,突发级别:Pod 中至少有一个容器设置了 CPU 和 Memory 的 requests 和 limits,但是这些值不相等。此时,如果资源足够,那么这些 Pod 会被相应地调度,但是它们的性能可能会受到限制,因为它们能够使用的资源是有上限的。BestEffort
,尽力级别:Pod 中的容器没有设置 CPU 和 Memory 的 requests 和 limits。此时,这些容器会尽力地获取集群中未被占用的资源,但是它们的资源使用是无法保证的,因为它们需要等待集群中已分配的所有资源使用完毕才能够获得更多的资源。
4、

5、
[root@k8s2 pod]# kubectl apply -f pod.yml //更新该 Pod 的配置
[root@k8s2 pod]# kubectl delete -f pod.yml //删除pod
init容器
[root@k8s2 pod]# vim init-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app.kubernetes.io/name: MyApp
spec:
containers:
- name: myapp-container
image: busybox
command: ['sh', '-c', 'echo The app is running! && sleep 3600']
initContainers:
- name: init-myservice
image: busybox
command: ['sh', '-c', "until nslookup myservice.default.svc.cluster.local; do echo waiting for myservice; sleep 2; done"]

[root@k8s2 pod]# kubectl apply -f init-pod.yaml
[root@k8s2 pod]# kubectl get pod
在init容器没有成功运行之前,主容器不会被运行
添加svc定义
[root@k8s2 pod]# vim myservice.yaml
---
apiVersion: v1
kind: Service
metadata:
name: myservice
spec:
ports:
- protocol: TCP
port: 80
targetPort: 9376

svc解析成功后,init容器退出,主容器运行
[root@k8s2 pod]# yum install -y bind-utils
[root@k8s2 pod]# dig -t A myservice.default.svc.cluster.local. @10.96.0.10
[root@k8s2 pod]# kubectl get pod

探针
存活探针
[root@k8s2 pod]# vim liveness-pod.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: liveness-http
spec:
containers:
- name: liveness
image: nginx
livenessProbe:
tcpSocket:
port: 8080
initialDelaySeconds: 3
periodSeconds: 3
[root@k8s2 pod]# kubectl apply -f liveness-pod.yaml

[root@k8s2 pod]# kubectl apply -f liveness-pod.yaml
在存活探针检测失败导致容器不断被重启

就绪探针
[root@k8s2 pod]# vim liveness-pod.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: liveness-http
spec:
containers:
- name: liveness
image: nginx
livenessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 3
periodSeconds: 3
readinessProbe:
httpGet:
path: /test.html
port: 80
initialDelaySeconds: 5
periodSeconds: 5

就绪探针失败导致容器一直未就绪


创建测试页面
[root@k8s2 pod]# kubectl exec liveness-http -- touch /usr/share/nginx/html/test.html
就绪探针成功
创建svc
[root@k8s2 pod]# kubectl expose pod liveness-http --port 80 --target-port 80
就绪容器自动上线

删除测试页面
[root@k8s2 pod]# kubectl exec liveness-http -- rm /usr/share/nginx/html/test.html
就绪探针失败,容器未就绪

在svc中容器自动下线

控制器
replicaset
ReplicaSet用于保证指定数量的 Pod 副本一直运行
[root@k8s2 pod]# vim rs-example.yml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: replicaset-example
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx

replicaset是通过标签匹配pod
[root@k8s2 pod]# kubectl label pod replicaset-example-n2kf2 app=myapp --overwrite //n2kf2 myapp标签

[root@k8s2 pod]# kubectl label pod replicaset-example-n2kf2 app=nginx --overwrite
replicaset自动控制副本数量,pod可以自愈
[root@k8s2 pod]# kubectl delete pod replicaset-example-n2kfs

deployment
Deployment 的主要作用是实现应用程序的无缝升级和回滚
[root@k8s2 pod]# vim deployment-example.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-example
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:v1


版本迭代
[root@k8s2 pod]# vim deployment-example.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-example
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:v2

replicaset就是deployment的一个版本

回滚
[root@k8s2 pod]# vim deployment-example.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-example
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:v1
滚动更新策略
[root@k8s2 pod]# vim deployment-example.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-example
spec:
minReadySeconds: 5
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:v1
//minReadySeconds 的值为 5 秒,表示在新版本的 Pod 启动后,需要等待至少 5 秒才会继续滚动升级。maxSurge 的值为 1,表示在滚动升级的过程中,除了当前的 Pod 数量之外,最多会创建一个新的 Pod。maxUnavailable 的值为 0,表示在滚动升级的过程中,不允许任何 Pod 不可用


暂停与恢复
暂停,避免触发不必要的线上更新
[root@k8s2 pod]# kubectl rollout pause deployment deployment-example
[root@k8s2 pod]# vim deployment-example.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-example
spec:
minReadySeconds: 5
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
replicas: 6
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: nginx
resources:
limits:
cpu: 0.5
memory: 200Mi
requests:
cpu: 0.5
memory: 200Mi

调整副本数,不受影响
但是更新镜像和修改资源并没有触发更新
恢复后开始触发更新
[root@k8s2 pod]# kubectl rollout resume deployment deployment-example

statefulset
当使用StatefulSet时,Pod的名称会包括一个索引,例如web-0
,web-1
,web-2
等,这些索引是按顺序递增的,并且在Pod的生命周期中保持不变
StatefulSet保证每个Pod的名称在整个集群中是唯一的,例如,如果您有一个名为web-0
的Pod,任何时候都不能重新创建一个叫做web-0
的Pod。
[root@k8s2 pod]# vim statefulset.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
selector:
matchLabels:
app: nginx
serviceName: "nginx"
replicas: 3
minReadySeconds: 3
template:
metadata:
labels:
app: nginx
spec:
terminationGracePeriodSeconds: 10
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
name: web
daemonset
Taints(污点)是一种标记,可以应用于节点,用于表示这个节点上的某些条件不适合运行某些 Pod。



添加回来

[root@k8s2 pod]# vim daemonset-example.yml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: daemonset-example
spec:
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
tolerations:
- effect: NoSchedule //表示不允许调度新的 Pod 到该节点上
operator: Exists //表示只要这个污点存在,就能容忍。
containers:
- name: nginx
image: nginx
job
Job是用于处理一次性任务的控制器,它可以保证任务成功完成后自动停止。Job中包含一个或多个Pods,在Pods成功完成后便完成任务。如果Pods失败,则Job会重启Pods,直到任务完成。
[root@k8s2 pod]# vim job.yml
apiVersion: batch/v1
kind: Job
metadata:
name: pi
spec:
completions: 6 //完成任务的Pod数量
parallelism: 2 //并行执行的Pod数量
template:
spec:
containers:
- name: pi
image: perl:5.34.0
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
restartPolicy: Never //完成后不会重启
backoffLimit: 4 //当任务执行失败时,最多重试的次数



cronjob
CronJob是用于处理周期性任务的控制器,它类似于传统的Cron定时任务,可以基于指定的时间或时间间隔来执行任务。CronJob会创建一个Job对象来运行任务,Job对象运行完成后自动被删除。如果Job对象出现错误,CronJob将会自动重启Job对象,直到任务成功完成。
[root@k8s2 pod]# vim cronjob.yml
apiVersion: batch/v1
kind: CronJob
metadata:
name: hello
spec:
schedule: "* * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox
imagePullPolicy: IfNotPresent
command:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
restartPolicy: OnFailure
//每分钟的每一秒都会执行一次,使用busybox镜像来打印当前时间并输出"Hello from the Kubernetes cluster"
