K8s部署tomcat mysql 与ingress暴露服务

来自linux中国网wiki
Evan讨论 | 贡献2022年9月16日 (五) 03:05的版本
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳到导航 跳到搜索

see also

Kubernetes ingress基础#最简单的使用例子

总文件

[root@iZwz9ignqjb7qvobn1fuocZ tomcat]# ls 
mysql-rc.yaml  mysql-svc.yaml  myweb-rc.yaml  myweb-svc.yaml

mysql

创建mysql RC对象

[root@master tomcat]#  cat mysql-rc.yaml 
apiVersion: v1
kind: ReplicationController
metadata:
  name: mysql
spec:
  replicas: 1
  selector:
    app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
        containers:
        - image: mysql:5.7
          name: mysql
          resources:
            limits:
              cpu: "1"
              memory: 0.5Gi
          ports:
          - containerPort: 3306
          env:
          - name: MYSQL_ROOT_PASSWORD
            value: "123456"


[root@iZwz9ignqjb7qvobn1fuocZ tomcat]# kubectl create -f  mysql-rc.yaml 
replicationcontroller/mysql created
[root@iZwz9ignqjb7qvobn1fuocZ tomcat]# kubectl get rc 
NAME    DESIRED   CURRENT   READY   AGE
mysql   1         1         0       10s

创建mysql Service对象

cat mysql-svc.yaml 
apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  ports:
  - name: mysql-svc
    port: 3306
#    targetPort: 3306
#    nodePort: 31101
  selector:
    app: mysql
#  type: NodePort



[root@iZwz9ignqjb7qvobn1fuocZ tomcat]# kubectl create -f  mysql-svc.yaml 
service/mysql created
[root@iZwz9ignqjb7qvobn1fuocZ tomcat]# kubectl get svc 
NAME                            TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
kubernetes                      ClusterIP   172.21.0.1     <none>        443/TCP    52m
mysql                           ClusterIP   172.21.6.69    <none>        3306/TCP   12s
tailored-armadillo-hello-helm   ClusterIP   172.21.7.186   <none>        80/TCP     11m


tomcat

部署tomcat服务,我们需要做两件事,一是创建RC(Replication Controller),二是创建Service。RC是kubernetes中的副本控制器,也就是说,RC负责自动部署容器化应用。Service是我们访问tomcat服务的入口地址,我们是通过Service来对该服务就行访问的。

https://hub.docker.com/r/kubeguide/tomcat-app

创建tomcat RC对象


vi tomcat.yaml

apiVersion: v1
kind: ReplicationController
metadata:
  name: myweb
spec:
  replicas: 2
  selector:
    app: myweb
  template:
    metadata:
      labels:
        app: myweb
    spec:
      containers:
        - name: myweb
          image: docker.io/kubeguide/tomcat-app:v1
          ports:
          - containerPort: 8080
          env:
          - name: MYSQL_SERVICE_HOST
            value: 'mysql'
          - name: MYSQL_SERVICE_PORT
            value: '3306'

kubectl create -f  tomcat.yaml



July  2022 可能不行 
 cat myweb-rc.yaml 
apiVersion: v1
kind: ReplicationController
metadata:
  name: myweb
spec:
  replicas: 2
  selector:
    app: myweb
  template:
    metadata:
      labels:
        app: myweb
    spec:
      containers:
      - image: registry.cn-shenzhen.aliyuncs.com/evan886/k8s:debian_tomcatv1
      #我的那个debian的好像不行#- image: kubeguide/tomcat-app:v1
        name: myweb
        resources:
          limits:
            cpu: "1.0"
            memory: 400Mi
        ports:
        - containerPort: 8080
        env:
#        - name: MYSQL_SERVICE_HOST
#          value: 'mysql'
#        - name: MYSQL_SERVICE_PORT
#          value: '3306'
#






[root@ tomcat]# kubectl create -f  myweb-rc.yaml 
replicationcontroller/myweb created

 kubectl get pods 
NAME                                            READY   STATUS    RESTARTS   AGE
mysql-fkkf7                                     1/1     Running   0          2m51s
myweb-fp4mz                                     1/1     Running   0          34s

tomcat service

 创建了RC对象还不行,虽然RC对象为我们自动部署了2个pod,但是我们还需要一个Service对象来作为入口地址来对创建好的tomcat进行访问,所以,接下来我们的任务就是创建Service对象

root@k8s-master:~/tmp# cat myweb-svc.yaml 
apiVersion: v1
kind: Service
metadata:
  name: myweb
spec:
  type: NodePort
  ports:
    - port: 8080
      nodePort: 31101
  selector:
    app: mysql
