一、虚拟机准备

干净的Centsot7.4、4G内存、2个CPU

最小化安装,最好带虚拟化

二、执行初始化脚本

注意:脚本中配置静态网卡根据实际网卡名称配置,我用的是ens33

可以用 sed -i "s/ens33/(实际网卡名)/g" 文件路径 进行替换

#!/bin/bash
echo "正在执行安装k8s环境初始化..."
#关闭防火墙
/usr/bin/iptables -F >/dev/null >&
/usr/bin/iptables -X >/dev/null >&
/usr/bin/systemctl disable firewalld.service >/dev/null >&
/usr/bin/systemctl stop firewalld.service >/dev/null >&
echo "执行关闭防火墙..." #禁用SELINUX
/usr/sbin/setenforce
/usr/bin/sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/' /etc/selinux/config
echo "执行关闭selinux..." #关闭swap
/usr/sbin/swapoff -a
echo "vm.swappiness = 0">> /etc/sysctl.conf
echo "执行关闭swap..." #配置静态网络
echo "配置静态网络..."
cat << EOF >/etc/sysconfig/network-scripts/ifcfg-ens33
TYPE="Ethernet"
BOOTPROTO="static"
IPADDR="`ifconfig ens33 | grep broadcast | awk -F " " '{print $2}'`"
NETMASK="`ifconfig ens33 | grep broadcast | awk -F " " '{print $4}'`"
GATREWAY="`route -n | grep UG | awk -F " " '{print$2}'`"
NAME="ens33"
DEVICE="ens33"
ONBOOT="yes"
PEERDNS="yes"
DNS1="114.114.114.114"
DNS2="8.8.8.8"
DNS3="`route -n | grep UG | awk -F " " '{print$2}'`"
EOF cat <<EOF >/etc/sysconfig/network
GATEWAY=`route -n | grep UG | awk -F " " '{print$2}'`
EOF #配置yum源
cat << EOF >/etc/yum.repos.d/.repo
[]
name=
baseurl=http://mirrors.163.com/centos/7/os/x86_64/
gpgcheck=
enable=
EOF cat << EOF >/etc/yum.repos.d/epel.repo
[epel]
name=epel
baseurl=https://mirrors.aliyun.com/epel/7/x86_64/
enabled=
gpgcheck=
EOF
echo "写入网络yum源..." #更新yum源
yum clean all >/dev/null >&
yum makecache >/dev/null >&
echo "更新yum源..." #安装wget、ansible
yum install -y wget >/dev/null >&
yum install -y ansible >/dev/null >&
echo "安装wget、ansible工具..." #定义ansible组
cat << EOF >>/etc/ansible/hosts
[k8s]
`ifconfig ens33 | grep broadcast | awk -F " " '{print $2}'`
EOF
echo "配置ansible组..." #设置主机名
echo -n "请输入主机名:"
read -p "" name
/usr/bin/hostnamectl --static set-hostname $name
if [ $? = ]
then
echo "修改主机名成功..."
else
echo "修改主机名失败..."
exit
fi echo "初始化完成..."
echo "正在执行重启操作..."
sleep
/usr/sbin/init

三、执行ssh进行主机验证

ssh root@本机ip

若不执行,ansible执行会报错

四、ansible部署k8s环境准备

注意:shell脚本我统一放在/root/start-sh/目录下

mkdir -p /root/start-sh/

cd /root/start-sh/

vim docker-k8s.sh

创建docker-k8s.sh脚本,其内容是拉取k8s镜像

