kubernetes项目部署
目录
一、容器交付流程
二、k8s平台部署项目流程
三、在K8s平台部署项目
一、容器交付流程
容器交付流程通常分为四个阶:开发阶段、持续集成阶段、应用部署阶段和运维阶段。
开发阶段:开发应用程序,编写Dockerfile;
持续集成阶段:对应用程序编译打包、使用Dockerfile构建镜像,把镜像推送到镜像仓库;
应用部署阶段:基于镜像创建pod,使用deploy控制器暴露服务得到service,使用ingress提供域名访问服务;
运维阶段:对应用程序进行监控和版本升级等;
二、k8s平台部署项目流程
1.制作镜像(通过Dockerfile)
2.推送到镜像仓库中(阿里云镜像、网易镜像)
3.控制器部署镜像(Deployment)
4.对外暴露应用(Service、Ingress)
5.运维(监控、升级)
三、在K8s平台部署项目
部署环境准备
# 可以使用 yum 在线 或离线的下载 安装
yum install java-1.8.0-openjdk maven git -y
maven 项目 编译
# 代码编译构建
cd /opt/tomcat-java-demo-master
mvn clean package -Dmaven.test.skip=true
编译完成的 WAR 包
第一步:制作镜像
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
推送到镜像仓库
项目Demo地址:
# 项目地址
https://github.com/lizhenliang/tomcat-java-demo
第二步:使用控制器部署镜像
配置可信任每台节点Node(如果仓库是HTTPS访问不用配置)
[root@k8s-master java-demo]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"],
"insecure-registries": ["harbor.winneryun.com"]
}
将镜像仓库认证凭据保存在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"
第三步:对外暴露应用
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
查看部署的服务
访问地址:http://example.ctnrs.com/
第四步:增加公网负载均衡器
安装 nginx , 配置 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
成功访问: