K8S實(shí)戰(zhàn)之部署java應(yīng)用
kubernetes基本概念
Pod:
1.最小的部署單元,
2.一組容器的集合
3.一個(gè)pod中的容器共享網(wǎng)絡(luò)命名空間
4.生命周期是短暫的
Controllers:
1.Deployment:部署無狀態(tài)應(yīng)用
2.Statefulset:部署有狀態(tài)應(yīng)用
3.Replicaset:確保預(yù)期的Pod副本數(shù)量
4.Daemonset: 確保node運(yùn)行同一個(gè)pod
5.Job:一次性任務(wù)
6.Cronjob:定時(shí)任務(wù)
更高級(jí)層次對(duì)象,部署和管理pod
Service:
防止Pod失聯(lián),找到你需要Pod
定義一組Pod的負(fù)載均衡訪問策略
Label:標(biāo)簽,附加到某個(gè)資源上,用于關(guān)聯(lián)對(duì)象,查詢和篩選
Namespaces:命名空間,將對(duì)象進(jìn)行邏輯上隔離或者資源隔離
項(xiàng)目遷移到k8s平臺(tái)是怎么樣的流程?
基礎(chǔ)鏡像(一個(gè)空白的鏡像)---------------運(yùn)行環(huán)境鏡像(py/php/go/java)---------------項(xiàng)目鏡像(項(xiàng)目打包)
實(shí)戰(zhàn)項(xiàng)目開始
服務(wù)器ip 服務(wù)器名稱 192.168.106.102 k8s-master 192.168.106.103 k8s-node01 192.168.106.104 k8s-node02 192.168.106.103 mysql數(shù)據(jù)庫
1.0將java源碼程序項(xiàng)目包導(dǎo)入到master服務(wù)器上,如圖所示
1將db目錄下數(shù)據(jù)文件導(dǎo)入mysql中(node01)
#192.168.106.103數(shù)據(jù)庫執(zhí)行
source /root/tables_ly_tomcat.sql
grant all on test.* to 'test'@'%' identified by "Zhangfan@123"; #增加一個(gè)授權(quán)用戶,在master登錄能正常登錄
修改連接數(shù)據(jù)庫的配置文件,改成剛才數(shù)據(jù)授權(quán)的用戶和ip
vim src/main/resources/application.yml
2開始制作鏡像java鏡像(master)
我在之前的博文中講了如何構(gòu)建java鏡像 https://blog.csdn.net/weixin_43546282/article/details/112210164
cat Dockerfile
FROM centos:7
MAINTAINER zhangfan
ENV VERSION=8.5.61
RUN yum install java-1.8.0-openjdk wget curl unzip iproute net-tools -y &&
yum clean all &&
rm -rf /var/cache/yum/*
RUN wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v${VERSION}/bin/apache-tomcat-${VERSION}.tar.gz &&
tar zxf apache-tomcat-${VERSION}.tar.gz &&
mv apache-tomcat-${VERSION} /usr/local/tomcat &&
rm -rf apache-tomcat-${VERSION}.tar.gz /usr/local/tomcat/webapps/* &&
mkdir /usr/local/tomcat/webapps/test &&
echo "ok" > /usr/local/tomcat/webapps/test/status.html &&
sed -i '1a JAVA_OPTS="-Djava.security.egd=file:/dev/./urandom"' /usr/local/tomcat/bin/catalina.sh &&
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ENV PATH $PATH:/usr/local/tomcat/bin
WORKDIR /usr/local/tomcat
EXPOSE 8080
CMD ["catalina.sh", "run"]
安裝編譯環(huán)境
yum -y install java-1.8.0-openjdk maven
cd /root/tomcat-java-demo-master
vim src/main/resources/application.yml #修改配置文件指定剛才創(chuàng)建用戶的數(shù)據(jù)庫
mvn clean package -Dmaven.test.skip=true
編譯完成如圖所示,生成一個(gè)war包
制作新Dockerfile
cat Dockerfile
FROM tomcat:v1
LABEL test www.zhangsan.com
RUN rm -rf /usr/local/tomcat/webapps/*
ADD target/*.war /usr/local/tomcat/webapps/ROOT.war
#注意這里命名 ` zhangfan5391621/java-demo` zhangfan5391621表示docker hub倉庫登錄名/java-demo為項(xiàng)目名
docker build -t zhangfan5391621/java-demo .
構(gòu)建完成如圖所示,新項(xiàng)目鏡像構(gòu)建完成
3將鏡像推入docker hub倉庫中
https://hub.docker.com/repository/docker/zhangfan5391621/zf_test
docker push zhangfan5391621/java-demo #上傳到docker hub倉庫中去
4生成k8s-Pod
kubectl create deployment java-demo --image=zhangfan5391621/java-demo --dry-run -o yaml > deploy.yaml
在本地生成一個(gè)yaml標(biāo)準(zhǔn)文件
cat deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: java-demo
name: java-demo
spec:
replicas: 3 #三個(gè)副本
selector:
matchLabels:
app: java-demo
template:
metadata:
labels:
app: java-demo
spec:
containers:
- image: zhangfan5391621/java-demo
name: java-demo
kubectl apply -f deploy.yaml #構(gòu)建pod
kubectl get pods #全部狀態(tài)running說明構(gòu)建成功
檢查pod日志,無錯(cuò)誤說明成功,如果報(bào)錯(cuò)狀態(tài)不對(duì)用下面命令檢查
`kubectl describe nodes k8s-node01 #檢查node`
`kubectl logs java-demo-876cf6688-57jzf`
5 暴露應(yīng)用svc
kubectl expose deployment java-demo --port=80 --target-port=8080 --type=NodePort -o yaml --dry-run > svc.yaml
java-demo :剛才創(chuàng)建deployment時(shí)的名稱, --port=80:容器內(nèi)部訪問端口 --target-port=8080 :指定pod內(nèi)部端口 --type=NodePort: 指定外部訪問的端口為隨機(jī)生成端口
kubectl apply -f svc.yaml #創(chuàng)建svc
kubectl get pods,svc
http://192.168.106.102:30187/ #任意節(jié)點(diǎn)ip:30187都能正常訪問
至此項(xiàng)目部署完成
整個(gè)完成順序是