1)版本统一
- Docker 18.09.0
- kubeadm-1.14.0-0
- kubelet-1.14.0-0
- kubectl-1.14.0-0
- k8s.gcr.io/kube-apiserver:v1.14.0
- k8s.gcr.io/kube-controller-manager:v1.14.0
- k8s.gcr.io/kube-scheduler:v1.14.0
- k8s.gcr.io/kube-proxy:v1.14.0
- k8s.gcr.io/pause:3.1
- k8s.gcr.io/etcd:3.3.10
- k8s.gcr.io/coredns:1.3.1
- calico:v3.9
2)准备三台centos7环境
mkdir D:VMk8s-docker-centos7 【创建目录】
cd D:VMk8s-docker-centos7 【切换目录】
type nul>Vagrantfile 【创建Vagrantfile文件】
2.1)Vagrantfile 【编辑文件】
boxes = [
{
:name => "master-kubeadm-k8s",
:eth1 => "192.168.0.21",
:mem => "2048",
:cpu => "2",
:sshport => 22230
},
{
:name => "worker01-kubeadm-k8s",
:eth1 => "192.168.0.22",
:mem => "2048",
:cpu => "2",
:sshport => 22231
},
{
:name => "worker02-kubeadm-k8s",
:eth1 => "192.168.0.23",
:mem => "2048",
:cpu => "2",
:sshport => 22232
}
]
Vagrant.configure(2) do |config|
config.vm.box = "centos/7"
boxes.each do |opts|
config.vm.define opts[:name] do |config|
config.vm.hostname = opts[:name]
config.vm.network :public_network, ip: opts[:eth1]
config.vm.network "forwarded_port", guest: 22, host: 2222, id: "ssh", disabled: "true"
config.vm.network "forwarded_port", guest: 22, host: opts[:sshport]
config.vm.provider "vmware_fusion" do |v|
v.vmx["memsize"] = opts[:mem]
v.vmx["numvcpus"] = opts[:cpu]
end
config.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--memory", opts[:mem]]
v.customize ["modifyvm", :id, "--cpus", opts[:cpu]]
v.customize ["modifyvm", :id, "--name", opts[:name]]
end
end
end
end
2.2)添加下载好的centos7的镜像到virtualbox
vagrant box add centos7 E:tmpvagrant-centos-7.2.box
2.3)根据当前目录的Vagrantfile配置文件启动虚拟机
vagrant up
2.4)连接三台机器并进行相关配置
vagrant ssh master-kubeadm-k8s 【进入manager-node】
vagrant ssh worker01-kubeadm-k8s 【进入worker01-node】
vagrant ssh worker02-kubeadm-k8s 【进入worker02-node】
改成可以密码登陆root账户sudo -i 【进入root账户】
vi /etc/ssh/sshd_config 【设置root账户可以密码登陆】
修改PasswordAuthentication yes
passwd 【修改密码】
systemctl restart sshd 【重启sshd服务】
2.5)用xshell连接三台机器
主0:192.168.0.21
从1:192.168.0.22
从2:192.168.0.23
2.6)配置静态ip避免重启重新分配ip
2.7)更新yml源
yum -y update
2.8)安装相关组件
yum install -y conntrack ipvsadm ipset jq sysstat curl iptables libseccomp
组件功能简述:
conntrack 【连接跟踪(CONNTRACK),顾名思义,就是跟踪并且记录连接状态。Linux为每一个经过网络堆栈的数据包,生成一个新的连接记录项】
ipvsadm
【用于管理和监控虚拟机服务lvs的组件】
ipset 【用于管理ip的组件】
jq 【把文本字符串格式化成json格式的工具】
sysstat 【它带有众多的系统资源监控工具,用于监控系统的性能和使用情况,在日常使用的工具中有相当一部分是来自sysstat工具包提供使用cron表达式来制定性能和活动数据的收集计划】
curl 【文件传输的工具,支持文件的上传、下载功能】
iptables 【防火墙框架,数据包过滤、数据包转发等功能】
libseccomp【用于容器管理的包,管理namespaces
、cgroups
、capabilities
以及文件系统来对容器控制】
2.9)卸载之前安装的docker
sudo yum remove docker docker latest docker-latest-logrotate
docker-logrotate docker-engine docker-client docker-client-latest docker-common
2.10)安装必要依赖
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
2.11)添加软件源信息
sudo yum-config-manager
--add-repo http:// mirrors.aliyun.com/dock er-ce/linux/centos/docker-ce.repo
查找软件源中某软件的信息
yum list | grep docker-ce
2.12)更新yum缓存
sudo yum makecache fast
3)安装docker
sudo yum install -y docker-ce-18.09.0 docker-ce-cli-18.09.0 http:// containerd.io 【指定安装docker版本】
3.1)启动docker并设置开机启动
sudo systemctl start docker && sudo systemctl enable docker
测试docker安装是否成功
docker -v
3.2)修改hosts文件【简化命令】
01master节点
设置master的hostname,并且修改hosts文件 sudo hostnamectl set-hostname m
02 两个worker
设置worker01/02的hostname,并且修改hosts文件 sudo hostnamectl set-hostname w1 sudo hostnamectl set-hostname w2
03 修改三台机器hosts文件,添加内容
vi /etc/hosts
192.168.0.21 m
192.168.0.22 w1
192.168.0.23 w2
04 使用ping测试一下
ping m
ping w1
ping w2
4)系统防火墙及参数配置
01 关闭防火墙
systemctl stop firewalld && systemctl disable firewalld
02 关闭selinux
setenforce 0
sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
03 关闭swap
swapoff -a
sed -i '/swap/s/^(.*)$/#1/g' /etc/fstab
04 配置iptables的ACCEPT规则
iptables -F && iptables -X && iptables -F -t nat && iptables -X -t nat && iptables -P FORWARD ACCEPT
05 设置系统参数
cat </etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
06 查看系统参数
sysctl --system
5)Installing kubeadm, kubelet and kubectl
01 `配置yum源`
# =======================================================
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
=======================================================
#
02 安装kubeadm&kubelet&kubectl
yum install -y kubeadm-1.14.0-0 kubelet-1.14.0-0 kubectl-1.14.0-0
03 docker和k8s设置同一个cgroup
vi /etc/docker/daemon.json
{
"exec-opts": ["native.cgroupdriver=systemd"]
}
systemctl restart docker 【重启docker,一定要执行】
sed -i "s/cgroup-driver=systemd/cgroup-driver=cgroupfs/g" /etc/systemd/system/kubelet.service.d/10-kubeadm.conf 【找不到内容没关系】
systemctl enable kubelet && systemctl start kubelet 【重启kubelet,一定要执行】
6)proxy/pause/scheduler等国内镜像
01 查看kubeadm使用的镜像
kubeadm config images list
# =======================================================
http://k8s.gcr.io/kube-apiserver:v1.14.0
http://k8s.gcr.io/kube-controller-manager:v1.14.0
http://k8s.gcr.io/kube-scheduler:v1.14.0
http://k8s.gcr.io/kube-proxy:v1.14.0
http://k8s.gcr.io/coredns:1.3.1
# =======================================================
02 解决国外镜像不能访问的问题
# 创建kubeadm.sh脚本,用于拉取镜像/打tag/删除原有镜像
vi kubeadm.sh
#!/bin/bash
set -e
KUBE_VERSION=v1.14.0
KUBE_PAUSE_VERSION=3.1
ETCD_VERSION=3.3.10
CORE_DNS_VERSION=1.3.1
GCR_URL=k8s.gcr.io
ALIYUN_URL=registry.cn-hangzhou.aliyuncs.com/google_containers
images=(kube-proxy:${KUBE_VERSION}
kube-scheduler:${KUBE_VERSION}
kube-controller-manager:${KUBE_VERSION}
kube-apiserver:${KUBE_VERSION}
pause:${KUBE_PAUSE_VERSION}
etcd:${ETCD_VERSION}
coredns:${CORE_DNS_VERSION})
for imageName in ${images[@]} ; do
docker pull $ALIYUN_URL/$imageName
docker tag $ALIYUN_URL/$imageName $GCR_URL/$imageName
docker rmi $ALIYUN_URL/$imageName
done
03 运行脚本和查看镜像
sh ./kubeadm.sh 【运行脚本】
docker images 【查看镜像】
7)kube init初始化master
官网:https://kubernetes.io/docs/reference/setup-tools/kubeadm/kubeadm/
初始化master节点
7.1) 初始化master节点
kubeadm reset 【初始化集群状态】
kubeadm init --kubernetes-version=1.14.0 --apiserver-advertise-address=192.168.0.21 --pod-network-cidr=10.244.0.0/16 【初始化master节点】
注意:记得保存好最后kubeadm join的信息。
Your Kubernetes control-plane has initialized successfully!
To start using your cluster, you need to run the following as a regular user:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
Then you can join any number of worker nodes by running the following on each as roo
t:
kubeadm join 192.168.0.21:6443 --token fy9jmz.ybb30vs70p061qz4
--discovery-token-ca-cert-hash sha256:9b642133470fb3eec3cd3731c8f93b3d6dd111dba2ee47506892dcbe57ce35ff
7.2)根据日志提示执行
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
7.3) 查看pod
等待一会儿,同时可以发现像etcd,controller,scheduler等组件都以pod的方式安装成功了
# 注意:coredns没有启动,需要安装网络插件
kubectl get pods -n kube-system 【查看kube-system的pods】
kubectl get pods --all-namespaces 【查看所有pods】
# =======================================================================================
NAME READY STATUS RESTARTS AGE
coredns-fb8b8dccf-f7g6g 0/1 Pending 0 7m30s
coredns-fb8b8dccf-hx765 0/1 Pending 0 7m30s
etcd-m 1/1 Running 0 6m30s
kube-apiserver-m 1/1 Running 0 6m36s
kube-controller-manager-m 1/1 Running 0 6m42s
kube-proxy-w9m72 1/1 Running 0 7m30s
kube-scheduler-m 1/1 Running 0 6m24s
# =======================================================================================
7.4)健康检查
curl -k https:// localhost:6443/healthz
输出ok表示正常
kube init流程
01 `进行一系列检查,以确定这台机器可以部署kubernetes`
02 `生成kubernetes对外提供服务所需要的各种证书可对应目录`
ls /etc/kubernetes/pki/*
03 `为其他组件生成访问kube-ApiServer所需的配置文件`
ls /etc/kubernetes/
admin.conf controller-manager.conf kubelet.conf scheduler.conf
04 `为 Master组件生成Pod配置文件`
ls /etc/kubernetes/manifests/*.yaml
kube-apiserver.yaml
kube-controller-manager.yaml
kube-scheduler.yaml
05 `生成etcd的Pod YAML文件`
ls /etc/kubernetes/manifests/*.yaml
kube-apiserver.yaml
kube-controller-manager.yaml
kube-scheduler.yaml
etcd.yaml
06 `一旦这些 YAML文件出现在被 kubelet监视的/etc/kubernetes/manifests/目录下,kubelet就会自动创建这些yaml文件定义的pod,即master组件的容器。master容器启动后,kubeadm会通过检查localhost:443/healthz这个master组件的健康状态检查URL,等待master组件完全运行起来`
07 `为集群生成一个bootstrap token`
08 `将ca.crt等Master节点的重要信息,通过ConfigMap的方式保存在etcd中,工后续部署node节点使用`
09 `最后一步是安装默认插件,kubernetes默认kube-proxy和DNS两个插件是必须安装的`
8)部署calico网络插件
# 选择网络插件
https://kubernetes.io/docs/concepts/cluster-administration/addons/
# 这里选择calico网络插件
https://docs.projectcalico.org/v3.9/getting-started/kubernetes/
# 注意:calico,同样在master节点上操作
01 `可以先手动pull一下` 【可能拉取较慢】
curl https:// docs.projectcalico.org/ v3.9/manifests/calico.yaml | grep image 【版本会变化,需要根据实际情况拉取镜像,查看yaml文件中需要的镜像】
踩坑1:虚拟机cpu与内存需要大于等于2
# =======================================================
image: calico/cni:v3.9.3
image: calico/pod2daemon-flexvol:v3.9.3
image: calico/node:v3.9.3
image: calico/kube-controllers:v3.9.3
# =======================================================
docker pull calico/cni:v3.9.3
docker pull calico/pod2daemon-flexvol:v3.9.3
docker pull calico/node:v3.9.3
docker pull calico/kube-controllers:v3.9.3
`官方镜像拉取太慢,用Jack老师的`
docker pull
http://
registry.cn-hangzhou.aliyuncs.com
/itcrazy2016/kube-controllers:v3.9.3
docker pull
http://
registry.cn-hangzhou.aliyuncs.com
/itcrazy2016/cni:v3.9.3
docker pull
http://
registry.cn-hangzhou.aliyuncs.com
/itcrazy2016/pod2daemon-flexvol:v3.9.3
docker pull
http://
registry.cn-hangzhou.aliyuncs.com
/itcrazy2016/node:v3.9.3
`打tag`
docker tag
http://
registry.cn-hangzhou.aliyuncs.com
/itcrazy2016/kube-controllers:v3.9.3
calico/kube-controllers:v3.9.3
docker tag
http://
registry.cn-hangzhou.aliyuncs.com
/itcrazy2016/cni:v3.9.3
calico/cni:v3.9.3
docker tag
http://
registry.cn-hangzhou.aliyuncs.com
/itcrazy2016/pod2daemon-flexvol:v3.9.3
calico/pod2daemon-flexvol:v3.9.3
docker tag
http://
registry.cn-hangzhou.aliyuncs.com
/itcrazy2016/node:v3.9.3
calico/node:v3.9.3
`删除http://registry.cn-hangzhou.aliyuncs.com/itcrazy2016/格式的镜像`
# 注意:打tag不会改变imageId,会删除calico的镜像【会删除所有】
docker rmi -f $(docker images http://registry.cn-hangzhou.aliyuncs.com/itcrazy2016/* -aq)
02 `在k8s中安装calico`
yum install -y wget
wget https://docs.projectcalico.org/v3.9/manifests/calico.yaml
kubectl apply -f calico.yaml
03 `确认一下calico是否安装成功`
kubectl get pods --all-namespaces -w 【实时查看所有的Pods】
9)kube join
01 记得保存初始化master节点的最后打印信息【注意这边大家要自己的,下面我的只是一个参考,worker上面执行】
kubeadm join 192.168.0.21:6443 --token l3nxcb.62ns6124urrzbuiu
--discovery-token-ca-cert-hash sha256:0ecd644c956fc2e573c570999e7570e34be4a5f172e1f7b7a0822cc3337
02 在master节点上检查集群信息
kubectl get nodes
=======================================================
NAME STATUS ROLES AGE VERSION
master-kubeadm-k8s Ready master 19m v1.14.0
worker01-kubeadm-k8s Ready <none> 3m6s v1.14.0
worker02-kubeadm-k8s Ready <none> 2m41s v1.14.0
=======================================================
10)再次体验Pod
01 `定义pod.yml文件,比如pod_nginx_rs.yaml` 【不能使用tab,只能用空格】
mkdir pod_nginx_rs
cd pod_nginx_rs
=======================================================
cat > pod_nginx_rs.yaml <<EOF
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx
labels:
tier: frontend
spec:
replicas: 3
selector:
matchLabels:
tier: frontend
template:
metadata:
name: nginx
labels:
tier: frontend
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
EOF
=======================================================
02 `根据pod_nginx_rs.yml文件创建pod`
kubectl apply -f pod_nginx_rs.yaml
03 `查看pod`
kubectl get pods
kubectl get pods -o wide
kubectl describe pod nginx
04 `感受通过rs将pod扩容`
kubectl scale rs nginx --replicas=5
kubectl get pods -o wide
05 `删除pod`
kubectl delete -f pod_nginx_rs.yaml
OVER