K8S部署nginx

创建目录

1
mkdir /opt/k8s/nginx-yaml

创建名称空间

1
kubectl create namespace nginx

创建deployment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
vim nginx-deploy.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: nginx
spec:
selector:
matchLabels:
app: nginx
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:alpine
ports:
- containerPort: 80
volumeMounts:
- mountPath: /etc/localtime
name: c-v-path
- mountPath: /etc/nginx/conf.d
name: c-v-path-ng
restartPolicy: Always
volumes:
- hostPath:
path: /etc/localtime
type: ''
name: c-v-path
- hostPath:
path: /root/opt/k8s/nginx-yaml/nginx-conf
type: ''
name: c-v-path-ng

创建service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
vim nginx-svc.yaml

apiVersion: v1
kind: Service
metadata:
name: nginx-service
namespace: nginx
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
nodePort: 30080
type: NodePort

创建

1
2
kubectl apply -f nginx-deploy.yaml
kubectl apply -f nginx-svc.yaml

查看pod在哪个节点

1
2
3
4
5
kubectl get deploy -n nginx -o wide

NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
nginx-deployment 3/3 3 3 48s nginx nginx:alpine app=nginx

查看service状态–端口是30080

1
2
3
4
5
kubectl get svc nginx-service -o wide

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
nginx-service NodePort 10.106.209.76 <none> 80:30080/TCP 26s app=nginx

访问

1
pod节点 + 端口

进入容器

1
kubectl exec -it pod名称 --sh

K8S部署nginx
http://example.com/2025/11/06/K8S部署nginx/
作者
Mr.xu
发布于
2025年11月6日
许可协议