yam文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 2
selector:
matchLabels:
app: mynginx
template:
metadata:
labels:
app: mynginx
spec:
containers:
- name: nginx
image: nginx:1.25
imagePullPolicy: IfNotPresent
---
apiVersion: v1
kind: Service
metadata:
name: nginx
spec:
selector:
app: mynginx
ports:
- port: 8000
protocol: TCP
targetPort: 80
重要部分:需要再service selector选择deployment的labels
port为暴漏的端口,targetport为nginx端口
kubectl apply -f nginx.yml启动
查看pod,service
[admin@localhost k8s]$ kubectl get service -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5h32m <none>
nginx ClusterIP 10.105.245.100 <none> 8000/TCP 9m40s app=mynginx
[admin@localhost k8s]$ kubectl get po -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-58fd4875cd-5lfjw 1/1 Running 0 11m 10.244.0.39 minikube <none> <none>
nginx-58fd4875cd-pw7qp 1/1 Running 0 11m 10.244.0.37 minikube <none> <none>
nginx-58fd4875cd-qqsmp 1/1 Running 0 11m 10.244.0.38 minikube <none> <none>
servie启动成功
service会虚拟一个ip以轮训的方式访问后端nginx
测试,进入一个nginx终端
kubectl exec -it nginx-58fd4875cd-5lfjw -- /bin/bash
curl service ip
root@nginx-58fd4875cd-5lfjw:/# curl 10.105.245.100:8000
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
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>
成功!