【容器应用系列教程】kubernetes无状态负载集deployment
一、关于deployment无状态负载集介绍
- 实际使用时,不建议直接创建Pod
- deployment创建流程- deployement—>- RS(副本集) ---->- Pod
 
- deployment优势- 自动维护Pod的副本
- 支持滚动更新
 
- 自动维护
- 适用场景:频繁更新的业务
- 标签选择器(重要)
- 每种资源都会有对应的标签,标签的实际体现是键值对的数据
- 利用标签在RS副本集、POD之间映射关系
 
二、创建deployment
1.创建
[root@k8s-master wordpress]# vim nginx.yml 
apiVersion: apps/v1
kind: Deployment	#资源类型为Deployment
metadata:
    name: nginx
    namespace: wordpress
spec:
    replicas: 4	#创建4个副本集
    selector:
        matchLabels:
            app: nginx	#对应的标签,以键值对的方式,此处应与下面的标签保持一致
    template:
        metadata:
            labels:
                app: nginx	#创建一个对应的标签,用于上面replicas控制副本集
        spec:
            containers:
            - name: nginx
              image: nginx:1.20
              imagePullPolicy: IfNotPresent
              ports:
              - containerPort: 80
[root@k8s-master wordpress]# kubectl create -f nginx.yml 
deployment.apps/nginx created
[root@k8s-master wordpress]# kubectl get pod -n wordpress
NAME                     READY   STATUS    RESTARTS   AGE
nginx-6897679c4b-gbpkq   1/1     Running   0          9s
nginx-6897679c4b-szbkr   1/1     Running   0          9s
nginx-6897679c4b-vlqhj   1/1     Running   0          9s
nginx-6897679c4b-wckrb   1/1     Running   0          9s
2.查看所有无状态负载创建的容器
[root@k8s-master wordpress]# kubectl get deploy -n wordpress
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   4/4     4            4           53s
- READY显示可用且就绪的副本数
- UP-TO-DATE显示已更新到最新版本的副本数
- AVAILABLE表示可用于处理请求的副本数
- AGE表示部署的运行时间
3.查看副本数
[root@k8s-master wordpress]# kubectl get rs -n wordpress
NAME               DESIRED   CURRENT   READY   AGE
nginx-6897679c4b   4         4         4       2m18s
- DESIRED预期副本数
- CURRENT当前副本数
- READY就绪的副本数
4.测试副本数
故意删除一个Pod
[root@k8s-master wordpress]# kubectl delete pod nginx-6897679c4b-wckrb -n wordpress
pod "nginx-6897679c4b-wckrb" deleted
被删除的
Pod状态变成正在停止
同时,又创建了一个新的Pod出来
[root@k8s-master ~]# kubectl get pod -n wordpress
NAME                     READY   STATUS        RESTARTS   AGE
nginx-6897679c4b-gbpkq   1/1     Running       0          15m
nginx-6897679c4b-kndqq   1/1     Running       0          4s	#副本集又自动创建了一个新的
nginx-6897679c4b-szbkr   1/1     Running       0          15m
nginx-6897679c4b-vlqhj   1/1     Running       0          15m
nginx-6897679c4b-wckrb   0/1     Terminating   0          15m
5.滚动更新
[root@k8s-master wordpress]# vim nginx.yml
apiVersion: apps/v1
kind: Deployment
metadata:
    name: nginx
    namespace: wordpress
spec:
    replicas: 4
    selector:
        matchLabels:
            app: nginx
    strategy:	#滚动更新
        type: RollingUpdate
        rollingUpdate:
            maxSurge: 2	#同时更新2个,一共4个容器
            maxUnavailable: 1	#允许更新失败1个,超过1个则视为整体更新失败
    template:
        metadata:
            labels:
                app: nginx
        spec:
            containers:
            - name: nginx
              image: nginx:latest	#更换最新版标签
              imagePullPolicy: IfNotPresent
              ports:
              - containerPort: 80
- template- 下面所有配置与直接定义pod信息一样
 
- 下面所有配置与直接定义
- replicas- 指定容器的副本数
 
