I have a scenario like:
- Have a single deployment containing two containers and have different ports like:
template: {
spec: {
containers: [
{
name: container1,
image: image1,
command: [...],
args: [...],
imagePullPolicy: IfNotPresent,
ports: [
{
name: port1,
containerPort: 80,
},
],
.............
},
{
name: container2,
image: image1,
command: [...],
args: [...],
imagePullPolicy: IfNotPresent,
ports: [
{
name: port2,
containerPort: 81,
},
],
------------
}
]
}
}
- A service having multiple ports pointing to those containers like:
spec: {
type: ClusterIP,
ports: [
{
port: 7000,
targetPort: 80,
protocol: 'TCP',
name: port1,
},
{
port: 7001,
targetPort: 81,
protocol: 'TCP',
name: port2,
}
]
}
The problem I am facing is I can connect to the container having port 80 using service name and port 7000 but I can't connect to the container having port 81 using service name and port 7001. Did I miss anything here? Also, note that both containers have identical images having different command and args for the internal logic.
CodePudding user response:
You can use two services or one service with two exposed ports you can try 2 services : with the deployment like this :
spec:
containers:
- name: container1
image:image1
ports:
- containerPort: 8080
- name: container2
image: image1
ports:
- containerPort: 8081
and the services :
kind: Service
apiVersion: v1
metadata:
name: container1
annotations:
version: v1.0
spec:
selector:
component: <deployment>
ports:
- name: container1
port: 8080
targetPort: 8080
type: ClusterIP
---
kind: Service
apiVersion: v1
metadata:
name: container2
annotations:
version: v1.0
spec:
selector:
component: <deployment>
ports:
- name: container2
port: 8080
targetPort: 8081
type: ClusterIP
CodePudding user response:
I am trying to re-produce this using kind and I can't. Here is my cluster config file:
apiVersion: kind.x-k8s.io/v1alpha4
kind: Cluster
nodes:
- role: control-plane
extraPortMappings:
- containerPort: 30000
hostPort: 30000
listenAddress: 127.0.0.1
- containerPort: 30001
hostPort: 30001
listenAddress: 127.0.0.1
then issue to create the cluster: kind create cluster --config cluster.yaml
Then I have a sample deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-and-wiremock
spec:
selector:
matchLabels:
app: nginx-and-wiremock
replicas: 1
template:
metadata:
labels:
app: nginx-and-wiremock
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
- name: wiremock
image: wiremock/wiremock:2.32.0
ports:
- containerPort: 8080
I use nginx
and wiremock
images, that expose different ports: 80
and 8080
. Deploy this one via : kubectl apply -f nginx-wiremock-deployment.yaml
.
I also deploy a service.yaml
:
apiVersion: v1
kind: Service
metadata:
labels:
app: nginx-and-wiremock
name: nginx-and-wiremock
spec:
ports:
- name: nginx
nodePort: 30000
targetPort: 80
port: 80
- name: wiremock
nodePort: 30001
targetPort: 8080
port: 8080
selector:
app: nginx-and-wiremock
type: NodePort
Once this is up and running:
curl localhost:30000
curl localhost:30001/__admin/mappings
Both of the respond just fine.