TH小破站

  • 首页
  • 容器
    • 认识Docker
    • DockerFile详解
  • k8s
    • 认识Kubernetes
    • Kubernetes部署--(kubeadm方式)
    • kubeasz部署k8s集群
    • Ansible-binary部署k8s集群-1.31.2
  • Ceph
    • 认识分布式存储
    • CPEH部署
  • CI/CD
    • DevOps认识
    • 部署--Gitlab
  • ELK
    • ELK简介
    • Elasticsearch
    • Kibana部署
    • Logstash
    • ELK--收集 tomcat 日志(实战)
  • HD
    • 认识--大数据
    • Hadoop-单机部署
    • Hadoop-伪分布部署
    • Hadoop-完全分布部署
    • Hadoop-(HA)完全分布部署
    • Ambari自动部署--Hadoop
  • Nginx
    • Nginx功能介绍
    • Nginx安全选项
    • Nginx配置文件简单介绍
    • 网站服务器部署
  • 监控
    • ZABBIX-部署(yum)
    • ZABBIX-编译安装
  • 云计算
    • KVM 概述
    • OpenStack企业级部署(T版)
    • OpenStack之dashboard部署
    • OpenStack-controller的高可用
    • openstack(Caracal版_ubuntu版本)部署
  • 数据库
    • mariadb与mysql
    • mysql主从
    • MySQL--冷备(实战)
    • MySQL--热备(dump表)
    • MySQL--热备(dump库)
    • 数据恢复实战案例
    • Xtrabackup实现MySql8.x全量备份
    • MyCat实现数据库读写分离
    • MHA实现数据库高可用
    • Percona XtraDB(PXC)实现数据库集群高可用
    • 数据库压力测试
    • PostgreSQL 基操
    • PostgreSQL备份恢复
    • PostgreSQL高可用
    • MongDB安装配置基础使用
    • MongoDB实现复制集
    • MongoDB备份还原
  • 自动化
    • ssh远程服务原理及配置
    • Ansible剧本文件应用
    • Ansible管理服务介绍
    • Ansible自动化部署node_exporter(包含升级,回滚)
    • Saltstack-部署安装
  • 脚本
    • 一键查看服务器资源利用率
    • 一键生成SSL证书
    • 保存指定数量的文件/目录
    • 初始化服务器
    • 扫描可用IP
    • 批量分发SSH密钥
    • 监控多主机磁盘脚本
    • 一键安装MySQL8.x脚本
    • Python脚本监控ES集群
    • 资源巡检脚本
    • 查看各用户使用的cpu占比
热衷于分享
架构师成长之路 !!!
  1. 首页
  2. 云原生
  3. 正文

istio微服务项目实战

2024年8月25日 891点热度 3人点赞 0条评论

istio项目实战

部署项目

代码仓库地址

https://github.com/GoogleCloudPlatform/microservices-demo

下载部署yaml

 root@k8s-master01:~# git clone https://github.com/cnych/istio-demo.git

部署项目

 root@k8s-master01:~# cd istio-demo/kubernetes-manifests/
 root@k8s-master01:~/istio-demo/kubernetes-manifests# kubectl apply -f namespaces/
 root@k8s-master01:~/istio-demo/kubernetes-manifests# kubectl apply -f services/
 root@k8s-master01:~/istio-demo/kubernetes-manifests# kubectl apply -f deployments/

查看pod状态

 root@k8s-master01:~# for ns in ad cart checkout currency email frontend loadgenerator payment product-catalog recommendation redis shipping; do kubectl get po -n $ns; done;
 NAME                        READY   STATUS    RESTARTS   AGE
 adservice-c786d6958-8b2wm   1/1     Running   0          9m18s
 NAME                           READY   STATUS    RESTARTS   AGE
 cartservice-84588969d8-j47zz   1/1     Running   0          9m18s
 NAME                               READY   STATUS    RESTARTS   AGE
 checkoutservice-56947dc4b4-r97mj   1/1     Running   0          9m18s
 NAME                               READY   STATUS    RESTARTS   AGE
 currencyservice-65bc7984d6-p4r4b   1/1     Running   0          9m18s
 NAME                            READY   STATUS    RESTARTS   AGE
 emailservice-665d65d8bb-ps9s7   1/1     Running   0          9m18s
 NAME                        READY   STATUS    RESTARTS   AGE
 frontend-59786f459c-hw5m9   1/1     Running   0          9m18s
 NAME                             READY   STATUS    RESTARTS   AGE
 loadgenerator-5cc7fd95bc-sgdvw   1/1     Running   0          9m18s
 NAME                             READY   STATUS    RESTARTS   AGE
 paymentservice-bcd9f97f8-sgc4b   1/1     Running   0          9m18s
 NAME                                     READY   STATUS    RESTARTS   AGE
 productcatalogservice-69f7686596-4k5rc   1/1     Running   0          9m18s
 NAME                                     READY   STATUS    RESTARTS   AGE
 recommendationservice-5679c8f4d6-fqqzm   1/1     Running   0          9m17s
 NAME                          READY   STATUS    RESTARTS   AGE
 redis-cart-6fdfbf5757-c5hp5   1/1     Running   0          9m18s
 NAME                               READY   STATUS    RESTARTS   AGE
 shippingservice-7bb759bfbb-6t9lf   1/1     Running   0          9m18s