#!/bin/bash
#下载镜像
docker pull mirrorgooglecontainers/kube-apiserver:v1.14.0
docker pull mirrorgooglecontainers/kube-controller-manager:v1.14.0
docker pull mirrorgooglecontainers/kube-scheduler:v1.14.0
docker pull mirrorgooglecontainers/kube-proxy:v1.14.0
docker pull mirrorgooglecontainers/pause:3.1
docker pull mirrorgooglecontainers/etcd:3.3.
docker pull coredns/coredns:1.3.
docker pull registry.cn-shenzhen.aliyuncs.com/cp_m/flannel:v0.10.0-amd64
#给镜像打tag
docker tag mirrorgooglecontainers/kube-apiserver:v1.14.0 k8s.gcr.io/kube-apiserver:v1.14.0
docker tag mirrorgooglecontainers/kube-controller-manager:v1.14.0 k8s.gcr.io/kube-controller-manager:v1.14.0
docker tag mirrorgooglecontainers/kube-scheduler:v1.14.0 k8s.gcr.io/kube-scheduler:v1.14.0
docker tag mirrorgooglecontainers/kube-proxy:v1.14.0 k8s.gcr.io/kube-proxy:v1.14.0
docker tag mirrorgooglecontainers/pause:3.1 k8s.gcr.io/pause:3.1
docker tag mirrorgooglecontainers/etcd:3.3. k8s.gcr.io/etcd:3.3.
docker tag coredns/coredns:1.3. k8s.gcr.io/coredns:1.3.
docker tag registry.cn-shenzhen.aliyuncs.com/cp_m/flannel:v0.10.0-amd64 quay.io/coreos/flannel:v0.10.0-amd64
#删除原镜像
docker rmi mirrorgooglecontainers/kube-apiserver:v1.14.0
docker rmi mirrorgooglecontainers/kube-controller-manager:v1.14.0
docker rmi mirrorgooglecontainers/kube-scheduler:v1.14.0
docker rmi mirrorgooglecontainers/kube-proxy:v1.14.0
docker rmi mirrorgooglecontainers/pause:3.1
docker rmi mirrorgooglecontainers/etcd:3.3.
docker rmi coredns/coredns:1.3.
docker rmi registry.cn-shenzhen.aliyuncs.com/cp_m/flannel:v0.10.0-amd64

