在看尚硅谷的k8s视频中,学到ingress代理的时候,由于之前按照视频安装了V1.15.1,后面环境又出了问题,重新安装了

16.1的,为这次失败埋下了伏笔。

教案中的yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx-dm
spec:
  replicas: 2
  template:
    metadata:
      labels:
        name: nginx
    spec:
      containers:
        - name: nginx
          image: wangyanglinux/myapp:v1
          imagePullPolicy: IfNotPresent
          ports:
          - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
spec:
  ports:
    - port: 80
      targetPort: 80
      protocol: TCP
  selector:
    name: nginx
后面会贴出我修改的
创建后发现pod是没有问题的,但svc创建成功,却不能访问
[root@k8s-master mnt]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP   4d23h
nginx-svc    ClusterIP   10.102.185.217   <none>        80/TCP    14m
[root@k8s-master mnt]# curl 10.102.185.217
curl: (7) Failed connect to 10.102.185.217:80; 拒绝连接
[root@k8s-master mnt]# kubectl get pod
NAME                     READY   STATUS    RESTARTS   AGE
nginx-64ddb75745-28lfx   1/1     Running   0          52s
nginx-64ddb75745-mmmxx   1/1     Running   0          52s
[root@k8s-master mnt]# kubectl get pod -o wide
NAME                     READY   STATUS    RESTARTS   AGE   IP            NODE         NOMINATED NODE   READINESS GATES
nginx-64ddb75745-28lfx   1/1     Running   0          58s   10.244.1.29   k8s-node02   <none>           <none>
nginx-64ddb75745-mmmxx   1/1     Running   0          58s   10.244.2.32   k8s-node01   <none>           <none>
[root@k8s-master mnt]# curl 10.244.1.29
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@k8s-master mnt]# curl 10.244.2.32
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@k8s-master mnt]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.17.0.1:31615 rr
  -> 10.244.2.31:80               Masq    1      0          0
TCP  172.17.0.1:32044 rr
  -> 10.244.2.31:443              Masq    1      0          0
TCP  192.168.122.1:31615 rr
  -> 10.244.2.31:80               Masq    1      0          0
TCP  192.168.122.1:32044 rr
  -> 10.244.2.31:443              Masq    1      0          0
TCP  192.168.180.130:31615 rr
  -> 10.244.2.31:80               Masq    1      0          0
TCP  192.168.180.130:32044 rr
  -> 10.244.2.31:443              Masq    1      0          0
TCP  10.96.0.1:443 rr
  -> 192.168.180.130:6443         Masq    1      3          0
TCP  10.96.0.10:53 rr
  -> 10.244.0.8:53                Masq    1      0          0
  -> 10.244.0.9:53                Masq    1      0          0
TCP  10.96.0.10:9153 rr
  -> 10.244.0.8:9153              Masq    1      0          0
  -> 10.244.0.9:9153              Masq    1      0          0
TCP  10.100.186.209:80 rr
  -> 10.244.2.31:80               Masq    1      0          0
TCP  10.100.186.209:443 rr
  -> 10.244.2.31:443              Masq    1      0          0
TCP  10.102.185.217:80 rr
TCP  10.244.0.0:31615 rr
  -> 10.244.2.31:80               Masq    1      0          0
TCP  10.244.0.0:32044 rr
  -> 10.244.2.31:443              Masq    1      0          0
TCP  10.244.0.1:31615 rr
  -> 10.244.2.31:80               Masq    1      0          0
TCP  10.244.0.1:32044 rr
  -> 10.244.2.31:443              Masq    1      0          0
TCP  127.0.0.1:31615 rr
  -> 10.244.2.31:80               Masq    1      0          0
TCP  127.0.0.1:32044 rr
  -> 10.244.2.31:443              Masq    1      0          0
UDP  10.96.0.10:53 rr
  -> 10.244.0.8:53                Masq    1      0          0
  -> 10.244.0.9:53                Masq    1      0          0
10.102.185.217:80 rr,并没有指向一个3xxxx端口
最后才发现是service里面的标签有问题,应该是版本的问题,因为视频中15.1就没有问题。
[root@k8s-master mnt]# cat ingress-http.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: wangyanglinux/myapp:v1
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 80
[root@k8s-master mnt]#
[root@k8s-master mnt]# cat ingress-svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
  namespace: default
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
    protocol: TCP
  selector:
    app: nginx

[root@k8s-master mnt]#
[root@k8s-master mnt]# vim ingress-svc.yaml
[root@k8s-master mnt]# kubectl create -f ingress-svc.yaml
service/nginx-svc created
[root@k8s-master mnt]# kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        4d23h
nginx-svc    NodePort    10.111.72.240   <none>        80:31041/TCP   4s
[root@k8s-master mnt]# curl 10.111.72.240
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>

所以在学习k8s的过程中,最好注意一下所用的版本

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