Kuberneters Pod调度基础

一、Replication Controller

Replication Controller(复制控制器,RC),使Pod副本保持在预期值

[root@k8s-master ~]# vim replicationcontroller-nginx.yaml

apiVersion: v1

kind: ReplicationController

metadata:

name: nginx

spec:

replicas: 3

selector:

app: nginx

template:

metadata:

name: nginx

labels:

app: nginx

spec:

containers:

  • name: nginx

image: nginx:1.7.9

ports:

  • containerPort: 80

(1)创建replicationController

[root@k8s-master ~]# ku create -f replicationcontroller-nginx.yaml

replicationcontroller/nginx created

[root@k8s-master ~]# ku get pod

NAME READY STATUS RESTARTS AGE

nginx-bmsx8 1/1 Running 0 8s

nginx-brpgz 1/1 Running 0 8s

(2)删除Pod

[root@k8s-master ~]# ku delete pod nginx-bmsx8 -n default

pod "nginx-bmsx8" deleted

[root@k8s-master ~]# ku get pod

NAME READY STATUS RESTARTS AGE

nginx-9hbr6 1/1 Running 0 4s

nginx-brpgz 1/1 Running 0 12m

(3)删除replicationcontroller

[root@k8s-master ~]# ku delete -f replicationcontroller-nginx.yaml

replicationcontroller "nginx" deleted

二、ReplicaSet

ReplicaSet (复制集,RS)基于集合的标签选择器的下一代Replication Controller,用于Deployment协调创建,删除和更新。ReplicaSet支持标签选择器,建议使用Deployment自动管理ReplicaSet。

标签是用来标识k8s对象的一组附加在其上的一组键值对,通过标签筛选和排除一组对象。

标签选择器用来选择一组对(标签并不能表示唯一一个对象)
标签选择器
标签定义的方式:
基于等式的定义: app=nginx
基于键值对定义方式: app : nginx
基于集合标签定义方式: {key : app,operator:in,values:[nginx,apache]}

(1)编辑ReplicaSet文件

[root@k8s-master ~]# vim replicaset-example.yaml

apiVersion: apps/v1

kind: ReplicaSet

metadata:

name: frontend

labels:

app: guestbook

tier: frontend

spec:

modify replicas according to your case

replicas: 3

selector:

matchLabels:

tier: frontend

matchExpressions:

  • {key: tier, operator: In, values: [frontend]}

template:

metadata:

labels:

app: guestbook

tier: frontend

spec:

containers:

  • name: php-redis

image: nginx:1.7.9

resources:
requests:
cpu: 100m
memory: 100Mi

env:

  • name: GET_HOSTS_FROM

value: dns

If your cluster config does not include a dns service, then to

instead access environment variables to find service host

info, comment out the 'value: dns' line above, and uncomment the

line below.

value: env

ports:

  • containerPort: 80

备注:

requests:请求资源限制,分配资源必须达到此要求

limits:最多可以请求多少资源

单位m:cpu计量单位是毫核(m)。一个节点cpu核心数量乘以1000,得到总结点cpu数量。如:一个节点有两核,那么该节点cpu总和量为2000m,该容器启动时为100/2000核心(5%)。

(2)创建RS

[root@k8s-master ~]# ku create -f replicaset-example.yaml

replicaset.apps/frontend created

(3)查看Pod

[root@k8s-master ~]# ku get pod

NAME READY STATUS RESTARTS AGE

frontend-8dv56 1/1 Running 0 8s

frontend-d6d82 1/1 Running 0 8s

frontend-dzmgf 1/1 Running 0 8s

(4)删除RS

[root@k8s-master ~]# ku delete -f replicaset-example.yaml

replicaset.apps "frontend" deleted

三、无状态应用管理Deployment

无状态服务(stateless service)单次进行请求处理,不依赖其他请求。Deployment用来管理RS,为Pod和RS提供声明性更新,以及其他功能,生产环境中使用Deployment替代RS。

