1. 程式人生 > 實用技巧 >k8s之yum安裝

k8s之yum安裝

一、安裝docker

centos

#安裝一些依賴包
   yum install -y yum-utils device-mapper-persistent-data lvm2
   yum -y install https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/Packages/containerd.io-1.2.13-3.1.el7.x86_64.rpm &> /dev/null

#新增源資訊
    cat > /etc/yum.repos.d/docker.repo <<EOF
[docker]
name
=docker gpgcheck=0 baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/8/x86_64/stable/ EOF #更新安裝docker-ce yum clean all yum -y install docker-ce #配置映象加速器 mkdir -p /etc/docker cat > /etc/docker/daemon.json <<EOF { "registry-mirrors": ["https://registry.docker-cn.com","http://hub-mirror.c.163.com
","https://docker.mirrors.ustc.edu.cn"] } EOF
#啟動docker
  systemctl enable --now docker

ubuntu

#更新軟體包及安裝依賴包
    apt-get update
    apt-get -y install apt-transport-https ca-certificates curl software-properties-common

#安裝GPG證書
    curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
#寫入軟體源資訊 echo 'deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu bionic stable' >> /etc/apt/sources.list #更新源資訊 apt -y update #配置映象加速器 mkdir -p /etc/docker cat > /etc/docker/daemon.json <<EOF { "registry-mirrors": ["https://registry.docker-cn.com","http://hub-mirror.c.163.com","https://docker.mirrors.ustc.edu.cn"] } EOF #安裝docker apt-get -y install docker-ce

二、利用阿里雲配置原始檔和安裝k8s

要點:每個節點都要部署docker,kubelet,kubeadm,kubectl

Debian / Ubuntu

#配置源
apt-get update && apt-get install -y apt-transport-https
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add - 
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF  
#安裝
apt-get update
apt-get install -y kubelet kubeadm kubectl

CentOS / RHEL / Fedora

#配置yum源
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[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
#設定selinux
setenforce 0
#安裝
yum install -y kubelet kubeadm kubectl
systemctl enable kubelet && systemctl start kubelet

三、啟動前準備

1.關閉防火牆

  systemctl stopfirewalld.service

2.關閉selinux(ubuntu無需做此步)

  setenforce 0

3.關閉交換分割槽

  swapoff -a(臨時)

  永久關閉:vim /etc/fstab(將與交換分割槽相關的行註釋掉)

四、配置k8s

1、初始化master,生成控制平臺

kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.19.4 --control-plane-endpoint 10.0.0.48 --apiserver-bind-port=6443 --apiserver-advertise-address 10.0.0.48 --pod-network-cidr 10.244.0.0/16

--image-repository    #設定一個映象倉庫,預設為k8s.gcr.io
--kubernetes-version    #指定安裝k8s版本,預設為stable-1
--control-plane-endpoint    #為控制平臺指定一個穩定的IP地址或DNS名稱,即配置一個可以長期使用切是高可用的VIP或者域名,k8s 多master高可用基於此引數實現
--apiserver-bind-port      #API Server繫結的埠,預設為6443
--apiserver-advertise-address   #K8S API Server將要監聽的監聽的本機IP
--pod-network-cidr    #設定pod ip地址範圍

2、配置kube-config檔案和網路元件

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

#配置網路元件
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl apply -f kube-flannel.yml

#檢視flannel容器是否執行
kubectl get pods -n kube-system

3、新增worker節點到K8S叢集

#在worker節點上執行,初始化生成的token預設只有24小時,如需長期有效,可在初始化時指定引數(--token-ttl #指定token過期時間,預設為24小時,0為永不過期)
kubeadm join kubeapi.magedu.com:6443 --token auxv3l.rz0fvcne1w8fpi5c \
    --discovery-token-ca-cert-hash sha256:70dde5dd65aa1c7cdfd4a6ccf30f682da7ac2077134c177b83f39c58476e6f59

#檢視容器執行的節點
kubectl get nodes -o wide

4、建立service及配置pod數

#在k8s上執行容器
kubectl create deployment damoapp --image="ikubernetes/demoapp:v1.0"

#控制平臺上配置pods個數
kubectl scale deployment demoapp --replicas=3 (deployment要和前面執行容器的名字一樣)

#刪除Pod
kubectl delete pods +pod名稱
kubectl delete pods damoapp-59bb775755-5sn4q

#建立service
kubectl create service clusterip +容器名稱 +協議及埠
kubectl create service clusterip damoapp --tcp=80:80

#檢視建立的service
kubectl get service

5、故障排錯

1、linux主機名不能有下劃線_,否則無法部署

2、The connection to the server apiserver.k8s:6443 was refused - did you specify the right host or port
#檢查docker服務是否啟動

3、節點加入時出現以下:
[ERP0R FileContent--proc-sys-net-bridge-bridge-nf-call-iptables] : /proc/sys/net/bridge/bridge-nf-call-iptables contents are not set to 1
#一般此項docker會自動設定,如未設定,則自己手動設定
vim /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables = 1
#生效
sysctl -p /etc/sysctl.d/k8s.conf