访问访问

 root@k8s-master01:~# kubectl get svc -n frontend
 NAME                TYPE           CLUSTER-IP       EXTERNAL-IP      PORT(S)        AGE
 frontend            ClusterIP      173.96.134.17    <none>           80/TCP         10m
 frontend-external   LoadBalancer   173.96.151.247   192.168.139.40   80:32518/TCP   10m

http://192.168.139.40/或者http://192.168.139.241:32518/

将项目进行istio注入

首先将上面的命名空间标记为istio-injection=enabled,这样在该命名空间中的Pod就会自动注入Istio的sidecar :

 `注入sidecar{envoy}`
 root@k8s-master01:~# for ns in ad cart checkout currency email frontend loadgenerator payment product-catalog recommendation redis shipping; do kubectl label ns $ns istio-injection=enabled --overwrite; done;
 `重启pod`
 root@k8s-master01:~# for ns in ad cart checkout currency email frontend loadgenerator payment product-catalog recommendation redis shipping; do kubectl rollout restart deploy -n $ns; done;

再次查看这些Pod 的时候,我们会发现每个Pod 都变成了两个容器了,其中一个就是新增的istio-proxy 的容器:

 root@k8s-master01:~# for ns in ad cart checkout currency email frontend loadgenerator payment product-catalog recommendation redis shipping; do kubectl get po -n $ns; done;
 NAME                        READY   STATUS    RESTARTS   AGE
 adservice-85cdcb558-689c6   2/2     Running   0          2m1s
 NAME                           READY   STATUS    RESTARTS   AGE
 cartservice-7ddf696466-46zqq   2/2     Running   0          2m1s
 NAME                               READY   STATUS    RESTARTS   AGE
 checkoutservice-6644d8c98b-nsds4   2/2     Running   0          2m1s
 NAME                               READY   STATUS    RESTARTS   AGE
 currencyservice-686985f85d-mdl9n   2/2     Running   0          2m1s
 NAME                            READY   STATUS    RESTARTS   AGE
 emailservice-75d986695b-xnbcw   2/2     Running   0          2m2s
 NAME                        READY   STATUS    RESTARTS   AGE
 frontend-5f588c974b-6jdsl   2/2     Running   0          2m1s
 NAME                             READY   STATUS    RESTARTS   AGE
 loadgenerator-7b857d965c-52lbm   2/2     Running   0          2m1s
 NAME                             READY   STATUS    RESTARTS   AGE
 paymentservice-d5dfb56b9-78jtl   2/2     Running   0          2m1s
 NAME                                     READY   STATUS    RESTARTS   AGE
 productcatalogservice-6d9c59fcf7-9v7l8   2/2     Running   0          2m1s
 NAME                                     READY   STATUS    RESTARTS   AGE
 recommendationservice-5f9dffdbfc-hvtpn   2/2     Running   0          2m
 NAME                          READY   STATUS    RESTARTS   AGE
 redis-cart-79dd6cb6c5-5r7xq   2/2     Running   0          2m
 NAME                               READY   STATUS    RESTARTS   AGE
 shippingservice-8568cdb9d6-lrttz   2/2     Running   0          2m

配置项目进行istio调度

创建frontend的入口网关gateway

 root@k8s-master01:~/istio-demo/istio-manifests# cat frontend-gateway.yaml 
 apiVersion: networking.istio.io/v1beta1
 kind: Gateway
 metadata:
   name: frontend-gateway
   namespace: frontend
 spec:
   selector:
     istio: ingressgateway
   servers:
     - port:
         number: 80
         name: http
         protocol: HTTP
       hosts:
         - "*"
 root@k8s-master01:~/istio-demo/istio-manifests# kubectl apply -f frontend-gateway.yaml

