#chiroito ’s blog

Java を中心とした趣味の技術について

Htpasswd ファイルを使って OpenShift にユーザを追加する

最初にユーザを作る時はパスワードファイル(passwordfile)も同時に作成します。

$ htpasswd -c -B -b passwordfile user1 MyPassword!

2人目移行のユーザを作る時はパスワードファイルの作成は不要なので、-cオプションを外します。

$ htpasswd -B -b passwordfile user2 MyPassword!

oauth.yaml

apiVersion: config.openshift.io/v1
kind: OAuth
metadata:
  name: cluster
spec:
  identityProviders:
  - name: Local Password
    mappingMethod: claim
    type: HTPasswd
    htpasswd:
      fileData:
        name: htpasswd

secretを作成し、アイデンティティプロバイダをクラスタへ追加することでユーザの認証が行える様にします。

$ oc create secret generic htpasswd --from-file=passwordfile -n openshift-config
$ oc apply -f oauth.yaml

OpenShift にユーザが作られるには最初にログインしたタイミングのようです。試しにログインしてみましょう

$ oc login -u user1 -p MyPassword!
$ oc whoami
user1
$ oc login -u user2 -p MyPassword!
$ oc whoami
user2

ユーザを確認してみます

$ oc get users
NAME    UID                                    FULL NAME   IDENTITIES
user1   567540df-306a-4e0b-9b12-4f34c5a906c5               Local Password:user1
user2   1cfb4f52-f1b2-48f0-adc3-9096006c60bf               Local Password:user2

user1 に管理者権限を付与してみます。

oc adm policy add-role-to-user cluster-admin user1

参考情報

AWS 上に構築した OpenShift に Infra ノードを追加する

コンテナイメージレジストリやルータなどインフラを構成する要素のみをホストするようなマシンをインフラノードと言います。今回はこの様なノードを AWS 上で構築した OpenShift 4.3 に追加します。

インフラノードとワーカーノードの違いは以下の要素を含んでいるかどうかです。

spec:
  template:
    metadata:
      labels:
        machine.openshift.io/cluster-api-machine-role: infra 
        machine.openshift.io/cluster-api-machine-type: infra 
    spec:
      metadata:
        labels:
          node-role.kubernetes.io/infra: "" 

MachineSet を作成

以下の Yaml はインフラノードとなる MachineSet のサンプルです。

chiroito-cluster と言う名前のクラスタに us-east-2a-infra と言う名前の MachineSet を追加します。

AWS特有の情報は以下で作ります。

  • リージョンは us-east-2
  • アベイラビリティゾーンは us-east-2a
  • インスタンスタイプは m5.large
  • セキュリティグループは chiroito-worker-infra-sg

machineset.yaml

apiVersion: machine.openshift.io/v1beta1
kind: MachineSet
metadata:
  labels:
    machine.openshift.io/cluster-api-cluster: chiroito-cluster 
  name: us-east-2a-infra 
  namespace: openshift-machine-api
spec:
  replicas: 2
  selector:
    matchLabels:
      machine.openshift.io/cluster-api-cluster: chiroito-cluster 
      machine.openshift.io/cluster-api-machineset: us-east-2a-infra 
  template:
    metadata:
      labels:
        machine.openshift.io/cluster-api-cluster: chiroito-cluster 
        machine.openshift.io/cluster-api-machine-role: infra 
        machine.openshift.io/cluster-api-machine-type: infra 
        machine.openshift.io/cluster-api-machineset: us-east-2a-infra 
    spec:
      metadata:
        labels:
          node-role.kubernetes.io/infra: "" 
      providerSpec:
        value:
          ami:
            id: ami-0d8f77b753c0d96dd
          apiVersion: awsproviderconfig.openshift.io/v1beta1
          blockDevices:
            - ebs:
                iops: 0
                volumeSize: 120
                volumeType: gp2
          credentialsSecret:
            name: aws-cloud-credentials
          deviceIndex: 0
          iamInstanceProfile:
            id: chiroito-cluster-worker-profile 
          instanceType: m5.large
          kind: AWSMachineProviderConfig
          placement:
            availabilityZone: us-east-2a
            region: us-east-2
          securityGroups:
            - filters:
                - name: tag:Name
                  values:
                    - chiroito-cluster-worker-sg 
          subnet:
            filters:
              - name: tag:Name
                values:
                  - chiroito-cluster-private-us-east-2a 
          tags:
            - name: kubernetes.io/cluster/chiroito-cluster 
              value: owned
          userDataSecret:
            name: worker-user-data

この Yaml を使って MachineSet を作ります。

$ oc create -f machineset.yaml

確認

MachineSet が作成されたかを確認します。

$ oc get machinesets -n openshift-machine-api
NAME                DESIRED   CURRENT   READY   AVAILABLE   AGE
us-east-2a-infra    2         2         2       2           20h
us-east-2a-worker   2         2         2       2           21h

MachineSet によって Machine が作成されたかを確認します。

oc get machines -n openshift-machine-api
NAME                        PHASE     TYPE        REGION      ZONE         AGE
chiroito-cluster-master-0   Running   m5.xlarge   us-east-2   us-east-2a   21h
chiroito-cluster-master-1   Running   m5.xlarge   us-east-2   us-east-2b   21h
chiroito-cluster-master-2   Running   m5.xlarge   us-east-2   us-east-2c   21h
us-east-2a-infra-5kbl6      Running   m5.large    us-east-2   us-east-2a   20h
us-east-2a-infra-wvpx4      Running   m5.large    us-east-2   us-east-2a   20h
us-east-2a-worker-7gp74     Running   m5.large    us-east-2   us-east-2c   20h
us-east-2a-worker-tss5j     Running   m5.large    us-east-2   us-east-2c   20h

