从零搭建Kubernetes下的nignx和tomcat
“本文主要讲解kubernetes下搭建nginx和tomcat环境具体工作”
1、综述
i 创建nfs持久化存储,持久化存储用于存储前端静态文件和pod产生日志文件。
ii 创建configmap,因为服务横向扩展之后,可能分配到一台不存在配置的计算节点上,建议把配置文件存储到kubernetes的configmap中,可以kubectl edit configmap对配置文件进行修改,修改完成后,每个节点能够及时更新配置。
iii 开发tomcat和nginx Deployment脚本,pod是Kubernetes最小调度单元,一个pod中可以管理多个容器进程,其实现机制是通过共享存储和共享网络的形式,而Deployment是一个控制器,根据指定策略进行发布pod和用来控制pod的数量跟期望pod数量一致(Deployment控制ReplicateSet ReplicateSet控制pod副本的数量,pod所属于replicaset,同一个replicaset下的pod都是一样的)。
iiii 开发tomcat和nginx的Service脚本,service是一个抽象概念,定义了一个服务的多个pod逻辑合集和访问pod的策略,一般把service称为微服务,service通过Label selector跟服务的pod绑定,无论pod如何变化对服务来说都是透明的。
iiii 启动上述pod和Service。
2、搭建NFS服务器
#master节点安装nfs
yum -y install nfs-utils
#创建nfs目录
mkdir -p /nfs/data/
#修改权限
chmod -R 777 /nfs/data
#编辑export文件
vim /etc/exports
/nfs/data *(rw,no_root_squash,sync)
#配置生效
exportfs -r
#查看生效
exportfs
#启动rpcbind、nfs服务
systemctl restart rpcbind && systemctl enable rpcbind
systemctl restart nfs && systemctl enable nfs
#查看 RPC 服务的注册状况
rpcinfo -p localhost
#showmount测试
showmount -e 192.168.92.5
#所有node节点安装客户端
yum -y install nfs-utils
systemctl start nfs && systemctl enable nfs
3、configmap配置存储
创建configmap
kubectl create configmap conf --from-file=conf.ini
查看configmap
kubectl describe configmap conf
编辑configmap
kubectl edit configmap conf
4、开发tomcat Deployment脚本
apiVersion: apps/v1
kind: Deployment
metadata:
name: my1-deployment
spec:
selector:
matchLabels:
app: my1-cluster
replicas: 1
template:
metadata:
labels:
app: my1-cluster
spec:
initContainers:
- image: docker.hub.com/xxx/my2:v2
name: my2-war
imagePullPolicy: IfNotPresent
command: ["cp", "my2.war", "/opt"]
volumeMounts:
- mountPath: /app
name: my2-vol-webapps
- image: docker.hub.com/xxx/my1:v1
name: my1-war
imagePullPolicy: IfNotPresent
command: ["cp", "my1.war", "/opt"]
volumeMounts:
- mountPath: /opt
name: my1-vol-webapps
containers:
- name: my2
image: docker.hub.com/ops/tomcat:8.5
ports:
- containerPort: 8080
# hostPort: 8001
volumeMounts:
- mountPath: /usr/local/tomcat-my2/logs
name: tomcat-my2-vol-log
- mountPath: /home/logs/my2
name: my2-vol-log
- mountPath: /home/hub/conf/my2
name: my2-config
- mountPath: /usr/local/tomcat-my1/logs
name: tomcat-my1-vol-log
- mountPath: /home/logs/my1
name: my1-vol-log
- mountPath: /home/hub/conf/my1
name: my1-config
volumes:
- name: my2-vol-log
nfs:
server: 192.168.92.5
path: /nfs/data/logs/my2
- name: tomcat-my2-vol-log
nfs:
server: 192.168.92.5
path: /nfs/data/logs/tomcat-my2
- name: my2-config
configMap:
name: my2-config
items:
- key: conf.properties
path: conf.properties
- name: my1-vol-webapps
emptyDir: {}
- name: my1-vol-log
nfs:
server: 192.168.92.5
path: /nfs/data/logs/my1
- name: tomcat-my1-vol-log
nfs:
server: 192.168.92.5
path: /nfs/data/logs/tomcat-my1
- name: my1-config
configMap:
name: my1-config
items:
- key: conf.ini
path: conf.ini
---
apiVersion: v1
kind: Service
metadata:
name: tomcat-service
labels:
app: tomcat
spec:
type: ClusterIP
ports:
- port: 8080
selector:
app: my1-cluster
5、开发nginx Deployment脚本
apiVersion: ops/v1
kind: Deployment
metadata:
name: nginx-web
spec:
replicas: 1
selector:
matchLabels:
app: nginx-web
template:
metadata:
labels:
app: nginx-web
spec:
containers:
- name: nginx-web
image: docker.hub.com/ops/nginx:1.15
ports:
- containerPort: 80
volumeMounts:
- mountPath: /usr/local/nginx/logs/
name: nginx-vol-logs
- mountPath: /home/nginx/nginx/conf/cert/ssl/
name: nginx-vol-ssl
- mountPath: /usr/local/nginx/conf/http_vhost/
name: nginx-vol-http
- mountPath: /home/hub/ops/web/my1/
name: my1-vol-web
- mountPath: /home/hub/ops/web/my2/
name: my2-vol-web
volumes:
- name: nginx-vol-logs
nfs:
server: 192.168.95.2
path: /nfs/data/logs/nginx/
- name: nginx-vol-ssl
nfs:
server: 192.168.95.2
path: /nfs/data/ops/web/nginx/ssl/
- name: nginx-vol-http
nfs:
server: 192.168.95.2
path: /nfs/data/ops/web/nginx/nginx_web/
- name: nginx-vol-tcp
nfs:
server: 192.168.95.2
path: /nfs/data/ops/web/nginx/nginx_web_tcp/
- name: my1-vol-web
nfs:
server: 192.168.95.2
path: /nfs/data/ops/web/my1/
- name: my2-vol-web
nfs:
server: 192.168.95.2
path: /nfs/data/ops/web/my2/
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
labels:
app: nginx-web
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
nodePort: 31117
selector:
app: nginx-web
kind类型是Deployment支持滚动升级和横向扩展;
上述镜像是我从本地push到hub上的具体可以参考 如何使用docker?;
containerPort容器对外暴漏端口;
type是ClusterIP为内部service调用提供统一ip;
Service中的selector是用于跟服务绑定;
如果Deployment和Service在同一个yaml文件中,注意用“---”隔开。
6、启动或者停止上述yaml文件
#创建pod
kubectl create -f tomcat.yaml
kubectl create -f nginx.yaml
#停止pod
kubectl delete -f tomcat.yaml
kubectl delete -f nginx.yaml
7、总结
本文主要讲述了搭建一个后台服务环境,需要那些具体工作,以上就是Kubernetes搭建tomcat和nginx整个过程,你可以按照本文中所示去搭建一个kubernetes下的tomcat和nginx环境。
推荐阅读
原创不易,随手关注或者”在看“,诚挚感谢!