OpenShift - 管理

在本章中,我们将介绍如何管理节点,配置服务帐户等主题.

主节点和节点配置

在OpenShift中,我们需要使用start命令和OC来启动新服务器.在启动新的master时,我们需要使用master和start命令,而在启动新节点时,我们需要使用该节点和start命令.为此,我们需要为主服务器和节点创建配置文件.我们可以使用以下命令为主节点和节点创建基本配置文件.

对于主配置文件

$ openshift start master --write-config =/openshift.local.config/master


对于节点配置文件

$ oadm create-node-config --node-dir =/openshift.local.config/node-<node_hostname> --node =< node_hostname> --hostnames =< hostname>,< ip_address>


运行以下命令后,我们将获得可用作配置起点的基本配置文件.稍后,我们可以使用相同的文件来启动新服务器.

apiLevels:
- v1beta3
- v1
apiVersion: v1
assetConfig:
   logoutURL: ""
   masterPublicURL: https://172.10.12.1:7449
   publicURL: https://172.10.2.2:7449/console/
      servingInfo:
         bindAddress: 0.0.0.0:7449
         certFile: master.server.crt
         clientCA: ""
keyFile: master.server.key
   maxRequestsInFlight: 0
   requestTimeoutSeconds: 0
controllers: '*'
corsAllowedOrigins:
- 172.10.2.2:7449
- 127.0.0.1
- localhost
dnsConfig:
   bindAddress: 0.0.0.0:53
etcdClientInfo:
   ca: ca.crt
   certFile: master.etcd-client.crt
   keyFile: master.etcd-client.key
   urls:
   - https://10.0.2.15:4001
etcdConfig:
   address: 10.0.2.15:4001
   peerAddress: 10.0.2.15:7001
   peerServingInfo:
      bindAddress: 0.0.0.0:7001
      certFile: etcd.server.crt
      clientCA: ca.crt
      keyFile: etcd.server.key
   servingInfo:
      bindAddress: 0.0.0.0:4001
      certFile: etcd.server.crt
      clientCA: ca.crt
      keyFile: etcd.server.key
   storageDirectory: /root/openshift.local.etcd
etcdStorageConfig:
   kubernetesStoragePrefix: kubernetes.io
   kubernetesStorageVersion: v1
   openShiftStoragePrefix: openshift.io
   openShiftStorageVersion: v1
imageConfig:
   format: openshift/origin-${component}:${version}
   latest: false
kind: MasterConfig
kubeletClientInfo:
   ca: ca.crt
   certFile: master.kubelet-client.crt
   keyFile: master.kubelet-client.key
   port: 10250
kubernetesMasterConfig:
   apiLevels:
   - v1beta3
   - v1
   apiServerArguments: null
   controllerArguments: null
   masterCount: 1
   masterIP: 10.0.2.15
   podEvictionTimeout: 5m
   schedulerConfigFile: ""
   servicesNodePortRange: 30000-32767
   servicesSubnet: 172.30.0.0/16
   staticNodeNames: []
masterClients:
   externalKubernetesKubeConfig: ""
   openshiftLoopbackKubeConfig: openshift-master.kubeconfig
masterPublicURL: https://172.10.2.2:7449
networkConfig:
   clusterNetworkCIDR: 10.1.0.0/16
   hostSubnetLength: 8
   networkPluginName: ""
   serviceNetworkCIDR: 172.30.0.0/16
oauthConfig:
   assetPublicURL: https://172.10.2.2:7449/console/
   grantConfig:
      method: auto
   identityProviders:
   - challenge: true
   login: true
   name: anypassword
   provider:
      apiVersion: v1
      kind: AllowAllPasswordIdentityProvider
   masterPublicURL: https://172.10.2.2:7449/
   masterURL: https://172.10.2.2:7449/
   sessionConfig:
      sessionMaxAgeSeconds: 300
      sessionName: ssn
      sessionSecretsFile: ""
   tokenConfig:
      accessTokenMaxAgeSeconds: 86400
      authorizeTokenMaxAgeSeconds: 300
