部署Java项目到k8s集群
docker build -f dockerfile地址 -t 新镜像名字:TAG .
·
一、编写dockfile
FROM openjdk:11
ADD *.jar app
CMD java -jar app --server.port=8088
二、制作镜像(记得最后加 . )
docker build -f dockerfile地址 -t 新镜像名字:TAG .
三、推送到自己的阿里云镜像仓库
docker pull registry.cn-hangzhou.aliyuncs.com/jbhub/helloword:[镜像版本号]
四、制作k8s部署deployment
使用命令行导出一个模板
kubectl create deployment hello-k8s --image=registry.cn-hangzhou.aliyuncs.com/jbhub/helloword:v1.0 --dry-run -o yaml > deployment.yaml
长成这样
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: hello-k8s
name: hello-k8s
spec:
replicas: 2
selector:
matchLabels:
app: hello-k8s
template:
metadata:
labels:
app: hello-k8s
spec:
containers:
- image: registry.cn-hangzhou.aliyuncs.com/jbhub/helloword:v1.0
name: helloword
五、然后做一次部署
kubectl apply -f deployment.yaml
部署后查看一下deployment是否已经成功
kubectl get deployment
看是否有pod
kubectl get pods
六、暴露deployment端口给外部访问,并根据deployment去生成service对应的yaml
kubectl expose deployment hello-k8s --port=80 --target-port=8088 --type=NodePort --dry-run -o yaml > service.yaml
其中hello-k8s是deployment的名字,target-port是java应用的端口,具体长成下面这样
apiVersion: v1
kind: Service
metadata:
labels:
app: hello-k8s
name: hello-k8s
spec:
ports:
- port: 80
protocol: TCP
targetPort: 8088
selector:
app: hello-k8s
type: NodePort
七、通过以下方法可以查看现有pod、deployment、svc的yaml
kubectl get pod hello-k8s-88df5bf95-94hql -o yaml
kubectl get svc hello-k8s -o yaml
更多推荐
已为社区贡献2条内容
所有评论(0)