kubernetes實戰(十一):k8s使用openLDAP統一認證

一、基本概念php

  爲了方便管理和集成jenkins,k8s、harbor、jenkins均使用openLDAP統一認證。html

 

二、部署openLDAPpython

  此處將openLDAP部署在k8s上,openLDAP能夠在集羣以外存在,不必定非要在k8s上部署openLDAP。linux

  根據以前的文檔,openLDAP使用GFS進行數據持久化。nginx

  下載對應的openLDAP文件git

git clone https://github.com/dotbalo/k8s.git
cd k8s/openldap

  建立openLDAPgithub

[root@k8s-master01 openldap]# kubectl apply -f .
deployment.extensions/ldap created
persistentvolumeclaim/openldap-data created
secret/ldap-secret created
service/ldap-service created
deployment.extensions/phpldapadmin created
service/phpldapadmin created

  此處參考的是:https://github.com/osixia/docker-openldap,更新DN能夠更改environment下的yaml文件,默認的example.orgdocker

  建立ldap-ui-ingressapi

[root@k8s-master01 traefik]# kubectl create -f traefik-ldap.yaml 
ingress.extensions/ldap-ui created
[root@k8s-master01 traefik]# cat traefik-ldap.yaml 
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ldap-ui
  namespace: public-service
  annotations:
    kubernetes.io/ingress.class: traefik
spec:
  rules:
  - host: ldap.xxx.net
    http:
      paths:
      - backend:
          serviceName: phpldapadmin
          servicePort: 8080

 

三、查看驗證app

[root@k8s-master01 openldap]# kubectl get po,svc,pvc -n public-service | grep ldap
pod/ldap-944645448-pqj8b            1/1       Running   0          4m
pod/phpldapadmin-6c99c76cb6-knmpf   1/1       Running   0          4m

service/glusterfs-dynamic-openldap-data   ClusterIP   10.111.198.83    <none>        1/TCP             3m
service/ldap-service                      ClusterIP   10.98.153.44     <none>        389/TCP,636/TCP   4m
service/phpldapadmin                      ClusterIP   10.109.141.207   <none>        8080/TCP          4m

persistentvolumeclaim/openldap-data                              Bound     pvc-f251128b-ec17-11e8-8a89-000c293ad492   1Gi        RWX            gluster-heketi                4m

  訪問phpldapadmin:ldap.xxx.net

  默認DN:cn=admin,dc=example,dc=org,默認Password:admin(線上系統需自定義修改)

  登陸成功以下:

 

四、添加用戶和組

  建立Groups和People OU

 

  相同方式建立Groups

  建立組和用戶

  相同方式建立測試組,devops組

  建立用戶

  填寫基本信息,選擇組和Login Shell

  注意修改Common Name

 

 

  爲每一個用戶添加Email,沒有Email沒法登錄gitlab

 

五、配置k8s使用ldap登陸

  PS:因爲當時提交代碼時忘記提交k8s-ldap目錄,致使此目錄已丟失。如下操做能夠直接直接修改dotbalo/k8s-nginx-ldap鏡像裏面的nginx配置文件,而後自行建立deployment文件便可。

  修改openldap/k8s-ldap的nginx配置,server爲k8s dashboard的地址

  製做鏡像

[root@k8s-master01 k8s-ldap]# docker build -t dotbalo/k8s-nginx-ldap .
Sending build context to Docker daemon 22.02 kB
Step 1/10 : FROM python:2-alpine
 ---> f901fc789b69
Step 2/10 : COPY nginx-ldap-auth-daemon.py /usr/src/app/
 ---> Using cache
 ---> 2c09e174dc92
Step 3/10 : WORKDIR /usr/src/app/
 ---> Using cache
 ---> 80b2344aff04
Step 4/10 : RUN apk --no-cache add openldap-dev &&     apk --no-cache add --virtual build-dependencies build-base &&     pip install python-ldap &&     apk del build-dependencies
 ---> Using cache
 ---> 562bd91f40e4
Step 5/10 : ENV NGINX_VERSION 1.14.1
 ---> Using cache
 ---> ebeed109cb40
