部署 java 应用到 k8s 集群
部署java应用到Kubernetes集群简单流程样例
1.Java程序源码
https://github.com/linuxhub/SpringBootDemo
2.Java应用镜像
打包好的镜像: https://hub.docker.com/r/linuxhub/linuxhub_www
打包步骤如下:# 拉取代码
git clone https://github.com/linuxhub/SpringBootDemo.git
cd SpringBootDemo/
git checkout v0.1
# 编译代码
mvn clean package
# 打包镜像
docker build --rm --tag "linuxhub_www:v1.0" .
# 运行镜像
# docker run -p 8088:8088/tcp linuxhub_www:v1.0
# 上传镜像
docker tag linuxhub_www:v1.0 linuxhub/linuxhub_www:v1.0
docker push linuxhub/linuxhub_www:v1.0
3.部署 Java 应用 到 Kubernetes集群
3.1. 创建 Deployment
文件: linuxhub-web-deploy.yamlapiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: linuxhub-web-deploy
spec:
replicas: 2
template:
metadata:
labels:
app: linuxhub-web
spec:
containers:
- name: linuxhub-web
image: linuxhub/linuxhub_www:v1.0
ports:
- containerPort: 8088
部署 kubectl apply -f linuxhub-web-deploy.yaml
查看 Deployment
状态# 查看 Deployment
$ kubectl get Deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
linuxhub-web-deploy 2 2 2 2 1m
# 查看 pod
$ kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE
linuxhub-web-deploy-85bcd95868-7bf7g 1/1 Running 0 4m 172.30.30.6 k8s-node-12
linuxhub-web-deploy-85bcd95868-qzx7n 1/1 Running 0 4m 172.30.34.12 k8s-node-13
# 查看 labels
$ kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
linuxhub-web-deploy-85bcd95868-7bf7g 1/1 Running 0 3m app=linuxhub-web,pod-template-hash=4167851424
linuxhub-web-deploy-85bcd95868-qzx7n 1/1 Running 0 3m app=linuxhub-web,pod-template-hash=4167851424
3.2. 创建 service
文件: linuxhub-web-svc.yamlapiVersion: v1
kind: Service
metadata:
name: linuxhub-web-svc
labels:
app: linuxhub-web
spec:
type: ClusterIP
selector:
app: linuxhub-web
ports:
- name: http
port: 8088
targetPort: 8088
部署 kubectl apply -f linuxhub-web-svc.yam
查看 service
状态$ kubectl get service -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
linuxhub-web-svc ClusterIP 10.254.198.130 <none> 8088/TCP 18s app=linuxhub-web
$ kubectl describe service linuxhub-web-svc
Name: linuxhub-web-svc
Namespace: default
Labels: app=linuxhub-web
Annotations: kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app":"linuxhub-web"},"name":"linuxhub-web-svc","namespace":"default"},"spec...
Selector: app=linuxhub-web
Type: ClusterIP
IP: 10.254.119.59
Port: http 8088/TCP
TargetPort: 8088/TCP
Endpoints: 172.30.30.6:8088,172.30.34.12:8088
Session Affinity: None
Events: <none>
3.3.创建 Ingress
文件: linuxhub-web-ingress.yamlapiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: linuxhub-web-ingress
annotations:
kubernetes.io/ingress.class: traefik
spec:
rules:
- host: demo.linuxhub.cn
http:
paths:
- path: /
backend:
serviceName: linuxhub-web-svc
servicePort: 8088
部署 kubectl apply -f linuxhub-web-ingress.yaml
查看 ingress
状态$ kubectl get ingress
NAME HOSTS ADDRESS PORTS AGE
linuxhub-web-ingress demo.linuxhub.cn 80 2s
4.访问
正常业务前面还需配置负载均衡与DNS解析,另外上面流程也在jenkins中配置自动构建。
配置 host#192.168.8.12 demo.linuxhub.cn
192.168.8.13 demo.linuxhub.cn
访问URL
http://demo.linuxhub.cn/hello
http://demo.linuxhub.cn/index
本文作者 : 泽泽
原文链接 : http://www.linuxhub.cn/2019/04/18/deploy-k8s-java.html
版权声明 : 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
知识 & 情怀 | 二者兼得