Master节点的安装

1、首先在master节点安装ovs和ovn:

#!/bin/bash

sudo apt-get install openvswitch-common openvswitch-switch

sudo apt-get install ovn-common ovn-central ovn-host

  

源文件参见我的github(下文同理):https://github.com/YaoZengzeng/scripts/blob/master/ovn-central.sh

2、下载并安装ovn-kubernetes:

git clone https://github.com/openvswitch/ovn-kubernetes
cd ovn-kubernetes
pip install .

  

3、对ovn master节点进行初始化:

#!/bin/bash

NODE_NAME=

CENTRAL_IP=
LOCAL_IP= CLUSTER_IP_SUBNET=
MASTER_SWITCH_SUBNET= ovs-vsctl set Open_vSwitch . external_ids:ovn-remote="tcp:$CENTRAL_IP:6642" \
external_ids:ovn-nb="tcp:$CENTRAL_IP:6641" \
external_ids:ovn-encap-ip="$LOCAL_IP" \
external_ids:ovn-encap-type="geneve" ovs-vsctl set Open_vSwitch . external_ids:k8s-api-server="127.0.0.1:8080" ovn-k8s-overlay master-init \
--cluster-ip-subnet=$CLUSTER_IP_SUBNET \
--master-switch-subnet="$MASTER_SWITCH_SUBNET" \
--node-name="$NODE_NAME"

  

其中NODE_NAME为master节点的hostname,CENTRAL_IP和LOCAL_IP对于master节点是一样的,都是master节点自身的IP地址,CLUSTER_IP_SUBNET为整个集群的子网范围,例如192.168.0.0/16,而MASTER_SWITCH_SUBNET则为master节点上的pod能够分配的子网范围,例如192.168.1.0/24

源码链接:https://github.com/YaoZengzeng/scripts/blob/master/ovn-k8s-master.sh

4、最开始我是使用kubeadm搭建k8s集群的,但是当通过http://127.0.0.1:8080访问api server时会发现该端口并未打开,可能是kubeadm默认是不打开该端口的,因此只能使用手动方式搭建kubernetes。首先下载并安装kubernetes:

#!/bin/bash

# Install docker
apt-get update
apt-get install -y docker.io mkdir k8s
cd k8s wget https://github.com/kubernetes/kubernetes/releases/download/v1.5.3/kubernetes.tar.gz tar xvzf kubernetes.tar.gz ./kubernetes/cluster/get-kube-binaries.sh mkdir server cd server tar xvzf ../kubernetes/server/kubernetes-server-linux-amd64.tar.gz cd kubernetes/server/bin cp kube-apiserver kube-controller-manager kube-scheduler kubelet kubectl /usr/bin # Install cni
mkdir -p /opt/cni/bin && cd /opt/cni/bin wget https://github.com/containernetworking/cni/releases/download/v0.5.2/cni-amd64-v0.5.2.tgz tar xvzf cni-amd64-v0.5.2.tgz

源码连接:https://github.com/YaoZengzeng/scripts/blob/master/k8s-download.sh

5、接着运行kubernetes的各个组件:

#!/bin/bash

# On the master node, start etcd
docker run \
--net=host \
--detach \
gcr.io/google_containers/etcd:2.0.12 \
/usr/local/bin/etcd \
--addr=127.0.0.1:4001 \
--bind-addr=0.0.0.0:4001 \
--data-dir=/var/etcd/data # install kube-apiserver
nohup kube-apiserver \
--service-cluster-ip-range=192.168.200.0/24 \
--address=0.0.0.0 \
--etcd-servers=http://127.0.0.1:4001 \
--v=2 \
2>&1 > /dev/null & # install kube-controller-manager
nohup kube-controller-manager \
--master=127.0.0.1:8080 \
--v=2 \
2>&1 > /dev/null & # install kube-scheduler
nohup kube-scheduler \
--master=127.0.0.1:8080 \
--v=2 \
2>&1 > /dev/null &

源码链接:https://github.com/YaoZengzeng/scripts/blob/master/k8s-master.sh

  

6、最后,启动ovn-k8s-watcher对k8s apiserver进行同步:

#!/bin/bash

ovn-k8s-watcher \
--overlay \
--pidfile \
--log-file \
-vfile:info \
-vconsole:emer \
--detach

  

源码链接:https://github.com/YaoZengzeng/scripts/blob/master/ovn-k8s-watcher.sh