创建frontend的virtualservice

 root@k8s-master01:~/istio-demo/istio-manifests# cat frontend-virtualservice.yaml 
 # frontend-virtualservice.yaml
 apiVersion: networking.istio.io/v1beta1
 kind: VirtualService
 metadata:
   name: frontend-ingress
   namespace: frontend
 spec:
   hosts:
     - "*"
   gateways:
     - frontend-gateway
   http:
     - route:
         - destination:
             host: frontend
             port:
               number: 80
 root@k8s-master01:~/istio-demo/istio-manifests# kubectl apply -f frontend-virtualservice.yaml
 root@k8s-master01:~/istio-demo/istio-manifests# kubectl get svc -n istio-system -owide -l app=istio-ingressgateway
 NAME                   TYPE           CLUSTER-IP       EXTERNAL-IP      PORT(S)                                                                      AGE   SELECTOR
 istio-ingressgateway   LoadBalancer   173.96.242.153   192.168.139.41   15021:30391/TCP,80:30916/TCP,443:31474/TCP,31400:31809/TCP,15443:30091/TCP   35d   app=istio-ingressgateway,istio=ingressgateway

访问测试

http://192.168.139.241:30916/或者http://192.168.139.41

金丝雀发布

Canary部署会将一小部分流量路由到微服务的新版本,然后我们可以逐步发布到整个用户群,同时逐步淘汰和弃用旧版本。如果在此过程中出现问题,可以将流量切换回旧版本。

这里我们以productcatalog-service服务为例来说明如何进行金丝雀发布。首先为现有的productcatalog服务添加一个version=v1的标签,表示该服务的版本为v1:

 root@k8s-master01:~/istio-demo/istio-manifests/canary-service# kubectl label pod productcatalogservice-6d9c59fcf7-9v7l8 -n product-catalog version=v1
 root@k8s-master01:~/istio-demo/istio-manifests/canary-service# kubectl get po -n product-catalog --show-labels
 NAME                                     READY   STATUS    RESTARTS   AGE   LABELS
 productcatalogservice-6d9c59fcf7-9v7l8   2/2     Running   0          32m   app=productcatalogservice,pod-template-hash=6d9c59fcf7,security.istio.io/tlsMode=istio,service.istio.io/canonical-name=productcatalogservice,service.istio.io/canonical-revision=latest,version=v1

定义DestinationRule、VirtualService

 root@k8s-master01:~/istio-demo/istio-manifests/canary-service# cat destination-v1.yaml 
 apiVersion: networking.istio.io/v1beta1
 kind: DestinationRule
 metadata:
   name: productcatalogservice
   namespace: product-catalog
 spec:
   host: productcatalogservice.product-catalog.svc.cluster.local
   subsets:
     - labels:
         version: v1
       name: v1
 root@k8s-master01:~/istio-demo/istio-manifests/canary-service# cat virtualservice-v1.yaml 
 apiVersion: networking.istio.io/v1beta1
 kind: VirtualService
 metadata:
   name: productcatalogservice
   namespace: frontend
 spec:
   hosts:
     - productcatalogservice.product-catalog.svc.cluster.local
   http:
     - route:
         - destination:
             host: productcatalogservice.product-catalog.svc.cluster.local
             subset: v1
 ​
 root@k8s-master01:~/istio-demo/istio-manifests/canary-service# kubectl apply -f destination-v1.yaml
 root@k8s-master01:~/istio-demo/istio-manifests/canary-service# kubectl apply -f virtualservice-v1.yaml
 root@k8s-master01:~/istio-demo/istio-manifests/canary-service# kubectl get vs -n frontend 
 NAME                                                       GATEWAYS               HOSTS                                                         AGE
 virtualservice.networking.istio.io/frontend-ingress        ["frontend-gateway"]   ["*"]                                                         28m
 virtualservice.networking.istio.io/productcatalogservice                          ["productcatalogservice.product-catalog.svc.cluster.local"]   47s
 root@k8s-master01:~/istio-demo/istio-manifests/canary-service# kubectl get dr -n product-catalog 
 NAME                    HOST                                                      AGE
 productcatalogservice   productcatalogservice.product-catalog.svc.cluster.local   91s
 ​

