其他分享
首页 > 其他分享> > K8s部署一个tomcat容器

K8s部署一个tomcat容器

作者:互联网

1 部署方法1:使用命令创建

创建一个命名空间

[root@centos64-master ~]# kubectl create ns test
namespace/test created

创建一个deployment

[root@centos64-master ~]# kubectl create deployment tomcat1 --image=tomcat -n test
deployment.apps/tomcat1 created

创建一个service

[root@centos64-master ~]# kubectl expose deployment tomcat1 --port=80 --target-port=8080 --type=NodePort --name=tocmat1 --namespace=test
service/tocmat1 exposed

查询是否创建成功

[root@centos64-master ~]# kubectl get pod,svc,node -n test -o wide
NAME                           READY   STATUS    RESTARTS   AGE   IP              NODE             NOMINATED NODE   READINESS GATES
pod/tomcat1-84d6474cb7-zcrjf   1/1     Running   0          20m   10.244.203.24   centos64-node2   <none>           <none>

NAME                    TYPE       CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE   SELECTOR
service/tocmatservice   NodePort   10.108.234.123   <none>        8080:32256/TCP   54m   app=tomcat1

NAME                   STATUS   ROLES                  AGE   VERSION   INTERNAL-IP       EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION           CONTAINER-RUNTIME
node/centos64-master   Ready    control-plane,master   51d   v1.20.1   192.168.188.243   <none>        CentOS Linux 7 (Core)   3.10.0-1160.el7.x86_64   docker://20.10.7
node/centos64-node1    Ready    <none>                 51d   v1.20.1   192.168.188.244   <none>        CentOS Linux 7 (Core)   3.10.0-1160.el7.x86_64   docker://20.10.7
node/centos64-node2    Ready    <none>                 51d   v1.20.1   192.168.188.245   <none>        CentOS Linux 7 (Core)   3.10.0-1160.el7.x86_64   docker://20.10.7
[root@centos64-master ~]# 

通过http://node IP:端口 可以访问tomcat服务:

访问的页面:http://192.168.188.245:32256/

 可以看到可以访问了,但是没有页面文件,接下来要创建一个页面文件。

登录Pod:

[root@centos64-master ~]# kubectl get pod -n test -o wide
NAME                       READY   STATUS    RESTARTS   AGE   IP              NODE             NOMINATED NODE   READINESS GATES
tomcat1-84d6474cb7-zcrjf   1/1     Running   0          25m   10.244.203.24   centos64-node2   <none>           <none>


[root@centos64-master ~]# kubectl exec -it tomcat1-84d6474cb7-zcrjf /bin/bash -n test
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@tomcat1-84d6474cb7-zcrjf:/usr/local/tomcat# 

进入webapps目录,创建ROOT目录,创建index.jsp文件

root@tomcat1-84d6474cb7-zcrjf:/usr/local/tomcat# cd webapps
root@tomcat1-84d6474cb7-zcrjf:/usr/local/tomcat/webapps# mkdir ROOT 
root@tomcat1-84d6474cb7-zcrjf:/usr/local/tomcat/webapps# cd ROOT
root@tomcat1-84d6474cb7-zcrjf:/usr/local/tomcat/webapps/ROOT# echo '<h1>Hello tomcat</h1>' >index.jsp
root@tomcat1-84d6474cb7-zcrjf:/usr/local/tomcat/webapps/ROOT# ls -lrt
total 4
-rw-r--r-- 1 root root 22 Jul 25 14:46 index.jsp

然后刷新访问页面:http://192.168.188.245:32256/

 

测试页面或者:cat > index.html

<html>
<head>
<meta charset="utf-8">
<title>测试系统</title>
</head>
<body>
    <h1>测试页面</h1>
    <p>我的第一个页面。</p>
</body>
</html>

2  部署方法2:使用yaml文件创建

cat > tomcat.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat1
  labels:
    app: demo
spec:
  replicas: 1
  selector:
    matchLabels:
      app: demo
  template:
    metadata:
      labels:
        app: demo
    spec:
      containers:
      - name: tomcat
        image: demo-tomcat:1.0 
        ports:
        - containerPort: 80

kubectl create -f tomcat.yaml
kubectl describe pod demo-tomcat-7f6bb4d7cd-fdbzw

cat > tomcatService.yaml

apiVersion: v1
kind: Service
metadata:
  name: tomcatservice
spec:
  type: NodePort
  ports:
    - port: 8080
      targetPort: 8080
  selector:
    app: demo

kubectl create -f tomcatService.yaml
kubectl get srv

其他步骤和方法1一样。

标签:容器,tomcat,kubectl,--,tomcat1,K8s,root,centos64
来源: https://blog.csdn.net/a007zheng/article/details/119089301