Kubernetes project deployment

Table of contents

 1. Container delivery process

2. k8s platform deployment project process

3. Deploy the project on the K8s platform


1. Container delivery process


The container delivery process is usually divided into four stages: development stage, continuous integration stage, application deployment stage, and operation and maintenance stage.

Development phase: developing application, writing Dockerfile;

Continuous integration phase: Compile and package the application, use Dockerfile to build the image, and push the image to the mirror warehouse;

Application deployment stage: create pod based on image, use deploy controller to expose service to get service, use ingress to provide domain name access service;

Operation and maintenance stage: monitoring and version upgrading of applications, etc.;


2. k8s platform deployment project process


1. Make a mirror image (via Dockerfile)

2. Push to the mirror warehouse (Aliyun Mirror, Netease Mirror)

3. Controller deployment image (Deployment)

4. Externally exposed applications (Service, Ingress)

5. Operation and maintenance (monitoring, upgrading)


3. Deploy the project on the K8s platform


Deployment environment preparation

#  可以使用 yum 在线 或离线的下载 安装
yum install java-1.8.0-openjdk maven git -y 

Maven project compilation

# 代码编译构建
cd  /opt/tomcat-java-demo-master
mvn clean package -Dmaven.test.skip=true 

Compiled WAR package

The first step: make a mirror image

cd  /opt/tomcat-java-demo-master
unzip   target/ly-simple-tomcat-0.0.1-SNAPSHOT.war -d  target/ROOT

## Dockerfile
FROM lizhenliang/tomcat 
LABEL maintainer www.ctnrs.com
RUN rm -rf /usr/local/tomcat/webapps/*
ADD target/ROOT /usr/local/tomcat/webapps/ROOT 

## 镜像构建
docker build  -f Dockerfile -t java-demo .


## 在项目中标记镜像
docker tag java-demo  harbor.winneryun.com/demo/java-demo:v1
## 推送镜像到当前项目 docker配置可信任 且登录成功
docker push  harbor.winneryun.com/demo/java-demo:v1

Push to the mirror warehouse

Project demo address:

# 项目地址
https://github.com/lizhenliang/tomcat-java-demo

Step 2: Deploy the image using the controller

Configure trustworthy each node Node (if the warehouse is accessed by HTTPS, no configuration is required)

[root@k8s-master java-demo]# cat /etc/docker/daemon.json 
{
  "registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"],
  "insecure-registries": ["harbor.winneryun.com"]
}

Save the mirror warehouse authentication credentials in K8s Secret

kubectl create secret docker-registry registry-auth --docker-username=admin --docker-password=Harbor12345 --docker-server=harbor.winneryun.com

configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: java-demo-config
data:
    application.yml: |
        server:
          port: 8080
        spring:
          datasource:
            url: jdbc:mysql://localhost:3306/test?characterEncoding=utf-8
            username: root
            password: 123456789
            driver-class-name: com.mysql.jdbc.Driver
          freemarker:
            allow-request-override: false
            cache: true
            check-template-location: true
            charset: UTF-8
            content-type: text/html; charset=utf-8
            expose-request-attributes: false
            expose-session-attributes: false
            expose-spring-macro-helpers: false
            suffix: .ftl
            template-loader-path:
              - classpath:/templates/

Deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: java-demo
spec:
  replicas: 2
  selector:
    matchLabels:
      project: www
      app: java-demo
  template:
    metadata:
      labels:
        project: www
        app: java-demo
    spec:
      imagePullSecrets:
      - name: registry-auth
      containers:
      - image: harbor.winneryun.com/demo/java-demo:v1
        name: java-demo
        volumeMounts:  ## configMap 挂载
        - name: config
          mountPath: "/usr/local/tomcat/webapps/ROOT/WEB-INF/classes/application.yml"
          subPath: application.yml
        resources:
          requests:
            cpu: 0.5
            memory: 500Mi
          limits: 
            cpu: 1
            memory: 1Gi
        livenessProbe:
          httpGet:
            path: /
            port: 8080
          initialDelaySeconds: 50
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /
            port: 8080
          initialDelaySeconds: 50
          periodSeconds: 10
      volumes:
      - name: config
        configMap:
          name: java-demo-config
          items:
          - key: "application.yml"
            path: "application.yml"

Step 3: Expose the application to the outside world

service.yaml

apiVersion: v1
kind: Service
metadata:
  name: java-demo 
spec:
  selector:
    project: www
    app: java-demo
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080 

ingress.yaml

apiVersion: networking.k8s.io/v1 
kind: Ingress
metadata: 
  name: java-demo
spec:
  rules:
  - host: example.ctnrs.com 
    http:
      paths:
      - path: / 
        pathType: Prefix 
        backend: 
          service:
            name: java-demo
            port: 
              number: 80

View deployed services

Access address: http://example.ctnrs.com/

Step 4: Add a public network load balancer

Install nginx, configure nginx.conf

yum install epel-release -y
yum install nginx  -y
systemctl start nginx

vim /etc/nginx/nginx.conf
#在server 的上面插入以下代码
    upstream webservers {
        server 192.168.2.118:80;
        server 192.168.2.210:80;
   }
    server{
        listen 80;
        server_name example.ctnrs.com; 
        location / {
          proxy_pass http://java-demo; proxy_set_header Host $Host;
        }
   }
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;  
.....

nginx -s reload

Successful access:

Guess you like

Origin blog.csdn.net/qq_35995514/article/details/130475256