一 工作目录
root@k8s-master-01:/data/k8s/yaml# pwd
/data/k8s/yaml
二 创建名称空间
1.1 编写nginx-ns.yaml
root@k8s-master-01:/data/k8s/yaml# cat namespace/nginx-ns.yaml
apiVersion: v1
kind: Namespace
metadata:
name: wgs
1.2 创建名称空间
root@k8s-master-01:/data/k8s/yaml# kubectl apply -f namespace/nginx-ns.yaml
namespace/wgs created
1.3 查看名称空间
root@k8s-master-01:/data/k8s/yaml# kubectl get ns
NAME STATUS AGE
default Active 32d
kube-node-lease Active 32d
kube-public Active 32d
kube-system Active 32d
kubernetes-dashboard Active 31d
wgs Active 19s
三 编写nginx.yaml文件
查看代码
root@k8s-master-01:/data/k8s/yaml# cat nginx/nginx.yaml
kind: Deployment
apiVersion: apps/v1
metadata:
labels:
app: wgs-nginx-deployment-label
name: wgs-nginx-deployment
namespace: wgs
spec:
replicas: 1
selector:
matchLabels:
app: wgs-nginx-selector
template:
metadata:
labels:
app: wgs-nginx-selector
spec:
containers:
- name: wgs-nginx-container
image: 192.168.174.120/baseimages/nginx-web1:v1.0
imagePullPolicy: Always
ports:
- containerPort: 80
protocol: TCP
name: http
- containerPort: 443
protocol: TCP
name: https
env:
- name: "nginx-version"
value: "1.20.2"
resources:
limits:
cpu: 2
memory: 1Gi
requests:
cpu: 500m
memory: 1Gi
---
kind: Service
apiVersion: v1
metadata:
labels:
app: wgs-nginx-service-label
name: wgs-nginx-service
namespace: wgs
spec:
type: NodePort
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
nodePort: 40002
- name: https
port: 443
protocol: TCP
targetPort: 443
nodePort: 40443
selector:
app: wgs-nginx-selector
四 创建nginx pod
root@k8s-master-01:/data/k8s/yaml# kubectl apply -f nginx/nginx.yaml
deployment.apps/wgs-nginx-deployment created
service/wgs-nginx-service created
五 查看nginx pod
root@k8s-master-01:/data/k8s/yaml# kubectl get pods -n wgs -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
wgs-nginx-deployment-9bcc5758d-49xs7 1/1 Running 0 2m38s 10.200.89.140 192.168.174.108 <none> <none>
六 查看创建的nginx Service
root@k8s-master-01:/data/k8s/yaml# kubectl get svc -n wgs
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
wgs-nginx-service NodePort 10.100.206.30 <none> 80:40002/TCP,443:40443/TCP 90s
七 查看暴露的端口
root@k8s-master-01:~# lsof -i :40002
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
kube-prox 1006 root 13u IPv4 847825 0t0 TCP *:40002 (LISTEN)
八 访问nginx
root@k8s-master-01:~# curl http://192.168.174.106:40002
hello nginx
root@k8s-master-01:~# curl http://192.168.174.106:40002/webapp/index.html
test webapp