CentOS 7 Minimal 虛擬機環境 Kubernetes(k8s)測試集羣安裝

原文連接:http://tabalt.net/blog/instal...node

1、安裝CentOS 7 Minimal 64位版 基礎虛擬機

一、新建虛擬機,掛載ISO文件,啓動後安裝,設置root密碼
二、登陸虛擬機,設置網絡linux

cat /proc/net/dev | grep -v lo  #查看網絡設備,如ens33
vim /etc/sysconfig/network-scripts/ifcfg-ens33  #ONBOOT=no修改成ONBOOT=yes
service network restart
ip address  #查看IP地址

三、測試上述IP地址是否能經過SecureCRT/XShell等工具登陸
四、安裝經常使用工具nginx

yum install -y net-tools wget vim tree git curl jq ntpdate ntp

五、調整時間git

vim /etc/ntp.conf
# 註釋如下內容:
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst
# 添加如下內容:
server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
server ntp3.aliyun.com iburst
server ntp4.aliyun.com iburst
server ntp5.aliyun.com iburst
server ntp6.aliyun.com iburst
server ntp7.aliyun.com iburst

systemctl start ntpd.service
systemctl enable ntpd.service

# 設置時區
timedatectl set-timezone Asia/Shanghai
# 將時間寫入硬件時鐘
timedatectl set-local-rtc 0
# 重啓依賴系統時間的服務
systemctl restart rsyslog
systemctl restart crond

2、安裝K8s基礎環境

一、從上述基礎虛擬機 建立完整克隆,命名爲 k8s-base-env,啓動並登陸
二、調整系統設置github

# 關閉防火牆
systemctl stop firewalld
systemctl disable firewalld

# 關閉郵件服務
systemctl stop postfix
systemctl disable postfix

# 關閉selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config 
setenforce 0

# 關閉swap
swapoff -a  #臨時
sed -ri 's/.*swap.*/#&/' /etc/fstab  #永久

# 將橋接的IPv4流量傳遞到iptables
cat > /etc/sysctl.d/kubernetes.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward=1
net.ipv4.tcp_tw_recycle=0
vm.swappiness=0
vm.overcommit_memory=1
vm.panic_on_oom=0
fs.inotify.max_user_instances=8192
fs.inotify.max_user_watches=1048576
fs.file-max=52706963
fs.nr_open=52706963
net.ipv6.conf.all.disable_ipv6=1
net.netfilter.nf_conntrack_max=2310720
EOF
sysctl --system

# 升級內核到4.4
rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
yum --enablerepo=elrepo-kernel install -y kernel-lt
grub2-set-default "CentOS Linux (4.4.214-1.el7.elrepo.x86_64) 7 (Core)" #根據具體版本設置
reboot
uname -r
yum update

三、安裝基礎軟件docker

# 添加阿里雲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

# 安裝kubeadm、kubelet和kubectl
yum install -y kubelet kubeadm kubectl
systemctl enable kubelet

# 安裝Docker
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
yum -y install docker-ce-18.06.1.ce-3.el7
systemctl enable docker && systemctl start docker
docker --version

# 安裝iptables並清空規則
yum install -y iptables iptables-services
systemctl start iptables && systemctl enable iptables && iptables -F && service iptables save

# 安裝ipvs
yum install -y conntrack ipvsadm ipset sysstat libseccomp
modprobe br_netfilter
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 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules
lsmod | grep -e ip_vs -e nf_conntrack_ipv4

3、安裝K8s 集羣

0、建議 電腦內存16G以上,並關閉一些耗資源較大的程序
一、從上述 K8s基礎環境虛擬機 建立3個帶連接的克隆,分別命名爲 k8s-master0一、k8s-node0一、k8s-node02
二、將 k8s-master01的配置調整爲2核2G,k8s-node0一、k8s-node02的配置調整爲2核4G
三、啓動以上3個虛擬機並登陸,分別設置主機名和hostsvim

# 根據實際狀況配置主機名
hostnamectl set-hostname k8s-master01
hostnamectl set-hostname k8s-node01
hostnamectl set-hostname k8s-node02 

# 根據實際狀況添加hosts配置
vim /etc/hosts
192.168.145.144 k8s-master01
192.168.145.145 k8s-node01
192.168.145.146 k8s-node02

四、分別設置靜態IP地址並記錄centos

