1.创建一个测试用的nginx pod.

[root@k8s-master ~]# kubectl run my-nginx --image=nginx --replicas=3 --port=80

2.查看获取IP情况

[root@k8s-master ~]# kubectl get pod -o wide
NAME                        READY   STATUS    RESTARTS   AGE   IP          NODE        NOMINATED NODE   READINESS GATES
my-nginx-64fc468bd4-kqfpv   1/1     Running   0          62m   10.2.28.2   10.88.0.2   <none>           <none>
my-nginx-64fc468bd4-nwr52   1/1     Running   0          62m   10.2.97.2   10.88.0.3   <none>           <none>
my-nginx-64fc468bd4-tcskd   1/1     Running   0          62m   10.2.28.3   10.88.0.2   <none>           <none>

3.测试联通性

[root@k8s-master ~]# ping  -c 3 10.2.28.2
PING 10.2.28.2 (10.2.28.2) 56(84) bytes of data.
64 bytes from 10.2.28.2: icmp_seq=1 ttl=63 time=0.288 ms
64 bytes from 10.2.28.2: icmp_seq=2 ttl=63 time=0.180 ms
64 bytes from 10.2.28.2: icmp_seq=3 ttl=63 time=0.192 ms

--- 10.2.28.2 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.180/0.220/0.288/0.048 ms
[root@k8s-master ~]# ping  -c 3 10.2.97.2
PING 10.2.97.2 (10.2.97.2) 56(84) bytes of data.
64 bytes from 10.2.97.2: icmp_seq=1 ttl=63 time=0.307 ms
64 bytes from 10.2.97.2: icmp_seq=2 ttl=63 time=0.204 ms
64 bytes from 10.2.97.2: icmp_seq=3 ttl=63 time=0.197 ms

--- 10.2.97.2 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.197/0.236/0.307/0.050 ms
[root@k8s-master ~]# ping  -c 3 10.2.28.3
PING 10.2.28.3 (10.2.28.3) 56(84) bytes of data.
64 bytes from 10.2.28.3: icmp_seq=1 ttl=63 time=0.317 ms
64 bytes from 10.2.28.3: icmp_seq=2 ttl=63 time=0.204 ms
64 bytes from 10.2.28.3: icmp_seq=3 ttl=63 time=0.203 ms

--- 10.2.28.3 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.203/0.241/0.317/0.054 ms

4.暴露服务,创建service.

[root@k8s-master ~]# kubectl expose deployment my-nginx --port=8080 --target-port=80 --external-ip=10.88.0.3
service "my-nginx" exposed

5.查看service明细。

[root@k8s-master ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
kubernetes   ClusterIP   10.1.0.1       <none>        443/TCP    20h
my-nginx     ClusterIP   10.1.116.155   10.88.0.3     8080/TCP   92s

6.测试svc访问

[root@k8s-master ~]# curl 10.88.0.3:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    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>

 

710c279b9f390e24c012b27336cb3d6b.png

 

 

 

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