k8s在pod内访问集群外部地址
一. 使用IP
apiVersion: v1
kind: Service
metadata:
name: nginx-ip
spec:
ports:
- protocol: TCP
port: 88
---
apiVersion: v1
kind: Endpoints
metadata:
name: nginx-ip
subsets:
- addresses:
- ip: 43.138.153.80
ports:
- port: 80 # IP相关的端口,可以是外部连接端口,也可以是内部容器连接端口
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: busybox
labels:
app: busybox
spec:
replicas: 1
selector:
matchLabels:
app: busybox
template:
metadata:
labels:
app: busybox
spec:
containers:
- name: busybox
image: appropriate/curl
command: ["/bin/sh", "-c", "sleep 3600"]
在busybox容器中curl nginx-ip:88 即可访问外部地址43.138.153.80
二. 使用域名
apiVersion: v1
kind: Service
metadata:
name: "baiducom" # 设置 IP 14.215.177.39和www.baidu.com 错误,不允许'.'
spec:
type: ExternalName
externalName: "www.baidu.com" # 设置 IP 14.215.177.39 无效
ports:
- port: 443
# 所以 externalName 的意义就是为外部域名设置一个别名,如上 为 www.baidu.com 设置别名 baiducom,以便 pod 内容器使用
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: busybox
labels:
app: busybox
spec:
replicas: 1
selector:
matchLabels:
app: busybox
template:
metadata:
labels:
app: busybox
spec:
containers:
- name: busybox
image: busybox
command: ["/bin/sh", "-c", "sleep 3600"]
busybox中curl baiducom即可访问百度
努力生活,融于自然