容器系列-08初始化POD使用步骤
# vi myapp.yaml
# kubectl apply -f myapp.yaml
# kubectl get pods
# kubectl get -f myapp.yaml
# kubectl describe -f myapp.yaml
# kubectl logs myapp-pod -c init-mydb
# vi services.yaml
# kubectl apply -f services.yaml
# kubectl get -f myapp.yaml
----演示
[root@xiaodongmaster1 initcontainer]# vi myapp.yaml
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app.kubernetes.io/name: MyApp
spec:
containers:
- name: myapp-container
image: busybox:1.28
command: ['sh', '-c', 'echo The app is running! && sleep 3600']
initContainers:
- name: init-myservice
image: busybox:1.28
command: ['sh', '-c', "until nslookup myservice.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for myservice; sleep 2; done"]
- name: init-mydb
image: busybox:1.28
command: ['sh', '-c', "until nslookup mydb.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for mydb; sleep 2; done"]
[root@xiaodongmaster1 initcontainer]# kubectl apply -f myapp.yaml
pod/myapp-pod created
[root@xiaodongmaster1 initcontainer]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Init:0/2 0 6s
nginx-7f466444dc-dnbz6 1/1 Running 3 3d5h
nginx-7f466444dc-mxgc8 1/1 Running 3 3d5h
nginx-test-75c685fdb7-dphvw 1/1 Running 1 27h
nginx-test-75c685fdb7-rvg8z 1/1 Running 1 27h
[root@xiaodongmaster1 initcontainer]# kubectl get -f myapp.yaml
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Init:0/2 0 32s
You have new mail in /var/spool/mail/root
[root@xiaodongmaster1 initcontainer]# kubectl describe -f myapp.yaml
Name: myapp-pod
Namespace: default
Priority: 0
Node: xiaodongnode1/192.168.126.181
Start Time: Mon, 03 Oct 2022 15:17:46 +0800
Labels: app.kubernetes.io/name=MyApp
Annotations: cni.projectcalico.org/podIP: 10.244.121.42/32
cni.projectcalico.org/podIPs: 10.244.121.42/32
Status: Pending
IP: 10.244.121.42
IPs:
IP: 10.244.121.42
Init Containers:
init-myservice:
Container ID: docker://5afd74f1ec116d98a0eef4f4a3c3b58a95f52b60df8e261888ef249c7ab0d6e3
Image: busybox:1.28
Image ID: docker://sha256:8c811b4aec35f259572d0f79207bc0678df4c736eeec50bc9fec37ed936a472a
Port: <none>
Host Port: <none>
Command:
sh
-c
until nslookup myservice.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for myservice; sleep 2; done
State: Running
Started: Mon, 03 Oct 2022 15:17:48 +0800
Ready: False
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-w7znj (ro)
init-mydb:
Container ID:
Image: busybox:1.28
Image ID:
Port: <none>
Host Port: <none>
Command:
sh
-c
until nslookup mydb.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for mydb; sleep 2; done
State: Waiting
Reason: PodInitializing
Ready: False
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-w7znj (ro)
Containers:
myapp-container:
Container ID:
Image: busybox:1.28
Image ID:
Port: <none>
Host Port: <none>
Command:
sh
-c
echo The app is running! && sleep 3600
State: Waiting
Reason: PodInitializing
Ready: False
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-w7znj (ro)
Conditions:
Type Status
Initialized False
Ready False
ContainersReady False
PodScheduled True
Volumes:
default-token-w7znj:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-w7znj
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 54s default-scheduler Successfully assigned default/myapp-pod to xiaodongnode1
Normal Pulled 53s kubelet Container image "busybox:1.28" already present on machine
Normal Created 52s kubelet Created container init-myservice
Normal Started 52s kubelet Started container init-myservice
[root@xiaodongmaster1 initcontainer]# kubectl logs myapp-pod -c init-myservice
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local
...
nslookup: can't resolve 'myservice.default.svc.cluster.local'
waiting for myservice
You have new mail in /var/spool/mail/root
[root@xiaodongmaster1 initcontainer]# kubectl logs myapp-pod -c init-mydb
Error from server (BadRequest): container "init-mydb" in pod "myapp-pod" is waiting to start: PodInitializing
[root@xiaodongmaster1 initcontainer]# vi services.yaml
---
apiVersion: v1
kind: Service
metadata:
name: myservice
spec:
ports:
- protocol: TCP
port: 80
targetPort: 9376
---
apiVersion: v1
kind: Service
metadata:
name: mydb
spec:
ports:
- protocol: TCP
port: 80
targetPort: 9377
[root@xiaodongmaster1 initcontainer]# kubectl apply -f services.yaml
service/myservice created
service/mydb created
[root@xiaodongmaster1 initcontainer]# kubectl get -f myapp.yaml
NAME READY STATUS RESTARTS AGE
myapp-pod 1/1 Running 0 3m20s
[root@xiaodongmaster1 initcontainer]#
1199

被折叠的 条评论
为什么被折叠?



