k8s apply yaml files does not get same result from command lines

5/25/2018

I have a spring boot application. I want to load to my k8s (master + kubectl, 172.16.100.83) Our image registry is at 192.168.1.229:5000.

1) With command window
a. docker pull 192.168.1.229:5000/gearboxrack/gearbox-rack-eureka-server
b. kubectl run gearbox-rack-eureka-server --image=192.168.1.229:5000/gearboxrack/gearbox-rack-eureka-server --port=8761 
c. kubectl expose deployment gearbox-rack-eureka-server --type NodePort
[root@master3 supwisdom]# kubectl get svc
NAME                         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
gearbox-rack-eureka-server   NodePort    10.103.109.185   <none>        8761:30719/TCP   6s
kubernetes                   ClusterIP   10.96.0.1        <none>        443/TCP          1h

Then I successfully launch the application by 172.16.100.83:30719. Notice now port is generated randomly by k8s.

Now I use yaml file to try to get some result/goal.

eureka_pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: gearbox-rack-eureka-server
  labels:
    purpose: platform_eureka_demo
spec:
  containers:
  - name:  gearbox-rack-eureka-server
    image: 192.168.1.229:5000/gearboxrack/gearbox-rack-eureka-server
    ports:
        - containerPort: 8761

my eureka_svc.yaml files is

apiVersion: v1
kind: Service
metadata:
  name: gearbox-rack-eureka-server
  labels:
    name: gearbox_rack_eureka_server
spec:
  type: NodePort
  ports:
    - port: 8761
      nodePort: 31501
      name: tcp


[root@master3 supwisdom]# kubectl apply -f eureka_pod.yaml
pod "gearbox-rack-eureka-server" created

[root@master3 supwisdom]# kubectl apply -f  eureka_svc.yaml
service "gearbox-rack-eureka-server" created
[root@master3 supwisdom]# kubectl get services
NAME                         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
gearbox-rack-eureka-server   NodePort    10.99.100.123   <none>        8761:31501/TCP   11s
kubernetes                   ClusterIP   10.96.0.1       <none>        443/TCP          2h

But now I try 172.16.100.83:31501, I could not get same result as command lines did. Are there something wrong with my steps?

-- user84592
kubernetes

1 Answer

5/25/2018

usually services need a selector to find the pod. Also you might need to specify targetPort in the service spec.

I think this should work in your case.

Add this to the pod labels

labels:
  app: my-app

Add this to the service

spec:
  selector:
    app: my-app
-- Nayden Dochev
Source: StackOverflow