居于KVM环境下搭建k8s环境

2019-10-18  本文已影响0人  无处安放的躁动

前言

修改配置

关闭防火墙

systemctl stop firewalld
systemctl enable firewalld

关闭selinux

# 临时关闭
setenforce 0
# 永久关闭
sed -i '7cSELINUX=disabled' /etc/selinux/config

设置IP地址

# 所有节点设置hostname
hostnamectl set-hostname k8smaster
# 设置地址
[root@k8smaster ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens3 
TYPE=Ethernet
BOOTPROTO=static
NAME=ens3
DEVICE=ens3
ONBOOT=yes
IPADDR=192.168.100.10
GATEWAY=192.168.100.1
NETMASK=255.255.255.0
DNS1=114.114.114.114
DNS2=8.8.8.8

注释最后一行,禁止swap

#
# /etc/fstab
# Created by anaconda on Fri Oct 18 03:53:32 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=69dd6e3a-7b95-4c70-a60c-3bdfbf974f30 /boot                   xfs     defaults        0 0
#/dev/mapper/centos-swap swap                    swap    defaults        0 0
# 关闭swap
swapoff -a && sysctl -w vm.swappiness=0

所有节点都需要安装的软件包

yum -y install vim lsof telnet net-tools 

所有节点安装软件包

安装docker

安装k8s

所有节点安装

vim /etc/yum.repos.d/k8s.repo
[k8s]
name=k8s
enabled=1
gpgcheck=0
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
yum install -y kubelet kubeadm kubectl 
systemctl enable kubelet
vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.100.10 k8smaster
192.168.100.11 k8snode1
192.168.100.12 k8snode2
echo "1" >/proc/sys/net/bridge/bridge-nf-call-iptables

master节点安装

初始化master

# 初始化master
kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.16.2 --apiserver-advertise-address 192.168.100.10 --pod-network-cidr=10.244.0.0/16 --v=5
kubeadm reset
ifconfig cni0 down
ip link delete cni0
ifconfig flannel.1 down
ip link delete flannel.1rm -rf /var/lib/cni/
rm -rf /var/lib/etcd/*

配置master

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

bash 自动补全

echo "source <(kubectl completion bash)" >> ~/.bashrc

检测

# 检测是否安装成功
[root@k8smaster etc]# kubectl get node
NAME        STATUS     ROLES    AGE     VERSION
k8smaster   NotReady   master   7m37s   v1.16.2

安装pod网络,

# 安装pod网络
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
# 重启
systemctl restart kubelet

下载成功后,status为ready

# 查看状态
[root@k8smaster etc]# kubectl get nodes
NAME        STATUS   ROLES    AGE   VERSION
k8smaster   Ready    master   12m   v1.16.2
# 查看pods
kubectl get pods -n kube-system

node节点加入k8s

记得token加入节点

# token上面初始化时候得到的
kubeadm join 192.168.100.10:6443 --token qvti7j.7hhybjfb803ryusb \
    --discovery-token-ca-cert-hash sha256:c14951ca3e5e6511736e13c92124464bfa47dca01eff001b17f4ae12c0bbexxxx

忘记token加入节点

# 获取toekn
kubeadm token list
# 获取sha256
openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'
systemctl stop kubelet
rm -rf /etc/kubernetes/*
kubeadm join 192.168.100.10:6443 --token qvti7j.7hhybjfb803ryusb \
    --discovery-token-ca-cert-hash sha256:c14951ca3e5e6511736e13c92124464bfa47dca01eff001b17f4ae12c0bbexxxx

基础操作

kubectl get nodes
kubectl get ns
kubectl get po -n kube-system
kubectl get po -n kube-system -o wide

参考文件

参考文件

上一篇下一篇

猜你喜欢

热点阅读