IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 系统运维 -> k8s入门与实践 -> 正文阅读

[系统运维]k8s入门与实践

一、Kubernetes安装

Kubernetes中文社区 | 中文文档

  1. 设置阿里云镜像

vim /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0

  1. 安装minikube

curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube

  1. 安装kubectl

curl -LO https://dl.k8s.io/release/v1.20.0/bin/linux/amd64/kubectl
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl

  1. 安装conntrack

yum install conntrack

二、基本操作

1.Minikube

前提: 安装docker(安装笔记)

  • 启动minikube

minikube start --vm-driver=none --image-mirror-country=‘cn’

  • 停止minikube

minikube stop

2.kubectl

  • 查看节点——kubectl get node(s)

    [root@*********************** ~]# kubectl get nodes
    NAME                      STATUS   ROLES                  AGE    VERSION
    ***********************   Ready    control-plane,master   4d3h   v1.22.3
    

    node是?于承载运?中的容器的。
    有?个node, 这个node的??是master。
    k8s也有集群的概念, 即:cluster, 包含master和node, 该节点即是master节点, ?是node节点。

  • 创建deployment

    [root@*********************** ~]# kubectl create deployment my-nginx --image nginx:latest
    deployment.apps/my-nginx created
    
  • 查看所有deployment——kubectl get deploy(ment)(s)

    [root@*********************** ~]# kubectl get deployment
    NAME       READY   UP-TO-DATE   AVAILABLE   AGE
    my-nginx   1/1     1            1           7m46s
    
  • 查看所有pod——kubectl get pod(s)

    [root@*********************** ~]# kubectl get pods
    NAME                       READY   STATUS    RESTARTS   AGE
    my-nginx-b7d7bc74d-qq56l   1/1     Running   0          8m46s
    
  • 查看所有pod信息以及ip和port——kubectl get pods -o wide

    [root@*********************** ~]# kubectl get pods -o wide
    NAME                       READY   STATUS    RESTARTS   AGE   IP           NODE                      NOMINATED NODE   READINESS GATES
    my-nginx-b7d7bc74d-qq56l   1/1     Running   0          12m   ---.--.-.-   ***********************   <none>           <none>
    
  • 查看所有service——kubectl get service(s)

    [root@*********************** ~]# kubectl get service(s)
    NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
    kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   4d3h
    
  • 查询所有命名空间——kubectl get namespace(s)

    [root@*********************** ~]# kubectl get namespaces
    NAME              STATUS   AGE
    default           Active   4d3h
    kube-node-lease   Active   4d3h
    kube-public       Active   4d3h
    kube-system       Active   4d3h
    

    1.默认的是default
    2.kube-*为k8s系统自身的命名空间

  • 修改副本数

    即修改pod的个数, 如不指定副本个数, 则默认就是?个pod

    # 将pods个数修改为3个
    [root@*********************** ~]# kubectl scale deployments/my-nginx --replicas=3
    deployment.apps/my-nginx scaled
    [root@*********************** ~]# kubectl get deployments
    NAME       READY   UP-TO-DATE   AVAILABLE   AGE
    my-nginx   1/3     3            1           33m
    [root@*********************** ~]# kubectl get pods
    NAME                       READY   STATUS    RESTARTS   AGE
    my-nginx-b7d7bc74d-ht5fx   1/1     Running   0          36s
    my-nginx-b7d7bc74d-mc9p5   1/1     Running   0          36s
    my-nginx-b7d7bc74d-qq56l   1/1     Running   0          33m
    
    # 将pods个数修改为2个
    [root@*********************** ~]# kubectl scale deployments/my-nginx --replicas=2
    deployment.apps/my-nginx scaled
    [root@*********************** ~]# kubectl get deployments
    NAME       READY   UP-TO-DATE   AVAILABLE   AGE
    my-nginx   2/2     2            2           38m
    [root@*********************** ~]# kubectl get pods
    NAME                       READY   STATUS    RESTARTS   AGE
    my-nginx-b7d7bc74d-mc9p5   1/1     Running   0          5m49s
    my-nginx-b7d7bc74d-qq56l   1/1     Running   0          38m
    

三、基本概念

1.Cluster

