11. Security Configurations Inside Kubernetes

11.1. Namespace

Most of the components provided in this project need to know the namespace. For Kubernetes (1.3+), the namespace is made available to the pod as part of the service account secret and is automatically detected by the client. For earlier versions, it needs to be specified as an environment variable to the pod. A quick way to do this is as follows:spring-doc.cn

      env:
      - name: "KUBERNETES_NAMESPACE"
        valueFrom:
          fieldRef:
            fieldPath: "metadata.namespace"

11.2. Service Account

For distributions of Kubernetes that support more fine-grained role-based access within the cluster, you need to make sure a pod that runs with spring-cloud-kubernetes has access to the Kubernetes API. For any service accounts you assign to a deployment or pod, you need to make sure they have the correct roles.spring-doc.cn

Depending on the requirements, you’ll need get, list and watch permission on the following resources:spring-doc.cn

Table 4. Kubernetes Resource Permissions
Dependency Resources

spring-cloud-starter-kubernetes-fabric8spring-doc.cn

pods, services, endpointsspring-doc.cn

spring-cloud-starter-kubernetes-fabric8-configspring-doc.cn

configmaps, secretsspring-doc.cn

spring-cloud-starter-kubernetes-clientspring-doc.cn

pods, services, endpointsspring-doc.cn

spring-cloud-starter-kubernetes-client-configspring-doc.cn

configmaps, secretsspring-doc.cn

For development purposes, you can add cluster-reader permissions to your default service account. On a production system you’ll likely want to provide more granular permissions.spring-doc.cn

The following Role and RoleBinding are an example for namespaced permissions for the default account:spring-doc.cn

kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  namespace: YOUR-NAME-SPACE
  name: namespace-reader
rules:
  - apiGroups: [""]
    resources: ["configmaps", "pods", "services", "endpoints", "secrets"]
    verbs: ["get", "list", "watch"]

---

kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: namespace-reader-binding
  namespace: YOUR-NAME-SPACE
subjects:
- kind: ServiceAccount
  name: default
  apiGroup: ""
roleRef:
  kind: Role
  name: namespace-reader
  apiGroup: ""