每日一Go-68、基于 Kind 的 Istio 本地实战(完整可跑)
为什么需要Service Mesh(服务网格)简单来说当你的架构从“单体”演进到“微服务”后原本简单的进程内调用变成了错综复杂的网络调用。Service Mesh服务网格的出现是为了把“业务逻辑”与“网络基础设施”彻底解耦。一、启动k8s集群$ kindcreatecluster--config kind-config.yaml --name golang-per-dayCreating clustergolang-per-day...• Ensuring node image(kindest/node:v1.35.0)...✓ Ensuring node image(kindest/node:v1.35.0) • Preparing nodes ...✓ Preparing nodes • Writing configuration ...✓ Writing configuration •Startingcontrol-plane ️...✓Startingcontrol-plane ️ • Installing CNI ...✓ Installing CNI • Installing StorageClass ...✓ Installing StorageClass • Joining worker nodes ...✓ Joining worker nodes Setkubectl contexttokind-golang-per-dayYou can nowuseyour clusterwith: kubectl cluster-info--context kind-golang-per-day二、安装 istioctl命令工具1. 下载 压缩包https://github.com/istio/istio/releases/download/1.28.2/istioctl-1.28.2-win-amd64.zip2. 解压到 ~/go/bin/istioctl.exe或者添加到系统的PATH里。三、在K8s中安装 Istio$ istioctl.exe install --set profiledemo -y |\ | \ | \ | \ /|| \ / || \ / || \ / || \ / || \ / || \ /______||__________\ ____________________ \__ _____/ \_____/ ✔ Istio core installed ⛵️ ✔ Istiod installed ✔ Egress gateways installed ✔ Ingress gateways installed ✔ Installation complete四、验证状态$ kubectl get pods-n istio-system NAME READYSTATUSRESTARTS AGE istio-egressgateway-67cf7cbcdd-6jfrg1/1Running028s istio-ingressgateway-6bcdbb9678-4jgk51/1Running028s istiod-666f895d5d-fgqbr1/1Running051s五、配置Ingress网关端口由于 Kind 无法像云厂商那样自动分配LoadBalancer的 External IP我们需要手动将 Istio 的入站服务改为NodePort并匹配我们在 Kind 配置中映射的端口30000 和 30001。执行以下补丁命令kubectl patch svc istio-ingressgateway -n istio-system --typejson -p[ {op: replace, path: /spec/type, value: NodePort}, {op: replace, path: /spec/ports/1/nodePort, value: 30000} ]六、在Istio中发布应用1. 创建命名空间//创建命名空间$ kubectl createnamespacecodee-junnamespace/codee-jun created2. 开启Sidercar自动注入Istio 的强大功能流量管理、安全、监控是依靠在你的应用容器旁运行的一个“边车”代理Envoy实现的。你需要告诉 Istio 在你部署应用时自动把这个代理“塞”进去。//给命名空间打标签$ kubectl labelnamespacecodee-jun istio-injectionenabled--overwritenamespace/codee-jun labeled3. 部署应用$ kubectlapply-f configmap.yaml configmap/golang-per-day-68-configmap created $ kubectl.exeapply-f app.yaml service/golang-per-day-68unchanged deployment.apps/golang-per-day-68created$ kubectl get pods-n codee-jun NAME READYSTATUSRESTARTS AGE golang-per-day-68-667956b8d7-dddbh2/2Running056s golang-per-day-68-667956b8d7-kj5cw2/2Running056s golang-per-day-68-667956b8d7-lxdwd2/2Running056s$ kubectl logs-f golang-per-day-68-667956b8d7-dddbh-n codee-jun Hello,Codee君![GIN-debug][WARNING]Creating anEngineinstancewiththe LoggerandRecovery middleware already attached.[GIN-debug][WARNING]Runningindebugmode.Switchtoreleasemodeinproduction.-usingenv: export GIN_MODErelease-usingcode: gin.SetMode(gin.ReleaseMode)[GIN-debug]GET/ping-- main.main.func1 (3 handlers)[GIN-debug][WARNING]You trustedallproxies,thisisNOTsafe.We recommend youtosetavalue.Pleasecheckhttps://github.com/gin-gonic/gin/blob/master/docs/doc.md#dont-trust-all-proxies for details.[GIN-debug]Listeningandserving HTTPon:80804. 配置网关与路由(Istio核心资源)为了让外部用户能访问你的应用你需要创建两个 Istio 专属资源Gateway和VirtualService。4.1 定义入口网关apiVersion: networking.istio.io/v1beta1 kind: Gateway metadata: name: golang-per-day-68-gateway namespace: codee-jun spec: selector: istio: ingressgateway # 使用 Istio 默认的网关控制器 servers: - port: number: 80 name: http protocol: HTTP hosts: - * # 或者填写你的具体域名如 myapp.example.com$ kubectlapply-f istio-ingress.yaml gateway.networking.istio.io/golang-per-day-68-gateway created4.2 定义虚拟服务apiVersion: networking.istio.io/v1beta1 kind: VirtualService metadata: name: golang-per-day-68-route namespace: codee-jun spec: hosts: - * gateways: - golang-per-day-68-gateway # 绑定上面定义的网关 http: - match: - uri: prefix: / # 匹配路径这里是匹配所有 route: - destination: host: golang-per-day-68 # 你的 K8s Service 名称 port: number: 8080 # Service 暴露的端口$ kubectlapply-f istio-route.yaml virtualservice.networking.istio.io/golang-per-day-68-route created浏览器访问看见以上信息恭喜你部署成功了流量的路径是完整的Windows 浏览器/Curl-Kind 端口转发 (80:30000)-Istio Ingress Gateway-Istio VirtualService 路由-golang-per-day-68 服务 (Pod)。七、安装流量监控 (Kiali)kubectl apply -f https://raw.githubusercontent.com/istio/istio/release-1.24/samples/addons/prometheus.yaml kubectl apply -f https://raw.githubusercontent.com/istio/istio/release-1.24/samples/addons/kiali.yaml八、查看流量图$ istioctl dashboard kiali http://localhost:20001/kiali友情链接:加班费计算器(vx小程序搜索“加班计”)*源码地址*评论区留言要如果您喜欢这篇文章请您点赞、分享、亮爱心万分感谢