为项目配置v2版本

 root@k8s-master01:~/istio-demo/istio-manifests/canary-service# kubectl create configmap products-v2 --from-file=products.json -n product-catalog 
 root@k8s-master01:~/istio-demo/istio-manifests/canary-service# cat productcatalog-v2.yaml 
 apiVersion: apps/v1
 kind: Deployment
 metadata:
   name: productcatalogservice-v2
   namespace: product-catalog
 spec:
   selector:
     matchLabels:
       app: productcatalogservice
       version: v2
   template:
     metadata:
       labels:
         app: productcatalogservice
         version: v2
     spec:
       volumes:
         - name: products
           configMap:
             name: products-v2
       containers:
         - env:
             - name: PORT
               value: "3550"
             - name: DISABLE_PROFILER
               value: "1"
           image: gcr.dockerproxy.com/google-samples/microservices-demo/productcatalogservice:v0.9.0
           imagePullPolicy: IfNotPresent
           livenessProbe:
             grpc:
               port: 3550
           name: server
           ports:
             - containerPort: 3550
               protocol: TCP
           volumeMounts:
             - name: products
               subPath: products.json
               mountPath: /src/products.json
           readinessProbe:
             grpc:
               port: 3550
           resources:
             limits:
               cpu: 200m
               memory: 128Mi
             requests:
               cpu: 100m
               memory: 64Mi
           securityContext:
             allowPrivilegeEscalation: false
             capabilities:
               drop:
                 - ALL
             privileged: false
             readOnlyRootFilesystem: true
       securityContext:
         fsGroup: 1000
         runAsGroup: 1000
         runAsNonRoot: true
         runAsUser: 1000
       serviceAccountName: product-catalog
       terminationGracePeriodSeconds: 5
 ​
 ​
 root@k8s-master01:~/istio-demo/istio-manifests/canary-service# kubectl apply -f productcatalog-v2.yaml
 root@k8s-master01:~/istio-demo/istio-manifests/canary-service# kubectl get po -n product-catalog --show-labels
 NAME                                        READY   STATUS    RESTARTS   AGE   LABELS
 productcatalogservice-6d9c59fcf7-9v7l8      2/2     Running   0          44m   app=productcatalogservice,pod-template-hash=6d9c59fcf7,security.istio.io/tlsMode=istio,service.istio.io/canonical-name=productcatalogservice,service.istio.io/canonical-revision=latest,version=v1
 productcatalogservice-v2-599558854c-lpc5r   2/2     Running   0          62s   app=productcatalogservice,pod-template-hash=599558854c,security.istio.io/tlsMode=istio,service.istio.io/canonical-name=productcatalogservice,service.istio.io/canonical-revision=v2,version=v2

更新定义DestinationRule、VirtualService

 root@k8s-master01:~/istio-demo/istio-manifests/canary-service# cat destination-v2.yaml 
 # destination-v2.yaml
 apiVersion: networking.istio.io/v1beta1
 kind: DestinationRule
 metadata:
   name: productcatalogservice
   namespace: product-catalog
 spec:
   host: productcatalogservice.product-catalog.svc.cluster.local
   subsets:
     - labels:
         version: v1
       name: v1
     - labels:
         version: v2
       name: v2
 ​
 root@k8s-master01:~/istio-demo/istio-manifests/canary-service# cat vs-split.yaml 
 # vs-split-traffic.yaml
 apiVersion: networking.istio.io/v1beta1
 kind: VirtualService
 metadata:
   name: productcatalogservice
   namespace: frontend
 spec:
   hosts:
     - productcatalogservice.product-catalog.svc.cluster.local
   http:
     - route:
         - destination:
             host: productcatalogservice.product-catalog.svc.cluster.local
             subset: v1
           weight: 75
         - destination:
             host: productcatalogservice.product-catalog.svc.cluster.local
             subset: v2
           weight: 25
 ​
 root@k8s-master01:~/istio-demo/istio-manifests/canary-service# kubectl apply -f destination-v2.yaml 
 root@k8s-master01:~/istio-demo/istio-manifests/canary-service# kubectl apply -f vs-split.yaml 

验证

http://192.168.139.241:30916/或者http://192.168.139.41或者http://192.168.139.40

标签: 暂无
最后更新:2024年8月25日

tanghao946

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复
文章目录
  • istio项目实战
    • 部署项目
    • 将项目进行istio注入
    • 配置项目进行istio调度
    • 金丝雀发布

COPYRIGHT © 2025 TH小破站. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

渝ICP备2023013789号

渝公网安备50011702500926号