Docker Private Registryの構築 on Kubernetes

# Docker Private Registryの構築 on Kubernetes
(2018/11時点)
## 環境
- OS: CentOS 7.4
- Kubernetes: v1.12
- Docker: docker-ce-18.06.1.ce
- ストレージ: NFS

## 手順
### 0. NFSの準備 (割愛)
### 1. NFS Clientのインストール (各ノード)
```bash:shell
# yum -y install nfs-utils
```
### 2. Persistent Volumeの追加
NFSをPersistent Volumeとして追加する。

```vim:pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv01
spec:
  capacity:
    storage: <capacity n[Gi]>
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: slow
  mountOptions:
    - hard
  nfs:
    server: <nfs server ip>
    path: <nfs shared directory>
```
```vim:pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: pvc01
  annotations:
    volume.kubernetes.io/storage-class: "nfs"
spec:
  accessModes:
    - ReadWriteOnce
  volumeMode: Filesystem
  resources:
    requests:
      storage: <capacity n[Gi]>
  storageClassName: slow
```
```bash:shell
# kubectl apply -f pv.yaml
# kubectl apply -f pvc.yaml
# kubectl get pv
NAME   CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM           STORAGECLASS   REASON   AGE
pv01   100Gi      RWO            Recycle          Bound    default/pvc01   slow                    21d
# kubectl get pvc
NAME    STATUS   VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
pvc01   Bound    pv01     100Gi      RWO            slow           21d
```
### 3. PodおよびServiceの作成
```vim:srv-pr-pod.yaml
kind: Service
apiVersion: v1
metadata:
  name: srv-pregistry
spec:
  selector:
    app: pregistry
  type: NodePort
  ports:
  - protocol: TCP
    port: 5000
    targetPort: 5000
    name: registry-http
---
kind: Pod
apiVersion: v1
metadata:
  name: pregistry
  labels:
    app: pregistry
spec:
  containers:
    - name: pregistry
      image: registry:2
      volumeMounts:
      - mountPath: "/var/lib/registry"
        name: pvc01-pnt
      ports:
      - containerPort: 5000
  volumes:
    - name: pvc01-pnt
      persistentVolumeClaim:
        claimName: pvc01
```
```bash:shell
# kubectl apply -f srv-pr-pod.yaml
# kubectl get pod
NAME                 READY   STATUS    RESTARTS   AGE
pregistry            1/1     Running   0          21d
# kubectl get service
NAME                     TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
srv-pregistry            NodePort    x.x.x.x          <none>        5000:32523/TCP   21d
```
Master IP:32523に接続するとdocker imageをpush/pullすることが可能となる。
### 4. Web Frontendの作成
konradkleine/docker-registry-frontendをDeployすることでブラウザからPrivate Registryの中身を確認することが可能となる。

```vim:srv-prf-pod.yaml
kind: Service
apiVersion: v1
metadata:
  name: srv-pregistry-frontend
spec:
  selector:
    app: pregistry-frontend
  type: NodePort
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
    name: registry-http-frontend
---
kind: Pod
apiVersion: v1
metadata:
  name: pregistry-frontend
  labels:
    app: pregistry-frontend
spec:
  containers:
    - name: pregistry-frontend
      image: konradkleine/docker-registry-frontend:v2
      ports:
      - containerPort: 80
      env:
      - name: ENV_DOCKER_REGISTRY_HOST
        value: <private registry pod ip>
      - name: ENV_DOCKER_REGISTRY_PORT
        value: "5000"
```
```bash:shell
# kubectl apply -f srv-prf-pod.yaml
```