最近也开始接触Service Mesh,这里选用最具代表性的Istio
要用ServiceMesh,先要安装k8s,但是传统的安装比较麻烦,这里推荐用kk
安装k8s
https://www.kubesphere.io/zh/docs/v3.3/quick-start/all-in-one-on-linux/

照着教程一步一步来就行了,最麻烦的就是网络问题。只要下载完成了,以后重装也很快。
成功后的控制台返回访问地址。


简单介绍一点k8s的使用
K8s运行镜像(可行但推荐用配置而不是命令)

kubectl get pod
kubectl get deployment
K8s删除pod

还会重新创建,deployment管理pod
要删除deployment:kubectl delete deployments.apps tomcat-test

扩容命令(扩容成10个) --replocas=10 很容易做扩展

搭建Istio
下载1.8.6(网络太差,直接到github用迅雷下载)
https://github.com/istio/istio/releases/
tar -zxvf istio-1.8.6-linux-amd64.tar.gz
export PATH=$PWD/bin:$PATH不行
export PATH=/root/istio-1.8.6/bin
/root/istio-1.8.6/bin
source /etc/profile
还是不行,直接再文件夹内运行安装命令
./istioctl install --set profile=demo(执行失败就手敲)
安装失败
1.网络问题(不是)
2.没有node节点
3.没有允许master节点可以调度
操作界面点击停止调度,再点一下开启调度,之后再安装成功了

安装成功

kubectl get svc -n istio-system 查看

istio注入的本质是k8s资源被istio改变

k8s默认命名空间开启自动注入Envoy Sidecar
很关键
kubectl label namespace default istio-injection=enabled
返回
namespace/default labeled
Kiali是一个基于服务网格的Istio管理控制台
kubectl apply -f samples/addons
其中具体会安装部署Promethues、Grafana、Zipkin等指标及链路采集服务

通过nodePort的方式对外暴露端口
Kiali要在别的机子上查看就需要暴露端口。

创建docker仓库
我是建了个阿里云的仓库

做包上传到docker仓库
docker build --rm -t system .
然后上传到docker仓库
这里自己看阿里库的说明跟着做就行了

使用配置文件启动service
接下来就是和平时使用docker不一样的地方了,之前说过k8s不推荐用命令运行镜像,推荐用配置文件的方式运行。
K8s配置解释
apiVersion: v1//版本号,例如v1
kind: Service//Pod
metadata://元数据
name: micro-order//Pod的名称
labels://自定义标签列表
app: micro-order
service: micro-order
spec://Pod中容器的详细定义
type: ClusterIP
ports:
- name: http
port: 80//服务访问端口 (有啥用?集群内其他pod访问本pod的时候)
targetPort: 9091//容器端口(本身服务定的端口)
selector:
app: micro-order
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: micro-order-v1
labels:
app: micro-order
version: v1
spec:
replicas: 2
selector:
matchLabels:
app: micro-order
version: v1
template:
metadata:
labels:
app: micro-order
version: v1
spec:
#设置的阿里云私有镜像仓库登陆信息的secret(对应2.1.2的设置)
imagePullSecrets:
- name: summerzhangregsecret
containers:
- name: micro-order
image: registry.cn-hangzhou.aliyuncs.com/myworknamespace/myworkwarehouse:zudp-micro-api-aly//容器的镜像名称要从网上拉,不是本地的
imagePullPolicy: Always
tty: true
ports:
- name: http
protocol: TCP
containerPort: 19091
其他属性
metadata.namespace:Pod所属的命名空间
metadata.annotation[]:自定义注解列表
spec.containers[]:Pod中的容器列表
spec.containers[].name:容器的名称

配置文件传到服务器上运行
kubectl apply -f micro-api.yaml
kubectl get pods 查看pod
kubectl delete -f XXX //删除服务
部署成功后观测
部署了一个服务但是有两个pod,其中一个就是Sidecar代理进程

查看状态可以看到

控制台也可以看到

问题排查
kubectl get pod查看pod状态
kubectl describe pod xxx 查看错误原因
配置gateway
这个gateway是istio自带的,不需要用我们项目里的gateway了。
kubectl get svc -n istio-system|grep istio-ingressgateway

调用时使用80端口映射的31847

接下来我们需要设置通过该网关访问微服务的逻辑,编写网关部署文件
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: gateway
spec:
selector:
istio: ingressgateway
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "*"
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: gateway
spec:
hosts:
- "*"
gateways:
- gateway
http:
- match:
- uri:
exact: /system
route:
- destination:
host: system
port:
number: 19090
增加微服务配置
之后增加服务就不用重新运行配置文件了
可以直接在kiali中修改配置


Feign改造
接下来就是feign改造了,没啥好方法,自己发挥吧,本质还是http发送。
网友评论