到此为止,master节点配置完成

Minion节点的安装

1、首先在minion节点安装ovs和ovn:

#!/bin/bash

sudo apt-get install openvswitch-common openvswitch-switch

sudo apt-get install ovn-common ovn-host

源码链接:https://github.com/YaoZengzeng/scripts/blob/master/ovn-host.sh

2、下载并安装ovn-kubernetes:

git clone https://github.com/openvswitch/ovn-kubernetes
cd ovn-kubernetes
pip install .

  

3、对ovn minion节点进行初始化:

#!/bin/bash

NODE_NAME=

CENTRAL_IP=
LOCAL_IP= K8S_API_SERVER_IP= CLUSTER_IP_SUBNET=
MINION_SWITCH_SUBNET= ovs-vsctl set Open_vSwitch . external_ids:ovn-remote="tcp:$CENTRAL_IP:6642" \
external_ids:ovn-nb="tcp:$CENTRAL_IP:6641" \
external_ids:ovn-encap-ip="$LOCAL_IP" \
external_ids:ovn-encap-type="geneve" ovs-vsctl set Open_vSwitch . \
external_ids:k8s-api-server="$K8S_API_SERVER_IP:8080" ovn-k8s-overlay minion-init \
--cluster-ip-subnet="$CLUSTER_IP_SUBNET" \
--minion-switch-subnet="$MINION_SWITCH_SUBNET" \
--node-name="$NODE_NAME"

  

NODE_NAME等变量和上文基本相同,其中LOCAL_IP是minion的IP地址,而CENTRAL_IP和K8S_API_SERVER_IP为master的IP地址

源码链接:https://github.com/YaoZengzeng/scripts/blob/master/ovn-k8s-minion.sh

4、和Master部分的步骤4相同,下载并安装kubernetes

5、运行kubernetes minion节点的组件,其实就是kubelet:

#!/bin/bash

K8S_API_SERVER_IP=

# Start kubelet
nohup kubelet \
--api-servers=http://$K8S_API_SERVER_IP:8080 \
--v=2 \
--address=0.0.0.0 \
--enable-server=true \
--network-plugin=cni \
--network-plugin-dir=/etc/cni/net.d \
2>&1 > /dev/null &

  

其中K8S_API_SERVER_IP变量为master节点的IP地址

源码链接:https://github.com/YaoZengzeng/scripts/blob/master/k8s-minion.sh

Gateway节点的安装

本文中,我们将Gateway也安装在Minion节点上 。官方对于Gateway节点的安装有两种方案,一种是使用专有的网卡eth1,另一种是公用eth0。在这里我们使用后者。

1、首先eth0的IP地址"$PHYSICAL_IP",我们需要创建一个网桥"breth0",接着将eth0作为端口添加到网桥上,之后再将"$PHYSICAL_IP"移到breth0上。同时,我们也需要把和eth0相关的路由修改为和breth0相关。

我们可以使用工具ovn-k8s-util完成上述操作。需要注意的是"$PHYSICAL_IP"必须是带掩码配置的,例如,10.33.74.138/24

2、初始化gateway

3、因为对于mgmt和North-South连接共享一个网卡,因此我们需要独立启动一个daemon用于分流,该daemon名称为ovn-k8s-gateway-helper。

具体的安装脚本如下:

#!/bin/bash

NODE_NAME=m1

CLUSTER_IP_SUBNET=192.168.0.0/16

PHYSICAL_IP=

EXTERNAL_GATEWAY=

K8S_API_SERVER_IP=

NIC=eth0

OVSBR=breth0

ovs-vsctl set Open_vSwitch . \
external_ids:k8s-api-server="$K8S_API_SERVER_IP:8080" ovn-k8s-util nics-to-bridge $NIC ovn-k8s-overlay gateway-init \
--cluster-ip-subnet="$CLUSTER_IP_SUBNET" \
--bridge-interface $OVSBR \
--physical-ip "$PHYSICAL_IP" \
--node-name="$NODE_NAME" \
--default-gw "$EXTERNAL_GATEWAY" ovn-k8s-gateway-helper --physical-bridge=$OVSBR --physical-interface=$NIC \
--pidfile --detach

其中EXTERNAL_GATEWAY为宿主机的默认网关

源码链接:https://github.com/YaoZengzeng/scripts/blob/master/ovn-k8s-gateway.sh

到此为止,完成了ovn-kubernetes的安装。

05-11 11:28