/etc/hosts 노드 정보 추가
# update-hosts-playbook.yaml
- name: Update /etc/hosts
hosts: node
become: true # Run all tasks with sudo
tasks:
- name: Add entries to /etc/hosts
lineinfile:
path: "/etc/hosts"
line: "{{ item }}"
with_items:
- "192.168.0.200 k8s-lb"
- "192.168.0.201 master1"
- "192.168.0.202 master2"
- "192.168.0.203 master3"
- "192.168.0.204 worker1"
- "192.168.0.205 worker2"
- "192.168.0.206 worker3"
# Optional: If you want to ensure that the lines are present and in order
# become: true
# order: keep
- name: Remove duplicate entries from /etc/hosts
command: sort -u -o /etc/hosts /etc/hosts
apt 업데이트 및 업그레이드
- name: install apt update and apt upgrade
hosts: node
tasks:
- name: Update and upgrade apt packages
become: 'True'
apt:
upgrade: 'yes'
update_cache: 'yes'
cache_valid_time: 86400 #One day
쿠버네티스 설치
- name: Install Kubernetes
hosts: node
tasks:
- name: APT install dependencies
become: true
apt:
force: yes
pkg:
- ca-certificates
- curl
- gnupg
- lsb-release
- name: Add GPG key for Kubernetes packages
become: true
shell: curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
- name: Add Kubernetes repository
become: true
shell: echo "deb http://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
- name: Update package lists again
become: true
shell: sudo apt-get update
- name: Install Kubernetes components
become: true
shell: sudo apt-get install -y kubelet kubeadm kubectl containerd
- name: Reload systemd and restart kubelet
become: true
shell: sudo systemctl daemon-reload && sudo systemctl restart kubelet
- name: Pull Kubernetes container images
become: true
shell: kubeadm config images pull
초기화 설정(ip forward/swap off/call iptables)
- name: Configure Kernel and Networking for Kubernetes
hosts: node
tasks:
- name: Load kernel module br_netfilter
become: true
shell: sudo modprobe br_netfilter
- name: Enable IP forwarding
become: true
shell: /sbin/sysctl -w net.ipv4.ip_forward=1
- name: Disable swap
become: true
shell: swapoff -a
kubectl 설정 리셋하기
# Kubernetes Node Initialization Playbook
- name: Reset Kubernetes Node
hosts: node
become: true # Run all tasks with sudo
vars:
kube_user: kingelip # Username for Kubernetes setup
kube_config_dir: "/home/{{ kube_user }}/.kube" # Directory path for Kubernetes config files
kube_config_files: # List of Kubernetes config files to remove
- config
- known_hosts
kube_manifest_files: # List of Kubernetes manifest files to remove
- kube-apiserver.yaml
- kube-controller-manager.yaml
- kube-scheduler.yaml
- etcd.yaml
tasks:
- name: Reset Kubernetes with kubeadm
shell: sudo kubeadm reset --force
ignore_errors: yes # Ignore errors in case there is no existing cluster
- name: Restart kubelet service
systemd:
name: kubelet
state: restarted
- name: Remove kubeconfig files
file:
path: "{{ kube_config_dir }}/{{ item }}"
state: absent
loop: "{{ kube_config_files }}"
- name: Remove Kubernetes manifest files
file:
path: "/etc/kubernetes/manifests/{{ item }}"
state: absent
loop: "{{ kube_manifest_files }}"
- name: Remove CNI network configuration directory
file:
path: /etc/cni/net.d
state: absent
- name: Reboot the node
command: shutdown -r now
async: 0
poll: 0
become: true
ignore_errors: true
- name: Wait for the node to become reachable
wait_for_connection:
timeout: 300 # Adjust the timeout as needed
에러해결 Unable to connect to the server: tls: failed to verify certificate: x509
# create-kubeconfig-playbook.yaml
- name: Create Kubernetes Config
hosts: master1
become: true # Run all tasks with sudo
tasks:
- name: Create kube directory
file:
path: "{{ ansible_env.HOME }}/.kube"
state: directory
- name: Copy admin.conf to kube directory
copy:
src: /etc/kubernetes/admin.conf
dest: "{{ ansible_env.HOME }}/.kube/config"
when: not ansible_filesystem.exists("{{ ansible_env.HOME }}/.kube/config")
- name: Set ownership for kubeconfig
file:
path: "{{ ansible_env.HOME }}/.kube/config"
owner: "{{ ansible_user_id }}"
group: "{{ ansible_user_id }}"
mode: '0600'
'기타정보' 카테고리의 다른 글
[정보] kubernetes 관리도구 k9s 설치 (0) | 2024.01.18 |
---|---|
[정보] kubernetes etcd 상태확인 (0) | 2024.01.18 |
[정보] kubernetes 트러블 슈팅 (0) | 2024.01.18 |
[정보] ubuntu 20.04 쿠버네티스 한방 설치(containerd/keepalived) - (단일구성/다중구성) (0) | 2024.01.17 |
[정보] 메일서버 쉽게 만들기 mail in a box (0) | 2024.01.09 |