资源的集合, k8s利?这些资源运?各种基于容器的应?。

2.Master

  • Cluster的?脑, 司令部
  • 主要的任务就是?来调度的, 决定应?应该放到哪?去执?, 为了?可?, 也可以运?多个master

3.Node

  • 负责运?容器应?
  • Master管理, 负责监控和容器状态的汇报

4.Pod

k8s的最?的?作单元, 包含1~N个容器

  • 使??式:

    • ?个pod运??个容器(最常?)

    • ?个pod运?多个容器(较少见)

      ?常紧密相关的?组容器, 并且需要资源的共享, ?起启动、?起停?

5.Controller

  • 管理Pod
  • 针对不同的业务场景, k8s提供了多种Controller, 其中包含: Deployment、ReplicaSet、DaemonSet、StatefulSet、Job
  • Deployment

    1.最常?的Controller, 可以管理Pod的多个副本(例如: --replicas=3), 并且可以确保Pod按照期望的状态去运?

    2.实质: 会?动创建ReplicaSet, 最终是由ReplicaSet创建pod

  • ReplicaSet

    也是管理Pod的多个副本

  • DaemonSet

    ?于每个Node最多只运??个Pod副本的创建

  • StatefulSet

    保证副本按照固定的顺序启动、更新、删除

6.Service

为Pod提供了负载均衡、固定的IP和Port——因为pod是不稳定的, IP会变化的, 所以需要?个固定的IP或Port

  • 区别:
    • Controller负责运?容器
    • Service负责访问容器

7.Namespace

资源隔离

四、k8s架构初识

1.kubelet是唯一没有以容器形式运行的Kubernetes组件
2.k8s架构中, 主要是由Master和Node组成的

1.执行流程

当执行部署应用并指定两个副本的时候

架构图

  • Kuberctl发送部署请求到API Server
  • API Server通知Controller Manager创建一个deployment资源
  • Scheduler执行调度任务, 将两个副本Pod分发到node1和node2上
  • node1和node2上的kubectl在各自的节点上创建并运行Pod

2.Master

  • API-Server

    属于前端交互接?, 提供基于Http/https RESTful API接收对应的指令

  • Scheduler

    负责决定将pod放到哪个Node上运?

  • Controller Manager

    ?常关键的组件, 管理Cluster中的各种资源

  • etcd

    负责保存k8s的配置信息和各种资源的状态信息, 如果数据发?了变化, etcd会快速通知相关的组件

3.Node

  • kubelet

    创建和运?容器

  • kube-proxy

    负责请求的转发, 如果有多个副本, 则会实现负载均衡

六、Deployment

1.创建方式

  • kubectl命令

    • kubectl run

      kubectl run nginx-deployment --image nginx:1.7.9 --replicas=2

      [root@*********************** nginx]# kubectl run nginx-deployment --image nginx:1.7.9 --replicas=2
      Flag --replicas has been deprecated, has no effect and will be removed in the future.
      pod/deployments created
      

      在命令行中通过参数指定资源的属性(但是, 在K8S v1.18.0以后, -–replicas已弃用, 推荐用 kubectl apply 创建 pods)

    • kubectl create

      kubectl create deployment nginx-deployment --image nginx:1.7.9 --replicas=2

  • 配置文件 + kubectl apply

    • 编写yml配置文件(文件内部, 自定义名称不能包含大写字母)

      例如:

      # 版本号
      apiVersion: apps/v1
      # 类型: Pod/ReplicationController/Deployment/Service/Ingress
      kind: Deployment
      metadata: 
        # Kind的名称
        name: nginx-deployment
      spec: 
        selector:
          matchLabels:
            # 容器标签的名称, 发布Service时, selector需要和这里对应
            app: nginx
        # 部署的实例数量(默认一个)
        replicas: 2
        template: 
        	# 至少定义一个label, key和value可以任意指定
          metadata:
            labels:
              app: nginx
          # 此部分描述Pod的规格, 定义Pod中每一个容器的属性, name和image是必需的
          spec:
            # 容器配置, 数组类型, 说明可以配置多个容器
            containers:
            # 容器名称(必须)
            - name: nginx
              # 容器镜像(必须)
              image: nginx:1.7.9
              # 策略: "Always", "IfNotPresent", "Never"
              # 只有镜像不存在时, 才会进行镜像拉取
              imagePullPolicy: IfNotPresent
              ports:
              # Pod端口
              - containerPort: 80
      
    • 执行命令

      [root@*********************** nginx]# kubectl apply -f nginx.yaml
      deployment.apps/nginx-deployment created
      

