我用自己的mac,装了docker,并在docker的dashboard中装了k8s,如下:
docker版本
k8s版本
我自己搞了个简单nodejs后端服务,放进docker部署,浏览器访问效果如下
curl访问
我配置了k8s的deployment和service,如下:
deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-server
namespace: test
labels:
deploy: nodejs-server
spec:
selector:
matchLabels:
pod: nodejs-web-server
replicas: 1
revisionHistoryLimit: 1
progressDeadlineSeconds: 60
strategy:
rollingUpdate:
maxSurge: 50%
maxUnavailable: 1
type: RollingUpdate
template: # 定义pod
metadata:
labels:
pod: nodejs-web-server
spec:
containers:
- name: web-service-server
image: web-service-image
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8081
restartPolicy: Always
service
apiVersion: v1
kind: Service
metadata:
name: node-server
namespace: test
labels:
service: service-test
spec:
selector:
pod: nodejs-web-server
type: ClusterIP
ports:
- name: server-port
protocol: TCP
targetPort: 8081 #pod对外暴露的端口
port: 8881 #service监听的端口
deployment信息
pod信息:
service信息
按理来说现在我使用curl命令访问clusterip是可以访问到的,但是:
浏览器也访问失败,我又配置了一下ingress试了一下
ingress配置:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: test-ingress
namespace: test
spec:
rules:
- host: test.test.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: node-server
port:
number: 8881
我在另外一个windows电脑上改了host文件,能访问到我的mac,但是访问test.test.com还是访问不了!
现在怀疑两个可能的问题,一个可能是我的deployment和service配置没写对,另一个就是网络问题,也有可能是在docker的dashboard中装的k8s有问题(但这个不应该啊)。总之改了很多次配置试了很多次,就是通不了,求大神帮忙看看!谢谢!!!
service的ClusterIP改成NodePort就好了,ClusterIP是集群内部访问的