nginx ingress controller配置

76 阅读1分钟

部署Pod

# go-v3.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: go-v3-deployment
spec:
  selector:
    matchLabels:
      app: go-v3
  replicas: 2 # 告知 Deployment 运行 2 个与该模板匹配的 Pod
  template:
    metadata:
      labels:
        app: go-v3
    spec:
      containers:
      - name: go-v3
        image: go:v3
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 4000
# 使用deployment创建pod
kubectl apply -f go-v3.yaml

创建service

# 创建service  gov3-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: go-v3-service
spec:
  selector:
     app: go-v3
  ports:
  - name: gov3-port
    protocol: TCP
    port: 4000
# 查看结果
[root@master]# kubectl get service
NAME            TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
go-v3-service   ClusterIP   10.102.254.161   <none>        4000/TCP         35s
# 查看endpoint是否正常
[root@master]# kubectl describe service go-v3-service 
Name:              go-v3-service
Namespace:         default
Labels:            <none>
Annotations:       <none>
Selector:          app=go-v3
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.102.254.161
IPs:               10.102.254.161
Port:              gov3-port  4000/TCP
TargetPort:        4000/TCP
Endpoints:         10.244.1.54:4000,10.244.2.174:4000  # 可以看到该service正确选择了pod
Session Affinity:  None
Events:            <none>

创建ingress

#  go-v3-ingress.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: go-v3-ingress
spec:
  ingressClassName: nginx # use only with k8s version >= 1.18.0
  tls:
  rules:
  - host: www.52anime.cn
    http:
      paths:
      - path: /api
        pathType: Prefix
        backend:
          service:
            name: go-v3-service
            port:
              number: 4000
# 应用ingress
kubectl apply -f go-v3-ingress.yaml
# 查看ingress是否创建成功
[root@master ~]# kubectl get ingress
NAME               CLASS   HOSTS            ADDRESS   PORTS   AGE
go-v3-ingress      nginx   www.52anime.cn             80      70s

image.png