前言

如同Linux操作系统安装完成后,管理员需为应用创建不同的用户,那么,K8S/OKD/Openshift集群同样也需如此,而在OKD/Openshift集群里,我们可集成OpenLDAP目录系统,方法如下所示。

OpenLDAP安装

本文使用helm安装openldap,首先将chars下载下来以方便查看:

git clone https://github.com/helm/charts

可选。镜像可先推送到私有仓库(PS:测试发现latest镜像有问题):

docker pull osixia/openldap:1.2.1
docker tag docker.io/osixia/openldap:1.2.1 okd-lr.zyl.io:5001/osixia/openldap:1.2.1
docker push okd-lr.zyl.io:5001/osixia/openldap:1.2.1

镜像以root用户运行(gosudo切换),赋权:

oc new-project auth-openshift
oc adm policy add-scc-to-user anyuid -z default

openldap char参数做定制:

cd charts/stable/openldap
cp values.yaml values_cs.yaml
vi values_cs.yaml
...

env:
  # LDAP将创建dc=zyl,dc=io域,组织名称为Zyl Inc.
  LDAP_ORGANISATION: "Zyl Inc."
  LDAP_DOMAIN: "zyl.io"
...

# Ldap域管理员(cn=admin,dc=zyl,dc=io)及config管理员(cn=admin,cn=config)密码
adminPassword: admin
configPassword: config

# 持久化存储,本例使用已创建好的glusterfs存储系统,其支持动态提供。
persistence:
  enabled: true
  storageClass: "glusterfs-app"
  accessMode: ReadWriteOnce
  size: 8Gi

执行helm命令安装:

helm install --name openldap -f values_cs.yaml .

Ldap启动后,创建了域dc=zyl,dc=iohdb管理员账户cn=admin,dc=zyl,dc=io。如下所示,在此域下创建用户与组信息:

% oc rsh deploy/openldap
% cat > users.ldif <<EOF
dn: ou=People,dc=zyl,dc=io
ou: People
objectClass: top
objectClass: organizationalUnit

dn: ou=Group,dc=zyl,dc=io
ou: Group
objectClass: top
objectClass: organizationalUnit

dn: uid=zyl,ou=People,dc=zyl,dc=io
uid: zyl
cn: zyl
objectClass: account
objectClass: posixAccount
objectClass: top
objectClass: shadowAccount
userPassword: changeme
loginShell: /bin/bash
uidNumber: 5000
gidNumber: 5000
homeDirectory: /home/zyl

dn: uid=admin,ou=People,dc=zyl,dc=io
uid: admin
cn: admin
objectClass: account
objectClass: posixAccount
objectClass: top
objectClass: shadowAccount
userPassword: changeme
loginShell: /bin/bash
uidNumber: 5001
gidNumber: 5001
homeDirectory: /home/admin

dn: cn=zyl,ou=Group,dc=zyl,dc=io
cn: zyl
objectClass: top
objectClass: posixGroup
gidNumber: 5000
memberUid: zyl

dn: cn=admin,ou=Group,dc=zyl,dc=io
cn: admin
objectClass: top
objectClass: posixGroup
gidNumber: 5001
memberUid: admin

dn: cn=openshift_user,ou=Group,dc=zyl,dc=io
cn: openshift_user
objectClass: top
objectClass: posixGroup
gidNumber: 6000
memberUid: zyl

dn: cn=openshift_admin,ou=Group,dc=zyl,dc=io
cn: openshift_admin
objectClass: top
objectClass: posixGroup
gidNumber: 6001
memberUid: admin
EOF
% ldapadd -x -w $LDAP_ADMIN_PASSWORD -D "cn=admin,dc=zyl,dc=io" -H ldapi:/// -f users.ldif
% ldapsearch -x -D "cn=admin,dc=zyl,dc=io" -w $LDAP_ADMIN_PASSWORD \
             -b dc=zyl,dc=io
# 可使用config管理员检查ldap config配置
% ldapsearch -x -D "cn=admin,cn=config" -w $LDAP_CONFIG_PASSWORD \
             -b cn=config "olcDatabase=config"

配置Master使用Ldap认证

OKD初始安装时若未配置openshift_master_identity_providers,则OKD默认使用如下认证,此认证方式允许任何用户登录集群。

