kubeadm部署k8s

Posted 水木,年華

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了kubeadm部署k8s相关的知识,希望对你有一定的参考价值。

服务器配置

服务器IP          主机名
192.168.80.1      master 
192.168.80.2      node1
192.168.80.3      node2 

安装docker
1、三台服务器安装docker源

[root@master ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
[root@master ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

2、安装docker

yum install -y docker-ce
#设置镜像加速
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://483txl0m.mirror.aliyuncs.com"]
}
EOF

3、设置开机自启

systemctl enable docker && systemctl start docker

查看版本
docker  --version

4、安装kubeadm、kubelet核kubectl

#指定安装源
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=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
EOF

5、添加映射

cat <<EOF >>/etc/hosts
192.168.80.1 master
192.168.80.2 node1
192.168.80.3 node2
EOF

#指定安装版本

[root@master ~]# yum install -y kubelet-1.15.0 kubeadm-1.15.0 kubectl-1.15.0
注:如果生产环境中,master隐藏,node节点被授权为master,也需安装,此次部署只在master上安装

[root@master ~]# rpm -qa | grep kube
#如果版本过高,需yum remove掉,然后重新下载

#设置开机自启动

[root@master ~]# systemctl enable kubelet

关闭swap分区

[root@master ~]# swapoff -a    #零时关闭
[root@master ~]# sed -ri 's/.*swap.*/#&/' /etc/fstab
#永久关闭

将桥接的IPv4流量传递到iptables的链

cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system

执行初始化

[root@master ~]# mkdir k8s && cd k8s
[root@master k8s]# kubeadm init \\
> --apiserver-advertise-address=192.168.80.1 \\
> --image-repository registry.aliyuncs.com/google_containers \\
> --kubernetes-version v1.15.0 \\
> --service-cidr=10.1.0.0/16 \\
> --pod-network-cidr=10.244.0.0/16



使用kubectl工具

[root@master k8s]# mkdir -p $HOME/.kube
[root@master k8s]# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
[root@master k8s]# sudo chown $(id -u):$(id -g) $HOME/.kube/config

#安装pod网络插件(flannel)

[root@master k8s]# wget https://raw.githubusercontent.com/coreos/flannel/a70459be0084506e4ec919aa1c114638878db11b/Documentation/kube-flannel.yml
[root@master k8s]# kubectl apply -f kube-flannel.yml 


#查看组件状态 && 查看节点状态

[root@master k8s]# kubectl get cs 
NAME                 STATUS    MESSAGE             ERROR
scheduler            Healthy   ok                  
controller-manager   Healthy   ok                  
etcd-0               Healthy   {"health":"true"}   
[root@master k8s]# kubectl get nodes
NAME     STATUS   ROLES    AGE   VERSION
master   Ready    master   54m   v1.15.0

node节点
docker 拉取flannel镜像

docker pull lizhenliang/flannel:v0.11.0-amd64

关闭swap分区

[root@node1 ~]# sed -ri 's/.*swap.*/#&/' /etc/fstab
[root@node1 ~]# cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
[root@node1 ~]# sysctl --system

#添加节点(kubeadm join,节点token会周期性变化,kubeadm token list查看token)

[root@node1 ~]# kubeadm join 192.168.80.1:6443 --token 69r545.52s3tofhq1i53uue --discovery-token-ca-cert-hash sha256:97cde0a8d912001121d6c1c09cb472136d57f1ca7ab420e00cd0b94bc619c973

master节点查看node状态 “三个ready”

[root@master k8s]# kubectl get nodes 
NAME     STATUS   ROLES    AGE     VERSION
master   Ready    master   156m    v1.15.0
node1    Ready    <none>   66m     v1.15.0
node2    Ready    <none>   6m27s   v1.15.0
[root@master k8s]# kubectl get pods --all-namespaces 


打node标签

[root@master k8s]# kubectl label node node1 node-role.kubernetes.io/node=node
[root@master k8s]# kubectl label node node2 node-role.kubernetes.io/node=node


查看名称空间

[root@master k8s]# kubectl get namespaces
NAME              STATUS   AGE
default           Active   3h10m
kube-node-lease   Active   3h10m
kube-public       Active   3h10m
kube-system       Active   3h10m

查看pod名称空间

root@master k8s]# kubectl get pod -n kube-system
NAME                             READY   STATUS    RESTARTS   AGE
coredns-bccdc95cf-257xp          1/1     Running   0          3h12m
coredns-bccdc95cf-qvzv5          1/1     Running   0          3h12m
etcd-master                      1/1     Running   0          3h10m
kube-apiserver-master            1/1     Running   0          3h11m
kube-controller-manager-master   1/1     Running   0          3h11m
kube-flannel-ds-amd64-g2k7l      1/1     Running   0          161m
kube-flannel-ds-amd64-q8hqf      1/1     Running   0          42m
kube-flannel-ds-amd64-z627q      1/1     Running   0          102m
kube-proxy-dvqp4                 1/1     Running   0          102m
kube-proxy-nz97w                 1/1     Running   0          3h12m
kube-proxy-qnzxg                 1/1     Running   0          42m
kube-scheduler-master            1/1     Running   0          3h11m

查看服务发现的资源

[root@master k8s]# kubectl get service 
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.1.0.1     <none>        443/TCP   3h18m

以上是关于kubeadm部署k8s的主要内容,如果未能解决你的问题,请参考以下文章

K8S集群架构的kubeadm部署+dashboard和Harbor仓库部署

5分钟用kubeadm部署k8s

5分钟用kubeadm部署k8s

Docker&K8s---通过kubeadm快速部署K8s

Docker&K8s---通过kubeadm快速部署K8s

Kubeadm 部署k8s集群