[root@k8s-master ~]# vim nginx-deployment.yaml

apiVersion: apps/v1

kind: Deployment

metadata:

name: nginx-deployment

labels:

name: nginx-deployment

spec:

replicas: 2

selector:

matchLabels:

app: nginx

template:

metadata:

labels:

app: nginx

spec:

containers:

  • name: nginx

image: nginx:1.7.9

ports:

  • name: nginx

containerPort: 80

(1)创建Deployment

[root@k8s-master ~]# ku create -f nginx-deployment.yaml

deployment.apps/nginx-deployment created

[root@k8s-master ~]# ku get pod

NAME READY STATUS RESTARTS AGE

nginx-deployment-6cf9b75cdd-56qzm 1/1 Running 0 9s

nginx-deployment-6cf9b75cdd-sxs5t 1/1 Running 0 9s

(2)更新Deployment

[root@k8s-master ~]# ku set image deployment nginx-deployment nginx=nginx:1.9.1 --record

Flag --record has been deprecated, --record will be removed in the future

[root@k8s-master ~]# ku set image deployment nginx-deployment nginx=nginx:1.12.1 --record

Flag --record has been deprecated, --record will be removed in the future

deployment.apps/nginx-deployment image updated

(3)查看更新

[root@k8s-master ~]# ku rollout status deployment.v1.apps/nginx-deployment

deployment "nginx-deployment" successfully rolled out

[root@k8s-master ~]# ku get rs

NAME DESIRED CURRENT READY AGE

nginx-deployment-55bbd8478b 2 2 2 6m50s

nginx-deployment-6cf9b75cdd 0 0 0 11m

nginx-deployment-7569c477b6 0 0 0 7m14s

[root@k8s-master ~]# ku describe deploy nginx-deployment (查看详细信息)

(4)回滚更新

[root@k8s-master ~]# ku set image deployment nginx-deployment nginx=dotbalo/canary:v1 --record

Flag --record has been deprecated, --record will be removed in the future

deployment.apps/nginx-deployment image updated

[root@k8s-master ~]# ku set image deployment nginx-deployment nginx=dotbalo/canary:v3 --record

Flag --record has been deprecated, --record will be removed in the future

deployment.apps/nginx-deployment image updated

(5)查看历史版本

[root@k8s-master ~]# ku rollout history deployment/nginx-deployment

deployment.apps/nginx-deployment

REVISION CHANGE-CAUSE

1 <none>

2 kubectl set image deployment nginx-deployment nginx=nginx:1.9.1 --record=true

3 kubectl set image deployment nginx-deployment nginx=nginx:1.12.1 --record=true

4 kubectl set image deployment nginx-deployment nginx=dotbalo/canary:v1 --record=true

5 kubectl set image deployment nginx-deployment nginx=dotbalo/canary:v3 --record=true

(6)查看某次更新详情

[root@k8s-master ~]# ku rollout history deployment/nginx-deployment --revision=3

deployment.apps/nginx-deployment with revision #3

Pod Template:

Labels: app=nginx

pod-template-hash=55bbd8478b

Annotations: kubernetes.io/change-cause: kubectl set image deployment nginx-deployment nginx=nginx:1.12.1 --record=true

Containers:

nginx:

Image: nginx:1.12.1

Port: 80/TCP

Host Port: 0/TCP

Environment: <none>

Mounts: <none>

Volumes: <none>

(7)回滚到指定版本

[root@k8s-master ~]# ku rollout undo deployment/nginx-deployment --to-revision=1

[root@k8s-master ~]# ku rollout history deployment/nginx-deployment (查看)

[root@k8s-master ~]#ku rollout undo deployment/nginx-deployment (回滚到上次版本)

(8)扩容Deployment

[root@k8s-master ~]# ku scale deployment.v1.apps/nginx-deployment --replicas=6

deployment.apps/nginx-deployment scaled

[root@k8s-master ~]# ku get pod

NAME READY STATUS RESTARTS AGE