2.删除指令

[root@*********************** ~]# kubectl delete deployment my-nginx
deployment.apps “my-nginx” deleted

注意点

  • 如配置2个pod, 对其中一个pod进行删除, 依然会根据deployment配置, 再补充为2个pod

  • 删除deployment时, pod会随之自动被删除

3.构建过程

kubectl——创建Deployment——创建ReplicaSet——创建Pod

Pod创建流程

  • deployment描述信息
[root@*********************** nginx]# kubectl describe deployment nginx-deployment
Name:                   nginx-deployment
Namespace:              default
	.
	.
	.
NewReplicaSet:   nginx-deployment-5d59d67564 (2/2 replicas created)
	.
	.
	.
  • Replicaset描述信息
[root@*********************** nginx]# kubectl describe rs nginx-deployment-5d59d67564
Name:           nginx-deployment-5d59d67564
Namespace:      default
	.
	.
	.	
	.
	.	
	.
Events:
  Type    Reason            Age   From                   Message
  ----    ------            ----  ----                   -------
  Normal  SuccessfulCreate  13m   replicaset-controller  Created pod: nginx-deployment-5d59d67564-mcrsm
  Normal  SuccessfulCreate  13m   replicaset-controller  Created pod: nginx-deployment-5d59d67564-qt7rc
  • Pod描述信息
[root@*********************** nginx]# kubectl describe pod nginx-deployment-5d59d67564-mcrsm
Name:         nginx-deployment-5d59d67564-mcrsm
Namespace:    default
	.
	.
	.
Controlled By:  ReplicaSet/nginx-deployment-5d59d67564
	.
	.
	.
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  17m   default-scheduler  Successfully assigned default/nginx-deployment-5d59d67564-mcrsm to ***********************
  Normal  Pulled     17m   kubelet            Container image "nginx:1.7.9" already present on machine
  Normal  Created    17m   kubelet            Created container nginx
  Normal  Started    17m   kubelet            Started container nginx

五、Label

