I have created pypiserver in kubernetes cluster, I have used https://hub.docker.com/r/pypiserver/pypiserver docker image. I need to create basic auth for the server which I created. I used this method https://kubernetes.github.io/ingress-nginx/examples/auth/basic/
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: pypiserver
labels:
app: pypiserver
annotations:
kubernetes.io/ingress.class: nginx
kubernetes.io/tls-acme: 'true'
ingress.kubernetes.io/auth-type: basic
ingress.kubernetes.io/auth-secret: secret
ingress.kubernetes.io/auth-realm: "Authentication Required - ok"
spec:
rules:
- host: example.com
http:
paths:
- path: /
backend:
serviceName: pypiservice
servicePort: 8080
tls:
- hosts:
- example.com
secretName: secret-tls
But my host name would be "www.example.com/8080" and I don't see ingress has any pod in kubernetes cluster. Ingress is running fine but I don't get auth for this host. (And also I have http://IP adress:8080 which I converted to domain through cloudflare)
Please let me know what am I doing wrong?
I don't know exactly what is your nginx ingress controller version, but I can share what worked for me. I've reproduced it on my GKE cluster.
I installed my nginx ingress controller following this guide. Basically it came down to running the following commands:
I'm using 1.13 version on my GKE so this tip is also applied in my case:
But I dealt with it quite differently. Basically you need your
Nodesto havekubernetes.io/os=linuxlabel so you can simply label them. Following command will do the job:Then we're heading to Provider Specific Steps which in case of GKE came down to applying the following
yaml:Then you may want to verify your installation:
or simply run:
It will also tell you if all the required resorces are properly deployed.
Next we need to write our ingress (ingress object/resource) containing
basic-authrelatedannotations. I was following same tutorial as mentioned in your question.First we need to create our
authfile containingusernameand hashedpassword:Once we have it, we need to create a
Secretobject which then we'll use in our ingress:Once it is created we can check if everything went well:
Alright, so far so good...
Then we need to create our
ingress resource/object.My
ingress-with-auth.yamlfile looks slightly different than the one in the instruction, namely I just addedkubernetes.io/ingress.class: nginxto make sure my nginx ingress controller is used rather than built-in GKE solution:In your example you may need to add
nginxprefix in yourbasic-authrelated annotations:so it looks like this:
First I used the address listed in my ingress resource (it doesn't appear there any more once I added
kubernetes.io/ingress.class: nginxannotation in myingressdefinition:When I tried to access
pypi-serverusing this IP it brought me directly to the page without a need of any authentication. But it looks like if you didn't define proper ingress class, the default is used instead so in practice youringressdefinition withauth-basicdetails isn't taken into consideration and isn't passed to the nginx ingress controller we installed in one of the previous steps.So what IP address should be used to access your app ? Run the following command which will show you both
CLUSTER-IP(can be accessed within your cluster from anyPodorNode) andEXTERNAL-IPof your nginx ingress controller:You can basically host many different websites in your cluster and all of them will be available through this IP. All of them can be available on default
http 80port (orhttps 443in your case). The only difference between them will be thehostnamethat you pass inhttpheader of yourhttp request.Since I don't have a domain pointing to this external IP address and can't simply access my website by going to
http://foo.bar.comI need to pass somehow thehostnameI'm requesting from35.111.112.113address. It can be done in a few ways:I installed in my Google Chrome browser ModHeader extension which allows me to modify my http request headers and set the
hostnameI'm requestig to any value I want.You can do it also using
curlas follows:You should be prompted for authentication.
If you don't provide
-u username:passwordflag you should get401 Authorization Required.Basically hat's all.
Let me know if it helped you. Don't hesitate to ask additional questions if something isn't completely clear.
One more thing. If something still doesn't work you may start from attaching to your nginx ingress controller
Pod(check yourPodname first by runningkubectl get pods -n ingress-nginx):and checking the content of your
/etc/nginx/nginx.conffile. Look forfoo.bar.com(or in your caseexample.com). It should contain similar lines:Then check if the file is present in the indicated location
/etc/ingress-controller/auth/default-ingress-with-auth.passwd.One note to your
Servicedefinition. The fact thatpypiservercontainer exposes specifically port 8080 doesn't mean that you need to use this port when accessing it via ingress. InServicedefinition the port exposed by theContaineris calledtargetPort. You need to specify it when defining yourServicebutServiceitself can expose completely different port. I defined myServiceusing following command:Note that the
typeshould be set toNodePortorLoadBalancer. Then in your ingress definition you don't have to use8080but80which is the port exposed by yourpypiserverService. Note that there isservicePort: 80in myingress object/resourcedefinition. Yourexample.comdomain in cloudflare should point with it'sA recordto yournginx ingress controllerLoadBalancerServiceIP (kubectl get svc -n ingress-nginx) without specifying any ports.