<p>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.</p>
<p>So I follow this <a href="https://www.youtube.com/watch?v=SJrvqQF6qA0&amp;list=LL&amp;index=1&amp;t=1117s" rel="nofollow noreferrer">youtube tutorial</a>. 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 (<a href="https://www.linode.com/docs/guides/create-a-self-signed-tls-certificate/" rel="nofollow noreferrer">https://www.linode.com/docs/guides/create-a-self-signed-tls-certificate/</a>).</p>
<p>Then I could generate MyCertTLS.crt and MyKeyTLS.key</p>
<pre><b>gayan@Gayan</b>:<b>/srv</b>$ cd certs
<b>gayan@Gayan</b>:<b>/srv/certs</b>$ ls
MyCertTLS.crt  MyKeyTLS.key
</pre>
<p>To create secret key for the kubernetes, I ran this command</p>
<p><code>sudo kubectl create secret tls my-tls --key=&quot;MyKeyTLS.key&quot; --cert=&quot;MyCertTLS.crt&quot; -n keycloak-test </code></p>
<p>But It's not working, I got this error,</p>
<pre><b>gayan@Gayan</b>:<b>/srv/certs</b>$ sudo kubectl create secret tls my-tls --key=&quot;MyKeyTLS.key&quot; --cert=&quot;MyCertTLS.crt&quot; -n keycloak-test
[sudo] password for gayan:                 
error: failed to create secret Post &quot;http://localhost:8080/api/v1/namespaces/keycloak-test/secrets?fieldManager=kubectl-create&amp;fieldValidation=Strict&quot;: dial tcp 127.0.0.1:8080: connect: connection refused
</pre>
<p><B>Note</B>: <br>
MiniKube is Running...<br>
And Ingress Addon also enabled...<br>
I have created a namespace called keycloak-test.</p>
<pre><b>gayan@Gayan</b>:<b>/srv/keycloak</b>$ 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
</pre>
<p>I am trying to fix this error. But I have no idea why I get this, looking for a solution from the genius community.</p>

Gayan Kodithuwakku
<p>I figured this out! I posted this, because this may helpful for someone.</p>
<p>I am getting that error,</p>
<pre><code>error: failed to create secret Post &quot;http://localhost:8080/api/v1/namespaces/keycloak-test/secrets?fieldManager=kubectl-create&amp;fieldValidation=Strict&quot;: dial tcp 127.0.0.1:8080: connect: connection refused
</code></pre>
<p>Because my kubernetes api-server is running on a different port.</p>
<p>You can view what port your kubernetes api-server is running by running this command,</p>
<pre><code>kubectl config view
</code></pre>
<p>Then for example, if you can see <code>server: localhost:40475</code> like that, It's mean your server running on <code>port 40475.</code></p>
<p>And kubernetes default port is <code>8443</code></p>
<p>Then you should mention the correct port on your kubectl command to create the secret.</p>
<p>So, I add <code>--server=https://localhost:40475</code> to my command.</p>
<pre><code>kubectl create secret tls my-tls --key=&quot;tls.key&quot; --cert=&quot;tls.crt&quot; -n keycloak-test --server=https://localhost:40475
</code></pre>
<p>And another thing, if you getting error like <code>permission denied</code></p>
<p>You have to change the ownership of your <code>tls.key</code> file and <code>tls.crt</code> file.</p>
<p>I did this by running these commands,</p>
<pre><code>sudo chmod 666 tls.crt

sudo chmod 666 tls.key
</code></pre>
<p>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.</p>
<p>So, by doing this way, I solved this issue!
Hope this will help to someone!!! Thanks!</p>
