用ansible安装K8S集群的相关配置

配置环境:centos7

将下列代码加入到每个节点的/etc/hosts文件中:

k8smaster 192.168.10.195 123456
k8sworker1 192.168.10.171 123456
k8sworker2 192.168.11.13 123456
k8sworker3 192.168.11.179 123456

将对应的机器使用 hostnamectl set-hostname xxxxx更改主机名

/etc/ansible/hosts文件中:

[k8smaster]
192.168.10.195
[k8sworkers]
192.168.10.171
192.168.11.13
192.168.11.179
[k8s]
192.168.10.195
192.168.10.171
192.168.11.13
192.168.11.179

搭建k8s环境的k8s.yml文件:

- hosts: k8s
  become: yes
  become_user: root
  tasks:
   - name: changefirewalld
     service:
      name: firewalld
      enabled: no
   - name: swapoff
     shell: swapoff -a
   - name: stopfirewalld
     service:
      name: firewalld
      state: stopped
   - name: SELINUX=disabled
     selinux: state=disabled
   - name: Copy file with owner and permissions
     copy:
      src: /etc/hosts
      dest: /etc/hosts
      owner: root
      group: root
      mode: '0644'
   - name: remove-old-docker
     yum:
      pkg: docker,docker-ce*
      state: removed
   - name: install-alirepo
     shell: |
           rm -rfv /etc/yum.repos.d/*;
           curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo;
           curl -o /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
   - name: install-k8s-packges
     yum:
      pkg: bash-completion,device-mapper-persistent-data,lvm2
      state: installed
   - name: prepare
     shell: |
           mkdir -p /etc/docker
           tee /etc/docker/daemon.json <<-'EOF'
           {
           "registry-mirrors": ["https://fl791z1h.mirror.aliyuncs.com"]
           }
           EOF
           systemctl daemon-reload
           systemctl restart docker
           cat < /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
   - name: install-kubetools
     yum:
      pkg: kubelet-1.20.7-0.x86_64,kubectl-1.20.7-0.x86_64,kubeadm-1.20.7-0.x86_64,docker-ce-18.06.3.ce-3.el7.x86_64
      state: installed
   - name: enable-kubelet
     service:
      name: kubelet
      enabled: yes
      state: started
   - name: enable-docker
     service:
      name: docker
      enabled: yes
      state: started

ansible-playbook k8s.yml运行

 然后初始化master节点:在master机器上运行:

echo y | kubeadm reset
kubeadm init --kubernetes-version=1.20.7  \
--apiserver-advertise-address=192.168.10.195   \
--image-repository registry.aliyuncs.com/google_containers  \
--service-cidr=10.10.0.0/16 --pod-network-cidr=10.122.0.0/16;
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
kubectl taint nodes --all node-role.kubernetes.io/master-

 运行完之后能获取到从机加入k8s集群的URL,在从机上先运行echo y | kubeadm reset

然后输入URL加入节点

然后在master上输入 kubectl get nodes 去查看节点,如果这里报错为:

The connection to the server localhost:8080 was refused - did you specify the right host or port?

就执行:

echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> /etc/profile
source /etc/profile

你可能感兴趣的:(ansible,k8s集权安装,kubernetes)