ついでにノードも確認しましょう。きちんと infra,worker となっていれば OK です。

oc get nodes -n openshift-machine-api
NAME                                         STATUS   ROLES          AGE   VERSION
ip-10-0-129-22.us-east-2.compute.internal    Ready    infra,worker   20h   v1.16.2
ip-10-0-132-220.us-east-2.compute.internal   Ready    master         21h   v1.16.2
ip-10-0-141-130.us-east-2.compute.internal   Ready    infra,worker   20h   v1.16.2
ip-10-0-145-141.us-east-2.compute.internal   Ready    master         21h   v1.16.2
ip-10-0-165-220.us-east-2.compute.internal   Ready    master         21h   v1.16.2
ip-10-0-166-143.us-east-2.compute.internal   Ready    worker         20h   v1.16.2
ip-10-0-169-180.us-east-2.compute.internal   Ready    worker         20h   v1.16.2

参考資料

AWS 上に IPI で OpenShift を構築したら削除処理が止まった

AWS も OpenShift も超初心者なので OpenShift を壊してしまったため IPI で再構築しようとopenshift-install destroy clusterで削除してたら止まってしまった。 openshift-install --log-level debug destroy clusterで再実行したら以下の様なログが出た。どうやら AWS の Security Group に依存関係があるため消せなくなったようだ。

DEBUG search for and delete matching resources by tag in us-east-2 matching aws.Filter{"kubernetes.io/cluster/cluster-9373-dlhk2":"owned"}
DEBUG DependencyViolation: The dhcpOptions 'dopt-085c2dada1f343ae4' has dependencies and cannot be deleted.
        status code: 400, request id: 45d53455-d1d7-41ce-ad6f-66934e8eca4b  arn="arn:aws:ec2:us-east-2:434612646751:dhcp-options/dopt-085c2dada1f343ae4"
DEBUG DependencyViolation: resource sg-0a444d2ee4b09f1a1 has a dependent object
        status code: 400, request id: b7665590-49ae-4179-98f8-1ffc1f268c5e  arn="arn:aws:ec2:us-east-2:434612646751:security-group/sg-0a444d2ee4b09f1a1"
DEBUG Skipping default security group               arn="arn:aws:ec2:us-east-2:434612646751:vpc/vpc-004bba1a4101c1ff0" id=vpc-004bba1a4101c1ff0 security group=sg-009a1a514280a6428
DEBUG deleting EC2 security group sg-0a444d2ee4b09f1a1: DependencyViolation: resource sg-0a444d2ee4b09f1a1 has a dependent object
        status code: 400, request id: e09c5452-30cc-40a5-a250-68206a0c0500  arn="arn:aws:ec2:us-east-2:434612646751:vpc/vpc-004bba1a4101c1ff0"
DEBUG search for and delete matching resources by tag in us-east-2 matching aws.Filter{"openshiftClusterID":"2ec853c4-fdb5-4048-b121-4b2cdb884949"}
DEBUG search for and delete matching resources by tag in us-east-1 matching aws.Filter{"kubernetes.io/cluster/cluster-9373-dlhk2":"owned"}
DEBUG NoSuchHostedZone: No hosted zone found with ID: Z05082143QIXIT5Y0HD82
        status code: 404, request id: 775594c5-092d-41a4-bac6-d610faad2aa1  arn="arn:aws:route53:::hostedzone/Z05082143QIXIT5Y0HD82"
DEBUG search for and delete matching resources by tag in us-east-1 matching aws.Filter{"openshiftClusterID":"2ec853c4-fdb5-4048-b121-4b2cdb884949"}

Webコンソールにはアクセスできないので、AWS CLI を使って対応していきます。 消せない Security Group は sg-0a444d2ee4b09f1a1 です。これを探してみると、default の Security Group である sg-009a1a514280a6428 に追加されているようです。

$ aws ec2 describe-security-groups
{
    "SecurityGroups": [
        {
            "Description": "default VPC security group",
            "GroupName": "default",
            "IpPermissions": [
                {
                    "IpProtocol": "-1",
                    "IpRanges": [],
                    "Ipv6Ranges": [],
                    "PrefixListIds": [],
                    "UserIdGroupPairs": [
                        {
                            "GroupId": "sg-009a1a514280a6428",
                            "UserId": "434612646751"
                        },
                        {
                            "GroupId": "sg-0a444d2ee4b09f1a1",
                            "UserId": "434612646751"
                        }
                    ]
                }
            ],
(略)

Security Group の ingress 情報を削除するのは aws ec2 revoke-security-group-ingress らしい。以下の様にして取り除きます。

aws ec2 revoke-security-group-ingress --group-id sg-009a1a514280a6428 --ip-permissions '[{"IpProtocol":"-1","UserIdGroupPairs":[{"GroupId":"sg-0a444d2ee4b09f1a1","UserId":"434612646751"}]}]'

確認してみます。

$ aws ec2 describe-security-groups
{
    "SecurityGroups": [
        {
            "Description": "default VPC security group",
            "GroupName": "default",
            "IpPermissions": [
                {
                    "IpProtocol": "-1",
                    "IpRanges": [],
                    "Ipv6Ranges": [],
                    "PrefixListIds": [],
                    "UserIdGroupPairs": [
                        {
                            "GroupId": "sg-021e9474e3577bea9",
                            "UserId": "434612646751"
                        }
                    ]
                }
            ],

消えた!これでopenshift-install destroy clusterで削除できるようになりました。