为了账号安全,请及时绑定邮箱和手机立即绑定

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

标签:
Kubernetes

简介

使用kube-router把k8s集群中的pod ipcluter i暴露集群外部,实现集群外的节点直接访问k8s的pod和svc

环境说明

本实验在已经安装配置好k8s集群基础之上进行实验,k8s安装参考博客其他文章。

实验架构

lab1: master 11.11.11.111lab2: node 11.11.11.112lab3: node 11.11.11.113lab4: 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-proxykubectl -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# 启动用于测试的deploymentkubectl 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 64512log stdout
EOF# 启动systemctl start bgpd
systemctl status bgpd
systemctl enable bgpd# 查看路由信息ip route

在lab4测试访问k8s集群中的pod和svc

# 在lab1上获取pod和svc信息kubectl get pods -o widekubectl get svc# 在lab4上访问
# 10.244.2.11 其中一个 nginx pod 的ip# 10.106.123.190 为 example-service 的 cluster ipcurl 10.244.2.11curl 10.106.123.190

清理

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



作者:CountingStars_
链接:https://www.jianshu.com/p/2229b65bf980


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消