1.安装istio
a 下载istioctl安装
下载
$ wget https://github.com/istio/istio/releases/download/1.7.3/istio-1.7.3-linux-amd64.tar.gz
配置
tar zxf istio-1.7.3-linux-amd64.tar.gz
cd istio-1.7.3 && cp bin/istioctl /usr/local/bin
b.安装iostio
#指定下载的镜像地址进行安装
istioctl install --set profile=demo --set values.pilot.image="harbor.assistfc.com/middleware/pilot:1.7.3" --set values.proxy.image="harbor.assistfc.com/middleware/proxyv2:1.7.3"
#效果如下
root@test-k8s-master1:~# kubectl get pod -n istio-system
NAME READY STATUS RESTARTS AGE
istio-egressgateway-7f6bd48c74-t9q6g 1/1 Running 0 6m16s
istio-ingressgateway-69f5465fff-szfql 1/1 Running 0 6m16s
istiod-7c57888-65564 1/1 Running 0 6m26s
c查看
# 查看提供的profile类型
$ istioctl profile list
# 获取kubernetes的yaml:
$ istioctl manifest generate --set profile=demo > istio-kubernetes-manifest.yaml #可查看要安装镜像image
d 卸载命令
$ istioctl manifest generate --set profile=demo | kubectl delete -f -
实践网络分流的权重
1.资源清单
后端服务
#后端一
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
service: bill-service
version: v1
name: bill-service-v1
namespace: istio-demo
spec:
replicas: 1
selector:
matchLabels:
service: bill-service
version: v1
template:
metadata:
labels:
service: bill-service
version: v1
spec:
containers:
- image: harbor.assistfc.com/middleware/nginx:alpine
name: bill-service
command: ["/bin/sh", "-c", "echo 'this is bill-service-v1'>/usr/share/nginx/html/index.html;nginx -g 'daemon off;'"]
#后端2
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
service: bill-service
version: v2
name: bill-service-v2
namespace: istio-demo
spec:
replicas: 1
selector:
matchLabels:
service: bill-service
version: v2
template:
metadata:
labels:
service: bill-service
version: v2
spec:
containers:
- image: harbor.assistfc.com/middleware/nginx:alpine
name: bill-service
command: ["/bin/sh", "-c", "echo 'this is bill-service-v2'>/usr/share/nginx/html/index.html;nginx -g 'daemon off;'"]
#service %50均分流量
apiVersion: v1
kind: Service
metadata:
labels:
service: bill-service
name: bill-service
namespace: istio-demo
spec:
ports:
- name: http
port: 9999
protocol: TCP
targetPort: 80
selector:
service: bill-service
type: ClusterIP
#实现90%的流量 到v1 10%到v2
需要通过istio的两个新的资源类型:VirtualService和DestinationRule实现
具体如下
使用Istio 注入sidecar
$ istioctl kube-inject -f service-tomecat-v2.yaml|kubectl apply -f -
$ istioctl kube-inject -f service-tomecat-v1.yaml|kubectl apply -f -
$ istioctl kube-inject -f front-tomecat.yaml|kubectl apply -f -
注入网络接管服务 proxyv2:1.7.3
查看容器的状态
#会启动新的容器
root@test-k8s-master1:~/istio-demo# kubectl get pod -n istio-demo
NAME READY STATUS RESTARTS AGE
bill-service-v1-86754676d6-qcstq 2/2 Running 0 86m
bill-service-v2-777d8d98c7-gzhth 2/2 Running 0 85m
front-tomcat-v1-9c866f69b-m56kw 2/2 Running 0 88m
#配置destination文件和vertuleservice
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: dest-bill-service
namespace: istio-demo
spec:
host: bill-service
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: vs-bill-service
namespace: istio-demo
spec:
hosts:
- bill-service
http:
- name: bill-service-route
route:
- destination:
host: bill-service
subset: v1
weight: 90
- destination:
host: bill-service
subset: v2
weight: 10
ervice:9999
this is bill-service-v1
root@test-k8s-master1:~/istio-demo# kubectl exec -it front-tomcat-v1-9c866f69b-m56kw -n istio-demo -c front-tomcat -- curl -s bill-service:9999
this is bill-service-v1
root@test-k8s-master1:~/istio-demo# kubectl exec -it front-tomcat-v1-9c866f69b-m56kw -n istio-demo -c front-tomcat -- curl -s bill-service:9999
this is bill-service-v1
root@test-k8s-master1:~/istio-demo# kubectl exec -it front-tomcat-v1-9c866f69b-m56kw -n istio-demo -c front-tomcat -- curl -s bill-service:9999
this is bill-service-v2
root@test-k8s-master1:~/istio-demo# kubectl exec -it front-tomcat-v1-9c866f69b-m56kw -n istio-demo -c front-tomcat -- curl -s bill-service:9999
this is bill-service-v1
以上为测试结果实现90%和10%的流量的切换