简体   繁体   中英

POD don't have access to internet with minikube

I have a python application using Slack API though slackclient lib. I works fine on a docker image. But then I created a deployment to Kubernetes and stated the application on Minikube. It just can't connect to the internet to get the Slack messages.

I am not using any K8S Services since I don't need to expose anything from my service, just call the Slack service already exposed on the internet.

What I need to do to enable that connection?

Update 1 (exception given):

10/03/2019 19:44:50 Starting new HTTPS connection (1): slack.com:443
10/03/2019 19:44:51 https://slack.com:443 "POST /api/rtm.start HTTP/1.1" 200 8581
Traceback (most recent call last):
  File "app.py", line 162, in <module>
    request, channel, ts, user = parse_slack_output(slack_client.rtm_read())
  File "/usr/local/lib/python3.6/site-packages/slackclient/client.py", line 128, in rtm_read
    json_data = self.server.websocket_safe_read()
  File "/usr/local/lib/python3.6/site-packages/slackclient/server.py", line 186, in websocket_safe_read
    data += "{0}\n".format(self.websocket.recv())
  File "/usr/local/lib/python3.6/site-packages/websocket/_core.py", line 313, in recv
    opcode, data = self.recv_data()
  File "/usr/local/lib/python3.6/site-packages/websocket/_core.py", line 330, in recv_data
    opcode, frame = self.recv_data_frame(control_frame)
  File "/usr/local/lib/python3.6/site-packages/websocket/_core.py", line 343, in recv_data_frame
    frame = self.recv_frame()
  File "/usr/local/lib/python3.6/site-packages/websocket/_core.py", line 377, in recv_frame
    return self.frame_buffer.recv_frame()
  File "/usr/local/lib/python3.6/site-packages/websocket/_abnf.py", line 361, in recv_frame
    self.recv_header()
  File "/usr/local/lib/python3.6/site-packages/websocket/_abnf.py", line 309, in recv_header
    header = self.recv_strict(2)
  File "/usr/local/lib/python3.6/site-packages/websocket/_abnf.py", line 396, in recv_strict
    bytes_ = self.recv(min(16384, shortage))
  File "/usr/local/lib/python3.6/site-packages/websocket/_core.py", line 452, in _recv
    return recv(self.sock, bufsize)
  File "/usr/local/lib/python3.6/site-packages/websocket/_socket.py", line 112, in recv
    "Connection is already closed.")
websocket._exceptions.WebSocketConnectionClosedException: Connection is already closed.

There is a problem when calling the function slack_client.rtm_read() in app.py

SLACK_BOT_TOKEN = environ.get('SLACK_BOT_TOKEN')
slack_client = SlackClient(SLACK_BOT_TOKEN)

def parse_slack_output(event_list):
    """
        The Slack Real Time Messaging API is an events firehose.
        this parsing function returns None unless a message is
        directed at the Bot, based on its ID.
    """
    if len(event_list) > 0:
        for event in event_list:
            if is_for_me(event):
                return event['text'], \
                       event['channel'], \
                       event['ts'], \
                       event['user']
    return None, None, None, None
if __name__ == '__main__':
    READ_WEBSOCKET_DELAY = 1 # 1 second delay between reading from firehose
    if slack_client.rtm_connect():
        while True:
            request, channel, ts, user = parse_slack_output(slack_client.rtm_read())

What is the error or exception you are getting while requesting that API ? Is it a DNS resolution issue ?

If it is a DNS resolution issue. Then check if KubeDNS or CoreDNS is running on your kubernetes cluster. If specified above is running then try pinging the host from your pod.

Edit 1:

Looks like the socket connection is getting closed while reading from the server. Try passing auto_reconnect argument while calling rtm_connect function.

slack_client.rtm_connect(auto_reconnect = True)

我使用slackclient创建了一个开源项目,其中对Minikube中一个正常工作的Slack机器人进行了所有调整: https//github.com/staticdev/k8s-python-slackbot

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM