天天看點

k8s使用kube-router暴露叢集中的pod和svc到外部

簡介

使用

kube-router

把k8s叢集中的

pod ip

cluter i

暴露叢集 外部,實作叢集外的節點直接通路k8s的pod和svc

環境說明

本實驗在已經安裝配置好k8s叢集基礎之上進行實驗,k8s安裝參考部落格其他文章。

實驗架構

lab1: master 11.11.11.111
lab2: node 11.11.11.112
lab3: node 11.11.11.113
lab4: external 11.11.11.114
複制代碼           

安裝

# 本次實驗重新建立了叢集,使用之前測試其他網絡插件的叢集環境沒有成功
# 可能是由于環境幹擾,實驗時需要注意

# 建立kube-router目錄下載下傳相關檔案
mkdir kube-router && cd kube-router
rm -f generic-kuberouter-all-features.yaml
wget https://raw.githubusercontent.com/cloudnativelabs/kube-router/master/daemonset/generic-kuberouter-all-features-advertise-routes.yaml

# 啟用pod網絡通信,網絡隔離政策,服務代理所有功能
# CLUSTERCIDR kube-controller-manager 啟動參數 --cluster-cidr 的值
# APISERVER kube-apiserver 啟動參數 --advertise-address 值
CLUSTERCIDR='10.244.0.0/16'
APISERVER='https://11.11.11.111:6443'
sed -i "s;%APISERVER%;$APISERVER;g" generic-kuberouter-all-features-advertise-routes.yaml
sed -i "s;%CLUSTERCIDR%;$CLUSTERCIDR;g" generic-kuberouter-all-features-advertise-routes.yaml

# 修改配置
      containers:
      - name: kube-router
        image: cloudnativelabs/kube-router
        imagePullPolicy: Always
        args:
        ...
        - "--peer-router-ips=11.11.11.114"
        - "--peer-router-asns=64513"
        - "--cluster-asn=64512"
        - "--advertise-cluster-ip=true"
        ...

# 部署
kubectl apply -f generic-kuberouter-all-features-advertise-routes.yaml

# 删除kube-proxy
kubectl -n kube-system delete ds kube-proxy

# 在每個節點上執行
# 如果是二進制安裝使用如下指令
systemctl stop kube-proxy

# 在每個節點上執行
# 清理kube-proxy留下的規則
docker run --privileged --net=host registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy-amd64:v1.10.2 kube-proxy --cleanup

# 檢視
kubectl get pods -n kube-system
kubectl get svc -n kube-system
複制代碼           

測試

# 測試之前請先安裝配置好kube-dns或者coredns

# 啟動用于測試的deployment
kubectl run nginx --replicas=2 --image=nginx:alpine --port=80
kubectl expose deployment nginx --type=NodePort --name=example-service-nodeport
kubectl expose deployment nginx --name=example-service

# 檢視
kubectl get pods -o wide
kubectl get svc -o wide

# dns及通路測試
kubectl run curl --image=radial/busyboxplus:curl -i --tty
nslookup kubernetes
nslookup example-service
curl example-service
複制代碼           

在lab4配置quagga

# 安裝
yum install -y quagga

# 配置
cat >/etc/quagga/bgpd.conf<<EOF
! -*- bgp -*-
!
! BGPd sample configuratin file
!
! $Id: bgpd.conf.sample,v 1.1 2002/12/13 20:15:29 paul Exp $
!
hostname lab4
password zebra
!
router bgp 64513
  bgp router-id 11.11.11.114
  neighbor 11.11.11.111 remote-as 64512
  neighbor 11.11.11.112 remote-as 64512
  neighbor 11.11.11.113 remote-as 64512
log stdout
EOF

# 啟動
systemctl start bgpd
systemctl status bgpd
systemctl enable bgpd

# 檢視路由資訊
ip route
複制代碼           

在lab4測試通路k8s叢集中的pod和svc

# 在lab1上擷取pod和svc資訊
kubectl get pods -o wide
kubectl get svc

# 在lab4上通路
# 10.244.2.11 其中一個 nginx pod 的ip
# 10.106.123.190 為 example-service 的 cluster ip
curl 10.244.2.11
curl 10.106.123.190
複制代碼           

清理

# 清理
kubectl delete svc example-service example-service-nodeport
kubectl delete deploy nginx curl           

本文轉自掘金-

k8s使用kube-router暴露叢集中的pod和svc到外部

繼續閱讀