Step 6/10 : RUN set -x  && mkdir -p       /tmp/src/nginx       /usr/lib/nginx/modules       /var/cache/nginx  && apk add --no-cache --virtual .build-deps       curl       gcc       gd-dev       geoip-dev       gnupg       libc-dev       libxslt-dev       linux-headers       make       openldap-dev       pcre-dev       tar       unzip       zlib-dev  && curl -fsSL http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz | tar vxz --strip=1 -C /tmp/src/nginx  && curl -fsSL https://github.com/kvspb/nginx-auth-ldap/archive/master.zip -o /tmp/nginx-auth-ldap-master.zip  && unzip -d /tmp/src /tmp/nginx-auth-ldap-master.zip  && cd /tmp/src/nginx  && addgroup -S nginx  && adduser -D -S -h /var/cache/nginx -s /sbin/nologin -G nginx nginx  && ./configure     --prefix=/usr/share/nginx     --sbin-path=/usr/sbin/nginx     --modules-path=/usr/lib/nginx/modules     --conf-path=/etc/nginx/nginx.conf     --error-log-path=/var/log/nginx/error.log     --http-log-path=/var/log/nginx/access.log     --pid-path=/var/run/nginx.pid     --lock-path=/var/run/nginx.lock     --http-client-body-temp-path=/var/cache/nginx/client_temp     --http-proxy-temp-path=/var/cache/nginx/proxy_temp     --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp     --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp     --http-scgi-temp-path=/var/cache/nginx/scgi_temp     --user=nginx     --group=nginx     --with-http_ssl_module     --with-http_realip_module     --with-http_addition_module     --with-http_sub_module     --with-http_dav_module     --with-http_flv_module     --with-http_mp4_module     --with-http_gunzip_module     --with-http_gzip_static_module     --with-http_random_index_module     --with-http_secure_link_module     --with-http_stub_status_module     --with-http_auth_request_module     --with-http_xslt_module=dynamic     --with-http_image_filter_module=dynamic     --with-http_geoip_module=dynamic     --with-threads     --with-stream     --with-stream_ssl_module     --with-stream_ssl_preread_module     --with-stream_realip_module     --with-stream_geoip_module=dynamic     --with-http_slice_module     --with-mail     --with-mail_ssl_module     --with-compat     --with-file-aio     --with-http_v2_module     --add-module=/tmp/src/nginx-auth-ldap-master  && make -j$(getconf _NPROCESSORS_ONLN)  && make install  && mkdir -vp       /etc/nginx/conf.d/       /usr/share/nginx/html/  && install -m644 html/index.html /usr/share/nginx/html/  && install -m644 html/50x.html /usr/share/nginx/html/  && ln -sf /dev/stdout /var/log/nginx/access.log  && ln -sf /dev/stderr /var/log/nginx/error.log  && apk add --no-cache --virtual .gettext gettext  && mv /usr/bin/envsubst /tmp/  && runDeps="$(     scanelf --needed --nobanner /usr/sbin/nginx /usr/lib/nginx/modules/*.so /tmp/envsubst       | awk '{ gsub(/,/, "\nso:", $2); print "so:" $2 }'       | sort -u       | xargs -r apk info --installed       | sort -u     )"  && apk add --no-cache --virtual .nginx-rundeps $runDeps  && apk del .build-deps  && apk del .gettext  && rm -fr     /etc/nginx/*.default     /tmp/*     /var/tmp/*     /var/cache/apk/*
 ---> Using cache
 ---> 3283f6c81d18
Step 7/10 : COPY start.sh /bin/
 ---> Using cache
 ---> c2204e898bb8
Step 8/10 : COPY nginx.conf /etc/nginx/
 ---> d99a904b384d
Removing intermediate container 5f086340c1e5
Step 9/10 : EXPOSE 8888
 ---> Running in 1b52b272eeda
 ---> 72825fcd8ea5
Removing intermediate container 1b52b272eeda
Step 10/10 : EXPOSE 8081
 ---> Running in fdce8ba84145
 ---> f4f3866e01b1
Removing intermediate container fdce8ba84145
Successfully built f4f3866e01b1

  修改k8s-ldap的ldap-deployment.yaml鏡像名稱

[root@k8s-master01 k8s-ldap]# kubectl create -f ldap-deployment.yaml
[root@k8s-master01 k8s-ldap]# kubectl get po,svc -n public-service | grep k8s-nginx

pod/k8s-nginx-ldap-5c5bf9595d-8pqw2   1/1       Running   0          4m
service/k8s-nginx-ldap                    NodePort    10.100.70.192   <none>        8081:31486/TCP    14m
[root@k8s-master01 k8s-ldap]#

  訪問:IP:NODEPORT

  輸入剛纔建立的帳號密碼,注意此時是沒有權限的

  官網的解釋以下

  貌似全部的請求頭都被清空,都變成了system:serviceaccount:kube-system:kubernetes-dashboard這個用戶

 

六、受權訪問

  在k8s安裝後,會自動建立一個有全局讀權限的clusterrole:view,將此權限受權給system:serviceaccount:kube-system:kubernetes-dashboard用戶

[root@k8s-master01 k8s-ldap]# kubectl create -f k8s-ldap-user-crb.yaml 
clusterrolebinding.rbac.authorization.k8s.io/k8s-ldap-user created

  建立完成後刷新dashboard,能夠看到有了只讀權限(secret和role沒有權限)

 

七、總結

  1) 與容器雲相比,容器雲的權限控制更爲完善,但是實現基於用戶的驗證,能夠區分openLDAP中不一樣的用戶不一樣的權限,而k8s被統一成了kubernetes-dashboard這個用戶,也有多是本身沒有配置成功,後期須要再次確認。

  2) 能夠限制openLDAP中的用戶只訪問某些namespace,須要自行定義權限。

  3) 公司沒有須要無需讓非ops員工訪問k8s。

  4) k8s使用openLDAP登陸非必須。

  5) 網上也有大神使用schema登陸的:https://icicimov.github.io/blog/virtualization/Kubernetes-LDAP-Authentication/,看了一下不是本身須要的。

參考:

    https://github.com/osixia/

    https://icicimov.github.io/blog/virtualization/Kubernetes-LDAP-Authentication/

    https://github.com/nginxinc/nginx-ldap-auth.git

 

贊助做者:

  

相關文章
相關標籤/搜索