cat /proc/net/dev | grep -v lo  #查看網絡設備,如ens33
vim /etc/sysconfig/network-scripts/ifcfg-ens33  #修改或新增如下下配置項
BOOTPROTO="static" # 默認爲dhcp
ONBOOT="yes" #開機啓用配置
IPADDR="192.168.145.144" #靜態IP地址,根據實際狀況分別設置
GATEWAY="192.168.145.2" #默認網關  
DNS1="192.168.145.2" #DNS

service network restart
ip address  #查看IP地址是否生效

五、部署Master節點api

# 在k8s-master01上執行
# 生成配置文件
kubeadm init \
  --apiserver-advertise-address=192.168.145.144 \
  --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

# 查看生成的配置文件  
tree /etc/kubernetes/

# 使用kubectl工具
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
kubectl get nodes

# 安裝Pod網絡插件
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml 
kubectl apply -f kube-flannel.yml

# 上述命令需確保可以訪問到quay.io倉庫,如長時間未成功拉取鏡像可嘗試手動拉取
docker pull quay.io/coreos/flannel:v0.11.0-amd64
# 也經過鏡像加速拉取後添加tag
docker pull quay.mirrors.ustc.edu.cn/coreos/flannel:v0.11.0-amd64
docker tag quay.mirrors.ustc.edu.cn/coreos/flannel:v0.11.0-amd64 quay.io/coreos/flannel:v0.11.0-amd64

# kube-proxy開啓 ipvs
kubectl edit cm kube-proxy -n kube-system #修改:mode: "ipvs"
# 批量刪除並自動重建kube-proxy
kubectl get pod -n kube-system | grep kube-proxy | awk '{system("kubectl delete pod "$1" -n kube-system")}'
ipvsadm -Ln #查看生成的ipvs規則

6 、部署 Node節點瀏覽器

# 在k8s-node0一、k8s-node02上執行
# 前面 kubeadm init輸出的日誌中找到相似以下的命令,注意IP和token必須是日誌中輸出的
kubeadm join 192.168.145.144:6443 --token 42w1zg.6ffi04kj88c1kesn \
    --discovery-token-ca-cert-hash sha256:69453c78d7e1c8d1f576e4b08bff84be29fb91b5e0584c67bdf226b85ceb548d
kubectl get nodes

七、測試集羣

kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl get svc | grep nginx #獲取IP、對外暴露的端口
nginx        NodePort    10.1.185.226   <none>        80:31721/TCP   2d18h
curl "http://10.1.185.226" #虛擬機內可經過SVC的IP訪問
curl "http://192.168.145.144:31721" #虛擬機外可經過節點IP加暴露的NodePort端口訪問

# 如鏡像拉取失敗可經過鏡像加速拉取後添加tag
docker pull dockerhub.azk8s.cn/library/nginx
docker tag dockerhub.azk8s.cn/library/nginx library/nginx

八、部署 Dashboard

wget https://raw.githubusercontent.com/kubernetes/dashboard/master/aio/deploy/recommended.yaml -O ./kubernetes-dashboard.yaml
vim ./kubernetes-dashboard.yaml # 修改kubernetes-dashboard Service部分
----------
kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kubernetes-dashboard
spec:
  type: NodePort #修改Service爲NodePort類型
  ports:
    - port: 443
      targetPort: 8443
      nodePort: 30001 #指定NodePort端口
  selector:
    k8s-app: kubernetes-dashboard
----------

kubectl apply -f ./kubernetes-dashboard.yaml
kubectl get pods,svc -n kubernetes-dashboard

# 如鏡像拉取失敗可經過鏡像加速拉取後添加tag
docker pull dockerhub.azk8s.cn/kubernetesui/metrics-scraper:v1.0.3
docker pull dockerhub.azk8s.cn/kubernetesui/dashboard:v2.0.0-rc5
docker tag dockerhub.azk8s.cn/kubernetesui/metrics-scraper:v1.0.3 kubernetesui/metrics-scraper:v1.0.3
docker tag dockerhub.azk8s.cn/kubernetesui/dashboard:v2.0.0-rc5 kubernetesui/dashboard:v2.0.0-rc5

# 建立service account並綁定默認cluster-admin管理員集羣角色
kubectl create serviceaccount dashboard-admin -n kubernetes-dashboard
kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kubernetes-dashboard:dashboard-admin
kubectl describe secrets -n kubernetes-dashboard $(kubectl -n kubernetes-dashboard get secret | awk '/dashboard-admin/{print $1}')

# 保存token,在虛擬機外使用火狐瀏覽器訪問以下地址,忽略證書報錯
https://192.168.145.144:30001/

原文連接:http://tabalt.net/blog/instal...

相關文章
相關標籤/搜索