I have created a local Minikube cluster, then a deployment for a hello-world example. After it is active, a service is created at the same time.
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-world
spec:
selector:
matchLabels:
run: hello-world-example
replicas: 2
template:
metadata:
labels:
run: hello-world-example
spec:
containers:
- name: hello-world
image: gcr.io/google-samples/hello-app:2.0 # Works
ports:
- containerPort: 8080
protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
name: hello-app-service
spec:
# type: NodePort
type: LoadBalancer
selector:
app.kubernetes.io/name: hello-world
ports:
- protocol: TCP
port: 8080
targetPort: 8080
# nodePort: 31579
I apply the deployment and service with kubectl apply-f hello-app.yaml
I end up with the following example services:
NAMESPACE | NAME | TARGET PORT | URL |
---|---|---|---|
default | hello-app-service | 8080 | http://IP:31813 |
default | kubernetes | No node port | |
kube-system | kube-dns | No node port |
Note: My actual IP is not actually "IP"
When I curl the URL for the hello-app-service, I end up with this:
curl: (7) Failed to connect to IP port 31813 after 0 ms: Connection refused
However, when I expose the deployment service manually in CLI with
kubectl expose deployment hello-world --type=LoadBalancer --port=8080
I get the following result:
NAMESPACE | NAME | TARGET PORT | URL |
---|---|---|---|
default | hello-app-service | 8080 | http://IP:31813 |
default | hello-world | 8080 | http://IP:32168 |
default | kubernetes | No node port | |
kube-system | kube-dns | No node port |
And after I curl the URL for new service "hello-world", I end with the proper result:
Hello, world! Version: 2.0.0 Hostname: hello-world-5bb7fff796-fmwl8
Can somebody please explain to me what I am doing wrong with the service? Why is the CLI service working, while the .yaml file service is not working, despite using the same configuration. I have tested both with the same exact service settings as the CLI command (Using LoadBalancer type) and also with NodePort and setting the specific port.
Versions: OS: Ubuntu 22.04.2 LTS Docker version: 24.0.2 Kubectl version: Client Version: v1.27.2 Kustomize Version: v5.0.1 Server Version: v1.26.3 Minikube version: v1.30.1