preface
The one-click deployment in this article is actually a process of deployment commands into shell scripts, of course, because it is published to the K8S cluster, so the issued commands and the required things will be a little different. The one-click deployment script in this article is based on building a suitable rapid development framework – continuous deployment one key release script design and implementation of this article. So I suggest you read the article first.
Knowledge to prepare
About basic image making
The base image can be used directly with OpenJDK: 8u212-JDK-alpine, but it is not functional enough to use directly, so we need to work on it. Add some necessary installation packages. And do some initialization.
- Modifying the Source Address
- Install tzdata- Time zone related package
- Ttf-dejavu and fontconfig, image captcha needs this library
- Curl – Simply install the curl tool
- Tini – Properly registers signal handlers (such as PID). If not used, services in the container default pid to 1
- Ospd-netstat Network diagnostic tool
- Initialize the Springboot run directory
- Change the default time zone
- Arthas is an open source Java diagnostic tool that requires tini in 5
- Others – not considered for the time being, hope big guy reply suggestions.
cat <<EOF > /mldong/docker/8u212-jdk-alpine-mldong/Dockerfile
# specify the base image
FROM openjdk:8u212-jdk-alpine
# Maintainer information
MAINTAINER mldong <[email protected]>
Change the source address and install some necessary libraries
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \
&& apk --update add --no-cache tzdata ttf-dejavu fontconfig curl tini ospd-netstat && rm -rf /var/cache/apk/* \
&& mkdir -p /app && mkdir -p /app/config \
&& cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone
# Install arthas- here is a copy from another image, tip
COPY --from=hengyunabc/arthas:latest /opt/arthas /opt/arthas
EOF
Copy the code
build
docker build -f /mldong/docker/8u212-jdk-alpine-mldong/Dockerfile -t Registry-vpc.cn-zhangjiakou.aliyuncs.com/mldong/java/8u212-jdk-alpine-mldong:1.0.Copy the code
login
docker login -u _username -p password registry-vpc.cn-zhangjiakou.aliyuncs.com
Copy the code
push
Docker push registry-vpc.cn-zhangjiakou.aliyuncs.com/mldong/java/8u212-jdk-alpine-mldong:1.0Copy the code
About service image creation
- Image making is defined using a Dockerfile file
- Use openJDK: 8u212-JDK-alpine as the base image
- Copy the JAR package to the container
- Copy the configuration file to the container
- Define external access ports
- K8s yamL command defines the container to run the command after startup.
About the mirror version number
You are advised to use the current time and Git version
About template
Template variables
The main template variable is the mirrored version, but to make it generic, it is recommended that the template variable be:
parameter | instructions |
---|---|
APP_NAME | The application of |
NAMESPACE | The namespace |
PROFILES | Environment definition (PROD -> Production,test-> Test) |
IMAGE_URL | Mirror address |
IMAGE_TAG | The mirror version |
PORT | Foreign port |
A template engine
This may not be called a template engine, but there are two solutions for simply substituting the above variables.
Plan 1: Use sed
Instructions:
Sed 's# template variable 1# value 1#g; S# template variable 2# value 2#g' template file > generated new fileCopy the code
Ex. :
The content of the document is as follows:
cat << EOF > test.tpl
apiVersion: v1
kind: Namespace
metadata:
name: {{APP_NAME}}-nodeport
namespace: {{NAMESPACE}}-{{PROFILES}}
EOF
Copy the code
Generate template
sed -e 's#{{APP_NAME}}#mldong-admin#g; s#{{NAMESPACE}}#mldong#g; s#{{PROFILES}}#test#g' test.tpl > test.yamlCopy the code
Scheme 2: Use ENVSUBst
Usually we need to get one or more environments to replace some variables in the system, in which case we can simply use SED to replace them. In this scenario, SED will suffice, but if we need to change a lot of variables, envsubst will be useful.
Instructions:
Envsubst < template file > New file generatedCopy the code
Ex. :
The content of the document is as follows:
cat << EOF > test2.tpl
apiVersion: v1
kind: Namespace
metadata:
name: ${APP_NAME}-nodeport
namespace: ${NAMESPACE}-${PROFILES}
EOF
Copy the code
Start by defining environment variables
export APP_NAME=mldong-admin
export NAMESPACE=mldong
export PROFILES=test
Copy the code
Then run the envsubst command to generate a template
envsubst < test2.tpl > test2.yaml
Copy the code
Start coding
The directory structure
├ ─ ─ / java_projects management end interface ├ ─ ─ mldong - admin ├ ─ ─ the config └ ─ ─ application - test. Yml ├ ─ ─ app. The jar jar package ├ ─ ─ k8s. TPL k8s release template ├ ─ ─ ├─ ├─ └ └ └ └ └ └ └ └ └ └ └ └ └ └ Manu.xml ├─ manu.txt ├─ manu.txt ├─ manu.txt ├─ manu.txt ├─ manu.txt ├─ manu.txt ├─ manu.txt ├─ manu.txtCopy the code
File,
/java_projects/mldong-admin/config/application-test.yml
Test environment configuration (for the sake of simplicity, here is not considered to add mysql to the template, using the database on the junior A student)
# JDBC configuration
jdbc.driver-class-name: com.mysql.cj.jdbc.Driver
jdbc.url: JDBC: mysql: / / 172.26.22.105:3306 / mldong? useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=Asia/Shanghai
jdbc.username: u_mldong
jdbc.password: D5fopH@B7Q
jdbc.max-idle: 10000
jdbc.max-wait: 10000
jdbc.min-idle: 5
Copy the code
/java_projects/mldong-admin/app.jar
This jar package is generated by the source code compiled and copied, and the image needs to be packaged in the same directory as the Dockerfile file
/java_projects/mldong-admin/Dockerfile
Image definition file
#Use the base image made aboveThe FROM registry-vpc.cn-zhangjiakou.aliyuncs.com/mldong/java/8u212-jdk-alpine-mldong:1.0#Maintainer information
MAINTAINER mldong <[email protected]>
#Creating an application Directory
# RUN mkdir -p /app && mkdir -p /app/config
#Go to the working directory
WORKDIR /app
#Copy the jar
COPY app.jar .
#Configuration Configuration file
COPY config/* .
#EXPOSE Mapping port
EXPOSE 8080
#ENTRYPOINT handles pid=1
ENTRYPOINT ["/sbin/tini", "--"]
#CMD runs the following command (if defined in the YAML file)commandWill be covered)
CMD ["/bin/sh","-c","set -e && java -jar app.jar --spring.profiles.active=dev --server.port=8080"]
Copy the code
/java_projects/mldong-admin/k8s.tpl
K8s service deployment template
apiVersion: v1
kind: Namespace
metadata:
name: {{NAMESPACE}}
---
apiVersion: v1
kind: Service
metadata:
name: {{APP_NAME}}-nodeport
namespace: {{NAMESPACE}}
spec:
type: NodePort
ports:
- port: 8080
targetPort: 8080
selector:
app: {{APP_NAME}}
---
apiVersion: v1
kind: Service
metadata:
name: {{APP_NAME}}
namespace: {{NAMESPACE}}
spec:
type: ClusterIP
ports:
- port: 8080
protocol: TCP
targetPort: 8080
selector:
app: {{APP_NAME}}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{APP_NAME}}
namespace: {{NAMESPACE}}
spec:
selector:
matchLabels:
app: {{APP_NAME}}
replicas: 1
template:
metadata:
labels:
app: {{APP_NAME}}
spec:
containers:
- name: {{APP_NAME}}
image: {{IMAGE_URL}}:{{IMAGE_TAG}}
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
name: port
protocol: TCP
command: ["/bin/sh"]
args: ["-c", "set -e && java -jar app.jar --spring.profiles.active={{PROFILES}} --server.port=8080"]
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
name: {{APP_NAME}}-ingress
namespace: {{NAMESPACE}}
spec:
rules:
- host: {{HOST}}
http:
paths:
- backend:
serviceName: {{APP_NAME}}
servicePort: 8080
path: /
Copy the code
/java_projects/mldong-admin/buildAndPublish.sh
Process:
- Compile the package
- Build the mirror
- Push to private server
- Publish YAML using template generation
- release
#! /bin/bash
#Source code in the root directory
source_dir=/java_projects/source/back
#Parent project directory
parent_dir=$source_dir/mldong
#Name of the project that needs to be packaged
project_name=mldong-admin
#Project Deployment Directory
project_dir=/java_projects/$project_name
#Git repository address (SSH mode, need to configure deployment public key)
[email protected]:mldong/mldong.git
#Mirror Warehouse Address
registry_url=registry-vpc.cn-zhangjiakou.aliyuncs.com
#Mirror warehouse space
registry_ns=mldong/java
#Username of the mirror warehouse
registry_username=username
#Mirror Warehouse Password
registry_password=password
#Address of the generated image
image_url=$registry_url/$registry_ns/$project_name
#The domain name bound to the service
host=c.mldong.com
#Environment definitionprofiles=test if [ -d "$source_dir" ]; ${source_dir}" cp -p $source_dir fi if [-d "$parent_dir"]; ${source_dir} fi if [-d "$parent_dir"]; Git pull" CD $parent_dir git pull else echo" Git clone" Git clone $git_URL $parent_dir fi git_version=$(git rev-parse HEAD) echo "git rev :${git_version}" image_tag=`date +"%Y%m%d%H%M"_``git describe --tags --always` cd $parent_dir mvn clean package -B#Here you need to determine whether the packaging was successfulif [ $? -ne 0 ]; Then echo "failed to pack" else #Copy the jar package
cp -r -f $parent_dir/$project_name/target/$project_name.jar $project_dir/app.jar
#Enter the project directory
cd $project_dir
#Build the mirror
docker build -t $registry_url/$registry_ns/$project_name:$image_tag .
#The login server
docker login -u $registry_username -p ${registry_password} $registry_url
#Push to private serverdocker push $registry_url/$registry_ns/$project_name:$image_tag sed -e "s#{{APP_NAME}}#$project_name#g; s#{{NAMESPACE}}#$project_name-$profiles#g; s#{{PROFILES}}#$profiles#g; s#{{IMAGE_URL}}#$image_url#g; s#{{IMAGE_TAG}}#$image_tag#g; s#{{HOST}}#$host#g" k8s.tpl > k8s.yaml #Deleting a Local Mirrordocker rmi $registry_url/$registry_ns/$project_name:$image_tag if [ $? -ne 0 ]; Else cat k8s.yaml kubectl apply -f k8s.yaml fi fiCopy the code
The verification results
summary
In this article, the rapid development framework of open source some time ago is used as an example, and the one-click deployment script is modified to K8S. In fact, before the script, is a new package image, push private server, template production, release process. Just add a few more commands. Of course, there might be some minor differences in the actual deployment, such as a bit more content in the template, or the script could be turned into Jenikns pipeline. But the basic principles are pretty much the same, and the flow is pretty much the same.
The source code
Gitee.com/mldong/mldo…
Related articles
Walk you through K8S – cluster creation and Hello World
Take you through K8S-ConfigMap and persistent storage
Take you hand in hand to play K8S – complete release of an externally accessible service
Docker-compose k8S-docker advanced Dockerfile and docker-compose