ansible-role-kubernetes-controller
This role is used in Kubernetes the not so hard way with Ansible - Control plane. It installs the Kubernetes API server, scheduler and controller manager. For more information about this role please have a look at Kubernetes the not so hard way with Ansible - Control plane.
Versions
I tag every release and try to stay with semantic versioning. If you want to use the role I recommend to checkout the latest tag. The master branch is basically development while the tags mark stable releases. But in general I try to keep master in good shape too. A tag 31.0.1+1.36.7 means this is release 31.0.1 of this role and it’s meant to be used with Kubernetes version 1.36.7 (but should work with any K8s 1.36.x release of course). If the role itself changes X.Y.Z before + will increase. If the Kubernetes version changes X.Y.Z after + will increase too. This allows to tag bugfixes and new major versions of the role while it’s still developed for a specific Kubernetes release. That’s especially useful for Kubernetes major releases with breaking changes.
Requirements
This role requires that you already created some certificates for Kubernetes API server (see Kubernetes the not so hard way with Ansible - Certificate authority (CA)). The role copies the certificates from k8s_ctl_ca_conf_directory (which is by default the same as k8s_ca_conf_directory used by githubixx.kubernetes_ca role) to the destination host.
Your hosts on which you want to install Kubernetes should be able to communicate with each other of course. To add an additional layer of security you can setup a fully meshed VPN with WireGuard e.g. (see Kubernetes the not so hard way with Ansible - WireGuard). This encrypts every communication between the Kubernetes nodes if the Kubernetes processes use the WireGuard interface. Using WireGuard actually makes it also easily possible to have a Kubernetes cluster that is distributed in various data centers e.g.
And of course an etcd cluster (see Kubernetes the not so hard way with Ansible - etcd cluster) to store the state of the Kubernetes cluster.
Supported OS
- Ubuntu 22.04 (Jammy Jellyfish)
- Ubuntu 24.04 (Noble Numbat) (recommended)
- Ubuntu 26.04 (Resolute Raccoon)
Changelog
Change history:
See full CHANGELOG.md
Recent changes:
31.0.1+1.36.7
- UPDATE
- update
k8s_ctl_release to 1.36.7
31.0.0+1.36.6
-
UPDATE
- update
k8s_ctl_release to 1.36.6
- add Ubuntu 26.04 support
- add explicit
nodes/pods, nodes/healthz, and nodes/configz permissions to the API-server-to-kubelet ClusterRole for stable fine-grained kubelet authorization; preserve existing permissions and binding
- create the admin kubeconfig directory on the configured delegation host
-
MOLECULE
- use Ubuntu 26.04 for
test-controller3 and add test-worker3 with Ubuntu 26.04
- verify exact control-plane binary, kubectl, and API server versions, kubelet RBAC permissions, and control-plane leader election leases
- fail health and admin access checks when either the request fails or its response is invalid
- store the admin kubeconfig under
/home/vagrant/k8s/configs, owned by vagrant, and symlink /home/vagrant/.kube/config for default kubectl access
-
UPGRADE NOTES
- Keep the existing audit log defaults of 30 days, 3 backups, and 100 MB. In Kubernetes 1.36, overriding
audit-log-maxsize to 0 disables rotation; upstream retention defaults do not replace this role’s explicit settings.
- Kubernetes 1.36 enables direct WebSocket streaming to supporting kubelets by default. Keep the API server’s kubelet client certificates and proxy permissions when customizing settings.
- Kubeconfigs still embed certificates. Automatic reload of file-based CA roots in Kubernetes 1.36 does not rotate embedded CA data; regenerate kubeconfigs when rotating their CA.
- IMPORTANT: While not part of this role but as
kube-apiserver depends on etcd: If you use etcd for Kubernetes and want to upgrade to etcd v3.6 you DEFINITELY want to upgrade to etcd v3.6.26 first! For further information:
30.0.0+1.35.5
-
UPDATE
- update
k8s_ctl_release to 1.35.5
-
UPGRADE NOTES
- Kubernetes 1.35 expects cgroup v2 for default kubelet behavior. This role only manages the control plane, but cluster operators should verify worker and kubelet hosts are already on cgroup v2 before upgrading.
- Kubernetes 1.35 requires
create permission for pods/exec, pods/attach, and pods/portforward upgrade paths. Review user-facing RBAC in adjacent roles or cluster manifests if you rely on those operations.
- IMPORTANT: While not part of this role but as
kube-apiserver depends on etcd: If you use etcd for Kubernetes and want to upgrade to etcd v3.6 you DEFINITELY want to upgrade to etcd v3.6.26 first! For further information:
29.0.0+1.34.4
-
UPDATE
- update
k8s_ctl_release to 1.34.4
-
OTHER
- replace injected
ansible_* facts usage with ansible_facts[...] (prepares for ansible-core 2.24 where INJECT_FACTS_AS_VARS default changes)
-
MOLECULE
28.0.0+1.33.6
- UPDATE
- update
k8s_ctl_release to 1.33.6
27.1.1+1.32.9
27.1.0+1.32.9
-
UPDATE
- update
k8s_ctl_release to 1.32.9
-
OTHER CHANGES
- fix for Ansible 2.19 in
kube-apiserver.service.j2: remove cluster_hosts() macro and adjust combine k8s_apiserver_settings and k8s_apiserver_settings_user task accordingly
defaults/main.yml: k8s_ctl_api_endpoint_host/k8s_admin_api_endpoint_host - simplify the complex default variables for endpoint hosts to avoid nested template construction
-
MOLECULE
- install
python3-kubernetes package instead kubernetes Pip in prepare.yml
27.0.0+1.32.8
-
BREAKING
- Removed Ubuntu 20.04 because reached end of life
- Introduce
k8s_apiserver_admission_plugins variable. Previously in k8s_apiserver_settings variable the values of enable-admission-plugins key was a string with list of admission plugins separated by commas. To make that string more readable k8s_apiserver_admission_plugins variable was introduced which is now a list of admissions plugins that is consumed by enable-admission-plugins. If you didn’t changed k8s_apiserver_admission_plugins variable or used your own settings nothing changed for you.
-
UPDATE
- update
k8s_ctl_release to 1.32.8
-
MOLECULE
- Removed Ubuntu 20.04 because reached end of life
- Fix
ansible-lint issues
Installation
-
Directly download from Github (Change into Ansible roles directory before cloning. You can figure out the role path by using ansible-config dump | grep DEFAULT_ROLES_PATH command):
git clone https://github.com/githubixx/ansible-role-kubernetes-controller.git githubixx.kubernetes_controller
-
Via ansible-galaxy command and download directly from Ansible Galaxy:
ansible-galaxy install role githubixx.kubernetes_controller
-
Create a requirements.yml file with the following content (this will download the role from Github) and install with
ansible-galaxy role install -r requirements.yml (change version if needed):
---
roles:
- name: githubixx.kubernetes_controller
src: https://github.com/githubixx/ansible-role-kubernetes-controller.git
version: 31.0.1+1.36.7
Role (default) variables
# The base directory for Kubernetes configuration and certificate files for
# everything control plane related. After the playbook is done this directory
# contains various sub-folders.
k8s_ctl_conf_dir: "/etc/kubernetes/controller"
# All certificate files (Private Key Infrastructure related) specified in
# "k8s_ctl_certificates" and "k8s_ctl_etcd_certificates" (see "vars/main.yml")
# will be stored here. Owner of this new directory will be "root". Group will
# be the group specified in "k8s_run_as_group"`. The files in this directory
# will be owned by "root" and group as specified in "k8s_run_as_group"`. File
# permissions will be "0640".
k8s_ctl_pki_dir: "{{ k8s_ctl_conf_dir }}/pki"
# The directory to store the Kubernetes binaries (see "k8s_ctl_binaries"
# variable in "vars/main.yml"). Owner and group of this new directory
# will be "root" in both cases. Permissions for this directory will be "0755".
#
# NOTE: The default directory "/usr/local/bin" normally already exists on every
# Linux installation with the owner, group and permissions mentioned above. If
# your current settings are different consider a different directory. But make sure
# that the new directory is included in your "$PATH" variable value.
k8s_ctl_bin_dir: "/usr/local/bin"
# The Kubernetes release.
k8s_ctl_release: "1.36.7"
# The interface on which the Kubernetes services should listen on. As all cluster
# communication should use a VPN interface the interface name is
# normally "wg0" (WireGuard),"peervpn0" (PeerVPN) or "tap0".
#
# The network interface on which the Kubernetes control plane services should
# listen on. That is:
#
# - kube-apiserver
# - kube-scheduler
# - kube-controller-manager
#
k8s_interface: "eth0"
# Run Kubernetes control plane service (kube-apiserver, kube-scheduler,
# kube-controller-manager) as this user.
#
# If you want to use a "secure-port" < 1024 for "kube-apiserver" you most
# probably need to run "kube-apiserver" as user "root" (not recommended).
#
# If the user specified in "k8s_run_as_user" does not exist then the role
# will create it. Only if the user already exists the role will not create it
# but it will adjust it's UID/GID and shell if specified (see settings below).
# So make sure that UID, GID and shell matches the existing user if you don't
# want that that user will be changed.
#
# Additionally if "k8s_run_as_user" is "root" then this role wont touch the user
# at all.
k8s_run_as_user: "k8s"
# UID of user specified in "k8s_run_as_user". If not specified the next available
# UID from "/etc/login.defs" will be taken (see "SYS_UID_MAX" setting in that file).
# k8s_run_as_user_uid: "999"
# Shell for specified user in "k8s_run_as_user". For increased security keep
# the default.
k8s_run_as_user_shell: "/bin/false"
# Specifies if the user specified in "k8s_run_as_user" will be a system user (default)
# or not. If "true" the "k8s_run_as_user_home" setting will be ignored. In general
# it makes sense to keep the default as there should be no need to login as
# the user that runs kube-apiserver, kube-scheduler or kube-controller-manager.
k8s_run_as_user_system: true
# Home directory of user specified in "k8s_run_as_user". Will be ignored if
# "k8s_run_as_user_system" is set to "true". In this case no home directory will
# be created. Normally not needed.
# k8s_run_as_user_home: "/home/k8s"
# Run Kubernetes daemons (kube-apiserver, kube-scheduler, kube-controller-manager)
# as this group.
#
# Note: If the group specified in "k8s_run_as_group" does not exist then the role
# will create it. Only if the group already exists the role will not create it
# but will adjust GID if specified in "k8s_run_as_group_gid" (see setting below).
k8s_run_as_group: "k8s"
# GID of group specified in "k8s_run_as_group". If not specified the next available
# GID from "/etc/login.defs" will be take (see "SYS_GID_MAX" setting in that file).
# k8s_run_as_group_gid: "999"
# Specifies if the group specified in "k8s_run_as_group" will be a system group (default)
# or not.
k8s_run_as_group_system: true
# By default all tasks that needs to communicate with the Kubernetes
# cluster are executed on local host (127.0.0.1). But if that one
# doesn't have direct connection to the K8s cluster or should be executed
# elsewhere this variable can be changed accordingly.
k8s_ctl_delegate_to: "127.0.0.1"
# The IP address or hostname of the Kubernetes API endpoint. This variable
# is used by "kube-scheduler" and "kube-controller-manager" to connect
# to the "kube-apiserver" (Kubernetes API server).
#
# By default the first host in the Ansible group "k8s_controller" is
# specified here. NOTE: This setting is not fault tolerant! That means
# if the first host in the Ansible group "k8s_controller" is down
# the worker node and its workload continue working but the worker
# node doesn't receive any updates from Kubernetes API server.
#
# If you have a loadbalancer that distributes traffic between all
# Kubernetes API servers it should
提示:代码过长已自动截断
The kube-apiserver settings defined in k8s_apiserver_settings can be overridden by defining a variable called k8s_apiserver_settings_user. You can also add additional settings by using this variable. E.g. to override audit-log-maxage and audit-log-maxbackup default values and add watch-cache add the following settings to group_vars/k8s.yml:
k8s_apiserver_settings_user:
"audit-log-maxage": "40"
"audit-log-maxbackup": "4"
"watch-cache": "false"
The same is true for the kube-controller-manager by adding entries to k8s_controller_manager_settings_user variable. For kube-scheduler add entries to k8s_scheduler_settings_user variable to override/add settings in k8s_scheduler_settings dictionary.
Example Playbook
- hosts: k8s_controller
roles:
- githubixx.kubernetes_controller
Testing
This role has a small test setup that is created using Molecule, libvirt (vagrant-libvirt) and QEMU/KVM. Please see my blog post Testing Ansible roles with Molecule, libvirt (vagrant-libvirt) and QEMU/KVM how to setup. The Molecule test configuration is in molecule/default.
Afterwards Molecule can be executed:
molecule converge
This will setup seven virtual machines (VM) covering Ubuntu 22.04, 24.04, and 26.04, including an Ubuntu 26.04 controller and worker-designated host. It installs a Kubernetes control plane but does not install kube-proxy or kubelet on the worker-designated hosts. Verification therefore permits an empty node list while checking admin API access. A small verification step is also included:
molecule verify
On test-assets, the admin kubeconfig is owned by vagrant at /home/vagrant/k8s/configs/admin.kubeconfig. The symlink /home/vagrant/.kube/config lets that user run kubectl without --kubeconfig:
sudo -iu vagrant
kubectl get namespaces
To clean up run
molecule destroy
License
GNU GENERAL PUBLIC LICENSE Version 3
http://www.tauceti.blog
评论