minikube addons enable ingress

kubectl apply -f whoami.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: whoami
spec:
  selector:
    matchLabels:
      run: whoami
  replicas: 2
  template:
    metadata:
      labels:
        run: whoami
    spec:
      containers:
      - name: whoami
        image: containous/whoami
        ports:
        - containerPort: 80
apiVersion: v1
kind: Service
metadata:
  name: whoami
  labels:
    run: whoami
spec:
  ports:
  - port: 80
    protocol: TCP
  selector:
    run: whoami
    

kubectl apply -f svc-whoami.yml

apiVersion: v1
kind: Service
metadata:
  name: whoami
  labels:
    run: whoami
spec:
  selector:
    run: whoami
  type: LoadBalancer
  externalIPs:
   - 172.17.0.83 
   # 可以是本机IP,也可以是公网IP
  ports:
   - port: 8081
     targetPort: 80
     # nodePort: <31514>

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: local-ingress
spec:
  rules:
  - host: localhost
    http:
      paths:
      - path: /
        backend:
          serviceName: whoami
          servicePort: 8081
$ kubectl get service
NAME         TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes   ClusterIP      10.96.0.1       <none>        443/TCP          47m
my-service   LoadBalancer   10.111.68.206   <pending>     8080:30723/TCP   31m
whoami       LoadBalancer   10.107.1.64     172.17.0.83   8081:30164/TCP   4m48s
$ curl 172.17.0.83:8081
Hostname: whoami-cd4db6b57-st27h
IP: 127.0.0.1
IP: 172.18.0.7
RemoteAddr: 172.18.0.1:22336
GET / HTTP/1.1
Host: 172.17.0.83:8081
User-Agent: curl/7.58.0
Accept: */*

minikube ip

kubectl get pods -l run=whoami -o yaml | grep podIP
kubectl get service
kubectl describe pods


https://stackoverflow.com/questions/49219171/expose-service-on-local-kubernetes


apiVersion: v1
kind: Service
metadata:
  name: apaches
spec:
  type: NodePort
  ports:
    - port: 80
      nodePort: 30000
  selector:
    app: apache

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: apache-ingress
spec:
  rules:
  - host: localhost
    http:
      paths:
      - path: /
        backend:
          serviceName: apaches
          servicePort: 80



Logo

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

更多推荐