policyConfig:
   bootstrapPolicyFile: policy.json
   openshiftInfrastructureNamespace: openshift-infra
   openshiftSharedResourcesNamespace: openshift
projectConfig:
   defaultNodeSelector: ""
   projectRequestMessage: ""
   projectRequestTemplate: ""
   securityAllocator:
      mcsAllocatorRange: s0:/2
      mcsLabelsPerProject: 5
      uidAllocatorRange: 1000000000-1999999999/10000
routingConfig:
   subdomain: router.default.svc.cluster.local
serviceAccountConfig:
   managedNames:
   - default
   - builder
   - deployer
   
masterCA: ca.crt
   privateKeyFile: serviceaccounts.private.key
   privateKeyFile: serviceaccounts.private.key
   publicKeyFiles:
   - serviceaccounts.public.key
servingInfo:
   bindAddress: 0.0.0.0:8443
   certFile: master.server.crt
   clientCA: ca.crt
   keyFile: master.server.key
   maxRequestsInFlight: 0
   requestTimeoutSeconds: 3600


节点配置文件

allowDisabledDocker: true
apiVersion: v1
dnsDomain: cluster.local
dnsIP: 172.10.2.2
dockerConfig:
   execHandlerName: native
imageConfig:
   format: openshift/origin-${component}:${version}
   latest: false
kind: NodeConfig
masterKubeConfig: node.kubeconfig
networkConfig:
   mtu: 1450
   networkPluginName: ""
nodeIP: ""
nodeName: node1.example.com

podManifestConfig:
   path: "/path/to/pod-manifest-file"
   fileCheckIntervalSeconds: 30
servingInfo:
   bindAddress: 0.0.0.0:10250
   certFile: server.crt
   clientCA: node-client-ca.crt
   keyFile: server.key
volumeDirectory: /root/openshift.local.volumes


Th是节点配置文件的样子.一旦我们有了这些配置文件,我们就可以运行以下命令来创建主服务器和节点服务器.

$ openshift start --master-config = /openshift.local.config/master/master-
config.yaml --node-config = /openshift.local.config/node-<node_hostname>/node-
config.yaml


管理节点

在OpenShift中,我们有OC命令行实用程序,主要用于执行所有操作OpenShift.我们可以使用以下命令来管理节点.

用于列出节点

$ oc get nodes
NAME                             LABELS
node1.example.com     kubernetes.io/hostname = vklnld1446.int.example.com
node2.example.com     kubernetes.io/hostname = vklnld1447.int.example.com


描述节点的详细信息

$ oc describe node <node name>


删除节点

$ oc delete node <node name>


在节点上列出pod

$ oadm manage-node <node1> <node2> --list-pods [--pod-selector=<pod_selector>] [-o json|yaml]


评估节点上的pod

$ oadm manage-node <node1> <node2> --evacuate --dry-run [--pod-selector=<pod_selector>]


配置认证

在OpenShift master中,有一个内置的OAuth服务器,可用于管理身份验证.所有OpenShift用户都从该服务器获取令牌,这有助于他们与OpenShift API进行通信.

OpenShift中有不同类型的身份验证级别,可以与主配置文件一起配置.

  • 全部允许

  • 全部拒绝

  • HTPasswd

  • LDAP

  • 基本身份验证

  • 请求标头

在定义主配置时,我们可以定义标识策略,我们可以在其中定义我们希望使用的策略类型.

允许全部

全部允许

oauthConfig:
   ...
   identityProviders:
   - name: Allow_Authontication
      challenge: true
      login: true
      provider:
         apiVersion: v1
         kind: AllowAllPasswordIdentityProvider


全部拒绝

这将拒绝访问所有用户名和密码.

oauthConfig:
   ...
   identityProviders:
   - name: deny_Authontication
      challenge: true
      login: true
      provider:
         apiVersion: v1
         kind: DenyAllPasswordIdentityProvider


HTPasswd

