Kubernetes笔记(一):十分钟部署一套K8s环境
【空山新雨】 人气:1
Kubernetes是Goole开源的一个容器编排引擎,它支持自动化部署、大规模可伸缩、应用容器化管理 —— 百度百科。
接触K8s也有半年多了,也基于阿里云平台搭建了包含多级服务、目前运行较为稳定的K8s集群(感兴趣的可参考 [k8s云集群混搭模式,可能帮你节省50%以上的服务成本](https://mp.weixin.qq.com/s/8s-OUUsZI0UyyyTgruEO1A), [k8s云集群混搭模式落地分享](https://mp.weixin.qq.com/s/VuSWn7cWacW6i_MtpvQ5Sw)),但一直没来得及对其进行系统的学习,本系列文章还像以前Docker系列一样,以笔记的形式进行记录与分享,会包括理论与实践,感兴趣的同学可以关注,一起探索下目前较为流行的容器化及服务编排解决方案。
工欲善其事,必先利其器,本文先介绍如何在本地自行搭建一套k8s集群,并且采用我们前面介绍过的Ansible来提高效率(参考 [Ansible简明教程](https://mp.weixin.qq.com/s/JIZE1RvN7Yop5dsOHJvStw))
本文所涉及的所有配置文件可在这里找到 [github](https://github.com/ronwxy/ops/tree/master/k8s/installation)
## 一. 准备服务器节点
如果还没有服务器,可以参考 [ubuntu18.04上搭建KVM虚拟机环境超完整过程](https://mp.weixin.qq.com/s/FVyzPVwwQ85AC4jlVZvF4g) 创建虚拟服务器。
服务器节点IP(hostname):
- 192.168.40.111 (kmaster)
- 192.168.40.112 (knode1)
- 192.168.40.113 (knode2)
- 192.168.40.114 (knode3)
操作系统版本:
- `cat /etc/redhat-release` : CentOS Linux release 7.6.1810 (Core)
- `uname -a` : 3.10.0-957.el7.x86_64
## 二. 配置Ansible
如果还没有Ansible环境,可以参考 [Ansible简明教程]https://mp.weixin.qq.com/s/JIZE1RvN7Yop5dsOHJvStw) 搭建。
1.在Ansible服务器上的/etc/hosts文件中添加k8s服务器节点信息(参考 [hosts](https://github.com/ronwxy/ops/blob/master/k8s/installation/basic/hosts))
```shell
192.168.40.111 kmaster
192.168.40.112 knode1
192.168.40.113 knode2
192.168.40.114 knode3
```
2.在Ansible服务器上的/etc/ansible/hosts文件中添加k8s服务器节点(参考 [ansible_hosts](https://github.com/ronwxy/ops/blob/master/k8s/installation/basic/ansible_hosts))
```shell
[k8s-all]
kmaster
knode1
knode2
knode3
[k8s-master]
kmaster
[k8s-nodes]
knode1
knode2
knode3
```
## 三. 修改k8s集群各节点/etc/hosts(非必须)
修改所有主机/etc/hosts文件,添加IP/主机名映射,方便通过主机名ssh访问
1.创建playbook文件(参考 [set_hosts_playbook.yml](https://github.com/ronwxy/ops/blob/master/k8s/installation/basic/set_hosts_playbook.yml))
```shell
vim set_hosts_playbook.yml
---
- hosts: k8s-all
remote_user: root
tasks:
- name: backup /etc/hosts
shell: mv /etc/hosts /etc/hosts_bak
- name: copy local hosts file to remote
copy: src=/etc/hosts dest=/etc/ owner=root group=root mode=0644
```
2.执行ansible-playbook
```shell
ansible-playbook set_hosts_playbook.yml
```
## 四. 安装Docker
在所有主机上安装Docker
1.创建playbook文件(参考 [install_docker_playbook.yml](https://github.com/ronwxy/ops/blob/master/k8s/installation/basic/install_docker_playbook.yml))
```shell
vim install_docker_playbook.yml
- hosts: k8s-all
remote_user: root
vars:
docker_version: 18.09.2
tasks:
- name: install dependencies
#shell: yum install -y yum-utils device-mapper-persistent-data lvm2
yum: name={{item}} state=present
with_items:
- yum-utils
- device-mapper-persistent-data
- lvm2
- name: config yum repo
shell: yum-config-manager --add-repo https://mirrors.ustc.edu.cnhttps://img.qb5200.com/download-x/docker-ce/linux/centoshttps://img.qb5200.com/download-x/docker-ce.repo
- name: install docker
yum: name=docker-ce-{{docker_version}} state=present
- name: start docker
shell: systemctl enable docker && systemctl start docker
```
2.执行ansible-playbook
```shell
ansible-playbook install_docker_playbook.yml
```
## 五. 部署k8s master
1.开始部署之前,需要做一些初始化处理:关闭防火墙、关闭selinux、禁用swap、配置k8s阿里云yum源等,所有操作放在脚本 [pre-setup.sh](https://github.com/ronwxy/ops/blob/master/k8s/installation/basic/pre-setup.sh) 中,并在2中playbook中通过script模块执行
2.创建playbook文件 [deploy_master_playbook.yml](https://github.com/ronwxy/ops/blob/master/k8s/installation/basichttps://img.qb5200.com/download-x/deploy_master_playbook.yml),只针对master节点,安装kubectl,kubeadm,kubelet,以及flannel(将kube-flannel.yml文件里镜像地址的quay.io改为quay-mirror.qiniu.com避免超时,参考 [kube-flannel.yml](https://github.com/ronwxy/ops/blob/master/k8s/installation/basic/kube-flannel.yml))
```shell
vim deploy_master_playbook.yml
- hosts: k8s-master
remote_user: root:q
vars:
kube_version: 1.16.0-0
k8s_version: v1.16.0
k8s_master: 192.168.40.111
tasks:
- name: prepare env
script: ./pre-setup.sh
- name: install kubectl,kubeadm,kubelet
yum: name={{item}} state=present
with_items:
- kubectl-{{kube_version}}
- kubeadm-{{kube_version}}
- kubelet-{{kube_version}}
- name: init k8s
shell: kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version {{k8s_version}} --apiserver-advertise-address {{k8s_master}} --pod-network-cidr=10.244.0.0/16 --token-ttl 0
- name: config kube
shell: mkdir -p $HOME/.kube && cp -i /etc/kubernetes/admin.conf $HOME/.kube/config && chown $(id -u):$(id -g) $HOME/.kube/config
- name: copy flannel yaml file
copy: src=./kube-flannel.yml dest=/tmp/ owner=root group=root mode=0644
- name: install flannel
shell: kubectl apply -f /tmp/kube-flannel.yml
- name: get join command
shell: kubeadm token create --print-join-command
register: join_command
- name: show join command
debug: var=join_command verbosity=0
```
3.执行ansible-playbook
```shell
ansible-playbook deploy_master_playbook.yml
```
4.上述命令执行完成会输出节点加入k8s集群的命令,如下图。记下该命令,后面部署node时会用到
![图片](https://img2020.cnblogs.com/other/632381/202004/632381-20200428113737900-1659324532.png)
## 六. 部署k8s node
1.同master一样,开始部署之前,需要做一些初始化处理:关闭防火墙、关闭selinux、禁用swap、配置k8s阿里云yum源等,所有操作放在脚本 [pre-setup.sh](https://github.com/ronwxy/ops/blob/master/k8s/installation/basic/pre-setup.sh) 中,并在2中playbook中通过script模块执行
2.创建playbook文件 [deploy_nodes_playbook.yml](https://github.com/ronwxy/ops/blob/master/k8s/installation/basichttps://img.qb5200.com/download-x/deploy_nodes_playbook.yml),针对除master外的其它集群节点,安装kubeadm,kubelet,并将节点加入到k8s集群中,使用的是前面部署master时输出的加入集群命令
```shell
vim deploy_nodes_playbook.yml
- hosts: k8s-nodes
remote_user: root
vars:
kube_version: 1.16.0-0
tasks:
- name: prepare env
script: ./pre-setup.sh
- name: install kubeadm,kubelet
yum: name={{item}} state=present
with_items:
- kubeadm-{{kube_version}}
- kubelet-{{kube_version}}
- name: start kubelt
shell: systemctl enable kubelet && systemctl start kubelet
- name: join cluster
shell: kubeadm join 192.168.40.111:6443 --token zgx3ov.zlq3jh12atw1zh8r --discovery-token-ca-cert-hash sha256:60b7c62687974ec5803e0b69cfc7ccc2c4a8236e59c8e8b8a67f726358863fa7
```
3.执行ansible-playbook
```shell
ansible-playbook deploy_nodes_playbook.yml
```
4.稍等片刻,即可在master节点上通过`kubectl get nodes`看到加入到集群中的节点,并且status为Ready状态,如下
```shell
[root@kmaster ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
kmaster Ready master 37m v1.16.0
knode1 Ready
加载全部内容