k8s配置ingress访问

一、说明

1.环境

k8s-master:192.168.1.152-254

k8s-work:192.168.1.155-157

ingress:nginx-ingress

二、应用部署

这里部署一个nginx程序

1.nginx.yaml文件

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
spec:
  selector:
    matchLabels:
      run: my-nginx
  replicas: 2
  template:
    metadata:
      labels:
        run: my-nginx
    spec:
      containers:
      - name: my-nginx
        image: nginx
        ports:
        - containerPort: 80

2.部署nginx应用程序

kubectl apply -f nginx.yaml

3.查看nginx部署结果

root@master-1-151:~# kubectl get Deployment
NAME       READY   UP-TO-DATE   AVAILABLE   AGE
my-nginx   2/2     2            2           47s

4.查看nginx的详细信息

root@master-1-151:~# kubectl describe Deployment my-nginx
Name:                   my-nginx
Namespace:              default
CreationTimestamp:      Fri, 29 Apr 2022 07:06:05 +0000
Labels:                 <none>
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               run=my-nginx
Replicas:               2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  run=my-nginx
  Containers:
   my-nginx:
    Image:        nginx
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   my-nginx-cf54cdbf7 (2/2 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  87s   deployment-controller  Scaled up replica set my-nginx-cf54cdbf7 to 2

5.查看nginx部署的pod信息

root@master-1-151:~# kubectl get pods -o wide
my-nginx-cf54cdbf7-7qzm9   1/1     Running   0               2m11s   10.244.5.14   work-1-156   <none>           <none>
my-nginx-cf54cdbf7-gs9bn   1/1     Running   0               2m11s   10.244.4.16   work-1-155   <none>   

三、配置服务

1.创建service-nginx.yaml文件

apiVersion: v1
kind: Service
metadata:
  name: service-nginx
  labels:
    run: service-nginx
spec:
  ports:
  - port: 80
    protocol: TCP
  selector:
    run: my-nginx      #这里关联的是Deployment文件中的.metadata.name: my-nginx

2.创建服务

root@master-1-151:~# kubectl apply -f service-nginx.yaml
service/service-nginx created

3.查看服务

root@master-1-151:~# kubectl get svc service-nginx
NAME            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
service-nginx   ClusterIP   10.96.212.204   <none>        80/TCP    6s

4.查看服务详细信息

root@master-1-151:~# kubectl describe svc service-nginx
Name:              service-nginx
Namespace:         default
Labels:            run=service-nginx
Annotations:       <none>
Selector:          run=my-nginx
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.96.212.204                   # 服务的ip地址
IPs:               10.96.212.204
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         10.244.4.16:80,10.244.5.14:80   # 这里即是关联Deployment部署的pods的ip和端口
Session Affinity:  None
Events:            <none>

5.测试服务是否可用

root@master-1-151:~# curl -I 10.96.212.204
HTTP/1.1 200 OK
Server: nginx/1.21.6
Date: Fri, 29 Apr 2022 07:18:47 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 25 Jan 2022 15:03:52 GMT
Connection: keep-alive
ETag: "61f01158-267"
Accept-Ranges: bytes

四、配置ingress

1.配置yaml文件

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: minimal-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx          # 这里要与部署的ingress匹配
  rules:
  - host: t.tok.fit                # 不配置,默认所有

  - http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: service-nginx
            port:
              number: 80

2.执行yaml文件

kubectl apply -f ingress.yaml 
ingress.networking.k8s.io/minimal-ingress created

3.查看执行结果

root@master-1-151:~# kubectl get ingress
NAME              CLASS   HOSTS       ADDRESS                                     PORTS   AGE
minimal-ingress   nginx   t.tok.fit   192.168.1.155,192.168.1.156,192.168.1.157   80      85m

五、验证

在浏览器访问http://192.168.1.155

访问http://t.tok.fit(需要绑定hosts,或者做dns解析)

这里返回的是404,这是因为nginx镜像中没有配置t.tok.fit

[root@test ~]# curl http://t.tok.fit
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx</center>
</body>
</html>

六、参考文档

Deployments

服务、负载和联网

服务

ingress

七、视频

Previous Post

k8s-配置ingress-nginx控制器

Next Post

工作基础之认真

Related Posts