一、创建单个nginx-pod文件
apiVersion: v1 #指定api版本,此值必须在kubectl apiversion中
kind: Pod #指定创建资源的角色/类型(pod\deployment\svc)
metadata: #资源的元数据/属性
name: nginx #资源的名字,在同一个namespace中必须唯一,pod显示名
labels: #设定资源的标签
app: web #为app标签打上web字段
namespace: test #存放的空间
spec: #指定该资源的内容
containers: #该POD运行容器的相关信息
- name: nginx-test #容器的名称,docker ps 看到的名称
image: test-harbor.cedarhd.com/public/nginx:curl #容器使用的镜像
ports: #指定容器的端口
- containerPort: 80 #容器使用的端口
[root@test-nodes1 ~]# kubectl create -f nginx-pod.yaml
pod/nginx created
[root@test-nodes1 ~]# kubectl get pod -o wide -n test
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx 1/1 Running 0 13s 172.7.21.7 test-nodes1.cedarhd.com
二、创建deployment控制器nginx-dp的yaml文件
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx-dp
namespace: test
spec:
replicas: 2 #复本数
template: #deployment模板内容
metadata:
labels:
app: nginx-proxy
spec:
containers:
- name: nginx
image: test-harbor.cedarhd.com/public/nginx:curl
ports:
- containerPort: 80
[root@test-nodes1 ~]# kubectl create -f nginx-dp.yaml
deployment.extensions/nginx-dp created
[root@test-nodes1 ~]# kubectl get pod -o wide -n test
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-dp-856666d759-cwqfc 1/1 Running 0 9m57s 172.7.21.7 test-nodes1.cedarhd.com
nginx-dp-856666d759-lvr9c 1/1 Running 0 9m57s 172.7.22.7 test-nodes2.cedarhd.com
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx-dp 2/2 2 2 8s
NAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-dp-856666d759 2 2 2 8s
三、创建svc服务并关联上面的deployment(通过app:nginx-proxy)
apiVersion: v1
kind: Service
metadata:
name: nginx-dp #svc的显示名
namespace: test
spec:
ports:
- port: 80
targetPort: 80
selector:
app: nginx-proxy #关联当前集群上app为nginx-proxy字段
[root@test-nodes1 ~]# kubectl get svc -n test
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-dp ClusterIP 192.168.249.33 80/TCP 6s
TCP 192.168.249.33:80 nq #相应的映射
-> 172.7.21.7:80 Masq 1 0 0
-> 172.7.22.7:80 Masq 1 0 0
四、根据上面svc资源,创建相应的ingress转发规则
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: mingkang-web
namespace: test
annotations:
kubernetes.io/ingress.class: traefik
spec:
rules:
- host: mingkang-web.cedarhd.com #ingress转发配匹URL地址
http:
paths:
- backend:
serviceName: nginx-dp #标签匹配到相应的svc name
servicePort: 80 #svc的使用端口
备注:此时我们只需要在DNS解释上,添加mignkang-web.cedarhd.com的IP地址到上层的NGINX代理服务器上,上层NGINX收到请求后,会把请求转发到集群的ingress上,ingress会匹配相应的内容URL,将数据发送到SVC的指定端口,再由SVC把数据转发到POD处理。
来源:51CTO
作者:wx592bc92b285c7
链接:https://blog.51cto.com/12965094/2482088