HTPasswd用于根据加密文件密码验证用户名和密码.

为了生成加密文件,以下是命令.

 $ htpasswd </path/to/users.htpasswd> <user_name>


使用加密文件.

oauthConfig:
   ...
   identityProviders:
   - name: htpasswd_authontication
      challenge: true
      login: true
      provider:
         apiVersion: v1
         kind: HTPasswdPasswordIdentityProvider
         file: /path/to/users.htpasswd


LDAP身份提供商

这是使用的用于LDAP身份验证,其中LDAP服务器在身份验证中起关键作用.

oauthConfig:
   ...
   identityProviders:
   - name: "ldap_authontication"
      challenge: true
      login: true
      provider:
         apiVersion: v1
         kind: LDAPPasswordIdentityProvider
         attributes:
            id:
            - dn
            email:
            - mail
            name:
            - cn
            preferredUsername:
            - uid
         bindDN: ""
         bindPassword: ""
         ca: my-ldap-ca-bundle.crt
         insecure: false
         url: "ldap://ldap.example.com/ou=users,dc=acme,dc=com?uid"


基本身份验证

在对服务器到服务器身份验证进行用户名和密码验证时使用.身份验证在基本URL中受到保护,并以JSON格式显示.

 ServiceAccountConfig:
oauthConfig:
   ...
   identityProviders:
   - name: my_remote_basic_auth_provider
      challenge: true
      login: true
      provider:
         apiVersion: v1
         kind: BasicAuthPasswordIdentityProvider
         url: https://www.vklnld908.int.example.com/remote-idp
         ca: /path/to/ca.file
         certFile: /path/to/client.crt
         keyFile: /path/to/client.key


配置服务帐户

服务帐户提供了一种访问OpenShift的灵活方式API公开用于身份验证的用户名和密码.

启用服务帐户

服务帐户使用密钥对公钥和私钥进行身份验证.使用私钥对API进行身份验证并根据公钥进行验证.

ServiceAccountConfig:
   ...
   masterCA: ca.crt
   privateKeyFile: serviceaccounts.private.key
   publicKeyFiles:
   - serviceaccounts.public.key
   - ...


创建服务帐户

使用以下命令创建服务帐户

$ Openshift cli create service account <name of server account>


使用HTTP代理

在大多数生产环境中,直接访问Internet受到限制.它们要么不暴露于Internet,要么通过HTTP或HTTPS代理公开.在OpenShift环境中,此代理机器定义被设置为环境变量.

这可以通过在位于/etc/下的主文件和节点文件上添加代理定义来完成. SYSCONFIG 的.这与我们对任何其他应用程序类似.

Master Machine

/etc/sysconfig/openshift-master

HTTP_PROXY=http://USERNAME:PASSWORD@172.10.10.1:8080/
HTTPS_PROXY=https://USERNAME:PASSWORD@172.10.10.1:8080/
NO_PROXY=master.vklnld908.int.example.com


节点机器

/etc/sysconfig/openshift-node

HTTP_PROXY=http://USERNAME:PASSWORD@172.10.10.1:8080/
HTTPS_PROXY=https://USERNAME:PASSWORD@172.10.10.1:8080/
NO_PROXY=master.vklnld908.int.example.com


完成后,我们需要重新启动主机和节点机.

对于Docker Pull

/etc/sysconfig/docker

HTTP_PROXY = http://USERNAME:PASSWORD@172.10.10.1:8080/
HTTPS_PROXY = https://USERNAME:PASSWORD@172.10.10.1:8080/
NO_PROXY = master.vklnld1446.int.example.com


为了让pod在代理环境中运行,可以使用 :

containers:
- env:
   - name: "HTTP_PROXY"
      value: "http://USER:PASSWORD@:10.0.1.1:8080"


OC环境命令可用于更新现有环境.

使用NFS的OpenShift存储

在OpenShift中,持久卷和持久性的概念卷声明形成持久存储.这是创建第一个持久卷的关键概念之一,后来声明了相同的卷.为此,我们需要在底层硬件上有足够的容量和磁盘空间.

