はじめに¶
大学とかでDockerやKubernetesでコンテナを起動していると429 Too Many RequestsとErrorかWarningが出てコンテナをPullできないときがあります.
解決方法¶
Docker¶
$ docker login
Kubernetes¶
手順 1. Docker login 1. config.jsonの確認 1. Secretの作成 1. PodのspecにSecretを参照するように記述
Docker login¶
Dockerと同様にdocker loginコマンドを実行します.
$ docker login
実行結果
$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: tororomeshi
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
Warningと出ていますね.
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
保存された場所が~/.docker/config.jsonになっていないですね.
dockerコマンドがsudoで起動されるように設定している環境だからだと思います.
なので,自分のディレクトリに移動させておきました..dockerディレクトリがない場合は作ってください.
$ sudo mv /root/.docker/config.json ~/.docker
$ ls -la
total 16
drwxrwx--- 2 c0118050 cdsl 4096 Jul 8 08:19 .
drwxr-xr-x 22 c0118050 cdsl 4096 Jul 8 08:09 ..
-rw------- 1 root root 107 Jul 8 08:18 config.json
-rwxrwxrwx 1 c0118050 cdsl 177 Mar 5 2021 config.json.old
実行例
$ sudo chown c0118050:cdsl config.json
$ chmod 777 config.json
config.jsonの確認¶
ここからはPull an Image from a Private RegistryURLを参考にSecretを作成します.
cat ~/.docker/config.json
実行結果
{
"auths": {
"https://index.docker.io/v1/": {
"auth": "dG9yb3JvbWVzaGk6UnNTNDV1NjhXQnBaciYv"
}
}
}
Secretの作成¶
config.jsonの絶対パスが必要なので確認します.
$ realpath ~/.docker/config.json
実行結果
/home/c0118050/.docker/config.json
先ほど確認した~/.docker/config.jsonをもとにSecretを作成します. --from-file=.dockerconfigjsonに絶対パスを代入してください.
kubectl create secret generic regcred \
--from-file=.dockerconfigjson=/home/c0118050/.docker/config.json \
--type=kubernetes.io/dockerconfigjson
実行結果
secret/regcred created
PodのspecにSecretを参照するように記述¶
そうしたら,Deployment,Replicaset,Pod,Deamonsetのspec:template:spec:に 以下の内容を記述します.
imagePullSecrets:
- name: regcred
記述例
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-hoge
labels:
app: mysql-hoge
spec:
replicas: 1
selector:
matchLabels:
app: mysql-hoge
template:
metadata:
labels:
app: mysql-hoge
spec:
containers:
- image: mysql:8.0.33-oracle
name: mysql-hoge
env:
- name: MYSQL_ROOT_PASSWORD
value: password
- name: MYSQL_DATABASE
value: _db
- name: MYSQL_USER
value: user
- name: MYSQL_PASSWORD
value: pass
ports:
- containerPort: 3306
name: mysql-hoge
volumeMounts:
- name: mysql-local-storage-hoge
mountPath: /var/lib/mysql
imagePullSecrets:
- name: regcred
volumes:
- name: mysql-local-storage-hoge
persistentVolumeClaim:
claimName: mysql-pvc-hoge
これで回避できます.