Kubernetes
AKSクラスタ構築
1.AKSクラスタ構築
コピー&ペースト用
コマンド
az aks create --resource-group training --name k8s --node-count 2 --node-vm-size Standard_B2s --enable-addons monitoring --generate-ssh-keys
コマンド結果
コマンド
# az aks create --resource-group training --name k8s --node-count 2 --node-vm-size Standard_B2s --enable-addons monitoring --generate-ssh-keys
AAD role propagation done[############################################] 100.0000%{
"aadProfile": null,
"addonProfiles": {
"KubeDashboard": {
"config": null,
"enabled": true,
"identity": null
},
"omsagent": {
"config": {
"logAnalyticsWorkspaceResourceID": "/subscriptions/602b7e73-bd85-4722-baae-08a0a2532d12/resourcegroups/defaultresourcegroup-ejp/providers/microsoft.operationalinsights/workspaces/defaultworkspace-602b7e73-bd85-4722-baae-08a0a2532d12-ejp"
},
"enabled": true,
"identity": null
}
},
"agentPoolProfiles": [
{
"availabilityZones": null,
"count": 2,
"enableAutoScaling": null,
"enableNodePublicIp": false,
"maxCount": null,
"maxPods": 110,
"minCount": null,
"mode": "System",
"name": "nodepool1",
"nodeLabels": {},
"nodeTaints": null,
"orchestratorVersion": "1.16.13",
"osDiskSizeGb": 128,
"osType": "Linux",
"provisioningState": "Succeeded",
"scaleSetEvictionPolicy": null,
"scaleSetPriority": null,
"spotMaxPrice": null,
"tags": null,
"type": "VirtualMachineScaleSets",
"vmSize": "Standard_B2s",
"vnetSubnetId": null
}
],
"apiServerAccessProfile": null,
"autoScalerProfile": null,
"diskEncryptionSetId": null,
"dnsPrefix": "k8s-training-602b7e",
"enablePodSecurityPolicy": null,
"enableRbac": true,
"fqdn": "k8s-training-602b7e-914497e9.hcp.japaneast.azmk8s.io",
"id": "/subscriptions/602b7e73-bd85-4722-baae-08a0a2532d12/resourcegroups/training/providers/Microsoft.ContainerService/managedClusters/k8s",
"identity": null,
"identityProfile": null,
"kubernetesVersion": "1.16.13",
"linuxProfile": {
"adminUsername": "azureuser",
"ssh": {
"publicKeys": [
{
"keyData": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDaVskDfRPLlcnoCBQZVryeym5xHwxRmYYdcBbq8StaE8Jq0Z8gEq00D1O/N7zoymoGvGs12MkMCHCJ++KHWK7HXdxzx/ZPyHQmaOfzfesR29La7eJsJu1ydvfLvGoEVeoLCH6ifZa48ktlZ4qj/8mX7qHZThDM6jlsLgjmkgx5AX2T6JsZGQGjDeblgLf1eq+4mDxurWv6eNd9vKRQx6gNA859MhJaxXuw+cA9kPidT1S0zEcd8YeKf57ieAwLV7i3kmHH4xQa5Yoc6dNebEfe0SfAEQIpEkU+GgQxt4FNsnTusrnbCOx8Xy0P51a5Wq+PiDQ2HByYrWLEHotztD0V"
}
]
}
},
"location": "japaneast",
"maxAgentPools": 10,
"name": "k8s",
"networkProfile": {
"dnsServiceIp": "10.0.0.10",
"dockerBridgeCidr": "172.17.0.1/16",
"loadBalancerProfile": {
"allocatedOutboundPorts": null,
"effectiveOutboundIps": [
{
"id": "/subscriptions/602b7e73-bd85-4722-baae-08a0a2532d12/resourceGroups/MC_training_k8s_japaneast/providers/Microsoft.Network/publicIPAddresses/077eea9d-514e-438d-bb7f-34bd66e1fe6b",
"resourceGroup": "MC_training_k8s_japaneast"
}
],
"idleTimeoutInMinutes": null,
"managedOutboundIps": {
"count": 1
},
"outboundIpPrefixes": null,
"outboundIps": null
},
"loadBalancerSku": "Standard",
"networkMode": null,
"networkPlugin": "kubenet",
"networkPolicy": null,
"outboundType": "loadBalancer",
"podCidr": "10.244.0.0/16",
"serviceCidr": "10.0.0.0/16"
},
"nodeResourceGroup": "MC_training_k8s_japaneast",
"privateFqdn": null,
"provisioningState": "Succeeded",
"resourceGroup": "training",
"servicePrincipalProfile": {
"clientId": "ab96924b-12c5-45f3-95a0-e1004fb49d93",
"secret": null
},
"sku": {
"name": "Basic",
"tier": "Free"
},
"tags": null,
"type": "Microsoft.ContainerService/ManagedClusters",
"windowsProfile": null
}
2.AKSクラスタクレデンシャル設定
コピー&ペースト用
コマンド
az aks get-credentials --resource-group training --name k8s
コマンド結果
コマンド
$ az aks get-credentials --resource-group training --name k8s
Merged "k8s" as current context in /home/ichikawa/.kube/config
3.AKSクラスタノード確認
コピー&ペースト用
コマンド
kubectl get nodes
コマンド結果
コマンド
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
aks-nodepool1-40321068-vmss000000 Ready agent 2m38s v1.17.11
aks-nodepool1-40321068-vmss000001 Ready agent 2m34s v1.17.11
kubectlコマンド演習
Podの作成
1.Nginxという名前のNginx Podを作成
コピー&ペースト用
コマンド
kubectl run nginx --image=nginx --restart=Never
コマンド結果
コマンド
# kubectl run nginx --image=nginx --restart=Never
pod/nginx created
2.Nginx Podの確認
コピー&ペースト用
コマンド
kubectl get pod
コマンド結果
コマンド
# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 2m6s
3.Nginx Podの削除
コピー&ペースト用
コマンド
kubectl delete pod nginx
コマンド結果
コマンド
# kubectl delete pod nginx
pod "nginx" deleted
4.Nginx Podをyamlを生成して、そのyamlから作成
コピー&ペースト用1
コマンド
kubectl run nginx --image=nginx --restart=Never --dry-run=client -o yaml > nginx.yaml
コマンド結果1
コマンド
# kubectl run nginx --image=nginx --restart=Never --dry-run=client -o yaml > nginx.yaml
コピー&ペースト用2
コマンド
kubectl create -f nginx.yaml
コマンド結果2
コマンド
# kubectl create -f nginx.yaml
pod/nginx created
コピー&ペースト用3
コマンド
kubectl get pod nginx
コマンド結果3
コマンド
# kubectl get pod nginx
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 33s
5.Nginx Podの詳細確認
コピー&ペースト用
コマンド
kubectl describe pod nginx
コマンド結果
コマンド
# kubectl describe pod nginx
Name: nginx
Namespace: default
Priority: 0
Node: aks-nodepool1-40321068-vmss000000/10.240.0.4
Start Time: Mon, 18 May 2020 05:04:06 +0000
Labels: run=nginx
Annotations: <none>
Status: Running
IP: 10.244.1.5
IPs: <none>
Containers:
nginx:
Container ID: docker://895493fb0e8d019483b3619cff4c09509f9e92c26d861d4626c5b71f2172b632
Image: nginx
Image ID: docker-pullable://nginx@sha256:30dfa439718a17baafefadf16c5e7c9d0a1cde97b4fd84f63b69e13513be7097
Port: <none>
Host Port: <none>
State: Running
Started: Mon, 18 May 2020 05:04:10 +0000
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-dwjvw (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-dwjvw:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-dwjvw
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 32s default-scheduler Successfully assigned default/nginx to aks-nodepool1-40321068-vmss000000
Normal Pulling 30s kubelet, aks-nodepool1-40321068-vmss000000 Pulling image "nginx"
Normal Pulled 28s kubelet, aks-nodepool1-40321068-vmss000000 Successfully pulled image "nginx"
Normal Created 27s kubelet, aks-nodepool1-40321068-vmss000000 Created container nginx
Normal Started 27s kubelet, aks-nodepool1-40321068-vmss000000 Started container nginx
6.Nginx Podの削除
コピー&ペースト用
コマンド
kubectl delete pod nginx
コマンド結果
コマンド
# kubectl delete pod nginx
pod "nginx" deleted
Labelの作成
1.nginxという名前でapp=v1というラベルを持つNginx Podを作成
コピー&ペースト用
コマンド
kubectl run nginx --image=nginx --restart=Never --labels=app=v1
コマンド結果
コマンド
# kubectl run nginx --image=nginx --restart=Never --labels=app=v1
pod/nginx created
2.すべてのPodラベル確認
コピー&ペースト用
コマンド
kubectl get pod --show-labels
コマンド結果
コマンド
# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 62s app=v1
3.app=v2にラベル名を変更
コピー&ペースト用
コマンド
kubectl label pod nginx app=v2 --overwrite
コマンド結果
コマンド
# kubectl label pod nginx app=v2 --overwrite
pod/nginx labeled
4.appラベルを持つPodのみ確認
コピー&ペースト用
コマンド
kubectl get pod -L app
コマンド結果
コマンド
# kubectl get pod -L app
NAME READY STATUS RESTARTS AGE APP
nginx 1/1 Running 0 2m59s v2
5.app=v2ラベルを持つPodのみ確認
コピー&ペースト用
コマンド
kubectl get pod -l app=v2
コマンド結果
コマンド
# kubectl get pod -l app=v2
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 3m48s
6.Nginx Podの削除
コピー&ペースト用
コマンド
kubectl delete pod nginx
コマンド結果
コマンド
# kubectl delete pod nginx
pod "nginx" deleted
Deployment作成
1.nginxという名前でバージョン1.19.1のNginx,レプリカ数2のDeploymentの作成
コピー&ペースト用1
コマンド
kubectl create deployment nginx --image=nginx:1.19.1 --dry-run=client -o yaml > nginx_dep.yaml
コマンド結果1
コマンド結果は表示されません。
コマンド
# kubectl create deployment nginx --image=nginx:1.19.1 --dry-run=client -o yaml > nginx_dep.yaml
コピー&ペースト用
コマンド
vim nginx_dep.yaml
nginx_dep.yamlのreplicasを1から2へ変更
コマンド
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: nginx
name: nginx
spec:
replicas: 2 #1から2へ変更
selector:
matchLabels:
app: nginx
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx:1.19.1
name: nginx
resources: {}
status: {}
作業全体
コマンド
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: nginx
name: nginx
spec:
replicas: 2 #1から2へ変更
selector:
matchLabels:
app: nginx
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx:1.19.1
name: nginx
resources: {}
status: {}
コピー&ペースト用
nginx_dep.yamlからDeploymentを作成
コマンド
kubectl create -f nginx_dep.yaml
コマンド結果
コマンド
# kubectl create -f nginx_dep.yaml
deployment.apps/nginx created
2.Podが2個生成されていることを確認
コピー&ペースト用
コマンド
kubectl get pod
コマンド結果
コマンド
# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-676646786c-fj8wp 1/1 Running 0 21s
nginx-676646786c-wttph 1/1 Running 0 21s
3.1podを削除して、セルフヒーリングを確認
コピー&ペースト用1
コマンド
kubectl delete pod nginx-676646786c-wttph
コマンド結果1
コマンド
# kubectl delete pod nginx-676646786c-wttph
pod "nginx-676646786c-wttph" deleted
コピー&ペースト用2
コマンド
kubectl get pod
コマンド結果2
コマンド
# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-676646786c-c7zrb 1/1 Running 0 24s
nginx-676646786c-fj8wp 1/1 Running 0 84s
4.Nginxを1.19.2にアップデート
コピー&ペースト用1
コマンド
kubectl set image deployment nginx nginx=nginx:1.19.2
コマンド結果1
コマンド
# kubectl set image deployment nginx nginx=nginx:1.19.2
deployment.apps/nginx image updated
コピー&ペースト用2
コマンド
kubectl describe deployment nginx
コマンド結果2
コマンド
# kubectl describe deployment nginx
Name: nginx
Namespace: default
CreationTimestamp: Fri, 18 Sep 2020 06:25:42 +0000
Labels: app=nginx
Annotations: deployment.kubernetes.io/revision: 2
Selector: app=nginx
Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: nginx:1.19.2
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-57bfdbd96b (2/2 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 2m20s deployment-controller Scaled up replica set nginx-676646786c to 2
Normal ScalingReplicaSet 33s deployment-controller Scaled up replica set nginx-57bfdbd96b to 1
Normal ScalingReplicaSet 31s deployment-controller Scaled down replica set nginx-676646786c to 1
Normal ScalingReplicaSet 31s deployment-controller Scaled up replica set nginx-57bfdbd96b to 2
Normal ScalingReplicaSet 29s deployment-controller Scaled down replica set nginx-676646786c to 0
5.Revision内容の確認
コピー&ペースト用1
コマンド
kubectl rollout history deployment nginx
コマンド結果1
コマンド
# kubectl rollout history deployment nginx
deployment.extensions/nginx
REVISION CHANGE-CAUSE
1 <none>
2 <none>
コピー&ペースト用2
コマンド
kubectl rollout history deployment nginx --revision=1
コマンド結果2
コマンド
# kubectl rollout history deployment nginx --revision=1
deployment.apps/nginx with revision #1
Pod Template:
Labels: app=nginx
pod-template-hash=676646786c
Containers:
nginx:
Image: nginx:1.19.1
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
コピー&ペースト用3
コマンド
kubectl rollout history deployment nginx --revision=2
コマンド結果3
コマンド
# kubectl rollout history deployment nginx --revision=2
deployment.apps/nginx with revision #2
Pod Template:
Labels: app=nginx
pod-template-hash=57bfdbd96b
Containers:
nginx:
Image: nginx:1.19.2
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
6.Nginxのバージョンを1.19.1にロールバック
コピー&ペースト用1
コマンド
kubectl rollout undo deployment nginx
コマンド結果1
コマンド
# kubectl rollout undo deployment nginx
deployment.apps/nginx rolled back
コピー&ペースト用2
コマンド
kubectl describe deployment nginx
コマンド結果2
コマンド
# kubectl describe deployment nginx
Name: nginx
Namespace: default
CreationTimestamp: Fri, 18 Sep 2020 06:25:42 +0000
Labels: app=nginx
Annotations: deployment.kubernetes.io/revision: 3
Selector: app=nginx
Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: nginx:1.19.1
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-676646786c (2/2 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 2m56s deployment-controller Scaled up replica set nginx-57bfdbd96b to 1
Normal ScalingReplicaSet 2m54s deployment-controller Scaled down replica set nginx-676646786c to 1
Normal ScalingReplicaSet 2m54s deployment-controller Scaled up replica set nginx-57bfdbd96b to 2
Normal ScalingReplicaSet 2m52s deployment-controller Scaled down replica set nginx-676646786c to 0
Normal ScalingReplicaSet 27s deployment-controller Scaled up replica set nginx-676646786c to 1
Normal ScalingReplicaSet 25s (x2 over 4m43s) deployment-controller Scaled up replica set nginx-676646786c to 2
Normal ScalingReplicaSet 25s deployment-controller Scaled down replica set nginx-57bfdbd96b to 1
Normal ScalingReplicaSet 22s deployment-controller Scaled down replica set nginx-57bfdbd96b to 0
7.Nginxのバージョンを revision 2 の nginx 1.19.2 にロールバック
コピー&ペースト用1
コマンド
kubectl rollout undo deployment nginx --to-revision=2
コマンド結果1
コマンド
# kubectl rollout undo deployment nginx --to-revision=2
deployment.apps/nginx rolled back
コピー&ペースト用2
コマンド
kubectl describe deployment nginx
コマンド結果2
コマンド
# kubectl describe deployment nginx
Name: nginx
Namespace: default
CreationTimestamp: Fri, 18 Sep 2020 06:25:42 +0000
Labels: app=nginx
Annotations: deployment.kubernetes.io/revision: 4
Selector: app=nginx
Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: nginx:1.19.2
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-57bfdbd96b (2/2 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 112s deployment-controller Scaled up replica set nginx-676646786c to 1
Normal ScalingReplicaSet 110s (x2 over 6m8s) deployment-controller Scaled up replica set nginx-676646786c to 2
Normal ScalingReplicaSet 110s deployment-controller Scaled down replica set nginx-57bfdbd96b to 1
Normal ScalingReplicaSet 107s deployment-controller Scaled down replica set nginx-57bfdbd96b to 0
Normal ScalingReplicaSet 44s (x2 over 4m21s) deployment-controller Scaled up replica set nginx-57bfdbd96b to 1
Normal ScalingReplicaSet 41s (x2 over 4m19s) deployment-controller Scaled down replica set nginx-676646786c to 1
Normal ScalingReplicaSet 41s (x2 over 4m19s) deployment-controller Scaled up replica set nginx-57bfdbd96b to 2
Normal ScalingReplicaSet 38s (x2 over 4m17s) deployment-controller Scaled down replica set nginx-676646786c to 0
8.deploymentの削除
コピー&ペースト用
コマンド
kubectl delete deployment nginx
コマンド結果
コマンド
# kubectl delete deployment nginx
deployment.extensions "nginx" deleted
9.deployment削除の確認
コピー&ペースト用
コマンド
kubectl get deployment
コマンド結果
コマンド
# kubectl get deployment
No resources found in default namespace.
Serviceの作成
1.nginxという名前のポッドを作成、ポート80を公開
コピー&ペースト用
コマンド
kubectl run nginx --image=nginx --restart=Never --port=80 --expose
コマンド結果
コマンド
# kubectl run nginx --image=nginx --restart=Never --port=80 --expose
service/nginx created
pod/nginx created
2.nginx serviceとエンドポイントのIP確認
コピー&ペースト用1
コマンド
kubectl get service nginx
コマンド結果1
コマンド
# kubectl get service nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx ClusterIP 10.0.66.43 <none> 80/TCP 12s
コピー&ペースト用2
コマンド
kubectl get ep
コマンド結果2
コマンド
# kubectl get ep
NAME ENDPOINTS AGE
kubernetes 20.46.184.97:443 34m
nginx 10.244.1.12:80 46s
3.busybox podを作成し、busyboxコンテナーからwgetコマンドでnginx serviceを経由したアクセス確認
コピー&ペースト用
コマンド
kubectl run busybox --image=busybox --restart=Never --rm -it /bin/sh
確認したServiceとエンドポイントのIPアドレスを入力
コマンド
wget -O- 10.244.1.12
コマンド
wget -O- 10.0.66.43
busyboxコンテナーからexit
コマンド
exit
コマンド結果
コマンド
# kubectl run busybox --image=busybox --restart=Never --rm -it /bin/sh
If you don't see a command prompt, try pressing enter.
/ # wget -O- 10.244.1.12
Connecting to 10.244.1.12 (10.244.1.12:80)
writing to stdout
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
- 100% |*********************************************************************************************************************************************************************************************| 612 0:00:00 ETA
written to stdout
/ # exit
pod "busybox" deleted
4.nginx podとnginx serviceの削除
コピー&ペースト用1
コマンド
kubectl delete pod nginx
コマンド結果1
コマンド
# kubectl delete pod nginx
pod "nginx" deleted
コピー&ペースト用2
コマンド
kubectl delete service nginx
コマンド結果2
コマンド
# kubectl delete service nginx
service "nginx" deleted
configmapの作成
1. “var1=docker” “var2=kubernetes”のconfigMap ‘config'を作成
コピー&ペースト用1
コマンド
kubectl create configmap config --from-literal=var1=docker --from-literal=var2=kubernetes
コマンド結果1
コマンド
# kubectl create configmap config --from-literal=var1=docker --from-literal=var2=kubernetes
configmap/config created
コピー&ペースト用2
コマンド
kubectl get configmap config
コマンド結果2
コマンド
# kubectl get configmap config
NAME DATA AGE
config 2 67s
コピー&ペースト用3
コマンド
kubectl get configmap config -o yaml
コマンド結果3
コマンド
# kubectl get configmap config -o yaml
apiVersion: v1
data:
var1: docker
var2: kubernetes
kind: ConfigMap
metadata:
creationTimestamp: "2020-09-30T05:47:17Z"
name: config
namespace: default
resourceVersion: "1829"
selfLink: /api/v1/namespaces/default/configmaps/config
uid: e2b56ed1-9f0f-4241-a32d-d380eed9791c
2.configMap ’config’をenv変数としてnginx podにロード
コピー&ペースト用1
コマンド
kubectl run --restart=Never nginx --image=nginx -o yaml --dry-run=client > nginx_configmap.yaml
コマンド結果1
コマンド結果は表示されません。
コマンド
# kubectl run --restart=Never nginx --image=nginx -o yaml --dry-run=client > nginx_configmap.yaml
コピー&ペースト用2
コマンド
vim nginx_configmap.yaml
コマンド
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
envFrom:
- configMapRef:
name: config
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
作業全体
コマンド
# vim nginx_configmap.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
envFrom:
- configMapRef:
name: config
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
3.Nginx Podの作成
コピー&ペースト用
コマンド
kubectl create -f nginx_configmap.yaml
コマンド結果
コマンド
# kubectl create -f nginx_configmap.yaml
pod/nginx created
4.環境変数が読み込まれていることを確認 ※var1=docker var2=kubernetesが表示される
コピー&ペースト用
コマンド
kubectl exec -it nginx -- env
コマンド結果
コマンド
# kubectl exec -it nginx -- env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=nginx
TERM=xterm
var1=docker
var2=kubernetes
KUBERNETES_PORT_443_TCP=tcp://10.0.0.1:443
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_ADDR=10.0.0.1
KUBERNETES_SERVICE_HOST=10.0.0.1
KUBERNETES_SERVICE_PORT=443
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT=tcp://10.0.0.1:443
NGINX_VERSION=1.19.2
NJS_VERSION=0.4.3
PKG_RELEASE=1~buster
HOME=/root
5.Nginx podの削除
コピー&ペースト用
コマンド
kubectl delete -f nginx_configmap.yaml
コマンド結果
コマンド
# kubectl delete -f nginx_configmap.yaml
pod "nginx" deleted
Multi-container Podの作成
busyboxイメージを利用して “echo Hello, Docker & Kubernetes !!; sleep 2400”というコマンドを実行する、2つのコンテナーを内包するPodを作成します。 2番目のコンテナーに接続して「ls」を実行してみましょう。
1.busybox yamlを作成
コピー&ペースト用
コマンド
kubectl run hello --image=busybox --restart=Never -o yaml --dry-run=client -- /bin/sh -c 'echo Hello, Docker & Kubernetes !!; sleep 2400' > multi.yaml
コマンド結果
コマンド結果は表示されません。
コマンド
# kubectl run hello --image=busybox --restart=Never -o yaml --dry-run=client -- /bin/sh -c 'echo Hello, Docker & Kubernetes !!; sleep 2400' > multi.yaml
2.2つ目のコンテナーを追記
コピー&ペースト用
コマンド
vim multi.yaml
コマンド
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: busybox
name: busybox
spec:
containers:
- args:
- /bin/sh
- -c
- echo Hello, Docker & Kubernetes !!; sleep 2400
image: busybox
name: hello
resources: {}
- args:
- /bin/sh
- -c
- echo Hello, Docker & Kubernetes !!; sleep 2400
image: busybox
name: hello2
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
作業全体
コマンド
# vim multi.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: busybox
name: busybox
spec:
containers:
- args:
- /bin/sh
- -c
- echo Hello, Docker & Kubernetes !!; sleep 2400
image: busybox
name: hello
resources: {}
- args:
- /bin/sh
- -c
- echo Hello, Docker & Kubernetes !!; sleep 2400
image: busybox
name: hello2
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
3.Pod作成
コピー&ペースト用1
コマンド
kubectl create -f multi.yaml
コマンド結果1
コマンド
# kubectl create -f multi.yaml
pod/hello created
コピー&ペースト用2
コマンド
kubectl get pod
コマンド結果2
コマンド
# kubectl get pod
NAME READY STATUS RESTARTS AGE
hello 2/2 Running 0 13s
4.2つ目のコンテナーにアクセス
コピー&ペースト用
コマンド
kubectl exec -it hello -c hello2 -- /bin/sh
コマンド
ls
コマンド
exit
コマンド結果
コマンド
# kubectl exec -it hello -c hello2 -- /bin/sh
/ # ls
bin dev etc home proc root sys tmp usr var
/ # exit
5.Podの削除
コピー&ペースト用
コマンド
kubectl delete pod hello
コマンド結果
コマンド
# kubectl delete pod hello
pod "hello" deleted