0%

本文使用istio版本号:1.4.2

Istio安装时,第一步就是创建了各种自定义资源类型(CRD),参考istio部署【在kubernetes上部署】,其中最重要的几个CRD包括:GatewayVirtualServiceDestinationRuleServiceEntry。主要架构如下图:
CRDs

DestinationRule

DestinationRule用于定义目标服务的访问策略,如在subset定义版本,定义负载均衡策略,熔断,一级TLS等。

基本示例

apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: reviews
spec:
  host: reviews
  subsets:
  - name: v1
    labels:
      version: v1
  - name: v2
    labels:
      version: v2

定义负载均衡策略

apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: reviews
spec:
  host: reviews
  trafficPolicy:
    loadBalancer:
      simple: RANDOM
  subsets:
  - name: v1
    labels:
      version: v1
  - name: v2
    labels:
      version: v2
    trafficPolicy:
      loadBalancer:
        simple: ROUND_ROBIN

定义熔断器

参考官网

apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: reviews
spec:
  host: reviews
  subsets:
  - name: v1
    labels:
      version: v1
    trafficPolicy:
      connectionPool:
        tcp:
          maxConnections: 100
      http:
        http1MaxPendingRequests: 100
        maxRequestsPerConnection: 100
    outlierDetection:
      consecutiveErrors: 1
      interval: 1s
      baseEjectionTime: 3m
      maxEjectionPercent: 100

VirtualService

VirtualService是最重要的配置接口,定义服务的所有路由规则,包括条件判断、权重、路径重写等。

定义权重示例

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews
spec:
  hosts:
    - reviews
  http:
  - route:
    - destination:
        host: reviews
        subset: v1
      weight: 90
    - destination:
        host: reviews
        subset: v2
      weight: 10

定义超时和重试策略

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: ratings
spec:
  hosts:
    - ratings
  http:
  - route:
    - destination:
        host: ratings
        subset: v1
    timeout: 10s
    retries:
      attempts: 3
      perTryTimeout: 2s

故障注入:模拟失败的场景

参考Fault Injection

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: ratings
spec:
  hosts:
  - ratings
  http:
  - fault:
      #定义10%请求+5秒延迟, 也可以是percent: 10
      delay:
        percentage:
          value: 10
        fixedDelay: 5s
 
      #定义10%请求返回400错误, 也可以是percent: 10
      abort:
        percentage:
          value: 10
        httpStatus: 400
    route:
    - destination:
        host: ratings
        subset: v1

条件判断——标签

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: ratings
spec:
  hosts:
  - ratings
  http:
  - match:
    - sourceLabels:
        app: reviews
        version: v2
    ...

条件判断——Header

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews
spec:
  hosts:
  - reviews
  http:
  - match:
    - headers:
        end-user:
          exact: jason
    route:
    - destination:
        host: reviews
        subset: v2

条件判断——URI路径

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: productpage
spec:
  hosts:
    - productpage
  http:
  - match:
    - uri:
        prefix: /api/v1
    ...

多条件判断

如果是嵌套在一个匹配语句中,为AND关系。

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: ratings
spec:
  hosts:
  - ratings
  http:
  - match:
    - sourceLabels:
        app: reviews
        version: v2
      headers:
        end-user:
          exact: jason
    ...

如果是单独的匹配语句,为OR关系。

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: ratings
spec:
  hosts:
  - ratings
  http:
  - match:
    - sourceLabels:
        app: reviews
        version: v2
    - headers:
        end-user:
          exact: jason
    ...

ServiceEntry

将外部服务接入到服务注册表中,让Istio中自动发现的服务能够访问和路由到这些手工加入的服务。与VirtualServiceDestinationRule配合使用。

apiVersion: networking.istio.io/v1alpha3
kind: ServiceEntry
metadata:
  name: foo-ext-svc
spec:
  hosts:
  - *.foo.com
  ports:
  - number: 80
    name: http
    protocol: HTTP
  - number: 443
    name: https
    protocol: HTTPS
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: bar-foo-ext-svc
spec:
  hosts:
    - bar.foo.com
  http:
  - route:
    - destination:
        host: bar.foo.com
    timeout: 10s