#  type: NodePort


root@k8s-master:~/tmp# kubectl create  -f  myweb-svc.yaml 
service/myweb created


root@k8s-master:~/tmp# kubectl get services 
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP          2d6h
mysql        ClusterIP   10.106.222.111   <none>        3306/TCP         12m
myweb        NodePort    10.104.92.71     <none>        8080:31101/TCP   42s

服务访问

集群外部访问服务

从集群的外部我们可以通过node IP + node端口号的方式来对服务进行访问。pod实际上已经分在了不同的node中了,我们只用找到其中一个pod所在的node就行了
[root@master tomcat]# kubectl  describe pod   myweb 
Name:           myweb-kdfvq
Namespace:      default
Priority:       0
Node:           node1/192.168.88.31
Start Time:     Tue, 16 Jul 2019 11:04:51 +0800
Labels:         app=myweb
Annotations:    <none>
Status:         Running
IP:             10.224.1.30
Controlled By:  ReplicationController/myweb



可以看到我这个pod所在的IP地址。刚才已经看到了,在yaml文件中我将物理端口设为30001。好的,接下来就可以从外部对服务进行访问了。 


evan@latop:~/ssh/5min$ curl http://192.168.88.31:30001/
version: 1

update

重装打包tomcat images 然后上传到ali 然后 rc 文件改为v2 再发布一次 就是  

evan@latop:~/downloads/tomcat$ curl http://192.168.88.31:30001/
version: 2


on aliyun

[root@iZwz9ignqjb7qvobn1fuocZ tomcat]# kubectl get services
NAME                            TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
kubernetes                      ClusterIP   172.21.0.1     <none>        443/TCP          55m
mysql                           ClusterIP   172.21.6.69    <none>        3306/TCP         3m10s
myweb                           NodePort    172.21.4.96    <none>        8080:30001/TCP   17s
tailored-armadillo-hello-helm   ClusterIP   172.21.7.186   <none>        80/TCP           14m
[root@iZwz9ignqjb7qvobn1fuocZ tomcat]# kubectl get svc 
NAME                            TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
kubernetes                      ClusterIP   172.21.0.1     <none>        443/TCP          55m
mysql                           ClusterIP   172.21.6.69    <none>        3306/TCP         3m17s
myweb                           NodePort    172.21.4.96    <none>        8080:30001/TCP   24s
tailored-armadillo-hello-helm   ClusterIP   172.21.7.186   <none>        80/TCP           14m




当部署完成后,单击左侧导航栏中的路由与负载均衡 > 服务,找到wordpress服务并查看其外端端点


更新服务--> 负载均衡-->   服务端口 这里为30001 


[root@iZwz9ignqjb7qvobn1fuocZ tomcat]# kubectl get services
NAME                            TYPE           CLUSTER-IP     EXTERNAL-IP    PORT(S)           AGE
kubernetes                      ClusterIP      172.21.0.1     <none>         443/TCP           68m
mysql                           ClusterIP      172.21.6.69    <none>         3306/TCP          15m
myweb                           LoadBalancer   172.21.4.96    119.23.93.84   30001:30001/TCP   12m




LoadBalancer  ip 
http://119.23.93.84:30001/demo/index.jsp

hosts

loadbalaip  a.com 



cat myweb.yml 

apiVersion: v1
kind: Service
metadata:
  creationTimestamp: '2019-07-10T12:24:18Z'
  name: myweb
  namespace: default
  resourceVersion: '10862'
  selfLink: /api/v1/namespaces/default/services/myweb
  uid: a3912df1-a30d-11e9-886c-00163e107776
spec:
  clusterIP: 172.21.4.96
  externalTrafficPolicy: Local
  healthCheckNodePort: 30205
  ports:
    - name: myweb-svc
      nodePort: 30001
      port: 30001
      protocol: TCP
      targetPort: 8080
  selector:
    app: myweb
  sessionAffinity: None
  type: LoadBalancer
status:
  loadBalancer:
    ingress:
      - ip: 119.23.93.84


其实就是ingress 




实例间服务发现(Headless Service)













troubleshooting

1. yaml文件的格式问题 
我初次编写yaml文件可是花了不少时间,就是因为格式的问题,总是创建不了RC或者Servcice。尤其是需要注意,缩进要用空格缩进而不能用TAB键缩进。否则是成功不了的。 

reference see also

kubernetes 学习笔记--使用Ingress 发布tomcat


k8s部署服务——内部服务关联


6.k8s部署tomcat通过ingress访问

6.k8s部署tomcat通过ingress访问