V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
fangwenxue
V2EX  ›  问与答

kubernetes 容器之间怎么进行网络通信

  •  
  •   fangwenxue · 2020-05-05 17:25:38 +08:00 · 1186 次点击
    这是一个创建于 1450 天前的主题,其中的信息可能已经有所发展或是发生改变。
    try:
        _redis_client = Redis('redis')
        print(_redis_client.info())
    except Exception as e:
        print(e)
    
    • docker yml
    version: "3"
    services:
      web:
        build: .
        ports:
          - "8080:8080"
        restart: always
    
      redis:
        image: redis:5-alpine
        ports:
          - "6379:6379"
        volumes:
          - "/data"
        restart: always
    
    • k8s.yml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: pytest
    spec:
      replicas: 1
      template:
        metadata:
          labels:
            app: pytest
        spec:
          containers:
            - name: web
              image: '..'
              ports:
                - containerPort: 8080
    
    ---
    
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: redis
    spec:
        spec:
          volumes:
            - name: redis-data
          containers:
            - name: redis
              image: redis:5-alpine
              ports:
                - containerPort: 6379
                  protocol: TCP
              volumeMounts:
                - name: redis-data
                  mountPath: /data
      selector:
        matchLabels:
          app: redis
    
    
    ---
    
    apiVersion: v1
    kind: Service
    metadata:
      name: web
    spec:
      type: NodePort
      ports:
      - name: "web"
        port: 8080
      selector:
        app: pytest
    
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: redis
    spec:
      type: NodePort
      ports:
      - name: "redis"
        port: 6379
      selector:
        app: redis
    
    • docker yml 可以连上 redis
    • k8s yml 连不上 redis

    怎么连上 k8s 的 redis

    3 条回复    2020-05-06 15:10:15 +08:00
    qoo2019
        1
    qoo2019  
       2020-05-05 17:49:53 +08:00 via iPhone
    最简单的 nodeport 映射个端口
    zeron889
        2
    zeron889  
       2020-05-06 12:22:27 +08:00
    这个有两种方式,一个是走 cluster ip 的 svc,和走 nodeport 的主机暴露端口
    julyclyde
        3
    julyclyde  
       2020-05-06 15:10:15 +08:00
    运行 docker 的时候别用 NAT 模式
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4193 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 82ms · UTC 05:26 · PVG 13:26 · LAX 22:26 · JFK 01:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.