[A-00236] Kubernetes 1000本ノック (1)
k8s学習のため、1000本ノックシリーズをやりたいと思います。
とりあえずはnginx使って超初歩的なk8s構成を作りたいと思います。
apiVersion: v1
kind: ConfigMap
metadata:
labels:
app: nginx-app
name: nginx
name: nginx-html
data:
index.html: |
<html>
<head>
<title>
Kubernetes Hello World
</title>
</head>
<body>
<h1>
Kubernetes Hello World
</h1>
<h2>
Kubernetes Nginx ConfigMap
</h2>
</body>
</html>
apiVersion: v1
kind: Service
metadata:
labels:
app: nginx-app
name: nginx
name: nginx-service
spec:
selector:
app: nginx-app
name: nginx
type: NodePort
ports:
- name: nginx-port
port: 80
protocol: TCP
targetPort: nginx-port
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx-app
name: nginx
name: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx-app
name: nginx
template:
metadata:
labels:
app: nginx-app
name: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
name: nginx-port
protocol: TCP
volumeMounts:
- name: nginx-html-file
mountPath: /usr/share/nginx/html/index.html
subPath: index.html
readOnly: true
volumes:
- name: nginx-html-file
configMap:
name: nginx-html
items:
- key: "index.html"
path: "index.html"
上記をapplyする
kubectl apply -f nginx.yml,nginx-configmap.yml
下記のコマンドでlocalhostのポート番号を確認
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 187d
nginx-service NodePort 10.99.243.88 <none> 80:31336/TCP 7s
ブラウザでアクセスして表示されればOK

また、他のConfigmapサンプルとSecretサンプルも作りました。
apiVersion: v1
kind: ConfigMap
metadata:
name: configmap-sample
data:
nginx.config: |
pid /var/run/nginx.pid;
events {
worker_connections 3;
}
http {
server {
root /usr/share/nginx/html;
}
}
nginx.html: |
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body>
<h1>Nginx Home Page for ConfigMap!</h1>
<p>This page is nginx page for sample of ConfigMap.</p>
<p>My Profile</p>
<p><a href="https://github.com/tamago0224">My Github</a>
<p><a href="https://twitter.com/tamago_0224">My Twitter</a>
</body>
</html>
---
apiVersion: v1
kind: Pod
metadata:
name: configmap-sample-pod
labels:
app: configmap-sample-pod
spec:
containers:
- name: nginx-pod
image: nginx:1.23
volumeMounts:
- name: nginx-config
mountPath: "/etc/nginx"
readOnly: true
- name: nginx-data
mountPath: "/usr/share/nginx/html"
readOnly: true
volumes:
- name: nginx-config
configMap:
name: configmap-sample
items:
- key: "nginx.config"
path: "nginx.conf"
- name: nginx-data
configMap:
name: configmap-sample
items:
- key: "nginx.html"
path: "index.html"
---
apiVersion: v1
kind: Service
metadata:
name: configmap-service-nginx
spec:
selector:
app: configmap-sample-pod
ports:
- protocol: TCP
port: 8080
targetPort: 80
apiVersion: v1
kind: Secret
metadata:
name: secret-sample
data:
nginx.config: cGlkIC92YXIvcnVuL25naW54LnBpZDsKCmV2ZW50cyB7CiAgICB3b3JrZXJfY29ubmVjdGlvbnMgMzsKfQoKaHR0cCB7CiAgICBzZXJ2ZXIgewogICAgICAgIHJvb3QgL3Vzci9zaGFyZS9uZ2lueC9odG1sOwogICAgfQp9Cg==
nginx.html: PCFET0NUWVBFIGh0bWw+CjxodG1sPgogICAgPGhlYWQ+CiAgICAgICAgPHRpdGxlPldlbGNvbWUgdG8gbmdpbnghPC90aXRsZT4KICAgIDwvaGVhZD4KICAgIDxib2R5PgogICAgICAgIDxoMT5OZ2lueCBIb21lIFBhZ2UgZm9yIENvbmZpZ01hcCE8L2gxPgogICAgICAgIDxwPlRoaXMgcGFnZSBpcyBuZ2lueCBwYWdlIGZvciBzYW1wbGUgb2YgQ29uZmlnTWFwLjwvcD4KICAgICAgICA8cD5NeSBQcm9maWxlPC9wPgogICAgICAgIDxwPjxhIGhyZWY9Imh0dHBzOi8vZ2l0aHViLmNvbS90YW1hZ28wMjI0Ij5NeSBHaXRodWI8L2E+PC9wPgogICAgICAgIDxwPjxhIGhyZWY9Imh0dHBzOi8vdHdpdHRlci5jb20vdGFtYWdvXzAyMjQiPk15IFR3aXR0ZXI8L2E+PC9wPgogICAgPC9ib2R5Pgo8L2h0bWw+Cg==
---
apiVersion: v1
kind: Pod
metadata:
name: secret-sample-pod
labels:
app: secret-sample-pod
spec:
containers:
- name: nginx-pod
image: nginx:latest
volumeMounts:
- name: nginx-config
mountPath: "/etc/nginx"
readOnly: true
- name: nginx-data
mountPath: "/usr/share/nginx/html"
readOnly: true
volumes:
- name: nginx-config
secret:
secretName: secret-sample
items:
- key: nginx.config
path: nginx.conf
- name: nginx-data
secret:
secretName: secret-sample
items:
- key: nginx.html
path: index.html
---
apiVersion: v1
kind: Service
metadata:
name: secret-service-nginx
spec:
selector:
app: secret-sample-pod
ports:
- protocol: TCP
port: 8080
targetPort: 80
上記2つはコンテナ内に入ってcurlしないとみれませんので注意してください。
exec -it secret-sample-pod -- /bin/sh
下記は別のconfigmapのサンプルです。
apiVersion: v1
kind: ConfigMap
metadata:
name: test-params
data:
john.name: John.Lucas
john.age: twenty-fifth
john.country: Detroit
john.friend.name: Carla.Mcdonald
john.friend.age: eighteen
john.friend.country: NewYork
apiVersion: v1
kind: Pod
metadata:
name: test
spec:
containers:
- name: test
image: nginx:alpine
env:
- name: john.name
valueFrom:
configMapKeyRef:
name: test-params
key: john.name
- name: john.age
valueFrom:
configMapKeyRef:
name: test-params
key: john.age
環境変数を設定する使い方になります。
applyしてcontainer内に入ると環境変数として設定されます。
user@usernoMacBook-Pro configmap_test2 % kubectl apply -f configmap.yml,pod.yml
configmap/test-params created
pod/test created
user@usernoMacBook-Pro configmap_test2 % kubectl exec -it test -- /bin/sh
/ # printenv
KUBERNETES_SERVICE_PORT=443
KUBERNETES_PORT=tcp://10.96.0.1:443
HOSTNAME=test
SHLVL=1
HOME=/root
PKG_RELEASE=1
DYNPKG_RELEASE=1
john.name=John.Lucas
john.age=twenty-fifth
TERM=xterm
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
NGINX_VERSION=1.27.4
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT=443
NJS_VERSION=0.8.9
KUBERNETES_PORT_443_TCP_PROTO=tcp
NJS_RELEASE=1
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
KUBERNETES_SERVICE_HOST=10.96.0.1
PWD=/
/ # exit
・Appendix
参考文献はこちら
https://qiita.com/yuichi1992_west/items/49470a7a347f5a932e98
https://zenn.dev/tamago0224/articles/f50d57c238aef4
コメントを残す