feat: add ansible playbook for kubernetes
This commit is contained in:
parent
bbe374ce12
commit
ab7e7c33b0
16
ansible/inventory.yaml
Normal file
16
ansible/inventory.yaml
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
workers:
|
||||||
|
hosts:
|
||||||
|
worker1:
|
||||||
|
ansible_host: 10.4.4.125
|
||||||
|
ansible_user: root
|
||||||
|
worker2:
|
||||||
|
ansible_host: 10.4.4.193
|
||||||
|
ansible_user: root
|
||||||
|
|
||||||
|
all_vms:
|
||||||
|
hosts:
|
||||||
|
master:
|
||||||
|
ansible_host: 10.4.4.162
|
||||||
|
ansible_user: root
|
||||||
|
children:
|
||||||
|
workers:
|
61
ansible/playbook.yaml
Normal file
61
ansible/playbook.yaml
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
---
|
||||||
|
- import_playbook: task-number-playbook.yaml
|
||||||
|
|
||||||
|
- name: Install k8s
|
||||||
|
hosts: all_vms
|
||||||
|
remote_user: root
|
||||||
|
tasks:
|
||||||
|
- name: Install k8s packages
|
||||||
|
apt_rpm:
|
||||||
|
name:
|
||||||
|
- kubernetes-kubeadm
|
||||||
|
- kubernetes-kubelet
|
||||||
|
- kubernetes-crio
|
||||||
|
- cri-tools
|
||||||
|
update_cache: true
|
||||||
|
- name: start and enable k8s services
|
||||||
|
service:
|
||||||
|
name: "{{ item }}"
|
||||||
|
enabled: yes
|
||||||
|
state: started
|
||||||
|
loop:
|
||||||
|
- crio
|
||||||
|
- kubelet
|
||||||
|
|
||||||
|
- name: Init k8s master
|
||||||
|
hosts: master
|
||||||
|
tasks:
|
||||||
|
- name: run kubeadm init
|
||||||
|
#shell: "kubeadm init --pod-network-cidr=10.244.0.0/16 --ignore-preflight-errors=FileAvailable--etc-kubernetes-manifests-kube-apiserver.yaml,FileAvailable--etc-kubernetes-manifests-kube-controller-manager.yaml,FileAvailable--etc-kubernetes-manifests-kube-scheduler.yaml,FileAvailable--etc-kubernetes-manifests-etcd.yaml | grep 'kubeadm join' -A 1 | sed 's|\\\\||' | xargs"
|
||||||
|
shell: "kubeadm init --pod-network-cidr=10.244.0.0/16 | grep 'kubeadm join' -A 1 | sed 's|\\\\||' | xargs"
|
||||||
|
register: kubeadm_init
|
||||||
|
changed_when: kubeadm_init.rc == 0
|
||||||
|
failed_when: kubeadm_init.rc not in [0, 2]
|
||||||
|
- name: save join command
|
||||||
|
copy:
|
||||||
|
content: "{{ kubeadm_init.stdout }}"
|
||||||
|
dest: kubeadm-join-command
|
||||||
|
when: kubeadm_init.rc == 0
|
||||||
|
- name: fetch join command
|
||||||
|
fetch:
|
||||||
|
src: kubeadm-join-command
|
||||||
|
dest: kubeadm-join-command
|
||||||
|
flat: yes
|
||||||
|
when: kubeadm_init.rc == 0
|
||||||
|
|
||||||
|
- name: Join k8s nodes
|
||||||
|
hosts: workers
|
||||||
|
tasks:
|
||||||
|
- name: run kubeadm join
|
||||||
|
command: "{{ lookup('file', 'kubeadm-join-command') }}"
|
||||||
|
register: kubeadm_join
|
||||||
|
changed_when: kubeadm_join.rc == 0
|
||||||
|
failed_when: kubeadm_join.rc not in [0, 2]
|
||||||
|
|
||||||
|
- name: Create network
|
||||||
|
hosts: master
|
||||||
|
tasks:
|
||||||
|
- name: create flannel network
|
||||||
|
command: kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
|
||||||
|
environment:
|
||||||
|
KUBECONFIG: /etc/kubernetes/admin.conf
|
26
ansible/task-number-playbook.yaml
Normal file
26
ansible/task-number-playbook.yaml
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
---
|
||||||
|
- name: Save global variables
|
||||||
|
hosts: all_vms
|
||||||
|
vars_prompt:
|
||||||
|
- name: task_number
|
||||||
|
private: no
|
||||||
|
when: not task_number
|
||||||
|
tasks:
|
||||||
|
- name: Add dummy host to save global vars
|
||||||
|
run_once: true
|
||||||
|
add_host:
|
||||||
|
name: DUMMY_GLOBAL_VARS
|
||||||
|
task_number: "{{ task_number }}"
|
||||||
|
|
||||||
|
- name: Maybe install apt-repo
|
||||||
|
hosts: all_vms
|
||||||
|
tasks:
|
||||||
|
- name: Install apt-repo
|
||||||
|
apt_rpm:
|
||||||
|
name: apt-repo
|
||||||
|
update_cache: true
|
||||||
|
when: hostvars.DUMMY_GLOBAL_VARS.task_number
|
||||||
|
- name: Add task
|
||||||
|
apt_repo:
|
||||||
|
repo: "{{ hostvars.DUMMY_GLOBAL_VARS.task_number }}"
|
||||||
|
when: hostvars.DUMMY_GLOBAL_VARS.task_number
|
23
main.py
23
main.py
@ -36,26 +36,31 @@ def main():
|
|||||||
node = min(nodes, key=lambda node: node['disk'])['node']
|
node = min(nodes, key=lambda node: node['disk'])['node']
|
||||||
template_id = 374
|
template_id = 374
|
||||||
prefix = 'stepchenkaos-test-k8s'
|
prefix = 'stepchenkaos-test-k8s'
|
||||||
|
vm_names = {
|
||||||
|
'master': f'{prefix}-master',
|
||||||
|
'worker1': f'{prefix}-node1',
|
||||||
|
'worker2': f'{prefix}-node2',
|
||||||
|
}
|
||||||
|
|
||||||
vm_id_master, upid_master = clone_template(
|
vm_id_master, upid_master = clone_template(
|
||||||
proxmox,
|
proxmox,
|
||||||
node,
|
node,
|
||||||
template_id,
|
template_id,
|
||||||
name=f'{prefix}-master',
|
name=vm_names['master'],
|
||||||
)
|
)
|
||||||
|
|
||||||
vm_id_worker1, upid_worker1 = clone_template(
|
vm_id_worker1, upid_worker1 = clone_template(
|
||||||
proxmox,
|
proxmox,
|
||||||
node,
|
node,
|
||||||
template_id,
|
template_id,
|
||||||
name=f'{prefix}-node1',
|
name=vm_names['worker1'],
|
||||||
)
|
)
|
||||||
|
|
||||||
vm_id_worker2, upid_worker2 = clone_template(
|
vm_id_worker2, upid_worker2 = clone_template(
|
||||||
proxmox,
|
proxmox,
|
||||||
node,
|
node,
|
||||||
template_id,
|
template_id,
|
||||||
name=f'{prefix}-node2',
|
name=vm_names['worker2'],
|
||||||
)
|
)
|
||||||
|
|
||||||
logger.info('Waiting for clone tasks to complete...')
|
logger.info('Waiting for clone tasks to complete...')
|
||||||
@ -91,16 +96,16 @@ def main():
|
|||||||
vm_ip_worker2 = get_vm_ip(proxmox, node, vm_id_worker2)
|
vm_ip_worker2 = get_vm_ip(proxmox, node, vm_id_worker2)
|
||||||
|
|
||||||
with open('vm_ids' ,'w') as ofile:
|
with open('vm_ids' ,'w') as ofile:
|
||||||
vm_ids = f'{vm_id_master}\tmaster\n'
|
vm_ids = f'{vm_id_master}\t{vm_names["master"]}\n'
|
||||||
vm_ids += f'{vm_id_worker1}\tworker1\n'
|
vm_ids += f'{vm_id_worker1}\t{vm_names["worker1"]}\n'
|
||||||
vm_ids += f'{vm_id_worker2}\tworker2\n'
|
vm_ids += f'{vm_id_worker2}\t{vm_names["worker2"]}\n'
|
||||||
logger.info(vm_ids)
|
logger.info(vm_ids)
|
||||||
ofile.write(vm_ids)
|
ofile.write(vm_ids)
|
||||||
|
|
||||||
with open('hosts' ,'w') as ofile:
|
with open('hosts' ,'w') as ofile:
|
||||||
hosts = f'{vm_ip_master}\tmaster\n'
|
hosts = f'{vm_ip_master}\t{vm_names["master"]}\n'
|
||||||
hosts += f'{vm_ip_worker1}\tworker1\n'
|
hosts += f'{vm_ip_worker1}\t{vm_names["worker1"]}\n'
|
||||||
hosts += f'{vm_ip_worker2}\tworker2\n'
|
hosts += f'{vm_ip_worker2}\t{vm_names["worker2"]}\n'
|
||||||
logger.info(hosts)
|
logger.info(hosts)
|
||||||
ofile.write(hosts)
|
ofile.write(hosts)
|
||||||
|
|
||||||
|
62
run.sh
Executable file
62
run.sh
Executable file
@ -0,0 +1,62 @@
|
|||||||
|
#!/bin/sh -exu
|
||||||
|
|
||||||
|
branch="${1:-"sisyphus"}"
|
||||||
|
task_number="${2-}"
|
||||||
|
|
||||||
|
rsync hosts root@10.4.4.162:/etc/hosts
|
||||||
|
rsync hosts root@10.4.4.125:/etc/hosts
|
||||||
|
rsync hosts root@10.4.4.193:/etc/hosts
|
||||||
|
ssh root@10.4.4.162 "systemctl restart network"
|
||||||
|
ssh root@10.4.4.125 "systemctl restart network"
|
||||||
|
ssh root@10.4.4.193 "systemctl restart network"
|
||||||
|
#rm -f hosts
|
||||||
|
|
||||||
|
ansible-playbook ansible/playbook.yaml -i ansible/inventory.yaml \
|
||||||
|
-e task_number="$task_number"
|
||||||
|
|
||||||
|
#rm -f kubeadm-join-command
|
||||||
|
|
||||||
|
rsync root@10.4.4.162:/etc/kubernetes/admin.conf ~/.kube/config
|
||||||
|
kubectl apply -f - <<'EOF'
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: nginx-deployment
|
||||||
|
spec:
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app: nginx
|
||||||
|
replicas: 2 # tells deployment to run 2 pods matching the template
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: nginx
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: nginx
|
||||||
|
image: obirvalger/my
|
||||||
|
ports:
|
||||||
|
- containerPort: 80
|
||||||
|
EOF
|
||||||
|
|
||||||
|
kubectl apply -f - <<'EOF'
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: nginx
|
||||||
|
labels:
|
||||||
|
app: nginx
|
||||||
|
spec:
|
||||||
|
type: NodePort
|
||||||
|
ports:
|
||||||
|
- port: 80
|
||||||
|
targetPort: 80
|
||||||
|
nodePort: 30007
|
||||||
|
selector:
|
||||||
|
app: nginx
|
||||||
|
EOF
|
||||||
|
|
||||||
|
sleep 180
|
||||||
|
|
||||||
|
curl 10.4.4.162:30007
|
||||||
|
kubectl exec $(kubectl get po | awk 'END{print $1}') -- nslookup nginx
|
Loading…
Reference in New Issue
Block a user