前面我们通过命令行、配置文件、可视化界面创建了pod,这个pod里面只有一个nginx容器,我们说过pod有一个唯一ip,且pod可以包含一个或多个容器,下面我们来部署多个容器的pod
apiVersion: v1
kind: Pod
metadata:
labels:
run: myapp
name: myapp
spec:
containers:
- image: nginx
name: nginx
- image: tomcat:8.5.68
name: tomcat
执行上面文件
kubectl apply -f multicontain-pod.yml
可以看到myapp这个pod有两个容器
[root@k8s-master opt]# kubectl get pod -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
my-nginx 1/1 Running 1 2d 10.244.36.124 k8s-node1 <none> <none>
myapp 2/2 Running 0 4m8s 10.244.36.98 k8s-node1 <none> <none>
nfs-client-provisioner-6c8cdddb58-kh555 1/1 Running 7 6d1h 10.244.36.79 k8s-node1 <none>
我们看到myapp这个pod分配到了10.244.36.98这个ip
这时候我们访问这个ip默认使用80端口,80端口是nginx的默认端口访问的就是nginx
[root@k8s-master opt]# curl 10.244.36.98 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> html { color-scheme: light dark; } body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
tomcat默认是8080端口
[root@k8s-master opt]# curl 10.244.36.98:8080 <!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/8.5.68</h3></body></html>[root@k8s-master opt]#
另外
在tomcat跟nginx容器内部互相访问使用localhost:端口即可,因为他们共享网络空间,也共享存储
同时,也因为他们共享网络跟存储,所以不可以部署两个nginx,因为端口会冲突