一、概念
k8s(Kubernetes):因为中间有八个字母,所以国人称k8s。
二、安装方法
yum安装:最简单,默认1.5.2版
编译安装:最难(大佬级别) golang环境
二进制安装:最繁琐
kubeadm:官方安装(网络)kubelet二进制,其他k8s组件全是容器
minikube:单机版,只适合体验
注意:默认yum安装docker是1.13版的,但是会出现容器之间通信不了,必须要降到1.12版的。
三、安装以往的docker版本(1.12)。
1、下载地址:http://vault.centos.org/7.4.1708/extras/x86_64/Packages/
wget http://vault.centos.org/7.4.1708/extras/x86_64/Packages/docker-common-1.12.6-71.git3e8e77d.el7.centos.x86_64.rpm
wget http://vault.centos.org/7.4.1708/extras/x86_64/Packages/docker-client-1.12.6-71.git3e8e77d.el7.centos.x86_64.rpm
wget http://vault.centos.org/7.4.1708/extras/x86_64/Packages/docker-1.12.6-71.git3e8e77d.el7.centos.x86_64.rpm
2、安装
yum localinstall docker-common-1.12.6-71.git3e8e77d.el7.centos.x86_64.rpm -y
yum localinstall docker-client-1.12.6-71.git3e8e77d.el7.centos.x86_64.rpm -y
yum localinstall docker-1.12.6-71.git3e8e77d.el7.centos.x86_64.rpm -y
3、查看版本
docker version
4、master节点安装etcd(k8s数据库kv类型存储),原生支持做集群
[root@k8s-master opt]# yum install etcd.x86_64 -y
5、修改配置文件第6行,第21行。
vim /etc/etcd/etcd.conf
6、启动服务
[root@k8s-master opt]# systemctl start etcd.service
[root@k8s-master opt]# systemctl enable etcd.service
[root@k8s-master opt]# netstat -ntl
7、写入测试数据
[root@k8s-master opt]# etcdctl set testdir/testkey0 oldboy 写入值
[root@k8s-master opt]# etcdctl get testdir/testkey0 获取值
8、集群健康检查
[root@k8s-master opt]# etcdctl -C http://192.168.125.130:2379 cluster-health
member 8e9e05c52164694d is healthy: got healthy result from http://192.168.125.130:2379
cluster is healthy
9、安装
[root@k8s-master opt]# yum install kubernetes-master.x86_64 -y
[root@k8s-master opt]# vim /etc/kubernetes/apiserver
23行,不删除,会有一个坑,后面这里会报错
修改配置文件:
[root@k8s-master opt]# vim /etc/kubernetes/config
10、启动服务
[root@k8s-master ~]# systemctl enable kube-apiserver.service
[root@k8s-master ~]# systemctl start kube-apiserver.service
[root@k8s-master ~]# systemctl enable kube-controller-manager.service
[root@k8s-master ~]# systemctl start kube-controller-manager.service
[root@k8s-master ~]# systemctl enable kube-scheduler.service
[root@k8s-master ~]# systemctl start kube-scheduler.service
api-server:接受并响应用户的请求
controller:控制器的概念,保证容器存活
schedule:调度器,选择启动容器的node节点
kubelet:通过调用docker管理容器的生命周期
kube-proxy :让容器被外界访问
11、在两个node节点安装服务
yum install kubernetes-node.x86_64 -y
12、修改配置文件
[root@k8s-node1 ~]# vim /etc/kubernetes/config
[root@k8s-node1 ~]# vim /etc/kubernetes/kubelet
13、两个node节点启动服务
[root@k8s-node1 ~]# systemctl start kubelet
[root@k8s-node1 ~]# systemctl enable kubelet
[root@k8s-node1 ~]# systemctl start kube-proxy
[root@k8s-node1 ~]# systemctl enable kube-proxy
[root@k8s-node1 ~]# systemctl status docker
[root@k8s-master ~]# setenforce 0
[root@k8s-node1 ~]# setenforce 0
[root@k8s-node2 ~]# setenforce 0
[root@k8s-master ~]# systemctl stop firewalld
[root@k8s-master ~]# systemctl disable firewalld
[root@k8s-master ~]# kubectl get nodes 查看哪些node节点可用
14、三台节点都要安装flannel网络
yum install flannel -y
15、三台节点都执行,修改flannl配置文件
sed -i 's#http://127.0.0.1:2379#http://192.168.125.130:2379#g' /etc/sysconfig/flanneld
16、重启服务
master节点执行:
[root@k8s-master /]# etcdctl mk /atomic.io/network/config '{"Network":"172.16.0.0/16"}' 设置网段
[root@k8s-master /]# systemctl enable flanneld.service
[root@k8s-master /]# systemctl start flanneld.service
[root@k8s-master /]# service docker restart
[root@k8s-master /]# systemctl restart kube-apiserver.service
[root@k8s-master /]# systemctl restart kube-controller-manager.service
[root@k8s-master /]# systemctl restart kube-scheduler.service
node:节点执行:
[root@k8s-node2 ~]# systemctl enable flanneld
[root@k8s-node2 ~]# systemctl start flanneld.service
[root@k8s-node2 ~]# service docker restart
[root@k8s-node2 ~]# systemctl restart kubelet.service
[root@k8s-node2 ~]# systemctl restart kube-proxy.service
17、测试容器网络连通性
[root@k8s-master ~]# ifconfig 查看flannel网卡IP地址
[root@k8s-master ~]# docker pull busybox pull一个专门测试网络的小镜像
[root@k8s-master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/busybox latest 1c35c4412082 11 days ago 1.219 MB
[root@k8s-master ~]# docker save docker.io/busybox:latest >docker_busybox.tar.gz 下载镜像
[root@k8s-master ~]# scp docker_busybox.tar.gz [email protected]:/opt 拷贝镜像至其他node节点上
[root@k8s-node1 opt]# docker load -i docker_busybox.tar.gz 导入镜像
[root@k8s-master opt]# docker run -it docker.io/busybox /bin/sh 进入容器,三台一起执行
18、创建本地仓库
[root@k8s-node2 opt]# vim /etc/sysconfig/docker 修改docker配置文件,三台主机上都要修改,由于这个docker版本低,所以是修改这个文件
加上 --registry-mirror=https://registry.docker-cn.com --insecure-registry=192.168.125.130:5000 这一句
19、创建私有仓库
[root@k8s-master opt]# docker pull hub.c.163.com/library/registry:latest 从网上pull包
[root@k8s-master opt]# docker images 查看镜像
[root@k8s-master opt]# docker tag 751f286bc25e registry:latest 重命名
[root@k8s-master opt]# docker rmi hub.c.163.com/library/registry:latest 删除原来的镜像
[root@k8s-master opt]# docker run -d -p 5000:5000 --restart=always --name registry -v /opt/myregistry:/var/lib/registry registry 起一个私有仓库
[root@k8s-master opt]# docker ps
20、pull安装nginx镜像
[root@k8s-master opt]# docker pull nginx:latest
[root@k8s-master opt]# docker run -it docker.io/nginx:latest /bin/bash
root@ff33fb456480:/# nginx -v 查看版本
nginx version: nginx/1.19.0
在容器中安装vim命令
apt-get update
apt-get install vim
21、上传至私有仓库
[root@k8s-master opt]# docker tag docker.io/nginx:latest 192.168.125.130:5000/nginx:1.19
[root@k8s-master opt]# docker push 192.168.125.130:5000/nginx:1.19
22、扩展拉取mysql镜像
[root@k8s-master opt]# docker pull mysql:5.7
[root@k8s-master opt]# docker images
[root@k8s-master opt]# docker run -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7 必须要初始化mysql的密码,容器才能拉起来
[root@k8s-master opt]# sudo docker exec -it mysql bash
root@52d7ccd55eaa:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.30 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
注意:本文有不理解之处,加QQ:1300536862,探讨
推荐:需要租用云服务器,域名等其他服务的可以扫描下方二维码,云服务器免费领取使用,华为云服务: