Java web 之kubernetes部署

1,872 阅读2分钟

一、摘要

本篇将之前的helloworld war 部署到kubernetes集群中,并可以被外部访问

二、架构图

核心概念解释查看阿里云k8s运维 lixiang的分享

三、部署步骤

  1. 构建本地image,push到docker 仓库
下面时helloworld 的image构建dockerfile

From tomcat:latest
WORKDIR /usr/local/tomcat/webapps
ADD webdemo-1.0-SNAPSHOT webdemo-1.0-SNAPSHOT
EXPOSE 80

k8s集群创建时需要拉取远程docker image 文件,故需要先推送本地镜像push到hub

docker用户名/image名:tag

docker build -t="venusforest2013/war_demo:v1" .
docker push venusforest2013/war_demo
  • 编写k8s.yaml文件,创建k8s集群
创建命令如下
kubectl create -f k8s.yaml
创建k8s集群的yaml文件
kind: Deployment
apiVersion: apps/v1
metadata:
labels:
app: forestwar
name: forestwar
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: forestwar
template:
metadata:
labels:
app: forestwar
# Comment the following annotation if Dashboard must not be deployed on master
annotations:
scheduler.alpha.kubernetes.io/tolerations: |
[
{
"key": "dedicated",
"operator": "Equal",
"value": "master",
"effect": "NoSchedule"
}
]
spec:
containers:
- name: forestwar
image: venusforest2013/war_demo:v1
imagePullPolicy: Always
ports:
- containerPort: 80
protocol: TCP
---
kind: Service
apiVersion: v1
metadata:
labels:
app: forestwar
name: forestwar
namespace: default
spec:
type: NodePort
ports:
- port: 80
targetPort: 8080
selector:
app: forestwar
  • 查看k8s集群
kubectl get pods --all-namespaces
Kubectl get deployments
Kubectl get services

  • 验证

四、其他常用kubectl操作


  • 创建k8s dashboard

Kubectl create -f https://raw.githubusercontent.com/kubernetes/dashboard/v1.5.1/src/deploy/kubernetes-dashboard.yaml
  • 版本查看

kubectl version
  • 查看pod详细信息

kubectl describe pod kubernetes-dashboard -n kube-system
  • 查看k8s cluster info

kubectl cluster-info

  • 查看node信息
kubectl get nodes

  • 查看特定namespace下的pod信息
get pods --namespace=kube-system

  • Kubectl dashboard使用
命令行创建
Kubectl proxy
or
kubectl port-forward kubernetes-dashboard-9746c5f4c-j2mbl -n kube-system 8001
浏览器打开

  • 命令行创建niginx deployment
kubectl run hello-nginx --image=nginx --port=80

  • 命令行暴露服务
kubectl expose deployment hello-nginx --type=NodePort

  • Kubectl get deployments
  • Kubectl get services

  • kubectl scale --replicas=3 deployment/hello-nginx


  • 删除service
Kubectl delete service xxx
  • 删除 deployment
Kubectl delete deployment


五、参考

  1. rominirani.com/tutorial-ge…
  2. www.kubernetes.org.cn/k8s 中文社区
  3. www.bilibili.com/video/av854… 阿里云公开课