k8s 定义了 Pod 和 Servive 之后,如何从外部访问服务?

2019-01-13 21:58:04 +08:00
 salamanderMH

问题

用 Rancher 搭建了 k8s 集群,架构 ,定义了
Pod

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.0
        ports:
        - containerPort: 80

Service

kind: Service
apiVersion: v1
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 9898
      targetPort: 80

主机都是在阿里云上的,发现 Service 的 ip 也是虚拟的 ip,如何从外部固定入口访问,看网上资料是要定义 ingress

2519 次点击
所在节点    问与答
8 条回复
hly9469
2019-01-13 22:05:46 +08:00
nodeport
elons
2019-01-13 22:20:51 +08:00
通过节点端口访问啊
a663
2019-01-13 22:30:12 +08:00
Ingress
kidlj
2019-01-13 22:36:48 +08:00
- pod: hostNetwork or hostPort
- service: NodePort or loadbalancer(公有云环境)
- ingress
tinybaby365
2019-01-13 23:06:23 +08:00
通过 NodePort 访问,你可以开发一个 Service controller,利用阿里云的 API,支持 LoadBalancer 类型的 Service
AltairT
2019-01-14 00:03:46 +08:00
service 和负载均衡啊,话说不如直接用阿里云的容器服务,省事很多。日志,监控啥的集成都会很方便。
salamanderMH
2019-01-14 10:59:32 +08:00
@kidlj
ingress 的例子是这样
```
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: influxdb
spec:
rules:
- host: influxdb.kube.example.com
http:
paths:
- backend:
serviceName: influxdb
servicePort: 8086
```
又要定义个 host
julyclyde
2019-01-15 15:17:31 +08:00
通过 ingress 是正规途径
或者 loadbalancer 模式的 service 也可以,但需要一个 loadbalancer plugin

nodeport 模式(其实 loadbalancer 模式也基于 nodeport )感觉设计上有问题,让所有 node 都开放同一个 port,人为把 port 逼成了紧俏资源

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

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

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

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

© 2021 V2EX