在VPS上搭建Istio环境

一、任务

  • 使用kubeadm搭建一个1master+2slaves的kubernates集群
  • 于集群上部署Istio,运行其BookInfo示例

二、准备

  • 在Vultr上租用三台VPS,配置均为2 vCore,4G RAM,OS都安装CentOS 7 x64。(实际搭建运行时未遇到性能问题。)
    • 需要注意的是,很多海外VPS的IP都被封掉了,ping不通。(可到站长之家之类的网站去确认,国内ping不通,海外可通。)对此可重建VPS直至获取可访问的IP,或者依赖科学上网的跳板机。
    • 另外,做验证只使用公网IP即可,无需使用数据中心的内网IP(当然实际的生产环境是使用内网IP的),甚至这三台VPS还可以是跨数据中心的,都可以搭建成功。
  • 基本的知识依据为官网指南,按使用顺序依次为

注1:安装kubeadm和kubernates集群时,预想坑会比较多,还查阅了一些技术博客和官网对比了一下,但发现除了说明顺序不一样外(官网把很多细节放到Notes里),技术博客中提到的要点,在官网基本上都是有所体现。 注2:按说前4篇做完就应该结束了,但存在一个问题:Istio官网上的上述文档只给出了存在外部负载均衡的环境下查找ingress的对外IP和端口的方法,新装的环境并不具备外部负载均衡,因此还需要阅读第5篇的内容,使用不具备外部负载均衡环境下的相应手段找到对外IP和端口。

三、实施

步骤一:任选1台VPS部署Master节点。命令的流水账如下:

systemctl stop firewalld
systemctl disable firewalld

cat <<EOF >  /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system

setenforce 0

swapoff -a

yum update

yum install -y docker
systemctl enable docker && systemctl start docker

cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF
yum install -y kubelet kubeadm kubectl
systemctl enable kubelet && systemctl start kubelet

kubeadm init --pod-network-cidr=10.244.0.0/16

export KUBECONFIG=/etc/kubernetes/admin.conf

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/v0.9.1/Documentation/kube-flannel.yml

kubectl get pods --all-namespaces
kubectl get nodes

步骤二:在剩下的2台VPS上分别部署Slave节点。2台的命令相同,流水账如下:

systemctl stop firewalld
systemctl disable firewalld

cat <<EOF >  /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system

setenforce 0

swapoff -a

yum update

yum install -y docker
systemctl enable docker && systemctl start docker

cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF
yum install -y kubelet kubeadm kubectl
systemctl enable kubelet && systemctl start kubelet

kubeadm join --token cb320d.dffffef07d403d95 45.38.123.45:6443 --discovery-token-ca-cert-hash sha256:83a5715933843b4ad623414bea22c9d710851c276b2050906b3a33ddf924480f

步骤三:在Master节点上部署Istio,运行BookInfo示例。命令的流水账如下:

curl -L https://git.io/getLatestIstio | sh -

cd istio-0.4.0

kubectl apply -f install/kubernetes/istio-auth.yaml
kubectl apply -f install/kubernetes/istio-auth.yaml
kubectl apply -f install/kubernetes/istio-initializer.yaml
kubectl apply -f install/kubernetes/istio-initializer.yaml
kubectl get svc -n istio-system
kubectl get pods -n istio-system

kubectl create -f samples/bookinfo/kube/bookinfo.yaml
kubectl get services
kubectl get pods

kubectl -n istio-system get po -l istio=ingress -o jsonpath='{.items[0].status.hostIP}'
kubectl -n istio-system get svc istio-ingress
curl -o /dev/null -s -w "%{http_code}\n" http://45.66.157.87:32384/productpage

kubectl create -f samples/bookinfo/kube/route-rule-all-v1.yaml
kubectl create -f samples/bookinfo/kube/route-rule-reviews-test-v2.yaml

四、验证

在浏览器中访问部署过程最后得到的URL:http://{host:port}/productpage,可以看见画面,并可在此画面确认针对不同版本的Reviews的请求路由效果。