nginx-deployment-5985c475f6-94jf7 1/1 Running 0 8s

nginx-deployment-5985c475f6-rvj95 1/1 Running 0 8s

nginx-deployment-5985c475f6-tzhk4 1/1 Running 0 16m

nginx-deployment-5985c475f6-vlvcv 1/1 Running 0 8s

nginx-deployment-86d6564694-4tsbm 0/1 ImagePullBackOff 0 16m

nginx-deployment-86d6564694-f7qps 0/1 ContainerCreating 0 8s

nginx-deployment-86d6564694-gpfrp 0/1 ContainerCreating 0 8s

nginx-deployment-86d6564694-lpdlg 0/1 ContainerCreating 0 8s

(9)暂停恢复更新

[root@k8s-master ~]# ku rollout pause deployment/nginx-deployment (暂停更新)

deployment.apps/nginx-deployment paused

[root@k8s-master ~]# ku rollout resume deployment/nginx-deployment (恢复更新)

deployment.apps/nginx-deployment resumed

(10)删除Deployment

[root@k8s-master ~]# ku delete -f nginx-deployment.yaml

deployment.apps "nginx-deployment" deleted

四、有状态应用管理 StatefulSet

StatefulSet(有状态集,sts)用于部署有状态且有序启动应用程序。

[root@k8s-master ~]# cat redis-statefulset.yaml

apiVersion: v1

kind: Service

metadata:

name: redis-svc

spec:

selector:

app: redis-sts

ports:

  • port: 6379

protocol: TCP

targetPort: 6379

apiVersion: apps/v1

kind: StatefulSet

metadata:

name: redis-sts

spec:

serviceName: redis-svc

replicas: 2

selector:

matchLabels:

app: redis-sts

template:

metadata:

labels:

app: redis-sts

spec:

containers:

  • image: redis:5-alpine

name: redis

ports:

  • containerPort: 6379

备注:

kind:service 定义一个名字为reids-svc

kind:statefulSet 定义名字为redis-sts的statefulset,replicas表示pod副本数。

(1)创建sts

[root@k8s-master ~]# ku create -f redis-statefulset.yaml

statefulset.apps/redis-sts created

(2)查看sts状态

[root@k8s-master ~]# ku get sts

NAME READY AGE

redis-sts 2/2 28s

[root@k8s-master ~]# ku get pod

NAME READY STATUS RESTARTS AGE

redis-sts-0 1/1 Running 0 30s

redis-sts-1 1/1 Running 0 20s

备注:

序号越小,创建时间越早,从AGE列可以看出,解决有状态启动顺序问题,例如,可以使redis-sts-0作为主节点,redis-sts-1作为从节点。

(3)sts扩缩容

[root@k8s-master ~]# ku scale sts redis-sts --replicas=3 (扩容)

statefulset.apps/redis-sts scaled

[root@k8s-master ~]# ku get pod

NAME READY STATUS RESTARTS AGE

redis-sts-0 1/1 Running 0 9m54s

redis-sts-1 1/1 Running 0 9m44s

redis-sts-2 1/1 Running 0 7s

[root@k8s-master ~]# ku scale sts redis-sts --replicas=2 (缩容)

statefulset.apps/redis-sts scaled

[root@k8s-master ~]# ku get pod

NAME READY STATUS RESTARTS AGE

redis-sts-0 1/1 Running 0 10m

redis-sts-1 1/1 Running 0 10m

(4)非级联删除

删除sts有两种方式:级联删除和非级联删除

使用非级联方式删除 statefulset 时,statefulSet 的 Pod 不会被删除。使用级联方式删除statefulSet 时,statefulSet 和它的 Pod 都会被删除。

[root@k8s-master ~]# kubectl delete statefulsetredis-sts--cascade=false 采用非级联删除

[root@k8s-master ~]# kubectl get sts 查看

[root@k8s-master ~]# kubectl get pod 查看管理的 pod发现 pod 并没有被删除

[root@k8s-master ~]# kubectl delete po redis-sts-0 删除 pod

