To make the mirror

mkdir nodeappk8s # Project name
cd nodeappk8s
npm init # Return all the way
vim app.js # Create file, contents below
npm i -S express # Install dependencies
# Add {"start": "node app.js"} to package.json
npm start # There is no problem testing your program
vim Dockerfile Create Dockerfile with contents belowDocker build-t pengj/ nodeAppk8s :v1.0.0.# Build image, please change to your account nameDocker run -p 8888:8888 pengj/ nodeAppk8s :v1.0.0# No problem testing your image
Here you need to create an image in your Docker repository and then perform the following operations; My address is: https://hub.docker.com/u/pengj
If you log in first and then create, you will be notified that the repository does not exist
docker login # Login, enter the account and passwordDocker push pengj/nodeappk8s: v1.0.0# Push image
Copy the code

App. Js content

var express = require('express');

var PORT = 8888;

var app = express();
app.get('/'.function (req, res) {
  res.send('Hello world\n');
});

app.listen(PORT);
console.log('Running on http://localhost:' + PORT);

Copy the code

Dockerfile content

# Node mirror version
FROM node:8-alpine
# Declare author
MAINTAINER Hapiman
# Create folder in image
RUN mkdir -p /home/Service
# Make this folder your working directory
WORKDIR /home/Service

# Copy all files from node project to Image folder
COPY . /home/Service

# RUN NPM install to install the project dependencies
RUN npm install

# Port number exposed to the host
EXPOSE 8888
Run the NPM start command to start the Node project
CMD [ "npm"."start" ]
Copy the code

The deployment of the mirror

The premise

If you have not set up a K8S cluster, refer to setting up a K8S cluster

The deployment of

# Add namespace named 'nob-kube-apps', contents below, will be used in nodeapp-k8s.yaml
kubectl apply -f node-kube-apps.yaml
# Create nodeApp-k8s.yaml on the master machine, the content is below, you need to change 'pengj/ nodeAppK8s :v1.0.0' to your own image repository or use it directly (save effort)
vim nodeapp-k8s.yaml
Kubectl delete -f nodeapp-k8s.yaml
kubectl apply -f nodeapp-k8s.yaml
# Output if the execution succeeds
# deployment "nodeapp-deploy" created
# service "nodeapp-svc" created
# ingress "nodeapp-ingress" created
Copy the code

check

# May appear 0/2, 1/2, wait for a moment OK
kubectl get deployments -n kube-apps |grep nodeapp
# nodeapp-deploy 2/2 2 2 109m
kubectl get svc -n kube-apps |grep nodeapp
# nodeapp-svc NodePort 10.97.144.116 < None > 8080:31000/TCP 109m
kubectl get ingress -n kube-apps |grep nodeapp
# nodeapp-ingress nodeappk8s.local 80 110m
kubectl get pods -n kube-apps |grep nodeapp
# nodeapp-deploy-5c84c9d676-m7zwc 1/1 Running 0 110m
# nodeapp-deploy-5c84c9d676-v8r6g 1/1 Running 0 110m
Copy the code

There are two access modes:

1. Using the cluster between visible IP access, only can be deployed in a cluster of machines available: curl http://10.97.144.116:8080/ping

2. Use the common IP address, but use port 32000 in the configuration file

The detailed code is here

The nodeapp-k8s.yaml file defines three types of resources: Deployment, Service and Ingress;

Deployment has set replicas: The strategy’s rolling strategy is RollingUpdate. The Resources area defines a resource limit for a POD. Health checks are set with livenessProbe and readinessProbe

Kube – apps. Yaml content

apiVersion: v1
kind: Namespace
metadata:
   name: node-kube-apps
   labels:
     name: node-kube-apps
Copy the code

Nodeapp – k8s. Yaml content

---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nodeapp-deploy
  namespace: node-kube-apps
  labels:
    k8s-app: nodeappk8s
spec:
  replicas: 2
  revisionHistoryLimit: 10
  minReadySeconds: 5
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
  template:
    metadata:
      labels:
        k8s-app: nodeappk8s
    spec:
      containers:
      - image: Pengj/nodeappk8s: v1.0.0
        imagePullPolicy: Always
        name: nodeappk8s
        ports:
        - containerPort: 8888
          protocol: TCP
        resources:
          limits:
            cpu: 100m
            memory: 100Mi
          requests:
            cpu: 50m
            memory: 50Mi
        livenessProbe:
          tcpSocket:
            port: 8888
          initialDelaySeconds: 10
          timeoutSeconds: 3
        readinessProbe:
          httpGet:
            path: /
            port: 8888
          initialDelaySeconds: 10
          timeoutSeconds: 2

---
apiVersion: v1
kind: Service
metadata:
  name: nodeapp-svc
  namespace: node-kube-apps
  labels:
    k8s-app: nodeappk8s
spec:
  type: NodePort
  ports:
    - name: api
      port: 8888
      targetPort: 8888
      nodePort: 32000
  selector:
    k8s-app: nodeappk8s

---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: nodeapp-ingress
  namespace: node-kube-apps
spec:
  rules:
  - host: nodeappk8s.local
    http:
      paths:
      - path: /
        backend:
          serviceName: nodeapp-svc
          servicePort: api
Copy the code

Reference documentation

Hand to hand teach you to write Kubernetes Golang service

Welcome to pay attention to the public number: programmer’s financial circle

A small circle of technology, finance, money making, to provide you with the most delicious content, updated daily!