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.