Kubernetes筆記(一):十分鐘部署一套K8s環境
阿新 • • 發佈:2020-04-28
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.cn/docker-ce/linux/centos/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/basic/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/basic/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 R