You’ve just set up your kubernetes cluster. Excellent, now you want to start deploying your specs…but they are on a repo on your local machine.
All good let’s setup your kubeconfig file so you can connect to your k8s api with kubectl
.
-
Log into your server
-
Create a service account spec:
kind: ServiceAccount
metadata:
name: admin-user
namespace: kube-system
-
Create the account
kubectl create -f server-account.yaml
-
Create the cluster role binding:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: admin-user
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: admin-user
namespace: kube-system
- Apply the role binding
kubectl apply -f admin-role-binding.yml
- Find the secrets used by the service account
kubectl describe serviceAccounts admin-user
Name: devacc
Namespace: default
Labels: <none>
Annotations: <none>
Image pull secrets: <none>
Mountable secrets: devacc-token-47p8n
Tokens: devacc-token-47p8n
Events: <none>
- Fetch the token
kubectl describe secrets devacc-token-47p8n
Keep the token
- Get the certificate info for the cluseter
kubectl config view --flatten --minify > cluster-cert.txt
cat cluster-cert.txt
Copy certificate-authority-data
and server
from the output.
- Now you can create your kubeconfig file
Create a file called my-service-account-config.yaml
and substitute the values for token
, certificate-authority-data
and server
apiVersion: v1
kind: Config
users:
- name: admin-user
user:
token: <replace this with token info>
clusters:
- cluster:
certificate-authority-data: <replace this with certificate-authority-data info>
server: <replace this with server info>
name: self-hosted-cluster
contexts:
- context:
cluster: self-hosted-cluster
user: devacc
name: devacc-context
current-context: devavv-context
-
Copy the file to
$HOME/.kube
-
Tell kubectl to use that context:
kubectl config –kubeconfig=$HOME/.kube/my-service-account-config.yaml set-context svcs-acct-context
It is better to append it to the base config