自动化部署Java应用到Kubernetes的策略可以分为以下几个步骤:
FROM openjdk:11-jre-slim
COPY target/myapp.jar /app/myapp.jar
CMD ["java", "-jar", "/app/myapp.jar"]
docker build -t myapp:latest .
templates
目录,并在其中创建一个deployment.yaml
文件:apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:latest
ports:
- containerPort: 8080
values.yaml
文件来配置你的Helm chart。例如:image: myapp:latest
replicas: 3
deploy.yml
:name: Deploy to Kubernetes
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Build and push
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: user/myapp:latest
- name: Deploy to Kubernetes
run: helm upgrade myapp ./helm-chart --namespace my-namespace --values values.yaml
kubectl
命令检查Pod的状态:kubectl get pods -n my-namespace
service.yaml
:apiVersion: v1
kind: Service
metadata:
name: myapp-service
spec:
selector:
app: myapp
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: LoadBalancer
然后应用该文件:kubectl apply -f service.yaml
最后,访问你的应用:curl http://<EXTERNAL-IP>
通过以上步骤,你可以实现Java应用到Kubernetes的自动化部署。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。