ansible镜像构建使用

js 复制代码
cat << EOF >Dockerfile

FROM alpine:latest

RUN echo "https://mirrors.tuna.tsinghua.edu.cn/alpine/v3.18/main" > /etc/apk/repositories \
    && echo "https://mirrors.tuna.tsinghua.edu.cn/alpine/v3.18/community" >> /etc/apk/repositories \
    && apk add --no-cache ansible openssh sshpass bash-doc \
    && apk update \
    && apk add tzdata \
    && mkdir /etc/ansible \
    && echo "StrictHostKeyChecking no" > /etc/ssh/ssh_config
 
#如何你是AWS账号就要copy密钥(不是的话就忽略)
#COPY k8s.pem /opt/k8s.pem
EOF

再来一个k8s文件

js 复制代码
cat << EOF > config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: ansible-hosts-configmap
data:
  hosts: |
    [aws]
    57.11.11.11    ansible_ssh_private_key_file=/opt/k8s.pem
    57.11.11.11    ansible_ssh_user=admin
EOF

如果你不是AWS的服务器可以跳过这里

js 复制代码
secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: k8s-prod-pem-secret
type: Opaque
data:
  k8s.pem: LS0tLS1CRUdJTiBSU0EgUFJ
  
  #转化密钥
#cat k8.pem | base64 -w 0
或者直接一条命令
shell 复制代码
kubectl create configmap k8s-prod-pem-configmap --from-file=k8s.pem -n devops-tools

不是AWS服务器使用这个yaml运行

yaml 复制代码
cat << EOF > test.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: ansible
spec:
  replicas: 1
  selector:
    matchLabels:
      app: ansible
  template:
    metadata:
      labels:
        app: ansible
    spec:
      volumes:
        - name: ansible-hosts
          configMap:
            name: ansible-hosts-configmap
        - name: k8s-prod-pem
          configMap:
            name: k8s-prod-pem-configmap
      containers:
        - name: ansible
          image: registry.cn-shenzhen.aliyuncs.com/jbjb/dockers:ansible-v01
          command: ["sleep", "3333"]
          volumeMounts:
            - name: ansible-hosts
              mountPath: /etc/ansible/hosts
              subPath: hosts
            - name: k8s-prod-pem
              mountPath: /opt/k8s.pem
              subPath: k8s.pem
              readOnly: true
EOF

AWS 服务器器使用

js 复制代码
cat << EOF > test.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: ansible
spec:
  replicas: 1
  selector:
    matchLabels:
      app: ansible
  template:
    metadata:
      labels:
        app: ansible
    spec:
      volumes:
        - name: ansible-hosts
          configMap:
            name: ansible-hosts-configmap
      containers:
        - name: ansible
          image: registry.cn-shenzhen.aliyuncs.com/jbjb/dockers:ansible-v01
          command: ["sleep", "3333"]
          volumeMounts:
            - name: ansible-hosts
              mountPath: /etc/ansible/hosts
              subPath: hosts
EOF
相关推荐
木雷坞5 小时前
K8s GPU 推理服务 ImagePullBackOff 排查与预热
云原生·容器·kubernetes·gpu算力
吴爃6 小时前
Spring Boot 项目在 K8S 中的打包、部署与运维发布实践
运维·spring boot·kubernetes
萧行之6 小时前
Docker部署Loki+Grafana+Vector实现全服务器日志监控(含N8N/SSH/Fail2ban监控)
服务器·docker·grafana
人工智能培训7 小时前
工程科研中的AI应用:结构力学分析技巧
人工智能·深度学习·机器学习·docker·容器
计算机安禾8 小时前
【Linux从入门到精通】第35篇:容器化技术预备——Docker安装与基本概念
linux·运维·docker
子木HAPPY阳VIP8 小时前
信创UOS,Docker 完整操作部署(Dockerfile部署方式)&排错整合
linux·运维·redis·nginx·docker·容器·tomcat
The Straggling Crow8 小时前
Monitoring 2026-04-30
kubernetes
AOwhisky8 小时前
Kubernetes调度与服务暴露:从“定时任务”到“服务发现”的完全指南
linux·运维·云原生·容器·kubernetes·服务发现
Cyber4K8 小时前
【Kubernetes专项】温故而知新,重温技术原理(6)
云原生·容器·kubernetes
AI服务老曹9 小时前
架构实战:基于 GB28181 与 RTSP 的异构设备统一接入方案,深度解析 Docker 化 AI 视频管理平台
人工智能·docker·架构