apiVersion: v1
kind: PersistentVolume
metadata:
   name: storage-unit1
spec:
   capacity:
      storage: 10Gi
   accessModes:
   - ReadWriteOnce
   nfs:
      path: /opt
      server: 10.12.2.2
   persistentVolumeReclaimPolicy: Recycle


接下来,使用OC create命令创建持久性卷.

$ oc create -f Storage-claim1.yaml
persistentvolume " Storage-clame1 " created


声明创建的音量.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
   name: Storage-clame1
spec:
   accessModes:
      - ReadWriteOnce
   resources:
      requests:
         storage: 5Gi


创建声明.

$ oc create -f Storage-claim1.yaml
persistentvolume " Storage-clame1 " created


用户和角色管理

用户和角色管理用于管理用户,他们对不同项目的访问和控制.

创建用户

预定义的模板可用于在OpenShift中创建新用户.

kind: "Template"
apiVersion: "v1"
parameters:
   - name: vipin
   required: true
objects:
   - kind: "User"
   apiVersion: "v1"
   metadata:
   name: "${email}"
   
- kind: "Identity"
   apiVersion: "v1"
   metadata:
      name: "vipin:${email}"
   providerName: "SAML"
   providerUserName: "${email}"
- kind: "UserIdentityMapping"
apiVersion: "v1"
identity:
   name: "vipin:${email}"
user:
   name: "${email}"


使用oc create -f<文件名>创建用户.

$ oc create –f vipin.yaml


使用以下命令删除OpenShift中的用户.

$ oc delete user <user name>


限制用户访问

ResourceQuotas和LimitRanges用于限制用户访问级别.它们用于限制群集上的pod和容器.

apiVersion: v1
kind: ResourceQuota
metadata:
   name: resources-utilization
spec:
   hard:
      pods: "10"


创建引用使用上面的配置

$ oc create -f resource-quota.yaml –n –Openshift-sample


描述资源报价

$ oc describe quota resource-quota  -n  Openshift-sample
Name:              resource-quota
Namespace:                              Openshift-sample
Resource           Used                  Hard
--------           ----                  ----
pods                3                    10


定义容器限制可用于限制部署容器将使用的资源.它们用于定义某些对象的最大和最小限制.

用户项目限制

这基本上用于用户的项目数量可以在任何时间点.它们基本上是通过以青铜,银和金的类别定义用户级别来完成的.

我们需要首先定义一个对象,该对象保存有多少项目的价值铜,银,和金类可以有.这些需要在master-confif.yaml文件中完成.

admissionConfig:
   pluginConfig:
      ProjectRequestLimit:
         configuration:
            apiVersion: v1
            kind: ProjectRequestLimitConfig
            limits:
            - selector:
               level: platinum
            - selector:
               level: gold
            maxProjects: 15
            - selector:
               level: silver
            maxProjects: 10
            - selector:
               level: bronze
            maxProjects: 5


重新启动主服务器.

将用户分配到特定级别.

$ oc label user vipin level = gold


将用户移出标签,如果必需.

$ oc label user <user_name> level-


向用户添加角色.

$ oadm policy add-role-to-user


从用户中删除角色.

$ oadm policy remove -role-from-user


向用户添加群集角色.

$ oadm policy add-cluster-role-to-user


从用户删除群集角色.

$ oadm policy remove-cluster-role-from-user


将角色添加到群组.

$ oadm policy add -role-to-user


从组中删除角色.

$ oadm policy remove -cluster-role-from-user


将群集角色添加到群组.

$ oadm policy add-cluster-role-to-group


从组中删除群集角色.

$ oadm policy remove-cluster-role-from-group <role> <groupname>


群集管理用户

这是用户具有管理权限的最强大的角色之一从创建到删除集群的完整集群.

$ oadm policy add-role-to-user admin <user_name> -n <project_name>


具有终极能力的用户

$ oadm policy add-cluster-role-to-user cluster-admin <user_name>