[root@k8s-master ~]# kubectl delete po redis-sts-1

(5)级联删除

[root@k8s-master ~]# kubectl create -f redis-statefulset.yaml (先创建出 statefulset)
备注:
如果提示服务已存在,先删除kubectl delete -f redis-statefulset.yaml

[root@k8s-master ~]# kubectl delete statefulset redis-sts (级联删除)

[root@k8s-master ~]# ku get pod (查看pod,则pod全部删除)

[root@k8s-master ~]# ku delete -f redis-statefulset.yaml (删除redis)

五、守护进程集 DaemonSet

daemonset(守护进程集,缩写为ds)和守护进程类似,确保全部节点上运行一个Pod副本,当新的节点加入时,也会新增一个Pod;当节点移除时,Pod会被回收,删除daemonset会删除所有Pod。

[root@k8s-master ~]# vim daemonset-nginx.yaml

apiVersion: apps/v1

kind: DaemonSet

metadata:

name: pod-controller

namespace: dev

labels:

controller: daemonset

spec:

selector:

matchLabels:

app: nginx-pod

template:

metadata:

labels:

app: nginx-pod

spec:

containers:

  • name: nginx

image: nginx:1.7.9

ports:

  • name: nginx-port

containerPort: 80

protocol: TCP

(1)创建daemonset

[root@k8s-master ~]# ku create namespace dev

namespace/dev created

[root@k8s-master ~]# ku create -f daemonset-nginx.yaml

daemonset.apps/pod-controller created

(2)查看daemonset

[root@k8s-master ~]# ku get ds -n dev -o wide

(3)查看所在节点

[root@k8s-master ~]# ku get pod -n dev -o wide

(4)删除 DaemonSet

[root@k8s-master ~]# kubectl delete ds pod-controller -n dev

六、计划任务CronJob

cronjob (计划任务,缩写为 cj) 用于以时间为基准的周期性地执行任务,这些自动化任务和运行在 Linux系统上的 CronJob 一样。

[root@k8s-master ~]# cat cronjob-example.yaml

apiVersion: batch/v1 #1.21版本以上 改为batch/v1

kind: CronJob

metadata:

name: hello

spec:

schedule: "*/1 * * * *"

jobTemplate:

spec:

template:

spec:

containers:

  • name: hello

image: busybox:v1

args:

  • /bin/sh

  • -c

  • date; echo Hello from the Kubernetes cluster

restartPolicy: OnFailure

(1)创建 Cronjob

[root@k8s-master ~]# kubectl create -f cronjob-example.yaml

(2)查看

[root@k8s-master ~]# kubectl get cj

等待一会后可以查看生成的 pod

(3)查看pod,并生成日志

[root@k8s-master ~]# kubectl get jobs

[root@k8s-master ~]# kubectl get pod 查看生成的 pod

[root@k8s-master ~]# kubectl logs -f hel1o-27743522-crnf8 查看 pod 的执行日志

(4)删除

[root@k8s-master ~]# ku delete cronjob hello

相关推荐
内核程序员kevin3 小时前
TCP Listen 队列详解与优化指南
linux·网络·tcp/ip
朝九晚五ฺ7 小时前
【Linux探索学习】第十四弹——进程优先级:深入理解操作系统中的进程优先级
linux·运维·学习
自由的dream7 小时前
Linux的桌面
linux
xiaozhiwise8 小时前
Makefile 之 自动化变量
linux
Kkooe8 小时前
GitLab|数据迁移
运维·服务器·git
wuxingge9 小时前
k8s1.30.0高可用集群部署
云原生·容器·kubernetes
久醉不在酒9 小时前
MySQL数据库运维及集群搭建
运维·数据库·mysql
志凌海纳SmartX10 小时前
趋势洞察|AI 能否带动裸金属 K8s 强势崛起?
云原生·容器·kubernetes
锅总10 小时前
nacos与k8s service健康检查详解
云原生·容器·kubernetes