K8s简单集群部署
1、安装虚拟机
2、这里的网络选择NAT模式与主机连接,启用DHCP 子网192.168.190.0 网关192.168.190.2 DNS 是阿里云的 223.5.5.5
3、配置静态IP:ip地址 子网掩码 网关 DNS配置如下
4、开启网络自动连接
5、这里的网卡驱动有问题,需要借助CCleaner Free修复驱动
6、重启网络
7、配置本地hosts解析
/etc/hosts
192.168.190.131 master
192.168.190.132 node1
192.168.190.133 node2
8、最小参数要求
2g内存 2核cpu 50G磁盘
9、启用时间同步
systemctl start chronyd
systemctl enable chronyd
10、关闭防火墙及selinux
/etc/selinux/config disabled
systemctl stop iptables
systemctl stop firewalld
systemctl disable iptables
systemctl disable firewalld
11、禁用swap分区
/etc/fstab
#swap
12、配置k8s网络
/etc/sysctl.d/kubernetes.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
13、加载配置
sysctl -p
14、配置ipset
yum install ipset ipvsadmin -y
modprobe br_netfilter
lsmod | grep br_netfilter
cat <<EOF > /etc/sysconfig/modules/ipvs.modules
#!/bin/bash
modprobe — ip_vs
modprobe — ip_vs_rr
modprobe — ip_vs_wrr
modprobe — ip_vs_sh
modprobe — nf_conntrack_ipv4
EOF
15、配置ipvs
chmod +x /etc/sysconfig/modules/ipvs.modules
/bin/bash /etc/sysconfig/modules/ipvs.modules
lsmod | grep -e ip_vs -e nf_conntrack_ipv4
16、重启生效
reboot
17、安装docker
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
yum list docker-ce –showduplicates
yum -y install –setopt=obsoletes=0 docker-ce-18.06.3.ce-3.el7
mkdir /etc/docker
18、配置网络加速
cat <<EOF > /etc/docker/daemon.json
{
“exec-opts”: [“native.cgroupdriver=systemd”],
“registry-mirrors”: [“https://kn0t2bca.mirror.aliyuncs.com”]
}
EOF
systemctl restart docker
19、检查docker
docker version
20、配置k8s源,这里需要先下载基础镜像,国网网站慢或者翻墙。这里选择aly的。然后打tag即可
。
/etc/yum.repos.d/kubernetes.repo
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
21、下载镜像并打tag
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.17.4
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.17.4
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.17.4
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.3-0
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.5
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.17.4
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.17.4 k8s.gcr.io/kube-apiserver:v1.17.4
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.17.4 k8s.gcr.io/kube-controller-manager:v1.17.4
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.17.4 k8s.gcr.io/kube-scheduler:v1.17.4
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.17.4 k8s.gcr.io/kube-proxy:v1.17.4
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1 k8s.gcr.io/pause:3.1
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.3-0 k8s.gcr.io/etcd:3.4.3-0
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.5 k8s.gcr.io/coredns:1.6.5
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.17.4
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.17.4
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.17.4
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.3-0
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.5
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.17.4
-
设置docker开机自启动
systemctl enable docker.service
22、安装k8s,三台主机都要安装
yum install –setopt=obsolete=0 kubeadm-1.17.4-0 kubelet-1.17.4-0 kubectl-1.17.4-0
/etc/sysconfig/kubelet
KUBELET_CGROUP_ARGS=”–cgroup-driver=systemd”
KUB_PROXY_MODE=”ipvs”
systemctl enable kubelet
23、配置主节点
kubeadm init –kubernetes-version=v1.17.4 –pod-network-cidr=10.244.0.0/16 –service-cidr=10.96.0.0/12 –apiserver-advertise-address=192.168.190.131
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
24、配置node节点,这里都要执行包括master
kubeadm join 192.168.190.131:6443 –token g4g207.46na22pl36t0hx4l \
> –discovery-token-ca-cert-hash sha256:da53134b17a67fc72494e7e264dad8ab0fabe1661c064fa3202db5fbc30be3ef
25、验证
[root@master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master NotReady master 3m34s v1.17.4
node1 NotReady <none> 18s v1.17.4
node2 NotReady <none> 5s v1.17.4
26、加载k8s网络
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl apply -f kube-flannel.yml
[root@master ~]# kubectl apply -f kube-flannel.yml
podsecuritypolicy.policy/psp.flannel.unprivileged created
clusterrole.rbac.authorization.k8s.io/flannel created
clusterrolebinding.rbac.authorization.k8s.io/flannel created
serviceaccount/flannel created
configmap/kube-flannel-cfg created
daemonset.apps/kube-flannel-ds created
27、验证nodes
[root@master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready master 13m v1.17.4
node1 Ready <none> 10m v1.17.4
node2 Ready <none> 10m v1.17.4
28、创建ngnix容器
[root@master ~]# kubectl create deployment nginx –image=nginx:1.14-alpine
deployment.apps/nginx created
29、配置NodePort
[root@master ~]# kubectl expose deployment nginx –port=80 –type=NodePort
service/nginx exposed
30、查看结果
[root@master ~]# kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 1/1 1 1 4m29s