• 开发
  • kubesphere 二次开发后端调试环境搭建

kubesphere 后端调试

本地环境搭建

1.安装go环境

$ wget https://dl.google.com/go/go1.13.linux-amd64.tar.gz

$ tar -C /usr/local -zxvf go1.13.linux-amd64.tar.gz

$ vim /etc/profile # 在~/.profile中添加以下内容
PATH=”$HOME/bin:$HOME/.local/bin:$PATH”
export PATH=$PATH:/usr/local/go/bin 
export GOROOT=/usr/local/go 
export GOPATH=$HOME/go 
export PATH=$PATH:$HOME/go/bin
export GOPROXY=https://goproxy.cn,direct

$ source /etc/profile #重新加载环境变量
$ mkdir /root/go #创建go目录

2. 新建运行目录并下载kubesphere代码

$ mkdir -p $GOPATH/src/kubesphere.io/
$ cd $GOPATH/src/kubesphere.io/
$ git clone https://github.com/kubesphere/kubesphere

3. 搭建Telepresence代理运行本地调试代码

Telepresence代理原理图

Telepresence需要python3,但是平台本身有很多东西依赖于python2
所以不要随意升级,而是同时用python2和python3
如果安装pyhton3之后yum不能用了
参考https://blog.csdn.net/zhongranxu/article/details/79859234

1.安装python3

# 1.安装python3
$ wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz
$ tar -xf Python-3.6.1.tgz 
$ cp -r Python-3.6.1 /usr/local/bin/
$ cd Python-3.6.1
$ ./configure
$ make
$ make install

# 设置默认使用python3
$ cd /usr/bin
$ mv python python.bak
$ ln -s /usr/local/bin/python3.6 /usr/bin/python
$ ln -sf /usr/local/bin/python3.6 /usr/bin/python3

#如果改完之后yum不能用了,修改不能用的这个文件#!/usr/bin/python为#!/usr/bin/python2

2.安装Telepresence需要的依赖

# 2.安装Telepresence需要的依赖
$ yum install -y sshfs conntrack torsocks socat git

出现找不到包的话,安装epel-release开启EPEL源,参考https://jpanj.com/2018/centos-install-epel-repo/

# 安装epel-release开启EPEL源
$ yum install epel-release
#或者
$ wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
$ yum install epel-release-latest-7.noarch.rpm

3.在/home/下载Telepresence的二进制文件安装

# 3.在/home/下载Telepresence的二进制文件安装
$ git clone https://github.com/telepresenceio/telepresence
$ cd telepresence

4.修改telepresence代码

# 4.修改网真源码local.py 中第59行'kubenetes.deafult'引用为'baidu',以及443端口改为80
$ vim /home/telepresence/telepresence/outbound/local.py

在这里插入图片描述

#执行安装
$ env PREFIX=/usr/local ./install.sh 
# 如果显示找不到python3,修改python3的配置文件

5.从集群拷贝配置文件

# 5.从k8s集群的master节点拷贝 /usr/local/bin/kubectl 与 /root/.kube/config 到本机相应目录
$ 这就不写了吧

