server.listen(8080, () => {
logger.log({
level: 'info',
message: 'Listening on port ' + port
})
})
FROM node:10-alpine
...
# Expose port
EXPOSE 8080
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment
spec:
selector:
matchLabels:
app: deployment
replicas: 2
template:
metadata:
labels:
app: deployment
spec:
containers:
- name: job-id-20
image: redacted/redacted
command: ["node", "backend/services.js"]
ports:
- name: http-port
containerPort: 8080
imagePullSecrets:
- name: docker-hub-credentials
dnsConfig:
options:
- name: ndots
value: "0"
apiVersion: v1
kind: Service
metadata:
name: service
spec:
ports:
- protocol: TCP
targetPort: 8080
port: 8080
selector:
app: deployment
type: LoadBalancer
$ kubectl --kubeconfig="k8s-1-13-4-do-0-nyc1-1552761372568-kubeconfig.yaml" get service/service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service LoadBalancer 10.245.239.60 1x4.2x9.1x8.x2 8080:30626/TCP 113s
$ curl --verbose http://1x4.2x9.1x8.x2:8080/
* Trying 1x4.2x9.1x8.x2...
* TCP_NODELAY set
* Connected to 1x4.2x9.1x8.x2 (1x4.2x9.1x8.x2) port 8080 (#0)
> GET / HTTP/1.1
> Host: 1x4.2x9.1x8.x2:8080
> User-Agent: curl/7.54.0
> Accept: */*
>
* Empty reply from server
* Connection #0 to host 1x4.2x9.1x8.x2 left intact
curl: (52) Empty reply from server
我希望流量通过服务路由到部署中的一个pod /副本。我究竟做错了什么?
创建映像时检查任何错误,检查部署后是否已成功部署pod,即它们应处于运行状态并检查服务。如果这些条件是正确的,我几乎看不到你的步骤中的任何错误。这是nodejs应用程序部署的一个简单示例。
index.js
var http = require('http');
var server = http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World!");
});
var port = 80;
server.listen(port);
console.log("Server running at http://localhost:%d", port);
Docker文件:
FROM node:0.10.40
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY ./nodejs-docs-hello-world ./
RUN npm install
CMD ["node", "index.js"]
网络部署:
apiVersion: v1
kind: ReplicationController
metadata:
labels:
name: web
name: web-controller
spec:
replicas: 2
template:
metadata:
labels:
name: web
spec:
containers:
- image: imagename
name: web
ports:
- containerPort: 80
name: http-server
网络服务:
apiVersion: v1
kind: Service
metadata:
name: web
labels:
name: web
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 80
protocol: TCP
selector:
name: web