I am new to Kubernetes. I just trying to create a tls secret using kubectl. My ultimate goal is deploy a keycloak cluster in kubernetes.
So I follow this youtube tutorial. But in this tutorial doesn’t mention how to generate my own tls key and tls cert. So to do that I use this documentation (https://www.linode.com/docs/guides/create-a-self-signed-tls-certificate/).
Then I could generate MyCertTLS.crt and MyKeyTLS.key
gayan@Gayan:/srv$ cd certs gayan@Gayan:/srv/certs$ ls MyCertTLS.crt MyKeyTLS.key
To create secret key for the kubernetes, I ran this command
sudo kubectl create secret tls my-tls --key="MyKeyTLS.key" --cert="MyCertTLS.crt" -n keycloak-test
But It’s not working, I got this error,
gayan@Gayan:/srv/certs$ sudo kubectl create secret tls my-tls --key="MyKeyTLS.key" --cert="MyCertTLS.crt" -n keycloak-test [sudo] password for gayan: error: failed to create secret Post "http://localhost:8080/api/v1/namespaces/keycloak-test/secrets?fieldManager=kubectl-create&fieldValidation=Strict": dial tcp 127.0.0.1:8080: connect: connection refused
Note:
MiniKube is Running…
And Ingress Addon also enabled…
I have created a namespace called keycloak-test.
gayan@Gayan:/srv/keycloak$ kubectl get namespaces NAME STATUS AGE default Active 3d19h ingress-nginx Active 119m keycloak-test Active 4m12s kube-node-lease Active 3d19h kube-public Active 3d19h kube-system Active 3d19h kubernetes-dashboard Active 3d19h
I am trying to fix this error. But I have no idea why I get this, looking for a solution from the genius community.
2
Answers
I figured this out! I posted this, because this may helpful for someone.
I am getting that error,
Because my kubernetes api-server is running on a different port.
You can view what port your kubernetes api-server is running by running this command,
Then for example, if you can see
server: localhost:40475
like that, It's mean your server running onport 40475.
And kubernetes default port is
8443
Then you should mention the correct port on your kubectl command to create the secret.
So, I add
--server=https://localhost:40475
to my command.And another thing, if you getting error like
permission denied
You have to change the ownership of your
tls.key
file andtls.crt
file.I did this by running these commands,
Then you should run above kubectl command, without sudo! It works !!!!!! If you run that command with sudo, It will ask username and passwords and it confused me and it did not work.
So, by doing this way, I solved this issue! Hope this will help to someone!!! Thanks!
In your examples,
kubectl get namespaces
works, butsudo kubectl create secret
doesn’t.You don’t need
sudo
to work with Kubernetes. In particular, the connection information is stored in a$HOME/.kube/config
file by default, but when yousudo kubectl ...
, that changes the home directory and you can’t find the connection information.The standard Kubernetes assumption is that the cluster is remote, and so your local user ID doesn’t really matter to it. All that does matter is the Kubernetes-specific permissions assigned to the user that’s accessing the cluster.