欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

《Centos7——k8s搭建》

程序员文章站 2022-03-01 13:05:11
...

k8s搭建

环境准备:
Centos7三台(192.168.179.130/192.168.179.131/192.168.179.132)
4G4核(3G4核也可以)

1、关闭防火墙

systemctl stop firewalld -----关闭防火墙

setenforce 0 -----关闭selinux

2、时间同步

yum -y install ntpdate

ntpdate pool.ntp.org

3、修改主机名(三台分别修改)

hostnamectl set-hostname k8s-master
hostnamectl set-hostname k8s-node1
hostnamectl set-hostname k8s-node2

4、 编辑hosts文件

vim /etc/hosts
192.168.179.130 k8s-master
192.168.179.131 k8s-node1
192.168.179.132 k8s-node2

5: 安装指定版本的docker

wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O
/etc/yum.repos.d/docker-ce.repo

或直接将docker-ce.repo(下面的内容)放到/etc/yum.repos/目录下
[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/

$basearch/stable
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-stable-debuginfo]
name=Docker CE Stable - Debuginfo $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/debug-

$basearch/stable
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-stable-source]
name=Docker CE Stable - Sources
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/source/stable
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-edge]
name=Docker CE Edge - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/

$basearch/edge
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-edge-debuginfo]
name=Docker CE Edge - Debuginfo $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/debug-

$basearch/edge
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-edge-source]
name=Docker CE Edge - Sources
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/source/edge
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-test]
name=Docker CE Test - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/

$basearch/test
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-test-debuginfo]
name=Docker CE Test - Debuginfo $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/debug-

$basearch/test
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-test-source]
name=Docker CE Test - Sources
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/source/test
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-nightly]
name=Docker CE Nightly - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/

$basearch/nightly
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-nightly-debuginfo]
name=Docker CE Nightly - Debuginfo $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/debug-

$basearch/nightly
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-nightly-source]
name=Docker CE Nightly - Sources
baseurl=https://mirrors.aliyun.com/docker-

ce/linux/centos/7/source/nightly
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

安装指定版本的docker:

yum -y install docker-ce-18.06.3.ce-3.el7 -----安装docker

版本验证:
[[email protected] docker]# docker --version
Docker version 18.06.3-ce, build d7080c1

6:运行docker

systemctl enable docker

systemctl start docker

查询可用版本:
yum list docker-ce --showduplicates | sort -r

7:docker镜像加速

[[email protected]-master docker]# cat daemon.json 
{
    "exec-opts": ["native.cgroupdriver=systemd"],
    "log-driver": "json-file",
    "log-opts": {
    "max-size": "100m"
    },
    "storage-driver": "overlay2",
    "registry-mirrors":[
        "https://kfwkfulq.mirror.aliyuncs.com",
        "https://2lqq34jg.mirror.aliyuncs.com",
        "https://pee6w651.mirror.aliyuncs.com",
        "http://hub-mirror.c.163.com",
        "https://docker.mirrors.ustc.edu.cn",
        "https://registry.docker-cn.com"
    ]
}
重启:
systemctl restart docker

8:上传k8s的yum源

cat /etc/yum.repos/k8s.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-

x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg 

https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg

yum -y install kubelet-1.17.0 kubeadm-1.17.0 kubectl-1.17.0(master上执行)

9:设置kubelet 开机自启

systemctl enable kubelet -----开机自启
先不要启动它!!

10 初始化集群

上传k8s压缩包k8s-v1.17.0.zip,解压,然后导入镜像
docker load -i k8s_v1.17.0.tar
在master节点上执行:
kubeadm init --apiserver-advertise-address=192.168.179.130 --image-

repository registry.aliyuncs.com/google_containers --kubernetes-version 

v1.17.0 --service-cidr=10.1.0.0/16 --pod-network-cidr=10.244.0.0/16
报错处理:
[ERROR FileContent--proc-sys-net-bridge-bridge-nf-call-iptables]: 

/proc/sys/net/bridge/bridge-nf-call-iptables contents are not set to 1
echo "1" > /proc/sys/net/bridge/bridge-nf-call-iptables
[ERROR Swap]: running with swap on is not supported. Please disable swap
swapoff -a  (3个节点都要执行)

master上执行:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

11 将node节点加入到集群中:

kubeadm join 192.168.179.130:6443 --token sbky7r.d2upkq8dhkjz033s \
    --discovery-token-ca-cert-hash 

sha256:d48fbf3738fddea473d9540b0966a723b1295e79a0adad71d7c731bb73e87994
验证:
[[email protected]-master yum.repos.d]# kubectl get nodes
NAME         STATUS     ROLES    AGE     VERSION
k8s-master   NotReady   master   7m24s   v1.17.0
k8s-node1    NotReady   <none>   2m23s   v1.17.0
k8s-node2    NotReady   <none>   27s     v1.17.0

12: 安装k8s的网络组件falneel

[[email protected]-master k8s-v1.17.0]# 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-amd64 created
daemonset.apps/kube-flannel-ds-arm64 created
daemonset.apps/kube-flannel-ds-arm created
daemonset.apps/kube-flannel-ds-ppc64le created
daemonset.apps/kube-flannel-ds-s390x created

13: 查看pod转态

[[email protected]-master k8s-v1.17.0]# kubectl get pod -A
NAMESPACE     NAME                                 READY   STATUS    

RESTARTS   AGE
kube-system   coredns-9d85f5447-jf28g              1/1     Running   0    

      14m
kube-system   coredns-9d85f5447-xrwgf              1/1     Running   0    

      14m
kube-system   etcd-k8s-master                      1/1     Running   0    

      14m
kube-system   kube-apiserver-k8s-master            1/1     Running   0    

      14m
kube-system   kube-controller-manager-k8s-master   1/1     Running   0    

      14m
kube-system   kube-flannel-ds-amd64-9qbnz          1/1     Running   0    

      29s
kube-system   kube-flannel-ds-amd64-jjzx9          1/1     Running   0    

      29s
kube-system   kube-flannel-ds-amd64-mq68h          1/1     Running   0    

      29s
kube-system   kube-proxy-5j497                     1/1     Running   0    

      10m
kube-system   kube-proxy-cl655                     1/1     Running   0    

      8m20s
kube-system   kube-proxy-d7wrw                     1/1     Running   0    

      14m
kube-system   kube-scheduler-k8s-master            1/1     Running   0    

      14m

14: 再次查看node状态

[[email protected]-master k8s-v1.17.0]# kubectl get nodes
NAME         STATUS   ROLES    AGE     VERSION
k8s-master   Ready    master   16m     v1.17.0
k8s-node1    Ready    <none>   11m     v1.17.0
k8s-node2    Ready    <none>   9m12s   v1.17.0
已经变为ready!!
相关标签: k8s docker centos