欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

k8s kubeadmin 安装

程序员文章站 2024-03-11 16:28:25
...
[[email protected] ~]# cat pre-setup.sh 
#!/bin/bash

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

#关闭selinux
setenforce 0
#永久关闭
sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/sysconfig/selinux
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config

#禁用swap
swapoff -a
#永久禁用
sed -i "s/.*swap.*/#&/" /etc/fstab

#修改内核参数
cat <<EOF >  /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF

#重新加载配置文件
sysctl --system

#配置阿里k8s 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
#修改文件句柄限制
echo "* soft nofile 65536" >> /etc/security/limits.conf
echo "* hard nofile 65536" >> /etc/security/limits.conf
echo "* soft nproc 65536"  >> /etc/security/limits.conf
echo "* hard nproc 65536"  >> /etc/security/limits.conf
echo "* soft  memlock  unlimited"  >> /etc/security/limits.conf
echo "* hard memlock  unlimited"  >> /etc/security/limits.conf

#更新缓存
yum clean all -y && yum makecache -y && yum repolist -y





#ansible 主机
# /etc/hosts 
10.12.12.181 node1
10.12.12.182 node2
10.12.12.183 node3

#[[email protected] ansible]# cat /etc/ansible/hosts  | tail -15

[k8s-all]
node1
node2
node3

[k8s-master]
node1

[k8s-nodes]
node2
node3



[[email protected] roles]# cat set_hosts_playbook.yml 
---
- hosts: k8s-all
  remote_user: root
  
  tasks:
    - name: backup /etc/hosts
      shell: mv /etc/hosts /etc/hosts_bak

    - name: copy local hosts file to remote
      copy: src=/etc/hosts dest=/etc/ owner=root group=root mode=0644 

#安装docker
[[email protected] roles]# cat install_docker_playbook.yml 
- hosts: k8s-all
  remote_user: root
  vars: 
     docker_version: 18.09.2

  tasks: 
     - name: install dependencies
       #shell: yum install -y yum-utils device-mapper-persistent-data lvm2 
       yum: name={{item}} state=present
       with_items:
          - yum-utils
          - device-mapper-persistent-data
          - lvm2

     - name: config yum repo
       shell: yum-config-manager --add-repo https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo

     - name: install docker
       yum: name=docker-ce-{{docker_version}} state=present

     - name: start docker
       shell: systemctl enable docker && systemctl start docker



#全部执行
yum install -y kubelet-1.20.4 kubeadm-1.20.4 kubectl-1.20.4

#master执行
kubeadm init \
--apiserver-advertise-address=10.12.12.181 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.20.4 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16

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

#node 执行
kubeadm join 10.12.12.181:6443 --token wfmgv4.cimd9aasqdsaevsq \
    --discovery-token-ca-cert-hash sha256:1629bdec040253728571c274b86ca8ba4cc1f04142758521ec93de34ab3c5046 

#需要安装网络插件
[[email protected] ~]# kubectl get node
NAME    STATUS     ROLES                  AGE     VERSION
node1   NotReady   control-plane,master   2m31s   v1.20.4
node2   NotReady   <none>                 57s     v1.20.4
node3   NotReady   <none>                 41s     v1.20.4

[[email protected] ~]# kubectl apply -f kube-flannel.yaml 


[[email protected] ~]# kubectl get pod -A
NAMESPACE     NAME                            READY   STATUS    RESTARTS   AGE
kube-system   coredns-7f89b7bc75-9gjrx        0/1     Pending   0          4m16s
kube-system   coredns-7f89b7bc75-f6j8n        0/1     Pending   0          4m16s
kube-system   etcd-node1                      1/1     Running   0          4m31s
kube-system   kube-apiserver-node1            1/1     Running   0          4m31s
kube-system   kube-controller-manager-node1   1/1     Running   0          4m31s
kube-system   kube-flannel-ds-amd64-2z8rw     1/1     Running   0          29s
kube-system   kube-flannel-ds-amd64-4pmhj     1/1     Running   0          29s
kube-system   kube-flannel-ds-amd64-vqwwk     1/1     Running   0          29s
kube-system   kube-proxy-562gr                1/1     Running   0          3m5s
kube-system   kube-proxy-6q6p9                1/1     Running   0          4m16s
kube-system   kube-proxy-t5bkd                1/1     Running   0          2m50s
kube-system   kube-scheduler-node1            1/1     Running   0          4m31s
[[email protected] ~]# kubectl get node
NAME    STATUS   ROLES                  AGE     VERSION
node1   Ready    control-plane,master   4m50s   v1.20.4
node2   Ready    <none>                 3m16s   v1.20.4
node3   Ready    <none>                 3m      v1.20.4

#dns 没起来 重启下
kubectl delete pod/coredns-7f89b7bc75-f6j8n -n kube-system

[[email protected] ~]# kubectl get pod -A
NAMESPACE     NAME                            READY   STATUS    RESTARTS   AGE
kube-system   coredns-7f89b7bc75-9gjrx        1/1     Running   0          6m3s
kube-system   coredns-7f89b7bc75-bkj2k        1/1     Running   0          15s
kube-system   etcd-node1                      1/1     Running   0          6m18s
kube-system   kube-apiserver-node1            1/1     Running   0          6m18s
kube-system   kube-controller-manager-node1   1/1     Running   0          6m18s
kube-system   kube-flannel-ds-amd64-2z8rw     1/1     Running   0          2m16s
kube-system   kube-flannel-ds-amd64-4pmhj     1/1     Running   0          2m16s
kube-system   kube-flannel-ds-amd64-vqwwk     1/1     Running   0          2m16s
kube-system   kube-proxy-562gr                1/1     Running   0          4m52s
kube-system   kube-proxy-6q6p9                1/1     Running   0          6m3s
kube-system   kube-proxy-t5bkd                1/1     Running   0          4m37s
kube-system   kube-scheduler-node1            1/1     Running   0          6m18s

相关标签: k8s篇