一、配置Pod的端口配置
1.1 编写yaml文件
编写 pod_ports.yaml文件,内容如下,设置port的name和containerPort,如下
apiVersion: v1
kind: Namespace
metadata:
name: dev
---
apiVersion: v1
kind: Pod
metadata:
name: pod-ports
namespace: dev
labels:
user: redrose2100
spec:
containers:
- name: nginx
image: nginx:1.17.1
ports:
- containerPort: 80
name: nginx-port
protocol: TCP
1.2 创建资源
使用如下命令创建资源
[root@master pod]# kubectl apply -f pod_ports.yaml
namespace/dev created
pod/pod-ports created
[root@master pod]#
1.3 查看Pod的IP和端口等信息
使用如下命令可以查看到ip和端口
[root@master pod]# kubectl get pod -n dev -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod-ports 1/1 Running 0 3m 10.244.2.36 node2 <none> <none>
[root@master pod]# kubectl describe pod pod-ports -n dev
Name: pod-ports
Namespace: dev
Priority: 0
Node: node2/192.168.16.42
Start Time: Tue, 22 Mar 2022 00:55:03 +0800
Labels: user=redrose2100
Annotations: <none>
Status: Running
IP: 10.244.2.36
IPs:
IP: 10.244.2.36
Containers:
nginx:
Container ID: docker://b3514575830f43fdedf27a2ed784f52c41183b22a7cd2050fad361ce0802ef41
Image: nginx:1.17.1
Image ID: docker-pullable://nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Tue, 22 Mar 2022 00:55:04 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-9c8z2 (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-9c8z2:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional: <nil>
DownwardAPI: true
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 3m8s default-scheduler Successfully assigned dev/pod-ports to node2
Normal Pulled 3m7s kubelet Container image "nginx:1.17.1" already present on machine
Normal Created 3m7s kubelet Created container nginx
Normal Started 3m7s kubelet Started container nginx
[root@master pod]#
1.4 使用IP和端口验证
在集群环境中使用ip+port访问nginx,如下,即能正确访问nginx
[root@master pod]# curl 10.244.2.36:80
<!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>
[root@master pod]#
1.5 删除资源
使用如下命令删除资源
[root@master pod]# kubectl delete -f pod_ports.yaml
namespace "dev" deleted
pod "pod-ports" deleted
[root@master pod]#