k8s部署Nginx实现外部访问
1.准备k8s环境
[root@master yaml]# kubectl get nodes ##两个节点均为Ready状态
NAME STATUS ROLES AGE VERSION
master Ready master 3d8h v1.18.1
node Ready <none> 3d8h v1.18.1
2.检查主机上是否存在Nginx镜像
[root@master cicd]# docker images |grep nginx
172.16.181.12/library/nginx latest 992e3b7be046 11 months ago 133MB
3.编写pod部署模板nginx-pod.yaml
[root@master yaml]# cat nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod ##pod名称
labels:
app: nginx ###写上这个pod的标签,方便svc连接
spec:
containers:
- name: nginx-pod ##pod容器名称
image: 172.16.181.12/library/nginx:latest ###镜像
imagePullPolicy: IfNotPresent ###镜像拉取策略
ports:
- containerPort: 80 ###容器端口
4.编写service部署模板文件nginx-svc.yaml
[root@master yaml]# cat nginx-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-svc
spec:
selector:
app: nginx ###关联容器标签
ports:
- port: 80 ###容器端口
nodePort: 30002 ###nodeport映射为30002端口,便于外部主机访问
type: NodePort ###svc类型为nodeport
5.kubectl命令执行模板文件
[root@master yaml]# kubectl apply -f nginx-pod.yaml
pod/nginx-pod created
[root@master yaml]# kubectl apply -f nginx-svc.yaml
service/nginx-svc created
[root@master yaml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
mall 5/5 Running 0 8h
nginx-pod 1/1 Running 0 90s
[root@master yaml]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d8h
nginx-svc NodePort 10.111.24.115 <none> 80:30002/TCP 23s
6.浏览器访问ip:30002