Gateway

提供外部服务访问接入,可发布任意内部端口的服务,供外部访问。配合VirtualService使用。

Bookinfo示例:

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: bookinfo-gateway
spec:
  selector:
    istio: ingressgateway # use istio default controller
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - "*"
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: bookinfo
spec:
  hosts:
  - "*"
  gateways:
  - bookinfo-gateway
  http:
  - match:
    - uri:
        exact: /productpage
    - uri:
        prefix: /static
    - uri:
        exact: /login
    - uri:
        exact: /logout
    - uri:
        prefix: /api/v1/products
    route:
    - destination:
        host: productpage
        port:
          number: 9080

docker镜像导入导出有两种方式

  • export/import
  • load/save

export/import

docker export -o "导出的镜像文件名.tar" CONTATINER

docker import "导出的镜像文件名.tar" "镜像名:版本号"

load/save

docker load --input "导出的镜像文件名.tar"

docker save -o "导出的镜像文件名.tar" "要导出的镜像名"

有时候需要在启动POD前,修改POD内核相关参数,除了使用initContainer还可以使用sysctl
参考链接Using sysctls in a Kubernetes Cluster

apiVersion: v1
kind: Pod
metadata:
  name: sysctl-example
spec:
  securityContext:
    sysctls:
    - name: kernel.shm_rmid_forced
      value: "0"
    - name: net.core.somaxconn
      value: "1024"
    - name: kernel.msgmax
      value: "65536"
  ...

了解 DevOps

DevOps 是指对企业文化、业务自动化和平台设计等方面进行全方位变革,从而实现迅捷、优质的服务交付,提升企业响应能力和价值。只有通过快速迭代的 IT 服务交付,这一切才能实现。DevOps可以将传统应用和最新的云原生应用与基础架构彼此相连。

DevOps简介

DevOps 就是开发(Development)、测试(QA)、运维(Operations)这三个领域的合并。
DevOps

从字面上来看,”DevOps”一词是由英文 Development(开发)和 Operations (运维)组合而成,但它所代表的理念和实践要比这广阔的多。DevOps 涵盖了安全、协作方式、数据分析等许多方面。

DevOps 强调通过一系列手段来实现既快又稳的工作流程,使每个想法(比如一个新的软件功能,一个功能增强请求或者一个 bug 修复)在从开发到生产环境部署的整个流程中,都能不断地为用户带来价值。这种方式需要开发团队和运维团队密切交流、高效协作并且彼此体谅。此外,DevOps 还要能够方便扩展,灵活部署。有了 DevOps,需求最迫切的工作就能通过自助服务和自动化得到解决;通常在标准开发环境编写代码的开发人员也可与 IT 运维人员紧密合作,加速软件的构建、测试和发布,同时保障开发成果的稳定可靠。

容器与 DevOps 有什么关系?

DevOps 可以加快一个想法从提出到部署的整个过程。DevOps 的核心在于,在应用的整个生命周期中,都要确保日常运维任务自动化和环境的标准化。容器可以提供标准化的环境,你需要一个平台来管理它们,同时提供内置的自动化功能并支持各种基础架构。

DevOps 与 CI/CD

选择支持流程的工具对于 DevOps 的成功至关重要。运维团队要跟上快速开发周期,就需要利用高度灵活的平台,并像开发团队对待代码一样,对待平台的基础架构。手动部署不仅速度慢,而且可能出错。因此,您也可通过自动化来简化平台调配和部署。

持续集成和持续部署管道(CI/CD)是实施 DevOps 的一大重要成果。CI/CD 可帮助您频繁地向客户交付应用并检验软件质量,而且只需极少的人工干预。

具体而言,CI/CD 在整个应用生命周期内(从集成和测试阶段,到交付和部署)都引入了持续自动化和持续监控,让您能够快速识别和改正问题与缺陷。这些彼此关联的实践通常被统称为“CI/CD 管道”,需要开发和运维团队以敏捷方式协同支持。

CICD简介

  • 持续集成(Continuous Integration ,CI)
  • 持续交付(Continuous Delivery)
  • 持续部署(Continuous Deploy)

参考链接

云原生CICD实现

