centos7部署k8s

环境准备:

3台vmware虚拟机(网络可以互通),一台master(2核2G),两台worker(2核2G)
centos7系统基础设置(执行完以下操作后重启使设置都生效):

#关闭防火墙
systemctl disable firewalld && systemctl stop firewalld

#关闭seliunx
#将SELINUX=enforcing改为SELINUX=disabled
vim /etc/selinux/config

#检查selinux状态
sestatus
SELinux status:                 disabled

#关闭swap分区
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab

#修改主机名,配置hosts文件
hostnamectl set-hostname master     #master上执行
hostnamectl set-hostname worker1    #节点1
hostnamectl set-hostname worker2    #节点2
vi /etc/hosts
10.17.0.100 master
10.17.0.101 worker1
10.17.0.102 worker2

安装docker

1.设置国内YUM源
cd /etc/yum.repos.d/
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

2.安装指定的docker版本
yum -y install docker-ce-18.09.7-3.el7 docker-ce-cli-18.09.7

3.设置docker使用阿里云加速
mkdir /etc/docker
cat > /etc/docker/daemon.json <<EOF
    {
      "registry-mirrors": ["https://ig2l319y.mirror.aliyuncs.com"],
      "exec-opts": ["native.cgroupdriver=systemd"]
    }
EOF

4.启动后台进程
systemctl enable docker && systemctl start docker

5.查看docker版本
docker -v

安装部署kubeadm和kubelet

1.设置k8s国内yum仓库
cat >/etc/yum.repos.d/kubernetes.repo<<EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF


2.安装kubeadm
yum install -y kubelet-1.16.2 kubeadm-1.16.2 kubectl-1.16.2 ipvsadm

3.设置k8s禁止使用swap
cat > /etc/sysconfig/kubelet<<EOF
KUBELET_CGROUP_ARGS="--cgroup-driver=systemd"
KUBELET_EXTRA_ARGS="--fail-swap-on=false"
EOF

4.设置内核参数
cat >  /etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
sysctl --system

5.设置kubelet开机启动
systemctl enable kubelet && systemctl start kubelet

6.加载IPVS模块
cat >/etc/sysconfig/modules/ipvs.modules<<EOF
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4
EOF
chmod +x /etc/sysconfig/modules/ipvs.modules
source /etc/sysconfig/modules/ipvs.modules
lsmod | grep -e ip_vs -e nf_conntrack_ipv

---------以上步骤在三个虚拟机上都要执行----------------

初始化master(仅在master机器上执行)

kubeadm init \
--apiserver-advertise-address=10.17.0.100 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.16.2 \
--service-cidr=10.17.0.0/16 \
--pod-network-cidr=10.17.0.0/16 \
--service-dns-domain=cluster.local \
--ignore-preflight-errors=Swap \
--ignore-preflight-errors=NumCPU


#执行结果
[root@localhost ~]kubeadm init --apiserver-advertise-address=10.17.0.100 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.16.2 --service-cidr=10.17.0.0/16 --pod-network-cidr=10.17.0.0/16 --service-dns-domain=cluster.local --ignore-pre
flight-errors=Swap --ignore-preflight-errors=NumCPU[init] Using Kubernetes version: v1.16.2
[preflight] Running pre-flight checks
	[WARNING Hostname]: hostname "m" could not be reached
	[WARNING Hostname]: hostname "m": lookup m on 10.17.0.1:53: no such host
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Activating the kubelet service
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [m kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.17.0.1 10.17.0.100]
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [m localhost] and IPs [10.17.0.100 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [m localhost] and IPs [10.17.0.100 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
[control-plane] Creating static Pod manifest for "kube-scheduler"
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[apiclient] All control plane components are healthy after 34.502001 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.16" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node m as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node m as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: k66p4b.j1ciug6w236is465
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy

Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:
#注意这部分,这三条命令需要在master本机上执行一下
  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 root:

#以下命令时worker节点加入master所需要的令牌,需要保存下来,在worker节点执行
kubeadm join 10.17.0.100:6443 --token k66p4b.j1ciug6w236is465 \
    --discovery-token-ca-cert-hash sha256:c7a49d348705982a8c3c4ea3fa31692ec645d4cc9f90294b38c49d18834cf4bd 
#检查k8s状态
kubectl cluster-info
#查看pod
kubectl get pods -n kube-system
#结果:
[root@localhost ~]# kubectl get pods -n kube-system
NAME                        READY   STATUS    RESTARTS   AGE
coredns-58cc8c89f4-5vzct    0/1     Pending   0          38m
coredns-58cc8c89f4-wrzl7    0/1     Pending   0          38m
etcd-m                      1/1     Running   0          37m
kube-apiserver-m            1/1     Running   0          37m
kube-controller-manager-m   1/1     Running   0          37m
kube-proxy-ggrth            1/1     Running   0          38m
kube-scheduler-m            1/1     Running   0          37m
#缺少coredns pod 这个需要再安装网络插件,只需要master上执行

# 在k8s中安装calico
kubectl apply -f https://docs.projectcalico.org/v3.9/manifests/calico.yaml

# 确认一下calico是否安装成功
kubectl -n kube-system get pod

[root@localhost ~]# kubectl -n kube-system get pod
NAME                                      READY   STATUS    RESTARTS   AGE
calico-kube-controllers-7cc97544d-hkb9w   1/1     Running   0          2m17s
calico-node-7njq6                         1/1     Running   0          2m17s
coredns-58cc8c89f4-5vzct                  1/1     Running   0          57m
coredns-58cc8c89f4-wrzl7                  1/1     Running   0          57m
etcd-m                                    1/1     Running   0          56m
kube-apiserver-m                          1/1     Running   0          56m
kube-controller-manager-m                 1/1     Running   0          56m
kube-proxy-ggrth                          1/1     Running   0          57m
kube-scheduler-m                          1/1     Running   0          56m

将work节点加入master

在worker节点上执行,master初始化的时候生成的令牌
kubeadm join 10.17.0.100:6443 --token k66p4b.j1ciug6w236is465 \
    --discovery-token-ca-cert-hash sha256:c7a49d348705982a8c3c4ea3fa31692ec645d4cc9f90294b38c49d18834cf4bd 

master上验证节点 加入情况

kubectl get nodes

-------以上单master集群已经搭建完毕

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