在Kubernetes(K8s)中,要实现开机自启动应用,通常会使用一个名为kubelet
的代理来启动你的应用。以下是一个简单的示例脚本,假设你的应用是一个名为myapp
的Docker镜像。
- 首先,创建一个名为
myapp-deployment.yaml
的Kubernetes部署配置文件:
yamlapiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deployment
spec:
replicas: 1
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:latest
ports:
- containerPort: 8080
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deployment
spec:
replicas: 1
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:latest
ports:
- containerPort: 8080
- 使用
kubectl
命令行工具应用配置:
shkubectl apply -f myapp-deployment.yaml
kubectl apply -f myapp-deployment.yaml
- 检查部署状态:
shkubectl get deployments
kubectl get deployments
- 创建名为
myapp-service.yaml
的Kubernetes服务配置文件:
yamlapiVersion: v1
kind: Service
metadata:
name: myapp-service
spec:
selector:
app: myapp
ports:
- protocol: TCP
port: 80
targetPort: 8080
apiVersion: v1
kind: Service
metadata:
name: myapp-service
spec:
selector:
app: myapp
ports:
- protocol: TCP
port: 80
targetPort: 8080
- 应用服务配置:
shkubectl apply -f myapp-service.yaml
kubectl apply -f myapp-service.yaml
- 检查服务状态:
shkubectl get services
kubectl get services
至此,你的应用已经成功在Kubernetes集群中部署并运行。接下来,我们要通过Kubernetes的kubelet
来设置开机自启动。
- 获取
kubelet
的配置文件路径:
在大多数Kubernetes集群中,kubelet
的配置文件通常位于/etc/systemd/system/kubelet.service.d/10-kubeadm.conf
。如果你的系统不同,请查找相应的配置文件路径。 8. 编辑kubelet
配置文件,添加以下内容:
ini[Service]
ExecStart=
ExecStart=/usr/bin/kubelet --container-runtime=remote --container-r
[Service]
ExecStart=
ExecStart=/usr/bin/kubelet --container-runtime=remote --container-r