LoginSignup
1
0

More than 3 years have passed since last update.

Ansible AWX 検証用に Kubernetes で systemd と sshd が動作する CentOS7 をデプロイする。

Last updated at Posted at 2019-03-03

概要

Ansible AWX の検証用ホストに CentOS7 が欲しかったんや。
以下を満たすことができる CentOS7 の Docker image と、
kubernetes で動作させるナレッジがあればよかった。

  • systemd が動作すること
  • sshd が動作すること
  • yum install ができること

結論

そんなものはなかった。
作るしかない。

実行環境

# kubeadm version
kubeadm version: &version.Info{Major:"1", Minor:"13", GitVersion:"v1.13.4", GitCommit:"c27b913fddd1a6c480c229191a087698aa92f0b1", GitTreeState:"clean", BuildDate:"2019-02-28T13:35:32Z", GoVersion:"go1.11.5", Compiler:"gc", Platform:"linux/amd64"}

前提

  • kubernetes 構築済み
    ⇒ ベアメタル3台構成

  • Service で type: LoadBalancer が利用できるように metallb を設定済

  • LoadBalancerIP に ping が飛ぶようにルーティングはばっちり
    ⇒ それぞれの CentOS7 が別々の IP Address を持つようにするため

  • Docker image は自作
    ⇒ 以下にアップしています。
      https://cloud.docker.com/repository/docker/solaris64sparc/centos7

  • 残念セキュリティなので注意が必要

docker で実行する場合は?

# docker run --privileged --name centos7no1 -v /sys/fs/cgroup:/sys/fs/cgroup:ro -p 2222:22 -d solaris64sparc/centos7:systemd-sshd

手順

Namespace を作る

# kubectl create ns awx-target

Deployment を作成する

centos7no1-deployment.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: kompose convert
    kompose.version: 1.17.0 (a74acad)
  creationTimestamp: null
  labels:
    io.kompose.service: centos7no1
  name: centos7no1
  namespace: awx-target
spec:
  replicas: 1
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        io.kompose.service: centos7no1
    spec:
      containers:
      - image: solaris64sparc/centos7:systemd-sshd
        name: centos7no1
        ports:
        - containerPort: 22
        resources: {}
        securityContext:
          privileged: true ★ docker run --privileged に相当
        volumeMounts:
        - name: cgroup
          mountPath: /sys/fs/cgroup ★ -v /sys/fs/cgroup:/sys/fs/cgroup:ro に相当
          readOnly: true
      restartPolicy: Always
      volumes:
         - name: cgroup
           hostPath:
             path: /sys/fs/cgroup
             type: Directory

Service の作成する

centos7no1-service.yaml

apiVersion: v1
kind: Service
metadata:
  annotations:
    kompose.cmd: kompose convert
    kompose.version: 1.17.0 (a74acad)
  creationTimestamp: null
  labels:
    io.kompose.service: centos7no1
  name: centos7no1
  namespace: awx-target
spec:
  type: LoadBalancer
  loadBalancerIP: 172.16.13.201 ★ IP Address を固定する
  ports:
  - name: "22"
    port: 22
    targetPort: 22
  selector:
    io.kompose.service: centos7no1

deploy する

# ls centos7no1
centos7no1-deployment.yaml  centos7no1-service.yaml

# kubectl create -f centos7no1
deployment.extensions/centos7no1 created
service/centos7no1 created

動作確認

ssh で 172.16.13.201 にログインできるか確認します。
ユーザは次の通りです。

ユーザ パスワード
root P@ssw0rd!
user P@ssword!

使い方

Ansible AWX のホストに指定して playbook の対象にしてみたり、ssh でログインして設定変更してみたり。
reboot を実行すると初期化されるので便利です。

参考:Dockerfile

FROM centos:7
# change root password
RUN echo 'root:P@ssw0rd!' | chpasswd

# create new user
RUN useradd user
RUN echo 'user:P@ssw0rd!' | chpasswd
RUN echo 'user          ALL=(ALL)       ALL' >> /etc/sudoers

# yum update, install openssh-server and other
RUN yum -y update; yum clean all
RUN yum -y install openssh-server passwd sudo; yum clean all
RUN mkdir /var/run/sshd

# ssh-keygen
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''

EXPOSE 22

ENV container docker
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == \
systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/multi-user.target.wants/*;\
rm -f /etc/systemd/system/*.wants/*;\
rm -f /lib/systemd/system/local-fs.target.wants/*; \
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
rm -f /lib/systemd/system/basic.target.wants/*;\
rm -f /lib/systemd/system/anaconda.target.wants/*;
VOLUME [ "/sys/fs/cgroup" ]

RUN systemctl enable sshd

CMD ["/usr/sbin/init"]
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0