init容器是在pod运行之前运行的容器,并包括一些应用镜像中不不存在的实用工具和安装脚本。
因为 Init 容器具有与应用容器分离的单独镜像,其启动相关代码具有如下优势:
注:在做此实验前,关闭iptables规则,使得k8s机器不能访问外网
实验代码来自官网:init容器官网介绍
[root@node1 manifest]# cat init.yml
apiVersion: v1
kind: Pod
metadata:
name: jd618
labels:
app: myapp
spec:
containers:
- name: jd001
image: busyboxplus:latest
command: ['sh', '-c', 'echo The app is running! && sleep 3600']
initContainers:
- name: init-myservice
image: busyboxplus:latest
command: ['sh', '-c', "until nslookup myservice.default.svc.cluster.local; do echo waiting for myservice; sleep 2; done"] #此处将官网的xxx换成default ...
- name: init-mydb
image: busyboxplus:latest
command: ['sh', '-c', "until nslookup mydb.default.svc.cluster.local; do echo waiting for mydb; sleep 2; done"]
创建,此时jd618一直处于init,准备未完成状态,只有当service创建完之后,才能处于running
[root@node1 manifest]# kubectl apply -f init.yml
pod/jd618 created
[root@node1 manifest]# kubectl get pod
NAME READY STATUS RESTARTS AGE
jd618 0/1 Init:0/2 0 8s
[root@node1 manifest]# kubectl get pod
NAME READY STATUS RESTARTS AGE
jd618 0/1 Init:0/2 0 10s
没有解析
当服务创建完成之后,再次查看状态
service.yml
[root@node1 manifest]# cat service.yml
kind: Service
apiVersion: v1
metadata:
name: myservice
spec:
ports:
- protocol: TCP
port: 80
targetPort: 9376
---
kind: Service
apiVersion: v1
metadata:
name: mydb
spec:
ports:
- protocol: TCP
port: 80
targetPort: 9377
[root@node1 manifest]# kubectl apply -f service.yml
service/myservice created
service/mydb created
[root@node1 manifest]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 > 443/TCP 44h
mydb ClusterIP 10.111.16.207 > 80/TCP 11s
myservice ClusterIP 10.108.94.5 > 80/TCP 12s
[root@node1 manifest]# kubectl get pod
NAME READY STATUS RESTARTS AGE
demo 1/1 Running 0 12m
jd618 1/1 Running 0 13m
影响的是kubectl get pod中RESTARTS下面的参数
在容器里加这
livenessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 1 #容器启动后的延迟
periodSeconds: 2 #每次探测间隔,单位是秒
timeoutSeconds: 1
完整版init.yml
apiVersion: v1
kind: Pod
metadata:
name: jd618
labels:
app: myapp
spec:
containers:
- name: jd001
image: myapp:v1
imagePullPolicy: IfNotPresent
livenessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 1
periodSeconds: 2
timeoutSeconds: 1
initContainers:
- name: init-myservice
image: busyboxplus:latest
command: ['sh', '-c', "until nslookup myservice.default.svc.cluster.local; do echo waiting for myservice; sleep 2; done"]
- name: init-mydb
image: busyboxplus:latest
command: ['sh', '-c', "until nslookup mydb.default.svc.cluster.local; do echo waiting for mydb; sleep 2; done"]
因为里有init容器,所以得保证svc服务开起来。
容器运行起来
测试:进入容器关闭nginx
影响的是kubectl get pod中READY下面的参数
service.yml
[root@node1 manifest]# cat service.yml
kind: Service
apiVersion: v1
metadata:
name: myservice
spec:
ports:
- protocol: TCP
port: 80
targetPort: 80
selector:
app: myapp
init.yml
[root@node1 manifest]# cat init.yml
apiVersion: v1
kind: Pod
metadata:
name: jd618
labels:
app: myapp
spec:
containers:
- name: jd001
image: myapp:v1
imagePullPolicy: IfNotPresent
livenessProbe: #存活检测
tcpSocket:
port: 80
initialDelaySeconds: 1
periodSeconds: 2
timeoutSeconds: 1
readinessProbe: #就绪检测
httpGet:
path: /hostname.html
port: 80
initialDelaySeconds: 1
periodSeconds: 3
timeoutSeconds: 1
initContainers: #Init容器
- name: init-myservice
image: busyboxplus:latest
command: ['sh', '-c', "until nslookup myservice.default.svc.cluster.local; do echo waiting for myservice; sleep 2; done"]
创建pod,
[root@node1 manifest]# kubectl apply -f init.yml
pod/jd618 created
因为里面有init容器,所以要运行service.yml服务
[root@node1 manifest]# kubectl apply -f service.yml
service/myservice created
正常是将Ip加到Endpoints里,为了看到实验效果,进到容器里:将/hostname.yml给注释掉。
如何恢复?
再运行一个pod2.yml,里面的标签改为app:myapp
[root@node1 manifest]# cat pod2.yml
apiVersion: apps/v1
kind: Deployment
metadata:
# Unique key of the Deployment instance
name: deployment-example
spec:
# 3 Pods should exist at all times.
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
# Apply this label to pods and default
# the Deployment label selector to this value
app: myapp
spec:
containers:
- name: myapp
# Run this image
image: myapp:v1