第一步:以nginx的容器为测试
build.sh脚本内容如下。
#!/bin/sh
path_current=`pwd`
path_script=$(cd "$(dirname "$0")"; pwd)
ver=$1
if [ "$ver" == "" ]; then
echo "should like: build.sh v2"
exit
fi
docker rmi ngtest:${ver}
docker stop mynginx
docker rm mynginx
docker run --name mynginx -d nginx:1.17.8
docker cp ./default.conf mynginx:/etc/nginx/conf.d/default.conf && docker commit mynginx ngtest:${ver}
docker stop mynginx
docker rm mynginx
echo "----------------list ngtest image---------------------"&&docker images|grep ngtest
与脚本同级目录下的default.conf配置文件如下:
server {
listen 80;
listen [::]:80;
server_name localhost;
location /test {
default_type application/json;
return 200 "vm4 - v2 - time:$date_gmt - remote:$remote_addr";
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
第二步:
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: ngtest-gateway
spec:
selector:
istio: ingressgateway # use istio default controller
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "mynginx.cn"
-----------------------------------------
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: ngtest
spec:
hosts:
- "ngtestsvc"
gateways:
- ngtest-gateway
http:
- match:
- uri:
prefix: /
route:
- destination:
host: ngtestsvc
port:
number: 1080
subset: v1
weight: 90
- destination:
host: ngtestsvc
port:
number: 1080
subset: v2
weight: 10
---------------------
apiVersion: v1
kind: Service
metadata:
name: ngtestsvc
spec:
type: NodePort
ports:
- port: 1080
targetPort: 80
name: http
selector:
app: ngtestapp
-------------------------------------
apiVersion: apps/v1
kind: Deployment
metadata:
name: ngtestapp.v1
spec:
replicas: 3
selector:
matchLabels:
app: ngtestapp
version: v1
template:
metadata:
labels:
app: ngtestapp
version: v1
spec:
containers:
- name: ngtest
image: ngtest:v1
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
env:
- name: DO_NOT_ENCRYPT
value: "true"
------------------------------------
apiVersion: apps/v1
kind: Deployment
metadata:
name: ngtestapp.v2
spec:
replicas: 3
selector:
matchLabels:
app: ngtestapp
version: v2
template:
metadata:
labels:
app: ngtestapp
version: v2
spec:
containers:
- name: ngtest
image: ngtest:v2
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
env:
- name: DO_NOT_ENCRYPT
value: "true"