CICD
流程:

  1. 用户本地完成开发;
  2. 代码提交到GitLab上;
  3. Gitlab收到代码提交请求后通过webhook触发Jenkins;
  4. Jenkins被触发后,首先从代码仓库拉取源码,进行构建、静态分析和单元测试,然后创建镜像推送到镜像仓库Harbor,最后调用Kubernetes API更新应用;
  5. Kubernetes从Harbor拉取最新镜像,更新应用。

在spring boot中,集成Mybatis可以使用完全注解的方式,完全不用新增任何配置文件。多条件判断,可以使用<script><set>搭配实现。

@Update

@Update("<script>update t_user " +
        "<set> " +
        "<if test='userName != null'> user_name = #{userName},</if>" +
        "<if test='userPwd != null'> user_pwd = #{userPwd},</if>" +
        "<if test='userRemark != null'> user_remark = #{userRemark},</if>" +
        "<if test='userPhone != null'> user_phone = #{userPhone},</if>" +
        "<if test='userEmail != null'> user_email = #{userEmail},</if>" +
        "</set> " +
        "where user_id = #{userId}</script>")

@Select

@Select("select id,name,description,enabled,deleted,date_created as dateCreated,last_modified as lastModified from admin_role (#{roleParam})")
//不使用 @Param 时,那么,此时 collection 需要定义为 list,否则会报错
@Select({
        "<script>",
        "SELECT * ",
        "FROM users WHERE id IN",
        "<foreach item='id' index='index' collection='ids' open='(' separator=',' close=')'>",
        "#{id}",
        "</foreach>",
        "</script>"
})
List<UserEntity> getUserById(@Param("ids") List<String> ids);

@Insert

@Insert("insert into t_alert_log (alert_name,severity,message,start_at,end_at) " +
            "values(#{alertName},#{severity},#{message},#{startAt},#{endAt})")
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")

@Update

@Update("update t_alert_log set status=true,remark=#{remark} where token=#{token}")

@ResultType

@ResultType(String.class)

注解方式传入表名和字段名

// 一般通过#{xxx}方式传入内容,但是表名和字段名不能通过这个方式.
// 动态传入表名和字段名用到${xxx}
@Delete("delete from ${tableName} where DATE(${columnName}) <= DATE(DATE_SUB(NOW(),INTERVAL #{interval} day));")
void delete(String tableName, String columnName, int interval);

Helm用途

做为Kubernetes的一个包管理工具,Helm具有如下功能:

  • 创建新的chart
  • chart打包成tgz格式
  • 上传chart到chart仓库或从仓库中下载chart
  • 在Kubernetes集群中安装或卸载chart
  • 管理用Helm安装的chart的发布周期
    Helm有三个重要概念:
  • chart:包含了创建Kubernetes的一个应用实例的必要信息
  • config:包含了应用发布配置信息
  • release:是一个chart及其配置的一个运行实例

Helm常用命令

操作类型 命令
添加仓库 helm repo add loki https://grafana.github.io/loki/charts
更新仓库 helm repo update
查看helm仓库列表 helm repo list
查看本地已安装的包 helm list (ls)
查看全部release(包括删除的…) helm list -a
查看helm版本 helm version
删除release helm delete loki
设置安装release名称 –name test
设置安装的namespace –namespace test
设置自定义属性 –set “loki.serviceName=loki”
从文件读取自定义属性集合 -f values.yaml
查找本地release的版本列表 helm search testapi -l
指定charts版本 –version 8.2.4
查看安装历史 helm history prometheus-operator
版本回滚 helm rollback prometheus-operator 1
打包chart helm package mychart
获取charts helm fetch stable/mysql –version 0.2.8 –untar
检查chart是否存在问题 helm lint mysql
创建一个本地仓库 helm serve –address 0.0.0.0:8879 –repo-path ./charts
创建一个chart helm create mychart
查看release状态 helm status mysql
更新release helm upgrade mysql -f mysql/values.yaml –set resources.requests.memory=1024Mi mysql
查看指定release的历史版本部署时部分配置信息 helm get –revision 1 mysql
对chart的模板和配置进行测试 helm install –dry-run –debug ./
查看release默认配置 helm inspect values stable/prometheus-operator