- selector- 指定标签选举器的工作方式,让选择器通过哪个标签来确认容器的副本数量正常
 
- strategy- 指定容器的滚动升级
- maxSurge- 每次升级的百分比,也可以是个绝对数字
 
- maxUnavailable- 升级失败的百分比,也可以是个绝对数字
 
- 支持的升级方式:
- Rollupdateing一次更新一批,一批完成后再继续更新
- ReCreate干掉所有容器,重新创建
 
 
[root@k8s-master wordpress]# kubectl apply -f ./nginx.yml
查看更新过程
[root@k8s-master ~]# kubectl get pod -n wordpress
NAME                     READY   STATUS        RESTARTS   AGE
nginx-6897679c4b-gbpkq   0/1     Terminating   0          24m
nginx-6897679c4b-szbkr   0/1     Terminating   0          24m	#正在停止旧版本的Pod
nginx-75b69bd684-6pjlg   1/1     Running       0          5s
nginx-75b69bd684-krk7g   1/1     Running       0          3s
nginx-75b69bd684-l546x   1/1     Running       0          5s
nginx-75b69bd684-m48jq   1/1     Running       0          5s
验证新版本容器
[root@k8s-master ~]# kubectl describe pod nginx-75b69bd684-l546x -n wordpress | grep -i "image"
    Image:          nginx:latest	#从1.20升级到了最新版本
6.回滚更新
查看更新历史
[root@k8s-master ~]# kubectl rollout history deploy nginx -n wordpress
deployment.apps/nginx 
REVISION  CHANGE-CAUSE	#可以看到有2个版本
1         <none>	#刚创建容器的版本
2         <none>	#现在的版本
版本回退
[root@k8s-master ~]# kubectl rollout undo deployment nginx -n wordpress --to-revision=1
deployment.apps/nginx rolled back
查看回退过程
[root@k8s-master wordpress]# kubectl get pod -n wordpress
NAME                     READY   STATUS        RESTARTS   AGE
nginx-6897679c4b-8mdtq   1/1     Running       0          9s
nginx-6897679c4b-bl48l   1/1     Running       0          8s
nginx-6897679c4b-jrwwp   1/1     Running       0          9s
nginx-6897679c4b-wxtmz   1/1     Running       0          9s	#旧版本已经正在运行
nginx-75b69bd684-6pjlg   0/1     Terminating   0          7m15s
nginx-75b69bd684-m48jq   0/1     Terminating   0          7m15s	#新版本正在被停止
验证版本
[root@k8s-master wordpress]# kubectl describe pod nginx-6897679c4b-jrwwp -n wordpress | grep -i "image"
    Image:          nginx:1.20	#可以看到版本回退到了1.20
三、DeamonSet守护进程集
1.作用
- 保证每个node节点运行且只运行一个pod- 例如监控应用,每个节点上都需要agent,或者ELK中的Filebeat收集日志
 
- 例如监控应用,每个节点上都需要
2.创建DeamonSet
[root@k8s-master wordpress]# vim daremonset.yml
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: nginx-daemon
  namespace: wordpress
  labels:
    k8s-app: nginx-daemon
spec:
  selector:
    matchLabels:
      app: nginx-daemon
  template:
    metadata:
      labels:
        app: nginx-daemon
    spec:
      containers:
      - name: nginx-daemon
        image: nginx
        imagePullPolicy: IfNotPresent
        tty: true
[root@k8s-master wordpress]# kubectl create -f ./daremonset.yml
daemonset.apps/nginx-daemon created
查看Pod详细信息
可以看到,
2个node节点,各一个Pod
[root@k8s-master wordpress]# kubectl get pods -n wordpress -o wide
NAME                 READY   STATUS    RESTARTS   AGE   IP                NODE                   NOMINATED NODE   READINESS GATES
nginx-daemon-hsd7h   1/1     Running   0          67s   192.168.242.148   k8s-node02.linux.com   <none>           <none>
nginx-daemon-tl75l   1/1     Running   0          67s   192.168.201.213   k8s-node01.linux.com   <none>           <none>
 
                     
                
             
                
评论