默认配置下, Scheduler会将Pod调度到所有可用的Node, 不过有些情况我们可以通过lable将Pod部署到指定的Node, 例如:

  • 将有大量磁盘I/O的Pod部署到配置了SSD的Node上
  • 将需要GPUPod部署到配置了GPU的Node上
  1. 查看节点的标签信息

    [root@*********************** nginx]# kubectl get node
    NAME                      STATUS   ROLES                  AGE     VERSION
    ***********************   Ready    control-plane,master   4d22h   v1.22.3
    [root@*********************** nginx]# kubectl get node --show-labels
    NAME                      STATUS   ROLES                  AGE     VERSION   LABELS
    ***********************   Ready    control-plane,master   4d22h   v1.22.3   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=***********************,kubernetes.io/os=linux,minikube.k8s.io/commit=76b94fb3c4e8ac5062daf70d60cf03ddcc0a741b,minikube.k8s.io/name=minikube,minikube.k8s.io/updated_at=2021_11_19T17_01_50_0700,minikube.k8s.io/version=v1.24.0,node-role.kubernetes.io/control-plane=,node-role.kubernetes.io/master=,node.kubernetes.io/exclude-from-external-load-balancers=
    
  2. 添加/删除标签

    # 添加
    [root@*********************** nginx]# kubectl label node *********************** disktype=ssd
    node/*********************** labeled
    [root@*********************** nginx]# kubectl get node --show-labels
    NAME                      STATUS   ROLES                  AGE     VERSION   LABELS
    ***********************   Ready    control-plane,master   4d22h   v1.22.3   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disktype=ssd,kubernetes.io/arch=amd64,kubernetes.io/hostname=***********************,kubernetes.io/os=linux,minikube.k8s.io/commit=76b94fb3c4e8ac5062daf70d60cf03ddcc0a741b,minikube.k8s.io/name=minikube,minikube.k8s.io/updated_at=2021_11_19T17_01_50_0700,minikube.k8s.io/version=v1.24.0,node-role.kubernetes.io/control-plane=,node-role.kubernetes.io/master=,node.kubernetes.io/exclude-from-external-load-balancers=
    
    # 删除
    [root@*********************** nginx]# kubectl label node *********************** disktype-
    node/*********************** labeled
    [root@*********************** nginx]# kubectl get node --show-labels
    NAME                      STATUS   ROLES                  AGE     VERSION   LABELS
    ***********************   Ready    control-plane,master   4d22h   v1.22.3   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=***********************,kubernetes.io/os=linux,minikube.k8s.io/commit=76b94fb3c4e8ac5062daf70d60cf03ddcc0a741b,minikube.k8s.io/name=minikube,minikube.k8s.io/updated_at=2021_11_19T17_01_50_0700,minikube.k8s.io/version=v1.24.0,node-role.kubernetes.io/control-plane=,node-role.kubernetes.io/master=,node.kubernetes.io/exclude-from-external-load-balancers=
    
  3. 修改xx.yml配置文件

  4. 重新部署Deployment

六、Job

容器架构图

1.容器类型划分

  • 容器按照持续运行时间可以分为

    • 服务类容器

      • 持续提供服务, 则需要一直运行, 比如HTTP Server、Daemon

      • Deployment、ReplicaSet和DaemonSet都用于管理服务类容器

    • 工作类容器

      • 一次性任务, 比如批处理程序, 完成后容器就退出

      • Job用于管理工作类容器

2.创建任务

  • 普通Job

    apiVersion: batch/v1
    kind: Job
    metadata:
      name: myjob
    spec:
      # 需要完成的数量
      # completions: 6
      # 并行数量
      # parallelism: 2
      template:
        metadata:
          name: myjob
        spec:
          containers:
          - name: hello      
            image: busybox
            command: ["echo", "hello k8s job!"]
            # command: ["invalid_command", "hello k8s job!"]
          restartPolicy: Never
          # restartPolicy: OnFailure
    
    • command: [“echo”, “hello k8s job!”]

      [root@*********************** deployConfigFile]# kubectl apply -f myJob.yaml
      job.batch/myjob created
      [root@*********************** ~]# kubectl get job
      NAME    COMPLETIONS   DURATION   AGE
      myjob   1/1           22s        16h
      [root@*********************** ~]# kubectl get pods
      NAME             READY   STATUS      RESTARTS   AGE
      myjob--1-jvttw   0/1     Completed   0          16h
      [root@*********************** ~]# kubectl logs myjob--1-jvttw
      hello k8s job!
      
    • command: [“invalid_command”, “hello k8s job!”]

      • restartPolicy: Never

        [root@*********************** deployConfigFile]# kubectl get pods
        NAME             READY   STATUS               RESTARTS   AGE
        myjob--1-2z8dk   0/1     ContainerCreating    0          9s
        myjob--1-rlzmk   0/1     ContainerCannotRun   0          28s
        ```
        
      • restartPolicy: OnFailure

        [root@*********************** deployConfigFile]# kubectl get pods
        NAME             READY   STATUS              RESTARTS     AGE
        myjob--1-xkbrj   0/1     RunContainerError   1 (1s ago)   35s
        
  • 定时Job

    # apiVersion: batch/v1
    apiVersion: batch/v2alpha1
    kind: CronJob
    metadata:
      name: hello
    spec:
      schedule: "*/1 * * * *"
      jobTemplate:
        spec:
          template:
            spec:
              containers:
              - name: hello      
                image: busybox
                command: ["echo", "hello k8s cronJob!"]
              restartPolicy: OnFailure
    
    • 可能出现的错误信息

      [root@*********************** deployConfigFile]# kubectl apply -f myCronJob.yaml
      error: unable to recognize "myCronJob.yaml": no matches for kind "CronJob" in version "batch/v2alpha1"
      
      • 查看支持的版本

        [root@*********************** deployConfigFile]# 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/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
        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
        
      • 引入batch/v2alpha1

        [root@*********************** deployConfigFile]# vim /etc/kubernetes/manifests/kube-apiserver.yaml 
        
        spec:
          containers:
          - command:
          - kube-apiserver
          - --runtime-config=batch/v2alpha1=true
           	  .
           	  .
         	  .
        
        • 可能需要手动重启——systemctl restart kubelet

        • 可能会出现Server无法重启的现象, 提升Kubernetes版本至1.5及其以上

    • batch/v1——参考文档

      [root@*********************** deployConfigFile]# kubectl apply -f myCronJob.yaml
      cronjob.batch/hello created
      [root@*********************** deployConfigFile]# kubectl get cronJob
      NAME    SCHEDULE      SUSPEND   ACTIVE   LAST SCHEDULE   AGE
      hello   */1 * * * *   False     0        <none>          37s
      [root@*********************** deployConfigFile]# kubectl get pods
      NAME                      READY   STATUS              RESTARTS   AGE
      hello-27296887--1-7nk7q   0/1     Completed           0          2m8s
      hello-27296888--1-dg6nh   0/1     Completed           0          67s
      hello-27296889--1-t5gqx   0/1     ContainerCreating   0          8s
      