创建k8s目录,编写kube-flannel.yaml文件(参照https://www.codesheep.cn/kube-flannel-yml/?spm=a2c4e.11153940.blogcont682810.11.6f853974iYJ4BU

该内容目的是pod的网络通信

mkdir -p /root/kube-system/

cd /root/kube-system/

vim kube-flannel.yaml

---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: flannel
rules:
- apiGroups:
- ""
resources:
- pods
verbs:
- get
- apiGroups:
- ""
resources:
- nodes
verbs:
- list
- watch
- apiGroups:
- ""
resources:
- nodes/status
verbs:
- patch
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: flannel
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: flannel
subjects:
- kind: ServiceAccount
name: flannel
namespace: kube-system
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: flannel
namespace: kube-system
---
kind: ConfigMap
apiVersion: v1
metadata:
name: kube-flannel-cfg
namespace: kube-system
labels:
tier: node
app: flannel
data:
cni-conf.json: |
{
"name": "cbr0",
"plugins": [
{
"type": "flannel",
"delegate": {
"hairpinMode": true,
"isDefaultGateway": true
}
},
{
"type": "portmap",
"capabilities": {
"portMappings": true
}
}
]
}
net-conf.json: |
{
"Network": "10.244.0.0/16",
"Backend": {
"Type": "vxlan"
}
}
---
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
name: kube-flannel-ds-amd64
namespace: kube-system
labels:
tier: node
app: flannel
spec:
template:
metadata:
labels:
tier: node
app: flannel
spec:
hostNetwork: true
nodeSelector:
beta.kubernetes.io/arch: amd64
tolerations:
- operator: Exists
effect: NoSchedule
serviceAccountName: flannel
initContainers:
- name: install-cni
image: quay.io/coreos/flannel:v0.10.0-amd64
command:
- cp
args:
- -f
- /etc/kube-flannel/cni-conf.json
- /etc/cni/net.d/10-flannel.conflist
volumeMounts:
- name: cni
mountPath: /etc/cni/net.d
- name: flannel-cfg
mountPath: /etc/kube-flannel/
containers:
- name: kube-flannel
image: quay.io/coreos/flannel:v0.10.0-amd64
command:
- /opt/bin/flanneld
args:
- --ip-masq
- --kube-subnet-mgr
resources:
requests:
cpu: "100m"
memory: "50Mi"
limits:
cpu: "100m"
memory: "50Mi"
securityContext:
privileged: true
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
volumeMounts:
- name: run
mountPath: /run
- name: flannel-cfg
mountPath: /etc/kube-flannel/
volumes:
- name: run
hostPath:
path: /run
- name: cni
hostPath:
path: /etc/cni/net.d
- name: flannel-cfg
configMap:
name: kube-flannel-cfg
---
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
name: kube-flannel-ds-arm64
namespace: kube-system
labels:
tier: node
app: flannel
spec:
template:
metadata:
labels:
tier: node
app: flannel
spec:
hostNetwork: true
nodeSelector:
beta.kubernetes.io/arch: arm64
tolerations:
- operator: Exists
effect: NoSchedule
serviceAccountName: flannel
initContainers:
- name: install-cni
image: quay.io/coreos/flannel:v0.10.0-arm64
command:
- cp
args:
- -f
- /etc/kube-flannel/cni-conf.json
- /etc/cni/net.d/10-flannel.conflist
volumeMounts:
- name: cni
mountPath: /etc/cni/net.d
- name: flannel-cfg
mountPath: /etc/kube-flannel/
containers:
- name: kube-flannel
image: quay.io/coreos/flannel:v0.10.0-arm64
command:
- /opt/bin/flanneld
args:
- --ip-masq
- --kube-subnet-mgr
resources:
requests:
cpu: "100m"
memory: "50Mi"
limits:
cpu: "100m"
memory: "50Mi"
securityContext:
privileged: true
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
volumeMounts:
- name: run
mountPath: /run
- name: flannel-cfg
mountPath: /etc/kube-flannel/
volumes:
- name: run
hostPath:
path: /run
- name: cni
hostPath:
path: /etc/cni/net.d
- name: flannel-cfg
configMap:
name: kube-flannel-cfg
---
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
name: kube-flannel-ds-arm
namespace: kube-system
labels:
tier: node
app: flannel
spec:
template:
metadata:
labels:
tier: node
app: flannel
spec:
hostNetwork: true
nodeSelector:
beta.kubernetes.io/arch: arm
tolerations:
- operator: Exists
effect: NoSchedule
serviceAccountName: flannel
initContainers:
- name: install-cni
image: quay.io/coreos/flannel:v0.10.0-arm
command:
- cp
args:
- -f
- /etc/kube-flannel/cni-conf.json
- /etc/cni/net.d/10-flannel.conflist
volumeMounts:
- name: cni
mountPath: /etc/cni/net.d
- name: flannel-cfg
mountPath: /etc/kube-flannel/
containers:
- name: kube-flannel
image: quay.io/coreos/flannel:v0.10.0-arm
command:
- /opt/bin/flanneld
args:
- --ip-masq
- --kube-subnet-mgr
resources:
requests:
cpu: "100m"
memory: "50Mi"
limits:
cpu: "100m"
memory: "50Mi"
securityContext:
privileged: true
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
volumeMounts:
- name: run
mountPath: /run
- name: flannel-cfg
mountPath: /etc/kube-flannel/
volumes:
- name: run
hostPath:
path: /run
- name: cni
hostPath:
path: /etc/cni/net.d
- name: flannel-cfg
configMap:
name: kube-flannel-cfg
---
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
name: kube-flannel-ds-ppc64le
namespace: kube-system
labels:
tier: node
app: flannel
spec:
template:
metadata:
labels:
tier: node
app: flannel
spec:
hostNetwork: true
nodeSelector:
beta.kubernetes.io/arch: ppc64le
tolerations:
- operator: Exists
effect: NoSchedule
serviceAccountName: flannel
initContainers:
- name: install-cni
image: quay.io/coreos/flannel:v0.10.0-ppc64le
command:
- cp
args:
- -f
- /etc/kube-flannel/cni-conf.json
- /etc/cni/net.d/10-flannel.conflist
volumeMounts:
- name: cni
mountPath: /etc/cni/net.d
- name: flannel-cfg
mountPath: /etc/kube-flannel/
containers:
- name: kube-flannel
image: quay.io/coreos/flannel:v0.10.0-ppc64le
command:
- /opt/bin/flanneld
args:
- --ip-masq
- --kube-subnet-mgr
resources:
requests:
cpu: "100m"
memory: "50Mi"
limits:
cpu: "100m"
memory: "50Mi"
securityContext:
privileged: true
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
volumeMounts:
- name: run
mountPath: /run
- name: flannel-cfg
mountPath: /etc/kube-flannel/
volumes:
- name: run
hostPath:
path: /run
- name: cni
hostPath:
path: /etc/cni/net.d
- name: flannel-cfg
configMap:
name: kube-flannel-cfg
---
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
name: kube-flannel-ds-s390x
namespace: kube-system
labels:
tier: node
app: flannel
spec:
template:
metadata:
labels:
tier: node
app: flannel
spec:
hostNetwork: true
nodeSelector:
beta.kubernetes.io/arch: s390x
tolerations:
- operator: Exists
effect: NoSchedule
serviceAccountName: flannel
initContainers:
- name: install-cni
image: quay.io/coreos/flannel:v0.10.0-s390x
command:
- cp
args:
- -f
- /etc/kube-flannel/cni-conf.json
- /etc/cni/net.d/10-flannel.conflist
volumeMounts:
- name: cni
mountPath: /etc/cni/net.d
- name: flannel-cfg
mountPath: /etc/kube-flannel/
containers:
- name: kube-flannel
image: quay.io/coreos/flannel:v0.10.0-s390x
command:
- /opt/bin/flanneld
args:
- --ip-masq
- --kube-subnet-mgr
resources:
requests:
cpu: "100m"
memory: "50Mi"
limits:
cpu: "100m"
memory: "50Mi"
securityContext:
privileged: true
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
volumeMounts:
- name: run
mountPath: /run
- name: flannel-cfg
mountPath: /etc/kube-flannel/
volumes:
- name: run
hostPath:
path: /run
- name: cni
hostPath:
path: /etc/cni/net.d
- name: flannel-cfg
configMap:
name: kube-flannel-cfg

yaml文件建好后使用一个脚本启动它

cd /root/start-sh/

vim start-pod-network

#!/bin/bash
#设置kubectl
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> /etc/profile
source /etc/profile
#安装pod network
/usr/sbin/sysctl net.bridge.bridge-nf-call-iptables=
kubectl apply -f /root/k8s/kube-system/kube-flannel.yaml

准备好k8s需要的环境文件后编写ansible剧本

mkdir -p /root/asnible

cd /root/ansible

vim k8s.yml

注意:init k8s我使用了一个awk获取本地ip也是根据ens33网卡

---
- hosts: k8s
remote_user: root
tasks:
- name: off iptables
shell: iptables -F && iptables -X
- name: wget CentOS-Base.repo
shell: wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
- name: update CentOS-Base.repo
shell: sed -i "s/[$]releasever/7/g" /etc/yum.repos.d/CentOS-Base.repo
- name: k8s.repo
shell: echo -e [kubernetes]"\n"name=Kubernetes Repo"\n"baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/"\n"gpgcheck=0"\n"gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg >/etc/yum.repos.d/k8s.repo
- name: wget docker-ce.repo
shell: wget http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker.repo
- name: yum makecache
shell: yum clean all && yum makecache
- name: install epel-release
yum: name=epel-release state=present
- name: install container-selinux
yum: name=container-selinux state=present
- name: install docker
yum: name=docker state=present
- name: update docker-selinux
shell: sed -i "s/OPTIONS='--selinux-enabled --log-driver=journald --signature-verification=false'/OPTIONS='--log-driver=journald --signature-verification=false'/g" /etc/sysconfig/docker
- name: start docker
service: name=docker enabled=yes state=restarted
- name: install kubelet
yum: name=kubelet state=present
- name: install kubeadm
yum: name=kubeadm state=present
- name: install kubectl
yum: name=kubectl state=present
- name: start kubelet
service: name=kubelet enabled=yes state=restarted
- name: pull k8s iso
shell: bash /root/start-sh/docker-k8s.sh
- name: off swap
shell: swapoff -a
- name: init k8s
ignore_errors: yes
shell: kubeadm init --kubernetes-version=v1.14.0 --apiserver-advertise-address `ifconfig ens33 | grep broadcast | awk -F " " '{print $2}'` --pod-network-cidr=10.244.0.0/
- name: install pod network
script: /root/start-sh/start-pod-network.sh
- name: source kubcetl
shell: source /etc/profile

五、执行ansible一键部署

cd /root/ansible/

ansible-playbook k8s.yml --ask-pass

输入密码

开始执行中,过程会有些慢,因为需拉取镜像

六、验证pod是否正常

完成后执行 kubectl get pod -n kube-system查看pod状态

若输入命令无效,再执行一次source /etc/profile

k8s环境部署好了

------------------------------------------------------------------------------------------------------------------------------------

技术不足望多见谅,若ansible有更好的优化方式可以留言交流

谢谢,祝工作顺利,身体健康

ansible一键部署k8s单机环境的更多相关文章

  1. Ansible自动化部署K8S集群

    Ansible自动化部署K8S集群 1.1 Ansible介绍 Ansible是一种IT自动化工具.它可以配置系统,部署软件以及协调更高级的IT任务,例如持续部署,滚动更新.Ansible适用于管理企 ...

  2. 使用Docker 一键部署 LNMP+Redis 环境

    使用Docker 部署 LNMP+Redis 环境 Docker 简介 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linu ...

  3. 【Kubernetes学习笔记】-使用Minikube快速部署K8S单机学习环境

    介绍 https://minikube.sigs.k8s.io/docs/ Minikube 用于快速在本地搭建 Kubernetes 单节点集群环境,它对硬件资源没有太高的要求,方便开发人员学习试用 ...

  4. 一键部署k8s

    本人学习安装kubernetes时,顺便整理了安装脚本,可以通过执行一个脚本,自动二进制安装好1台master+2台node的k8环境.方便需要学习k8s的同学. 百度网盘:https://pan.b ...

  5. 福利来了~Linux一键部署包,环境安装不用愁!!!

    前言 昨天一哥们的弟弟突然问我有没有部署过的Linux,公司连个运维都没有,服务器都要后端部署.... 你有没有相似的遭遇呢?公司规模小,后端即是运维,一份工资干两份活,哈哈~ 为了解决这老弟的困惑, ...

  6. oneinstack一键部署linux生产环境那点事(ubuntu)

    http://oneinstack.com/install/ (1)将oneinstack-full.tar.gz最新版安装文件上传至/usr/local/下 (2)解压tar xzvf oneins ...

  7. 通过ansible一键部署集群ntp时间同步

    环境准备 [root@server ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@server ~]# uname -r  ...

  8. ansible 一键部署

    start install: rpm   -Uvh   https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm yum  in ...

  9. ansible一键部署LAMP

    一.实现ansible跟节点间无密码访问,不会配置的请看 文章 . 二.创建目录 $ mkdir -p playbooks/{files,templates}   三.创建php测试文件index.p ...

随机推荐

  1. 洗礼灵魂,修炼python(30)--装饰器(2)—>装饰器总结+进阶使用

    在上一篇博文的经典案例中,我想你应该对装饰器有很好的了解了,不过光有那些还不够真的,还需要总结和进阶一下,所以本篇博文解析装饰器进阶. 装饰器 1.什么是装饰器? 个人理解:装饰器又叫语法糖,指的是对 ...

  2. 【PAT】B1055 集体照(25 分)

    很简单的two points问题 ##注意:K是行数 #include<stdio.h> #include<string.h> #include<map> #inc ...

  3. 【Beta Scrum】冲刺!5/5

    1. 今日完成情况 人员 学号 分工 是否完成 完成情况 胡武成 031502610 解决短信内容,辅助web端解决在线编辑 Y 短信已解决,在线编辑已有解决方案 郭剑南 031502609 修改we ...

  4. 使用golang的slice来模拟栈

    slice(切片):底层数据结构是数组 stack(栈):一种先进后出的数据结构 普通版的模拟写入和读取的栈 package main import "fmt" //栈的特点是先进 ...

  5. ByteArrayInputStream与ByteArrayOutputStream_操作数组的流

    import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOExceptio ...

  6. 接上篇elasticsecrchi 进行搜索及时提示,数据库以及后台代码

    -- ------------------------------ Table structure for articles-- ----------------------------DROP TA ...

  7. 从头到尾使用Geth的说明-2-cli可用命令-有2个地方标红,之后查查源码后看看能不能解决

    geth - the go-ethereum command line interface 以太坊命令行接口 格式: geth [options] command [command options] ...

  8. 从头到尾使用Geth的说明-1-安装

    Geth 1.安装https://github.com/ethereum/go-ethereum/wiki/Installation-Instructions-for-Mac 1.首先先安装Homeb ...

  9. Rinkeby中测试币的申请

    https://www.rinkeby.io/#faucet 从这个页面可以看见测试币的请求是有要求的,下面说明怎么做: 之前使用的是google账号,即第二种方式来获取.后面gmail出现了问题,一 ...

  10. P1-Linux下安装MySQL及登录用户配置

    Linux下安装MySQL及登录用户配置 环境:Centos7.4    Mysql5.6 1. 查询是否安装 MySQL和MariaDB rpm -qa | grep -i mysql rpm -q ...