diff --git a/install_k8s_centos/hosts b/install_k8s_centos/hosts new file mode 100644 index 0000000..b570369 --- /dev/null +++ b/install_k8s_centos/hosts @@ -0,0 +1,9 @@ +[k8s-master] + + +[k8s-workers] + + +[k8s-workers:vars] +K8S_MASTER_NODE_IP= +K8S_API_SECURE_PORT=6443 diff --git a/install_k8s_centos/main.yml b/install_k8s_centos/main.yml new file mode 100644 index 0000000..bbaf68f --- /dev/null +++ b/install_k8s_centos/main.yml @@ -0,0 +1,25 @@ +- hosts: all + become: yes + user: centos + gather_facts: no + pre_tasks: + - name: 'atualizando repo' + raw: yum update -y + + - name: 'instalando o python' + raw: yum install python2 python -y + + roles: + - { role: install-k8s, tags: ["install_k8s_role"]} + +- hosts: k8s-master + become: yes + user: centos + roles: + - { role: create-cluster, tags: ["create_cluster_role"]} + +- hosts: k8s-workers + become: yes + user: centos + roles: + - { role: join-workers, tags: ["join_workers_role"]} diff --git a/install_k8s_centos/roles/create-cluster/README.md b/install_k8s_centos/roles/create-cluster/README.md new file mode 100644 index 0000000..225dd44 --- /dev/null +++ b/install_k8s_centos/roles/create-cluster/README.md @@ -0,0 +1,38 @@ +Role Name +========= + +A brief description of the role goes here. + +Requirements +------------ + +Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required. + +Role Variables +-------------- + +A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well. + +Dependencies +------------ + +A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles. + +Example Playbook +---------------- + +Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too: + + - hosts: servers + roles: + - { role: username.rolename, x: 42 } + +License +------- + +BSD + +Author Information +------------------ + +An optional section for the role authors to include contact information, or a website (HTML is not allowed). diff --git a/install_k8s_centos/roles/create-cluster/defaults/main.yml b/install_k8s_centos/roles/create-cluster/defaults/main.yml new file mode 100644 index 0000000..53be71b --- /dev/null +++ b/install_k8s_centos/roles/create-cluster/defaults/main.yml @@ -0,0 +1,2 @@ +--- +# defaults file for create-cluster \ No newline at end of file diff --git a/install_k8s_centos/roles/create-cluster/handlers/main.yml b/install_k8s_centos/roles/create-cluster/handlers/main.yml new file mode 100644 index 0000000..ccb7e14 --- /dev/null +++ b/install_k8s_centos/roles/create-cluster/handlers/main.yml @@ -0,0 +1,2 @@ +--- +# handlers file for create-cluster \ No newline at end of file diff --git a/install_k8s_centos/roles/create-cluster/meta/main.yml b/install_k8s_centos/roles/create-cluster/meta/main.yml new file mode 100644 index 0000000..3a212a9 --- /dev/null +++ b/install_k8s_centos/roles/create-cluster/meta/main.yml @@ -0,0 +1,53 @@ +galaxy_info: + author: your name + description: your description + company: your company (optional) + + # If the issue tracker for your role is not on github, uncomment the + # next line and provide a value + # issue_tracker_url: http://example.com/issue/tracker + + # Choose a valid license ID from https://spdx.org - some suggested licenses: + # - BSD-3-Clause (default) + # - MIT + # - GPL-2.0-or-later + # - GPL-3.0-only + # - Apache-2.0 + # - CC-BY-4.0 + license: license (GPL-2.0-or-later, MIT, etc) + + min_ansible_version: 2.4 + + # If this a Container Enabled role, provide the minimum Ansible Container version. + # min_ansible_container_version: + + # + # Provide a list of supported platforms, and for each platform a list of versions. + # If you don't wish to enumerate all versions for a particular platform, use 'all'. + # To view available platforms and versions (or releases), visit: + # https://galaxy.ansible.com/api/v1/platforms/ + # + # platforms: + # - name: Fedora + # versions: + # - all + # - 25 + # - name: SomePlatform + # versions: + # - all + # - 1.0 + # - 7 + # - 99.99 + + galaxy_tags: [] + # List tags for your role here, one per line. A tag is a keyword that describes + # and categorizes the role. Users find roles by searching for tags. Be sure to + # remove the '[]' above, if you add tags to this list. + # + # NOTE: A tag is limited to a single word comprised of alphanumeric characters. + # Maximum 20 tags per role. + +dependencies: [] + # List your role dependencies here, one per line. Be sure to remove the '[]' above, + # if you add dependencies to this list. + \ No newline at end of file diff --git a/install_k8s_centos/roles/create-cluster/tasks/init-cluster.yml b/install_k8s_centos/roles/create-cluster/tasks/init-cluster.yml new file mode 100644 index 0000000..5d297ec --- /dev/null +++ b/install_k8s_centos/roles/create-cluster/tasks/init-cluster.yml @@ -0,0 +1,37 @@ +- name: Reset Cluster + command: + kubeadm reset --force + register: kubeadmin_init +- name: Initialize Kubernetes master with kubeadm init. + command: + kubeadm init + register: kubeadmin_init +- name: Ensure .kube directory exists. + file: + path: ~/.kube + state: directory +- name: Symlink the kubectl admin.conf to ~/.kube/conf. + file: + src: /etc/kubernetes/admin.conf + dest: ~/.kube/config + state: link +- name: Configure weavenet networking. + shell: kubectl apply -f {{ default_kubernetes_cni_weavenet_manifestUrl }} + register: weavenet_result +- name: "Cluster token" + shell: kubeadm token list | cut -d ' ' -f1 | sed -n '2p' + register: K8S_TOKEN +- name: "CA Hash" + shell: openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //' + register: K8S_MASTER_CA_HASH +- name: "Add K8S Token and Hash to dummy host" + add_host: + name: "K8S_TOKEN_HOLDER" + token: "{{ K8S_TOKEN.stdout }}" + hash: "{{ K8S_MASTER_CA_HASH.stdout }}" +- name: '' + debug: + msg: "[Master] K8S_TOKEN_HOLDER K8S token is {{ hostvars['K8S_TOKEN_HOLDER']['token'] }}" +- name: '' + debug: + msg: "[Master] K8S_TOKEN_HOLDER K8S Hash is {{ hostvars['K8S_TOKEN_HOLDER']['hash'] }}" diff --git a/install_k8s_centos/roles/create-cluster/tasks/main.yml b/install_k8s_centos/roles/create-cluster/tasks/main.yml new file mode 100644 index 0000000..e20137c --- /dev/null +++ b/install_k8s_centos/roles/create-cluster/tasks/main.yml @@ -0,0 +1,3 @@ +--- +# tasks file for create-cluster +- include: init-cluster.yml diff --git a/install_k8s_centos/roles/create-cluster/tests/inventory b/install_k8s_centos/roles/create-cluster/tests/inventory new file mode 100644 index 0000000..878877b --- /dev/null +++ b/install_k8s_centos/roles/create-cluster/tests/inventory @@ -0,0 +1,2 @@ +localhost + diff --git a/install_k8s_centos/roles/create-cluster/tests/test.yml b/install_k8s_centos/roles/create-cluster/tests/test.yml new file mode 100644 index 0000000..2cca844 --- /dev/null +++ b/install_k8s_centos/roles/create-cluster/tests/test.yml @@ -0,0 +1,5 @@ +--- +- hosts: localhost + remote_user: root + roles: + - create-cluster \ No newline at end of file diff --git a/install_k8s_centos/roles/create-cluster/vars/main.yml b/install_k8s_centos/roles/create-cluster/vars/main.yml new file mode 100644 index 0000000..dd326c7 --- /dev/null +++ b/install_k8s_centos/roles/create-cluster/vars/main.yml @@ -0,0 +1,3 @@ +--- +# vars file for create-cluster +default_kubernetes_cni_weavenet_manifestUrl: "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')" diff --git a/install_k8s_centos/roles/install-k8s/README.md b/install_k8s_centos/roles/install-k8s/README.md new file mode 100644 index 0000000..225dd44 --- /dev/null +++ b/install_k8s_centos/roles/install-k8s/README.md @@ -0,0 +1,38 @@ +Role Name +========= + +A brief description of the role goes here. + +Requirements +------------ + +Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required. + +Role Variables +-------------- + +A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well. + +Dependencies +------------ + +A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles. + +Example Playbook +---------------- + +Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too: + + - hosts: servers + roles: + - { role: username.rolename, x: 42 } + +License +------- + +BSD + +Author Information +------------------ + +An optional section for the role authors to include contact information, or a website (HTML is not allowed). diff --git a/install_k8s_centos/roles/install-k8s/defaults/main.yml b/install_k8s_centos/roles/install-k8s/defaults/main.yml new file mode 100644 index 0000000..6066d8a --- /dev/null +++ b/install_k8s_centos/roles/install-k8s/defaults/main.yml @@ -0,0 +1,2 @@ +--- +# defaults file for install-k8s \ No newline at end of file diff --git a/install_k8s_centos/roles/install-k8s/handlers/main.yml b/install_k8s_centos/roles/install-k8s/handlers/main.yml new file mode 100644 index 0000000..88c0fef --- /dev/null +++ b/install_k8s_centos/roles/install-k8s/handlers/main.yml @@ -0,0 +1,2 @@ +--- +# handlers file for install-k8s \ No newline at end of file diff --git a/install_k8s_centos/roles/install-k8s/meta/main.yml b/install_k8s_centos/roles/install-k8s/meta/main.yml new file mode 100644 index 0000000..3a212a9 --- /dev/null +++ b/install_k8s_centos/roles/install-k8s/meta/main.yml @@ -0,0 +1,53 @@ +galaxy_info: + author: your name + description: your description + company: your company (optional) + + # If the issue tracker for your role is not on github, uncomment the + # next line and provide a value + # issue_tracker_url: http://example.com/issue/tracker + + # Choose a valid license ID from https://spdx.org - some suggested licenses: + # - BSD-3-Clause (default) + # - MIT + # - GPL-2.0-or-later + # - GPL-3.0-only + # - Apache-2.0 + # - CC-BY-4.0 + license: license (GPL-2.0-or-later, MIT, etc) + + min_ansible_version: 2.4 + + # If this a Container Enabled role, provide the minimum Ansible Container version. + # min_ansible_container_version: + + # + # Provide a list of supported platforms, and for each platform a list of versions. + # If you don't wish to enumerate all versions for a particular platform, use 'all'. + # To view available platforms and versions (or releases), visit: + # https://galaxy.ansible.com/api/v1/platforms/ + # + # platforms: + # - name: Fedora + # versions: + # - all + # - 25 + # - name: SomePlatform + # versions: + # - all + # - 1.0 + # - 7 + # - 99.99 + + galaxy_tags: [] + # List tags for your role here, one per line. A tag is a keyword that describes + # and categorizes the role. Users find roles by searching for tags. Be sure to + # remove the '[]' above, if you add tags to this list. + # + # NOTE: A tag is limited to a single word comprised of alphanumeric characters. + # Maximum 20 tags per role. + +dependencies: [] + # List your role dependencies here, one per line. Be sure to remove the '[]' above, + # if you add dependencies to this list. + \ No newline at end of file diff --git a/install_k8s_centos/roles/install-k8s/tasks/install.yml b/install_k8s_centos/roles/install-k8s/tasks/install.yml new file mode 100644 index 0000000..265e91f --- /dev/null +++ b/install_k8s_centos/roles/install-k8s/tasks/install.yml @@ -0,0 +1,68 @@ +- name: desativando o SeLinux + selinux: + state: disabled + +- name: desativando o Firewalld + yum: + name: firewalld + state: removed + +- name: instalando o iptables + yum: + name: iptables + state: present + +- name: desativando o swap + shell: swapoff -a + +- name: removendo o swap + replace: + path: /etc/fstab + regexp: '^([^#].*?\sswap\s+sw\s+.*)$' + replace: '# \1' + +- name: Permitindo o Forward no Centos + shell: echo 1 > /proc/sys/net/ipv4/ip_forward + +- name: gravando o forward persistente a reinicios + sysctl: + name: net.ipv4.ip_forward + reload: yes + state: present + value: '1' + +- name: Adicionando o Repositorio do Docker + shell: curl -fSsl https://download.docker.com/linux/centos/docker-ce.repo > /etc/yum.repos.d/docker-ce.repo + +- name: Adicionando o repo do kubernetes no YUM + yum_repository: + name: kubernetes + file: kubernetes + description: kubernetes + baseurl: https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64 + gpgcheck: yes + repo_gpgcheck: yes + gpgkey: + - https://packages.cloud.google.com/yum/doc/yum-key.gpg + - https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg + enabled: yes + state: present + +- name: Instalando o Docker + yum: + name: docker-ce + state: latest + +- name: iniciando o servico do Docker + systemd: + name: docker + state: started + +- name: Install k8s packages + yum: + name: "{{ packages }}" + vars: + packages: + - kubelet + - kubeadm + - kubectl \ No newline at end of file diff --git a/install_k8s_centos/roles/install-k8s/tasks/main.yml b/install_k8s_centos/roles/install-k8s/tasks/main.yml new file mode 100644 index 0000000..518fd2a --- /dev/null +++ b/install_k8s_centos/roles/install-k8s/tasks/main.yml @@ -0,0 +1,3 @@ +--- +# tasks file for install-k8s +- include: install.yml diff --git a/install_k8s_centos/roles/install-k8s/tests/inventory b/install_k8s_centos/roles/install-k8s/tests/inventory new file mode 100644 index 0000000..878877b --- /dev/null +++ b/install_k8s_centos/roles/install-k8s/tests/inventory @@ -0,0 +1,2 @@ +localhost + diff --git a/install_k8s_centos/roles/install-k8s/tests/test.yml b/install_k8s_centos/roles/install-k8s/tests/test.yml new file mode 100644 index 0000000..7491ccc --- /dev/null +++ b/install_k8s_centos/roles/install-k8s/tests/test.yml @@ -0,0 +1,5 @@ +--- +- hosts: localhost + remote_user: root + roles: + - install-k8s \ No newline at end of file diff --git a/install_k8s_centos/roles/install-k8s/vars/main.yml b/install_k8s_centos/roles/install-k8s/vars/main.yml new file mode 100644 index 0000000..e4baa9e --- /dev/null +++ b/install_k8s_centos/roles/install-k8s/vars/main.yml @@ -0,0 +1,2 @@ +--- +# vars file for install-k8s \ No newline at end of file diff --git a/install_k8s_centos/roles/join-workers/README.md b/install_k8s_centos/roles/join-workers/README.md new file mode 100644 index 0000000..225dd44 --- /dev/null +++ b/install_k8s_centos/roles/join-workers/README.md @@ -0,0 +1,38 @@ +Role Name +========= + +A brief description of the role goes here. + +Requirements +------------ + +Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required. + +Role Variables +-------------- + +A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well. + +Dependencies +------------ + +A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles. + +Example Playbook +---------------- + +Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too: + + - hosts: servers + roles: + - { role: username.rolename, x: 42 } + +License +------- + +BSD + +Author Information +------------------ + +An optional section for the role authors to include contact information, or a website (HTML is not allowed). diff --git a/install_k8s_centos/roles/join-workers/defaults/main.yml b/install_k8s_centos/roles/join-workers/defaults/main.yml new file mode 100644 index 0000000..c4cc343 --- /dev/null +++ b/install_k8s_centos/roles/join-workers/defaults/main.yml @@ -0,0 +1,2 @@ +--- +# defaults file for join-workers \ No newline at end of file diff --git a/install_k8s_centos/roles/join-workers/handlers/main.yml b/install_k8s_centos/roles/join-workers/handlers/main.yml new file mode 100644 index 0000000..7d3bb5b --- /dev/null +++ b/install_k8s_centos/roles/join-workers/handlers/main.yml @@ -0,0 +1,2 @@ +--- +# handlers file for join-workers \ No newline at end of file diff --git a/install_k8s_centos/roles/join-workers/meta/main.yml b/install_k8s_centos/roles/join-workers/meta/main.yml new file mode 100644 index 0000000..3a212a9 --- /dev/null +++ b/install_k8s_centos/roles/join-workers/meta/main.yml @@ -0,0 +1,53 @@ +galaxy_info: + author: your name + description: your description + company: your company (optional) + + # If the issue tracker for your role is not on github, uncomment the + # next line and provide a value + # issue_tracker_url: http://example.com/issue/tracker + + # Choose a valid license ID from https://spdx.org - some suggested licenses: + # - BSD-3-Clause (default) + # - MIT + # - GPL-2.0-or-later + # - GPL-3.0-only + # - Apache-2.0 + # - CC-BY-4.0 + license: license (GPL-2.0-or-later, MIT, etc) + + min_ansible_version: 2.4 + + # If this a Container Enabled role, provide the minimum Ansible Container version. + # min_ansible_container_version: + + # + # Provide a list of supported platforms, and for each platform a list of versions. + # If you don't wish to enumerate all versions for a particular platform, use 'all'. + # To view available platforms and versions (or releases), visit: + # https://galaxy.ansible.com/api/v1/platforms/ + # + # platforms: + # - name: Fedora + # versions: + # - all + # - 25 + # - name: SomePlatform + # versions: + # - all + # - 1.0 + # - 7 + # - 99.99 + + galaxy_tags: [] + # List tags for your role here, one per line. A tag is a keyword that describes + # and categorizes the role. Users find roles by searching for tags. Be sure to + # remove the '[]' above, if you add tags to this list. + # + # NOTE: A tag is limited to a single word comprised of alphanumeric characters. + # Maximum 20 tags per role. + +dependencies: [] + # List your role dependencies here, one per line. Be sure to remove the '[]' above, + # if you add dependencies to this list. + \ No newline at end of file diff --git a/install_k8s_centos/roles/join-workers/tasks/join-cluster.yml b/install_k8s_centos/roles/join-workers/tasks/join-cluster.yml new file mode 100644 index 0000000..8fc172a --- /dev/null +++ b/install_k8s_centos/roles/join-workers/tasks/join-cluster.yml @@ -0,0 +1,19 @@ +- name: + debug: + msg: "[Worker] K8S_TOKEN_HOLDER K8S token is {{ hostvars['K8S_TOKEN_HOLDER']['token'] }}" + +- name: + debug: + msg: "[Worker] K8S_TOKEN_HOLDER K8S Hash is {{ hostvars['K8S_TOKEN_HOLDER']['hash'] }}" + +- name: "Kubeadm reset node cluster config" + command: + kubeadm reset --force + register: kubeadm_reset_node + +- name: "Kubeadm join" + shell: + kubeadm join --token={{ hostvars['K8S_TOKEN_HOLDER']['token'] }} + --discovery-token-ca-cert-hash sha256:{{ hostvars['K8S_TOKEN_HOLDER']['hash'] }} + {{K8S_MASTER_NODE_IP}}:{{K8S_API_SECURE_PORT}} + diff --git a/install_k8s_centos/roles/join-workers/tasks/main.yml b/install_k8s_centos/roles/join-workers/tasks/main.yml new file mode 100644 index 0000000..037253a --- /dev/null +++ b/install_k8s_centos/roles/join-workers/tasks/main.yml @@ -0,0 +1,3 @@ +--- +# tasks file for join-workers +- include: join-cluster.yml diff --git a/install_k8s_centos/roles/join-workers/tests/inventory b/install_k8s_centos/roles/join-workers/tests/inventory new file mode 100644 index 0000000..878877b --- /dev/null +++ b/install_k8s_centos/roles/join-workers/tests/inventory @@ -0,0 +1,2 @@ +localhost + diff --git a/install_k8s_centos/roles/join-workers/tests/test.yml b/install_k8s_centos/roles/join-workers/tests/test.yml new file mode 100644 index 0000000..ed217da --- /dev/null +++ b/install_k8s_centos/roles/join-workers/tests/test.yml @@ -0,0 +1,5 @@ +--- +- hosts: localhost + remote_user: root + roles: + - join-workers \ No newline at end of file diff --git a/install_k8s_centos/roles/join-workers/vars/main.yml b/install_k8s_centos/roles/join-workers/vars/main.yml new file mode 100644 index 0000000..d95c62c --- /dev/null +++ b/install_k8s_centos/roles/join-workers/vars/main.yml @@ -0,0 +1,2 @@ +--- +# vars file for join-workers \ No newline at end of file