七、Service

1.作用

Service提供了固定的ip和端口, 并且里面包含一组pod, 即使Pod的ip发生变化, 但是面对客户端的是Service的固定ip和端口

  • Pod是不健壮的, Pod中的容器很可能因为各种原因发生故障而挂掉
  • Deployment等Controller会通过动态创建和销毁Pod来保证应用整体的健壮性, 所以应用才是是健壮的

2.创建

  • 创建Deployment

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: httpd
    spec:
      selector:
        matchLabels:
          run: httpd
      replicas: 3
      template:
        metadata:
          labels:
            name: httpd
        spec:
          containers:
          - name: httpd      
            image: httpd
            ports:
            - containerPort: 80
    
    [root@*********************** httpd]# kubectl apply -f httpd.yaml 
    deployment.apps/httpd created
    [root@*********************** httpd]# kubectl get pods
    NAME                     READY   STATUS    RESTARTS   AGE
    httpd-5cbd65d46c-f7hgm   1/1     Running   0          2m35s
    httpd-5cbd65d46c-qvmqw   1/1     Running   0          2m35s
    httpd-5cbd65d46c-xzbkv   1/1     Running   0          2m35s
    [root@*********************** httpd]# kubectl get pods -o wide
    NAME                     READY   STATUS    RESTARTS   AGE     IP           NODE                      NOMINATED NODE   READINESS GATES
    httpd-5cbd65d46c-f7hgm   1/1     Running   0          3m16s   172.18.0.6   ***********************   <none>           <none>
    httpd-5cbd65d46c-qvmqw   1/1     Running   0          3m16s   172.18.0.4   ***********************   <none>           <none>
    httpd-5cbd65d46c-xzbkv   1/1     Running   0          3m16s   172.18.0.5   ***********************   <none>           <none>
    [root@*********************** httpd]# curl 172.18.0.6
    <html><body><h1>It works!</h1></body></html>
    [root@*********************** httpd]# curl 172.18.0.5
    <html><body><h1>It works!</h1></body></html>
    [root@*********************** httpd]# curl 172.18.0.4
    <html><body><h1>It works!</h1></body></html>
    
  • 创建Service

    apiVersion: v1
    kind: Service
    metadata:
      name: httpd-svc
    spec:
      selector:
        run: httpd
      ports:
      - protocol: TCP
        port: 8080
        targetPort: 80
    
    [root@*********************** httpd]# kubectl apply -f httpdSvc.yaml 
    service/httpd-svc created
    [root@*********************** httpd]# kubectl get service
    NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
    httpd-svc    ClusterIP   10.106.23.93   <none>        8080/TCP   5m57s
    [root@*********************** httpd]# curl 10.106.23.93:8080
    <html><body><h1>It works!</h1></body></html>
    
  • 关联性

    • kubectl describe service httpd-svc

      [root@*********************** httpd]# kubectl describe service httpd-svc
      Name:              httpd-svc
      Namespace:         default
      Labels:            <none>
      Annotations:       <none>
      Selector:          run=httpd
      Type:              ClusterIP
      IP Family Policy:  SingleStack
      IP Families:       IPv4
      IP:                10.106.23.93
      IPs:               10.106.23.93
      Port:              <unset>  8080/TCP
      TargetPort:        80/TCP
      Endpoints:         172.18.0.4:80,172.18.0.5:80,172.18.0.6:80
      	.
      	.
      	.
      
    • iptables-save

      [root@*********************** httpd]# iptables-save
      	.
      	.
      	.
      -A KUBE-SERVICES -d 10.106.23.93/32 -p tcp -m comment --comment "default/httpd-svc cluster IP" -m tcp --dport 8080 -j KUBE-SVC-IYRDZZKXS5EOQ6Q6
      	.
      	.
      	.
      -A KUBE-SVC-IYRDZZKXS5EOQ6Q6 ! -s 10.244.0.0/16 -d 10.106.23.93/32 -p tcp -m comment --comment "default/httpd-svc cluster IP" -m tcp --dport 8080 -j KUBE-MARK-MASQ
      -A KUBE-SVC-IYRDZZKXS5EOQ6Q6 -m comment --comment "default/httpd-svc" -m statistic --mode random --probability 0.33333333349 -j KUBE-SEP-CERU7ZF6K2KWGLTG
      -A KUBE-SVC-IYRDZZKXS5EOQ6Q6 -m comment --comment "default/httpd-svc" -m statistic --mode random --probability 0.50000000000 -j KUBE-SEP-FEWNLSQ65IOCPPEH
      -A KUBE-SVC-IYRDZZKXS5EOQ6Q6 -m comment --comment "default/httpd-svc" -j KUBE-SEP-VEOTCWMCCOB2VENI
      -A KUBE-SEP-CERU7ZF6K2KWGLTG -s 172.18.0.4/32 -m comment --comment "default/httpd-svc" -j KUBE-MARK-MASQ
      -A KUBE-SEP-CERU7ZF6K2KWGLTG -p tcp -m comment --comment "default/httpd-svc" -m tcp -j DNAT --to-destination 172.18.0.4:80
      -A KUBE-SEP-FEWNLSQ65IOCPPEH -s 172.18.0.5/32 -m comment --comment "default/httpd-svc" -j KUBE-MARK-MASQ
      -A KUBE-SEP-FEWNLSQ65IOCPPEH -p tcp -m comment --comment "default/httpd-svc" -m tcp -j DNAT --to-destination 172.18.0.5:80
      -A KUBE-SEP-VEOTCWMCCOB2VENI -s 172.18.0.6/32 -m comment --comment "default/httpd-svc" -j KUBE-MARK-MASQ
      -A KUBE-SEP-VEOTCWMCCOB2VENI -p tcp -m comment --comment "default/httpd-svc" -m tcp -j DNAT --to-destination 172.18.0.6:80
      	.
      	.
      	.
      