6.设置KubeSphere核心的配置(官网参考

官网的比较全,但是你的环境安装了那些就配置那些不要放多余的,比如sonarQube、devops等等
redis不需要password就可以连接,对于需要key的,自行去集群查取secret,不要硬搬
比如:
在这里插入图片描述

# 6.设置KubeSphere核心的配置
# 新建文件并键入如下基本配置信息,还安装了其他的,请参考官网一一添上
$ vim /etc/kubesphere.yaml

文件内容如下:

kubernetes:
  kubeconfig: "/root/.kube/config"
  master: https://[后端节点ip]:6443
  qps: 1e+06
  burst: 1000000ip
ldap:
  host: openldap.kubesphere-system.svc:389
  managerDN: cn=admin,dc=kubesphere,dc=io
  managerPassword: admin
  userSearchBase: ou=Users,dc=kubesphere,dc=io
  groupSearchBase: ou=Groups,dc=kubesphere,dc=io
monitoring:
  endpoint: http://prometheus-k8s.kubesphere-monitoring-system.svc:9090
  secondaryEndpoint: http://prometheus-k8s-system.kubesphere-monitoring-system.svc:9090
redis:
  host: redis.kubesphere-system.svc
  port: 6379
  password: ""
  db: 0

在运行telepresence之前,提前预警

运行网真时异常退出可能会造成deployment没有正常swapping回去,以至于原来的ks-apiserver的pod丢失
正确的退出方式见文末(此处感谢大佬们的指点,解决了我的困惑)

7.运行Telepresence(示例)

# 7.运行Telepresence
$ telepresence  --also-proxy=[ks-apiserver的svc的ip,如下] --namespace kubesphere-system --swap-deployment ks-apiserver --expose 9090:9090 --run go run ./cmd/ks-apiserver/apiserver.go

在这里插入图片描述

4.常见问题

1.由Telepresence创出来的这个pod报错ErrImagePull
检查该pod镜像下载成功没有,集群中的每个节点都要检查
代理pod可能会调度到集群中的任意节点,一旦那个节点没有镜像又下载失败就会报错
在这里插入图片描述

2.运行telepresence总是自动退出
首先检查telepresence.log

3.报错telepresence invalid configuration: no configuration has been provided
检查kubesphere.yaml配置全没有

4.如何退出网真代理调式
ctrl+c或者crtl+break
在这里插入图片描述
这样会正常删除网真起的代理pod,并且将deployment swapping回去

5.如果网真异常退出怎么办?
杀掉占用9090端口进程并手动删除网真起的代理pod,去ks-installer的pod里面找到ks-apiserver deployment的yaml文件,重新执行

    TinaSprunt telepresence的swap-deployment 不是你理解的那样,swap-deployment是把目标deployment scale 到0,并没有删除deployment,新起了一个deployment来充当目标service的后端,本地telepresence退出后,原来的deployment会被scale回去的,如果你发现没有scale回去,很可能是你的telepresence意外退出了,手动scale就好了

      就像Jeff说的那样 你ctrl c以后 telepresence会自动清理pod并且把原来的deploy调整到应有的副本数 你应该是意外退出了那个进程 导致没有被还原 如果这样 可以通过ps -ef把不要的进程kill掉 然后手动调整副本数
      另外如果你不需要别的组件跟你沟通的话 可以直接 telepresence 然后在里面go run apiserver.go 这样你的这个进程也能连接到集群内的其他服务 你可以通过curl localhost:9090进行调试
      centos安装telepresence的话 可以看看这个脚本

        Jeff
        哦(⊙o⊙)~感谢大佬,刚才按照你说的实践了一下,确实是因为我异常退出了,才造成这种状况
        异常退出时网真没有将deployment Swapping回去
        当我正常退出时

        非常感谢,我先把帖子修改下

        TinaSprunt hello, 我在使用telepresence时,通过kubectl logs 查看新创建的代理pod总是报下面的错误,你知道是什么原因吗?
        84.4 TEL | 2020-11-07T18:13:07+0800 [twisted.scripts._twistd_unix.UnixAppLogger#info] twistd 20.3.0 (/usr/bin/python3.6 3.6.8) starting up.
        84.4 TEL | 2020-11-07T18:13:07+0800 [twisted.scripts._twistd_unix.UnixAppLogger#info] reactor class: twisted.internet.epollreactor.EPollReactor.
        **84.4 TEL | 2020-11-07T18:13:38+0800 [Poll#error] Failed to contact Telepresence client:
        84.4 TEL | 2020-11-07T18:13:38+0800 [Poll#error] An error occurred while connecting: 99: Address not available.
        84.4 TEL | 2020-11-07T18:13:38+0800 [Poll#warn] Perhaps it's time to exit?
        84.4 TEL | 2020-11-07T18:14:08+0800 [Poll#error] Failed to contact Telepresence client:
        84.4 TEL | 2020-11-07T18:14:08+0800 [Poll#error] An error occurred while connecting: 99: Address not available.
        84.4 TEL | 2020-11-07T18:14:08+0800 [Poll#warn] Perhaps it's time to exit?**
        84.4 TEL | (Cleanup) Re-scale original deployment

          qd19zzx 把执行命令过程再描述一下吧 有没有更多的日志

          qd19zzx
          和yuswift说的一样,希望能提供更详细一点的信息,因为Failed to contact的可能原因太多了
          我出现这个问题的时候是kubesphere.yaml写错了,这个你不用网真直接本地运行一下go run ./cmd/ks-apiserver/apiserver.go看报不报错就知道了,如果本地运行直接报了error先检查各项配置文件

          9 天 后

          这个方法感觉有点麻烦啊,我是直接通过docker跑kubesphere/ks-console:v3.0.0,将配置文件中的apiserver地址改成本地代码地址,然后就可以直接调试了

            joey_chen 这个思路不错啊!不过还要解决连接集群内服务的问题,比如Ldap和redis。 我个人使用kubectl forward port, 不知 @joey_chen 有没有其他高招。是否可以写个教程分享一下啊?

              joey_chen
              是的呢,console代码只要改一下local_config.yaml指定后端ip:port,无论放哪里都可以,后端的kubesphere的代码比较麻烦,理想情况是console —>本地goland运行后端kubesphere —>远程k8s环境,这样不用搭网真代理,但是正版goland要钱啊……好悲伤

              joey_chen
              老铁,你用的本地运行kubesphere的IDE是goland吗,还是别的可以远程调试不要钱的,求安利

                RolandMa1986 我是在本地docker-desktop跑了个容器版K8S,然后部署了一套ks,在ks部署完以后,将redis、ldap等服务的svc改为nodeport,将配置文件复制一份放在/etc/kubesphere目录下,并修改redis等的地址,这样本地代码就可以跑起来了

                14 天 后

                本地测试必须用telepresence吗,我是写个接口,本地启动就报错redis host找不到

                  9 个月 后

                  写的太好了,困扰我好几年的问题终于解决了