POD是可在Kubernetes中创建和管理的最小可部署计算单元。
Pod 是一组一个或多个容器,具有共享的存储和网络资源,以及如何运行容器的规范。Pod的内容始终位于同一位置,并在共享上下文中运行。
Pod模拟特定于应用程序的“逻辑主机”:它包含一个或多个相对紧密耦合的应用程序容器。在非云环境中,在同一物理或虚拟机上执行的应用程序类似于在同一逻辑主机上执行的云应用程序。
Env:
2节点k8s: 主192.168.40.151,从192.168.40.152
[root@centos152 ~]#
kubelet --version
Kubernetes v1.22.4
[root@centos152 ~]#
docker -v
Docker version
20.10.11, build dea9396
[root@centos152 ~]#
nginx-ingress-controller:v1.1.0
当前deployment、pod,一个deployment四个pod,pod内容器运行在8088端口,每个pod里面运行一个相同的java web程序 hello1
|
[root@centos152 hello1]# kubectl get
deployment -n tongweb-namespace -o
wide NAME READY
UP-TO-DATE AVAILABLE AGE
CONTAINERS IMAGES SELECTOR tongweb 4/4
4 4 4d17h tongweb tongweb/tongweb:7.0 app=tongweb [root@centos152
hello1]# kubectl get pod,svc -n tongweb-namespace -o wide NAME READY STATUS
RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES pod/tongweb-56dd4fdc46-gtnq7 1/1
Running 2 (3d11h ago) 4d17h
10.244.1.105 centos152 <none> <none> pod/tongweb-56dd4fdc46-jd7tq 1/1
Running 2 (3d11h ago) 4d17h
10.244.1.108 centos152 <none> <none> pod/tongweb-56dd4fdc46-k86br 1/1
Running 2 (3d11h ago) 4d17h
10.244.1.106 centos152 <none> <none> pod/tongweb-56dd4fdc46-pz2l9 1/1
Running 2 (3d11h ago) 4d17h
10.244.1.107 centos152 <none> <none> |
kubectl expose --help
--type='': Type for
this service: ClusterIP, NodePort, LoadBalancer, or ExternalName. Default is
'ClusterIP'.
--target-port='': Name
or number for the port on the container that the service should direct traffic
to.
--port='': The port
that the service should serve on. Copied from the resource being exposed, if
unspecified
--service-node-port-range
flag (default: 30000-32767)
https://kubernetes.io/docs/concepts/services-networking/service/
1.
使用nodeport方式
使用方法:kubectl
expose deployment 发布名 -n 命名空间 --port=服务内部端口 --target-port=容器运行端口
--type=NodePort --name 服务名称
示例如下:kubectl
expose deployment tongweb -n tongweb-namespace --port=8087 --target-port=8088 --type=NodePort
--name svc-nodeport
提供统一入口:
内部:http://10.101.152.54:8087/hello1/hello.jsp
外部:http://192.168.40.152:32437/hello1/hello.jsp
http://192.168.40.151:32437/hello1/hello.jsp
|
[root@centos152
hello1]# kubectl expose deployment tongweb -n tongweb-namespace --port=8087
--target-port=8088 --type=NodePort
--name svc-nodeport service/svc-nodeport
exposed [root@centos152
hello1]# kubectl get pod,svc -n tongweb-namespace -o wide NAME READY STATUS
RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES pod/tongweb-56dd4fdc46-gtnq7 1/1
Running 2 (3d12h ago) 4d18h
10.244.1.105 centos152 <none> <none> pod/tongweb-56dd4fdc46-jd7tq 1/1
Running 2 (3d12h ago) 4d18h
10.244.1.108 centos152 <none> <none> pod/tongweb-56dd4fdc46-k86br 1/1
Running 2 (3d12h ago) 4d18h
10.244.1.106 centos152 <none> <none> pod/tongweb-56dd4fdc46-pz2l9 1/1
Running 2 (3d12h ago) 4d18h
10.244.1.107 centos152 <none> <none> NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR service/svc-nodeport NodePort 10.101.152.54 <none> 8087:32437/TCP 21s
app=tongweb [root@centos152
hello1]# curl http://10.101.152.54:8087/hello1/hello.jsp this is a
test page! [root@centos152
hello1]# curl http://10.101.152.54:32437/hello1/hello.jsp ^C [root@centos152
hello1]# curl http://192.168.40.151:32437/hello1/hello.jsp this is a
test page! [root@centos152
hello1]# curl http://192.168.40.152:32437/hello1/hello.jsp this is a
test page! [root@centos152
hello1]# |
查看描述
|
[root@centos152
hello1]# kubectl describe svc
svc-nodeport -n tongweb-namespace Name: svc-nodeport Namespace: tongweb-namespace Labels: <none> Annotations: <none> Selector: app=tongweb Type: NodePort IP Family
Policy: SingleStack IP
Families: IPv4 IP: 10.101.152.54 IPs: 10.101.152.54 Port: <unset> 8087/TCP TargetPort: 8088/TCP NodePort: <unset> 32437/TCP Endpoints:
10.244.1.105:8088,10.244.1.106:8088,10.244.1.107:8088 + 1 more... Session
Affinity: None External
Traffic Policy: Cluster Events: <none> [root@centos152 hello1]# |
2.
使用 LoadBalancer方式
使用方法:kubectl
expose deployment 发布名 -n 命名空间 --port=服务内部端口 --target-port=容器运行端口
--type= LoadBalancer --name 服务名称
示例如下: kubectl
expose deployment tongweb -n
tongweb-namespace --port=8765 --target-port=8088 --type=LoadBalancer
--name=svc-loadbalancer
提供统一入口:
内部:http:// 10.109.178.214:8765/hello1/hello.jsp
外部:http://192.168.40.152:
30422/hello1/hello.jsp
http://192.168.40.151: 30422/hello1/hello.jsp
|
[root@centos152
hello1]# kubectl expose deployment tongweb
-n tongweb-namespace --port=8765 --target-port=8088
--type=LoadBalancer --name=svc-loadbalancer service/svc-loadbalancer
exposed [root@centos152
hello1]# kubectl get pod,svc -n tongweb-namespace -o wide NAME READY STATUS
RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES pod/tongweb-56dd4fdc46-gtnq7 1/1
Running 2 (3d12h ago) 4d18h
10.244.1.105 centos152 <none> <none> pod/tongweb-56dd4fdc46-jd7tq 1/1
Running 2 (3d12h ago) 4d18h
10.244.1.108 centos152 <none> <none> pod/tongweb-56dd4fdc46-k86br 1/1
Running 2 (3d12h ago) 4d18h
10.244.1.106 centos152 <none> <none> pod/tongweb-56dd4fdc46-pz2l9 1/1
Running 2 (3d12h ago) 4d18h
10.244.1.107 centos152 <none> <none> NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR service/svc-loadbalancer LoadBalancer 10.109.178.214 <pending> 8765:30422/TCP 7s
app=tongweb service/svc-nodeport NodePort 10.110.98.120 <none> 8088:30471/TCP 9m
app=tongweb [root@centos152
hello1]# curl http://10.109.178.214:8765/hello1/hello.jsp this is a
test page! [root@centos152
hello1]# curl http://10.109.178.214:8088/hello1/hello.jsp ^C [root@centos152
hello1]# [root@centos152
hello1]# curl http://192.168.40.152:30422/hello1/hello.jsp this is a
test page! [root@centos152
hello1]# curl http://192.168.40.151:30422/hello1/hello.jsp this is a
test page! [root@centos152 hello1]# |
查看描述
|
[root@centos152
hello1]# kubectl describe svc svc-loadbalance -n tongweb-namespace Name: svc-loadbalancer Namespace: tongweb-namespace Labels: <none> Annotations: <none> Selector: app=tongweb Type: LoadBalancer IP Family
Policy: SingleStack IP
Families: IPv4 IP: 10.109.178.214 IPs: 10.109.178.214 Port: <unset> 8765/TCP TargetPort: 8088/TCP NodePort: <unset> 30422/TCP Endpoints:
10.244.1.105:8088,10.244.1.106:8088,10.244.1.107:8088 + 1 more... Session
Affinity: None External
Traffic Policy: Cluster Events: <none> [root@centos152 hello1]# |
3.
使用 ClusterIP方式
使用方法:kubectl
expose deployment 发布名 -n 命名空间 --port=服务内部端口 --target-port=容器运行端口
--type= LoadBalancer --name 服务名称
示例如下: kubectl
expose deployment tongweb -n
tongweb-namespace --port=8086 --target-port=8088 --type=ClusterIP --name svc-clusterip
提供统一入口:
内部:http://10.96.137.97:8086/hello1/hello.jsp
|
[root@centos152
hello1]# kubectl expose deployment tongweb
-n tongweb-namespace --port=8086 --target-port=8088
--type=ClusterIP --name svc-clusterip service/svc-clusterip
exposed [root@centos152
hello1]# kubectl get pod,svc -n tongweb-namespace -o wide NAME READY STATUS
RESTARTS AGE
IP NODE NOMINATED NODE READINESS GATES pod/tongweb-56dd4fdc46-gtnq7 1/1
Running 2 (3d12h ago) 4d19h
10.244.1.105 centos152 <none> <none> pod/tongweb-56dd4fdc46-jd7tq 1/1
Running 2 (3d12h ago) 4d19h
10.244.1.108 centos152 <none> <none> pod/tongweb-56dd4fdc46-k86br 1/1
Running 2 (3d12h ago) 4d19h
10.244.1.106 centos152 <none> <none> pod/tongweb-56dd4fdc46-pz2l9 1/1
Running 2 (3d12h ago) 4d19h 10.244.1.107 centos152
<none>
<none> NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR service/svc-clusterip ClusterIP 10.96.137.97 <none> 8086/TCP 7s
app=tongweb service/svc-loadbalancer LoadBalancer 10.109.178.214 <pending> 8765:30422/TCP 41m
app=tongweb service/svc-nodeport NodePort 10.101.152.54 <none> 8087:32437/TCP 6m47s
app=tongweb [root@centos152
hello1]# curl http://10.96.137.97:8086/hello1/hello.jsp this is a
test page! [root@centos151
~]# curl http://10.96.137.97:8086/hello1/hello.jsp this is a
test page! |
查看描述
|
[root@centos151
~]# kubectl describe svc svc-clusterip -n tongweb-namespace Name: svc-clusterip Namespace: tongweb-namespace Labels: <none> Annotations: <none> Selector: app=tongweb Type: ClusterIP IP Family
Policy: SingleStack IP
Families: IPv4 IP: 10.96.137.97 IPs: 10.96.137.97 Port: <unset> 8086/TCP TargetPort: 8088/TCP Endpoints: 10.244.1.105:8088,10.244.1.106:8088,10.244.1.107:8088
+ 1 more... Session
Affinity: None Events: <none> [root@centos151 ~]# |
4.
使用 ingress nginx方式
先部署kubectl apply
-f deploy.yaml
暴露类型为clusterip 8086端口 服务名称为 svc-tongweb-clusterip
[root@centos151 ~]# kubectl expose
deployment tongweb -n tongweb-namespace
--port=8086 --target-port=8088 --type=ClusterIP
--name svc-tongweb-clusterip
service/svc-tongweb-clusterip
exposed
编写ingress yaml 文件
|
apiVersion:
networking.k8s.io/v1 kind: Ingress metadata: name: nginx-web1 namespace: tongweb-namespace annotations: # 指定 Ingress Controller 的类型 kubernetes.io/ingress.class:
"nginx" # 指定我们的 rules 的 path 可以使用正则表达式 nginx.ingress.kubernetes.io/use-regex:
"true" # 连接超时时间,默认为 5s nginx.ingress.kubernetes.io/proxy-connect-timeout:
"600" # 后端服务器回转数据超时时间,默认为 60s
nginx.ingress.kubernetes.io/proxy-send-timeout: "600" # 后端服务器响应超时时间,默认为 60s
nginx.ingress.kubernetes.io/proxy-read-timeout: "600" # 客户端上传文件,最大大小,默认为 20m nginx.ingress.kubernetes.io/proxy-body-size:
"10m" # URL 重写
nginx.ingress.kubernetes.io/rewrite-target: / spec: # 路由规则 rules: # 主机名,只能是域名, - host: www.test.com http: paths: - path: / pathType: Prefix backend: service: name: svc-tongweb-clusterip port:
number: 8086 |
发布:
[root@centos151 ~]# kubectl create -f ingress.yaml
[root@centos151 ~]# kubectl get
ingress --all-namespaces
NAMESPACE NAME CLASS HOSTS ADDRESS PORTS AGE
tongweb-namespace nginx-web1
<none> www.test.com 192.168.40.152 80
21d
提供统一入口:
外部:用域名访问
[root@centos152 ~]# curl
http://www.test.com/hello1/hello.jsp
[root@centos152 ~]# curl -k --tlsv1 https://www.test.com/hello1/hello.jsp




