首页 新闻 赞助 找找看

python 如何通过 socks5 代理访问 k8s 的 apiClient

0
悬赏园豆:80 [待解决问题]

不使用代理时访问 k8s apiclient 如下:

configuration = client.Configuration()
configuration.verify_ssl = False
configuration.host = "xxx"
configuration.api_key = {"authorization": "Bearer " + self.token}
c = api_client.ApiClient(configuration=configuration)
api = core_v1_api.CoreV1Api(c)
# 查询命名空间,该步骤成功
result = api.list_namespace()

由于k8s使用的是restClient,所以没办法传递socks5代理,目前使用该方法连接,但是无效:

configuration = client.Configuration()
configuration.verify_ssl = False
configuration.api_key = {"authorization": "Bearer " + self.token}
c = api_client.ApiClient(configuration=configuration)
configuration.host = "xxx"
configuration.proxy = "socks5://xxx:1080"
# 该步骤设置socks代理
c.rest_client.pool_manager = self.build_socks_proxy_manager(configuration)
api = core_v1_api.CoreV1Api(c)
# 查询命名空间,该步骤超时,无法连接
result = api.list_namespace()

def build_socks_proxy_manager(self, configuration, pools_size=4, maxsize=None):
  # 省略一些初始化步骤
  return SOCKSProxyManager(num_pools=pools_size,
                    maxsize=maxsize,
                    cert_reqs=cert_reqs,
                    ca_certs=ca_certs,
                    cert_file=configuration.cert_file,
                    key_file=configuration.key_file,
                    proxy_url=configuration.proxy,
                    **addition_pool_args)

雨落成尘的主页 雨落成尘 | 初学一级 | 园豆:122
提问于:2023-04-11 21:09
< >
分享
所有回答(3)
0

要通过 Socks5 代理访问 Kubernetes 的 APIClient,您需要使用 Kubernetes 的 API 客户端库。以下是一个使用 Python 的 kubernetes 库来实现此目的的示例代码:

python
from kubernetes import client, config

配置 Socks5 代理

config.load_kube_config()
config.kube_config['socks5_proxy'] = {
'host': 'your_host',
'port': 'your_port',
'username': 'your_username',
'password': 'your_password'
}

创建 API 客户端

api_client = client.AppsV1ApiClient()

使用 Socks5 代理访问 APIClient

api_client.api_version = 'v1'
api_client.namespace = 'default'
api_client.auth = client.Auth('your_username', 'your_password')
api_client.use_socks5_proxy = True
api_client.connect(config.kube_config['socks5_proxy'])

调用 API 方法

result = api_client.list_namespaced_deployments()
print(result)
在上面的代码中,我们首先加载了 Kubernetes 的配置文件,并设置了 Socks5 代理的主机名、端口、用户名和密码。然后,我们创建了一个 AppsV1ApiClient 对象,并使用 connect() 方法连接到 Socks5 代理。最后,我们调用了 list_namespaced_deployments() 方法,并将 Socks5 代理作为参数传递给它。

请注意,上面的代码仅提供了一个基本的示例,您需要根据自己的需求进行修改和调整。

国产小品牌 | 园豆:199 (初学一级) | 2023-04-12 16:23
0

你可以使用socks库中的socks5代理来为ApiClient设置代理。先使用以下代码安装socks库:

python

pip install PySocks
然后,你可以像这样构造一个代理器来管理代理连接:

python

import socks
import socket

def build_socks_proxy_manager(configuration):
socks.set_default_proxy(socks.SOCKS5, configuration.proxy.host, configuration.proxy.port)
socket.socket = socks.socksocket
# 然后将ApiClient的rest client的socket manager替换成代理器即可
return api_client.rest.Socks5ProxyManager(socks.SOCKS5, configuration.proxy.host, configuration.proxy.port)
最后,你可以按照你之前的方式构造ApiClient,并将上面构造的代理器传入ApiClient的rest client的socket manager参数即可:

python

configuration = client.Configuration()
configuration.verify_ssl = False
configuration.host = "xxx"
configuration.api_key = {"authorization": "Bearer " + self.token}
configuration.proxy = client.ApiClientConfiguration(host="xxx", port=1080, protocol="socks5")
c = api_client.ApiClient(configuration=configuration)
c.rest_client.pool_manager = self.build_socks_proxy_manager(configuration)
api = core_v1_api.CoreV1Api(c)
result = api.list_namespace()
这样应该就可以通过socks5代理访问k8s的api了。

Technologyforgood | 园豆:5468 (大侠五级) | 2023-04-13 21:44

如果您希望在 Python 中使用带有代理的 Kubernetes API 客户端,则可以将代理设置为系统级别的环境变量,并使用 Python 的 os.environ 模块调用它。 推荐使用 http://yeasy.gitbooks.io/kubernetes-practice-explain/content/docs/setup/http-proxy.html 中的方法将代理设置为系统级别环境变量,不建议在代码中指定代理,以减少代码的可维护性和移植性。

以下示例显示如何使用上述方法设置环境变量,并构建具有代理设置的 Kubernetes API 客户端:

import os
from kubernetes import client, config
import urllib3

# 设置代理环境变量
os.environ['HTTP_PROXY'] = 'http://myproxy:8080'
os.environ['HTTPS_PROXY'] = 'http://myproxy:8080'
# 初始化 Kubernetes 配置
api_config = client.Configuration()
# 取消 SSL 验证
api_config.verify_ssl = False
# 配置代理
proxies = urllib3.ProxyManager({
    "http": os.environ['HTTP_PROXY'],
    "https": os.environ['HTTPS_PROXY']
})
# 创建 Kubernetes API 客户端
api_client = client.ApiClient(configuration=api_config, http_client=proxies)
# 创建 API 对象
v1 = client.CoreV1Api(api_client)
# 使用 API 对象进行请求操作
ret = v1.list_pod_for_all_namespaces(watch=False)
# 打印结果
for i in ret.items:
    print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))

请注意,以上示例仅供参考,具体实现可能需要进行适当修改以适应您的环境和需求。同时,建议在代码中使用代理时,正确处理代理的验证和身份验证等问题,以确保代码的安全性。

支持(0) 反对(0) 小九九呀 | 园豆:383 (菜鸟二级) | 2023-06-17 20:24
0

你可以尝试使用http.client.HTTPSConnection类中的socksocket参数来使用socks代理,具体步骤如下:

  1. 安装pysocks模块,可使用pip进行安装,命令为pip install pysocks

  2. 导入http.client模块和socks模块,并创建一个socks.socksocket对象

import http.client
import socks

sock = socks.socksocket()
sock.set_proxy(socks.SOCKS5, "xxx", 1080) # xxx为socks服务器地址,1080为socks服务器端口号
  1. 创建一个HTTPSConnection对象,并在参数中使用sock对象的实例
conn = http.client.HTTPSConnection("your.cluster.url", context=self.context, sock=sock)
  1. 使用创建好的conn对象进行请求
conn.request("GET", "/api/v1/namespaces")
res = conn.getresponse()
print(res.status)

注意,其中的"your.cluster.url"需要替换为你集群的实际地址。以上代码中使用的是http.client.HTTPSConnection类,如果使用的是其他API client,可以查看API client的文档了解如何使用socks代理。

小九九呀 | 园豆:383 (菜鸟二级) | 2023-06-17 20:22
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册