connection to k8s cluster can be authenticated using the following options
- static text file with username and password
- static tokens
- certificates
- identity services like ldap & ad
- if cluster is set up using [[kubeadm]] then the above parameter needs to be specified in the definition file for [[kube api server]]
- to connect using curl use the following
curl -v -k https://cluster-address.k8s:6443/api/v1/pods -u "user1:password1"
- not recommended as it's not secure enough
- volume mounts can be used to share the file
- passwd using the parameter
--basic-auth-file=file.csvwhen [[kube api server]] is invoked or in the service config
- this works similiar to the above method. same parameter needs to specified. here instead of password specifiy token.
- to connect using curl use the following
curl -v -k https://cluster-address.k8s:6443/api/v1/pods --header "Authorization: Bearer cGFzc3dvcmQx"
- not recommended as it's not secure enough
systemkeyword must be be prefixed for the following kubernetes services as they are system services
[[certificates]]
Edit the kube-apiserver static pod configured by kubeadm to pass in the user details. The file is located at /etc/kubernetes/manifests/kube-apiserver.yaml
- apiVersion: v1
- kind: Pod
- metadata:
- name: kube-apiserver
- namespace: kube-system
- spec:
- containers:
-
- command:
-
- kube-apiserver
- image: k8s.gcr.io/kube-apiserver-amd64:v1.11.3
- name: kube-apiserver
- volumeMounts:
-
- mountPath: /tmp/users
- name: usr-details
- readOnly: true
- volumes:
-
- hostPath:
- path: /tmp/users
- type: DirectoryOrCreate
- name: usr-details
Modify the kube-apiserver startup options to include the basic-auth file
- apiVersion: v1
- kind: Pod
- metadata:
- creationTimestamp: null
- name: kube-apiserver
- namespace: kube-system
- spec:
- containers:
-
- command:
-
- kube-apiserver
-
- --authorization-mode=Node,RBAC
-
- --basic-auth-file=/tmp/users/user-details.csv
Create the necessary roles and role bindings for these users:
-
-
kind: Role
-
apiVersion: rbac.authorization.k8s.io/v1
-
metadata:
-
namespace: default
-
name: pod-reader
-
rules:
-
- apiGroups: [""] # "" indicates the core API group
-
resources: ["pods"]
-
verbs: ["get", "watch", "list"]
-
-
kind: RoleBinding
-
apiVersion: rbac.authorization.k8s.io/v1
-
metadata:
-
name: read-pods
-
namespace: default
-
subjects:
-
- kind: User
-
name: user1 # Name is case sensitive
-
apiGroup: rbac.authorization.k8s.io
-
roleRef:
-
kind: Role #this must be Role or ClusterRole
-
name: pod-reader # this must match the name of the Role or ClusterRole you wish to bind to
-
apiGroup: rbac.authorization.k8s.io
Once created, you may authenticate into the kube-api server using the users credentials
curl -v -k https://localhost:6443/api/v1/pods -u "user1:password123"