八、Rolling Update

k8s自带功能

  • 什么是滚动更新

    一次只更新一小部分副本, 成功后再更新更多的副本, 最终完成所有副本的更新

  • 优势

    最大好处是零停机, 整个更新过程始终有副本在运行, 从而保证了业务的连续性

  • 示例

    • 更新

      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: httpd
      spec:
        selector:
          matchLabels:
            run: httpd
        replicas: 3
        template:
          metadata:
            labels:
              run: httpd
          spec:
            containers:
            - name: httpd      
              image: httpd:2.2.31
              # image: httpd:2.2.32
              imagePullPolicy: IfNotPresent
              ports:
              - containerPort: 80
      
      [root@*********************** httpd]# kubectl apply -f httpdRollingUpdate.yaml
      deployment.apps/httpd configured
      [root@*********************** httpd]# vim httpdRollingUpdate.yaml
      [root@*********************** httpd]# kubectl apply -f httpdRollingUpdate.yaml
      deployment.apps/httpd configured
      [root@*********************** httpd]# kubectl get pod
      NAME                     READY   STATUS              RESTARTS   AGE
      httpd-55f56846f-875sg    1/1     Running             0          108s
      httpd-55f56846f-ggqb5    1/1     Running             0          108s
      httpd-55f56846f-tjn66    1/1     Running             0          108s
      httpd-846d8d79dd-d7bzk   0/1     ContainerCreating   0          13s
      [root@*********************** httpd]# kubectl describe deployment httpd
      Name:                   httpd
      Namespace:              default
      	.
      	.
      	.
      Events:
        Type    Reason             Age    From                   Message
        ----    ------             ----   ----                   -------
        Normal  ScalingReplicaSet  3m12s  deployment-controller  Scaled up replica set httpd-55f56846f to 3
        Normal  ScalingReplicaSet  97s    deployment-controller  Scaled up replica set httpd-846d8d79dd to 1
        Normal  ScalingReplicaSet  58s    deployment-controller  Scaled down replica set httpd-55f56846f to 2
        Normal  ScalingReplicaSet  58s    deployment-controller  Scaled up replica set httpd-846d8d79dd to 2
        Normal  ScalingReplicaSet  53s    deployment-controller  Scaled down replica set httpd-55f56846f to 1
        Normal  ScalingReplicaSet  53s    deployment-controller  Scaled up replica set httpd-846d8d79dd to 3
        Normal  ScalingReplicaSet  48s    deployment-controller  Scaled down replica set httpd-55f56846f to 0
      [root@*********************** httpd]# kubectl get rs
      NAME               DESIRED   CURRENT   READY   AGE
      httpd-55f56846f    0         0         0       3m49s
      httpd-846d8d79dd   3         3         3       2m14s
      
    • 回滚(–record)

      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: httpd
      spec:
        selector:
          matchLabels:
            run: httpd
        revisionHistoryLimit: 10
        replicas: 3
        template:
          metadata:
            labels:
              run: httpd
          spec:
            containers:
            - name: httpd      
              image: httpd:2.4.16
              imagePullPolicy: IfNotPresent
              ports:
              - containerPort: 80
      
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: httpd
      spec:
        selector:
          matchLabels:
            run: httpd
        revisionHistoryLimit: 10
        replicas: 3
        template:
          metadata:
            labels:
              run: httpd
          spec:
            containers:
            - name: httpd      
              image: httpd:2.4.18
              imagePullPolicy: IfNotPresent
              ports:
              - containerPort: 80
      
      [root@*********************** httpd]# kubectl apply -f httpd.v1.yaml --record
      deployment.apps/httpd created
      [root@*********************** httpd]# kubectl get deploy -o wide
      NAME    READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
      httpd   3/3     3            3           47s   httpd        httpd:2.4.16   run=httpd
      [root@*********************** httpd]# kubectl apply -f httpd.v2.yaml --record
      deployment.apps/httpd configured
      [root@*********************** httpd]# kubectl get deploy -o wide
      NAME    READY   UP-TO-DATE   AVAILABLE   AGE    CONTAINERS   IMAGES         SELECTOR
      httpd   3/3     3            3           100s   httpd        httpd:2.4.18   run=httpd
      [root@*********************** httpd]# kubectl rollout history deployment httpd
      deployment.apps/httpd 
      REVISION  CHANGE-CAUSE
      1         kubectl apply --filename=httpd.v1.yaml --record=true
      2         kubectl apply --filename=httpd.v2.yaml --record=true
      
    • 进行回滚

      [root@*********************** httpd]# kubectl rollout undo deployment httpd --to-revision=1
      deployment.apps/httpd rolled back
      [root@*********************** httpd]# kubectl rollout history deployment httpd
      deployment.apps/httpd 
      REVISION  CHANGE-CAUSE
      2         kubectl apply --filename=httpd.v2.yaml --record=true
      3         kubectl apply --filename=httpd.v1.yaml --record=true
      
  系统运维 最新文章
配置小型公司网络WLAN基本业务(AC通过三层
如何在交付运维过程中建立风险底线意识,提
快速传输大文件,怎么通过网络传大文件给对
从游戏服务端角度分析移动同步(状态同步)
MySQL使用MyCat实现分库分表
如何用DWDM射频光纤技术实现200公里外的站点
国内顺畅下载k8s.gcr.io的镜像
自动化测试appium
ctfshow ssrf
Linux操作系统学习之实用指令(Centos7/8均
上一篇文章      下一篇文章      查看所有文章
加:2021-12-08 14:12:36  更:2021-12-08 14:13:31 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/16 3:12:52-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码