...communication can be established through the service (like ServiceName:Port). But are there any other options to establish this connection?
您可以使用 Statefulset 创建“main-container” pod,其中生成的(例如作业)pod 可以通过 pod DNS 解析“main-container”pod 名称并连接回来。这是一个简单的例子:
cat << EOF | kubectl apply -f -
apiVersion: v1
kind: Service
metadata:
labels:
app: nginx
name: nginx
spec:
clusterIP: None
selector:
app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: nginx
spec:
serviceName: nginx
selector:
matchLabels:
app: nginx
replicas: 1
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx # Presume this is the container that expose 8082 in your diagram
image: nginx:alpine
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
EOF
假设您的“主容器”创建工作:
cat << EOF | kubectl apply -f -
apiVersion: batch/v1
kind: Job
metadata:
name: nginx
spec:
template:
metadata:
name: nginx
spec:
restartPolicy: Never
containers:
- name: nginx
image: busybox
imagePullPolicy: IfNotPresent
command: # Simulate the line from Pod#2 to Pod#1 in your diagram
- wget
- -qO-
- nginx-0.nginx
EOF
作业始终可以使用 DNS 名称到达 nginx pod(而不是nginx-0.nginx
服务) 。检查与kubectl logs <job pod>
。