% vi /etc/origin/master/master-config.yaml 
...
oauthConfig:
...
  identityProviders:
  - challenge: true
    login: true
    mappingMethod: claim
    name: allow_all
    provider:
      apiVersion: v1
      kind: AllowAllPasswordIdentityProvider
...

将所有Master配置的如下段删除:

  - challenge: true
    login: true
    mappingMethod: claim
    name: allow_all
    provider:
      apiVersion: v1
      kind: AllowAllPasswordIdentityProvider

替换为如下段:

  - challenge: true
    login: true
    mappingMethod: claim
    name: ldap_auth
    provider:
      apiVersion: v1
      attributes:
        email:
        - mail
        id:
        - dn
        name:
        - cn
        preferredUsername:
        - uid
      bindDN: cn=admin,dc=zyl,dc=io
      bindPassword: admin
      insecure: true
      kind: LDAPPasswordIdentityProvider
      url: ldap://openldap.auth-openshift.svc.cluster.local./ou=People,dc=zyl,dc=io?uid

注意:若启用TLS,即insecure: false,则需提供OpenLDAP的证书,如添加ca: my-ldap-ca.crt,而后将证书拷贝到Master上:/etc/origin/master/my-ldap-ca.crt

Ansible配置文件中的OSEv3.yaml加入以下段,避免升级时被还原回去。

##### Auth
openshift_master_identity_providers:
- name: ldap_auth
  challenge: true
  login: true
  kind: LDAPPasswordIdentityProvider
  bindDN: cn=admin,dc=zyl,dc=io
  bindPassword: admin
  url: ldap://openldap.auth-openshift.svc.cluster.local./ou=People,dc=zyl,dc=io?uid
  attributes:
    id: ['dn']
    email: ['mail']
    name: ['cn']
    preferredUsername: ['uid']
  insecure: true

而后分别重启Master节点:

master-restart api
master-restart controllers
oc get pod -n kube-system  
master-logs api api                   # 查看日志
master-logs controllers controllers

同步LDAP组信息到OKD

创建如下文件:

cat > rfc2307_config_user_defined.yaml <<EOF
---
kind: LDAPSyncConfig
apiVersion: v1
bindDN: cn=admin,dc=zyl,dc=io
bindPassword: admin
insecure: true
url: ldap://openldap.auth-openshift.svc.cluster.local
groupUIDNameMapping:
  "cn=openshift_admin,ou=Group,dc=zyl,dc=cn": openshift_admin
  "cn=openshift_user,ou=Group,dc=zyl,dc=cn": openshift_user
rfc2307:
    groupsQuery:
        baseDN: "ou=Group,dc=zyl,dc=io"
        scope: sub
        derefAliases: never
        filter: (objectClass=posixGroup)
    groupUIDAttribute: dn
    groupNameAttributes: [ cn ]
    groupMembershipAttributes: [ memberUid ] 
    usersQuery:
        baseDN: "ou=People,dc=zyl,dc=io"
        scope: sub
        derefAliases: never
        filter: (objectClass=posixAccount)
    userUIDAttribute: uid
    userNameAttributes: [ cn ]
EOF

执行如下命令同步:

% oc adm groups sync --sync-config=rfc2307_config_user_defined.yaml --confirm
group/zyl
group/admin
group/openshift_user
group/openshift_admin

openshift_admin作为管理员组、openshift_user为普通用户组,赋权:

oc adm policy add-cluster-role-to-group cluster-admin openshift_admin
oc adm policy add-cluster-role-to-group basic-user openshift_user

登录用户:

oc login -uadmin -pchangeme

用户登录后,OKD会生成自己的用户与LDAP对应:

% oc get groups
NAME              USERS
admin             admin
openshift_admin   admin
openshift_user    zyl
zyl               zyl                

% oc get users
NAME      UID                                    FULL NAME   IDENTITIES
admin     3c4ae0bf-338c-11e9-b2f8-52540042814f   admin       ldap_auth:uid=admin,ou=People,dc=zyl,dc=io

% oc get identities
NAME                                         IDP NAME    IDP USER NAME                      USER NAME   USER UID
ldap_auth:uid=admin,ou=People,dc=zyl,dc=io   ldap_auth   uid=admin,ou=People,dc=zyl,dc=io   admin       3c4ae0bf-338c-11e9-b2f8-52540042814f

参考文档


我是读书人
114 声望136 粉丝

随意记录,想着啥,写啥