0

I have installed a docker-desktop in my mac for kubernetes study. But I meet a problem for service can not access the selected pod. Not quite sure the root cause. Here is my DockerFile to create a hello-world webapp with golang. With check, after pod is deployed, inner docker, calling localhost:8081 can be reached.

FROM golang:1.16.15

RUN mkdir -p /var/lib/www && mkdir -p /var/lib/temp

WORKDIR /var/lib/temp
COPY . ./
RUN go env -w GOPROXY="https://goproxy.cn,direct"
RUN go mod tidy
RUN go build -o simplego
RUN mv ./simplego /var/lib/www/ && rm -rf /var/lib/temp

WORKDIR /var/lib/www

COPY ./build.sh ./

EXPOSE 8081

RUN chmod 777 ./simplego
RUN chmod 777 ./build.sh
ENTRYPOINT ["/bin/bash","./build.sh"]

The build.sh is just a ./simplego to trigger the webapp. Here is the deployment yaml file

apiVersion: apps/v1
kind: Deployment
metadata:
  name: simplego-k8s-deployment
  labels:
    app: simplego-k8s-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: simplego-k8s-p
  template:
    metadata:
      labels:
        app: simplego-k8s-p
    spec:
      containers:
      - name: simplego-k8s-pod
        image: simplego:1.0
        imagePullPolicy: Never
        ports:
          - containerPort: 8081
            name: simplego-port

And this is the service yaml

apiVersion: v1
kind: Service
metadata:
  name: simplego-service
  labels:
    app: simplego-service
spec:
  selector:
    app: simplego-k8s-p
  type: NodePort
  ports:
    - protocol: TCP
      port: 8081
      nodePort: 31082
      name: simplego-port

When check pod label

NAME                                       READY   STATUS    RESTARTS        AGE     IP          NODE             NOMINATED NODE   READINESS GATES   LABELS
simplego-k8s-deployment-6b4559b69f-pxsmh   1/1     Running   1 (3m46s ago)   5h53m   10.1.0.86   docker-desktop   <none>           <none>            app=simplego-k8s-p,pod-template-hash=6b4559b69f

And it is align with service

NAME               TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE     SELECTOR
kubernetes         ClusterIP   10.96.0.1      <none>        443/TCP          43d     <none>
simplego-service   NodePort    10.109.9.238   <none>        8081:31082/TCP   5h54m   app=simplego-k8s-p

But when I tried to use simplego-service:31082 or localhost:31082 in my mac to browse the service, all tries are failed.

Not quite sure the reason, may need help.

poisson0106
  • 77
  • 1
  • 11
  • Does this answer your question? [Expose port in minikube](https://stackoverflow.com/questions/40767164/expose-port-in-minikube) – Adiii Aug 21 '22 at 12:38
  • @Adiii, seems not... When I use the minikube command, it shows minikube is not started. But in docker-desktop, the kubernetes is started. I also try the kubectl port-forward kubectl port-forward svc/simplego-service 31082:31082 error: Service simplego-service does not have a service port 31082 NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 443/TCP 43d simplego-service NodePort 10.109.9.238 8081:31082/TCP 19h – poisson0106 Aug 22 '22 at 01:46
  • Minikibe start, run this – Adiii Aug 22 '22 at 03:05
  • @Adiii, since there is one kubernetes has been started. Will start another kubernetes be safe? – poisson0106 Aug 22 '22 at 03:13
  • > it shows minikube is not started, the error showing its not started? – Adiii Aug 22 '22 at 04:01
  • @Adiii, Here are two commands run in a short time. 1. minikube status There is no local cluster named "minikube" To fix this, run: "minikube start" 2. kubectl get pod NAME READY STATUS RESTARTS AGE simplego-k8s-deployment-6b4559b69f-pxsmh 1/1 Running 2 (3h23m ago) 22h So there is one kubernetes is running. But it seems not a instance of minikube – poisson0106 Aug 22 '22 at 04:51
  • now run `minikube service simplego-service` – Adiii Aug 22 '22 at 04:53
  • @Adiii, it still show minikube not starting. I guess the kubernetes in docker-desktop is not realized by minikube. minikube service simplego-service This control plane is not running! (state=Stopped) ❗ This is unusual - you may want to investigate using "minikube logs" To fix this, run: "minikube start" – poisson0106 Aug 23 '22 at 11:13

0 Answers0