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

2020-05-05 17:25:38 +08:00
 fangwenxue
try:
    _redis_client = Redis('redis')
    print(_redis_client.info())
except Exception as e:
    print(e)
version: "3"
services:
  web:
    build: .
    ports:
      - "8080:8080"
    restart: always

  redis:
    image: redis:5-alpine
    ports:
      - "6379:6379"
    volumes:
      - "/data"
    restart: always
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

怎么连上 k8s 的 redis

1187 次点击
所在节点    问与答
3 条回复
qoo2019
2020-05-05 17:49:53 +08:00
最简单的 nodeport 映射个端口
zeron889
2020-05-06 12:22:27 +08:00
这个有两种方式,一个是走 cluster ip 的 svc,和走 nodeport 的主机暴露端口
julyclyde
2020-05-06 15:10:15 +08:00
运行 docker 的时候别用 NAT 模式

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/668706

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX