项目作者: choerodon

项目描述 :
Kuberadmin ansible is a toolkit for simple and quick installing k8s cluster.
高级语言: HTML
项目地址: git://github.com/choerodon/kubeadm-ansible.git
创建时间: 2018-05-22T07:14:17Z
项目社区:https://github.com/choerodon/kubeadm-ansible

开源协议:Apache License 2.0

下载


This project is no longer supported,There is no plan to support any new features.The successor is open-hand/kubeadm-ha

Kubeadmin Ansible 中文

Kubeadmin ansible is a toolkit for simple and quick installing k8s cluster.

1. Environmental preparation

Note: Currently only centos 7.2+ is supported

Install the ansible run environment on the machine where the ansible script is to be executed:

  1. sudo yum install epel-release -y
  2. sudo yum install git python36 sshpass -y
  3. sudo python3.6 -m ensurepip
  4. sudo /usr/local/bin/pip3 install --no-cache-dir ansible==2.7.5 netaddr

Clone project:

  1. git clone https://github.com/choerodon/kubeadm-ansible.git

2. Modify hosts

Edit the inventory/hosts file under the toolkit, modify the access address, user name, and password of each machine and maintain the relationship between each node and role. The front name is the hostname of the machine. The user must have root privileges.

Note: The etcd node and the master node need to be on the same machine.

For example, if deploy a single-node cluster, configure it (reference):

  1. [all]
  2. node1 ansible_host=192.168.56.11 ansible_user=root ansible_ssh_pass=change_it ansible_become=true
  3. [kube-master]
  4. node1
  5. [etcd]
  6. node1
  7. [kube-node]
  8. node1

3. Modify the variable

Edit the inventory/vars file under the toolkit, and change the value of k8s_interface to the name of the ipv4 NIC (centos defaults to eth0). If not sure, use the ifconfig command to check it.

  1. k8s_interface: "eth0"

Note: If the names of the network card are not the same between the machines, delete the k8s_interface variable from the inventory/vars file and add an IP address to each machine in the inventory/host file. For example:

  1. [all]
  2. node1 ansible_host=192.168.56.11 ip=192.168.56.11 ansible_user=root ansible_ssh_pass=change_it ansible_become=true
  3. ...
  4. ...

If all machines access the external network as `proxy’, please configure the following variables, otherwise do not configure:

  1. http_proxy: http://1.2.3.4:3128
  2. https_proxy: http://1.2.3.4:3128
  3. no_proxy: localhost,127.0.0.0/8
  4. docker_proxy_enable: true

4. Deploy

If deploy on Alibaba Cloud, please read Alibaba Cloud Deployment first in this page.

Execute:

  1. ansible-playbook -i inventory/hosts -e @inventory/vars cluster.yml

View the status of the waiting pod for running:

  1. kubectl get po -n kube-system

If the deployment fails and you want to reset the cluster (all data), execute:

  1. ansible-playbook -i inventory/hosts reset.yml

5. Ingress TSL configuration

Reference: [TSL Configuration Notes] (docs/ingress-nginx.md)

6. Dashboard configuration

Reference: [Dashboard configuration instructions] (docs/dashboard.md)

7. Alibaba Cloud Deployment

Modify Hostname(*)

Modify the hostname of the ECS instance on the control panel of ECS. The name should preferably contain only lowercase letters, numbers, and dash. And keep consistent with the name in the ʻinventory/hosts` and the name of ECS console, restart to take effect.

Segment selection (*)

If the ECS server uses a private network, the segments of pod and service cannot overlap with the VPC segment. For example, refer to:

  1. # If the vpc segment is `172.*`
  2. kube_pods_subnet: 192.168.0.0/20
  3. kube_service_addresses: 192.168.255.0/20
  4. # If the vpc segment is `10.*`
  5. kube_pods_subnet: 172.16.0.0/16
  6. kube_service_addresses: 172.19.0.0/20
  7. # If the vpc segment is `192.168.*`
  8. kube_pods_subnet: 172.16.0.0/16
  9. kube_service_addresses: 172.19.0.0/20

Flannel type (*)

When deploying k8s on an ECS using a VPC network, the backend type of the flannel network needs to be ali-vpc. By default, the vxlan type is used in this script. Although the network is able to communicate in the VPC environment, the instability fluctuates. So it is recommended to use the ali-vpc type.

Therefore, set the default flannel network to not be installed by adding variables in the inventory/vars file:

  1. flannel_enable: false

After running the ansible script, manually install the flannel network plugin and create the configuration file kube-flannel-aliyun.yml on one of the master nodes:

  1. ---
  2. kind: ClusterRole
  3. apiVersion: rbac.authorization.k8s.io/v1beta1
  4. metadata:
  5. name: flannel
  6. rules:
  7. - apiGroups:
  8. - ""
  9. resources:
  10. - pods
  11. verbs:
  12. - get
  13. - apiGroups:
  14. - ""
  15. resources:
  16. - nodes
  17. verbs:
  18. - list
  19. - watch
  20. - apiGroups:
  21. - ""
  22. resources:
  23. - nodes/status
  24. verbs:
  25. - patch
  26. ---
  27. kind: ClusterRoleBinding
  28. apiVersion: rbac.authorization.k8s.io/v1beta1
  29. metadata:
  30. name: flannel
  31. roleRef:
  32. apiGroup: rbac.authorization.k8s.io
  33. kind: ClusterRole
  34. name: flannel
  35. subjects:
  36. - kind: ServiceAccount
  37. name: flannel
  38. namespace: kube-system
  39. ---
  40. apiVersion: v1
  41. kind: ServiceAccount
  42. metadata:
  43. name: flannel
  44. namespace: kube-system
  45. ---
  46. kind: ConfigMap
  47. apiVersion: v1
  48. metadata:
  49. name: kube-flannel-cfg
  50. namespace: kube-system
  51. labels:
  52. tier: node
  53. app: flannel
  54. data:
  55. cni-conf.json: |
  56. {
  57. "name": "cbr0",
  58. "type": "flannel",
  59. "delegate": {
  60. "isDefaultGateway": true
  61. }
  62. }
  63. net-conf.json: |
  64. {
  65. "Network": "[PodsSubnet]",
  66. "Backend": {
  67. "Type": "ali-vpc"
  68. }
  69. }
  70. ---
  71. apiVersion: extensions/v1beta1
  72. kind: DaemonSet
  73. metadata:
  74. name: kube-flannel-ds
  75. namespace: kube-system
  76. labels:
  77. tier: node
  78. app: flannel
  79. spec:
  80. template:
  81. metadata:
  82. labels:
  83. tier: node
  84. app: flannel
  85. spec:
  86. hostNetwork: true
  87. nodeSelector:
  88. beta.kubernetes.io/arch: amd64
  89. tolerations:
  90. - key: node-role.kubernetes.io/master
  91. operator: Exists
  92. effect: NoSchedule
  93. serviceAccountName: flannel
  94. initContainers:
  95. - name: install-cni
  96. image: registry.cn-hangzhou.aliyuncs.com/google-containers/flannel:v0.9.0
  97. command:
  98. - cp
  99. args:
  100. - -f
  101. - /etc/kube-flannel/cni-conf.json
  102. - /etc/cni/net.d/10-flannel.conf
  103. volumeMounts:
  104. - name: cni
  105. mountPath: /etc/cni/net.d
  106. - name: flannel-cfg
  107. mountPath: /etc/kube-flannel/
  108. containers:
  109. - name: kube-flannel
  110. image: registry.cn-hangzhou.aliyuncs.com/google-containers/flannel:v0.9.0
  111. command: [ "/opt/bin/flanneld", "--ip-masq", "--kube-subnet-mgr" ]
  112. securityContext:
  113. privileged: true
  114. env:
  115. - name: POD_NAME
  116. valueFrom:
  117. fieldRef:
  118. fieldPath: metadata.name
  119. - name: POD_NAMESPACE
  120. valueFrom:
  121. fieldRef:
  122. fieldPath: metadata.namespace
  123. - name: ACCESS_KEY_ID
  124. value: [YOUR_ACCESS_KEY_ID]
  125. - name: ACCESS_KEY_SECRET
  126. value: [YOUR_ACCESS_KEY_SECRET]
  127. volumeMounts:
  128. - name: run
  129. mountPath: /run
  130. - name: flannel-cfg
  131. mountPath: /etc/kube-flannel/
  132. volumes:
  133. - name: run
  134. hostPath:
  135. path: /run
  136. - name: cni
  137. hostPath:
  138. path: /etc/cni/net.d
  139. - name: flannel-cfg
  140. configMap:
  141. name: kube-flannel-cfg

Please pay attention to modify the parameter value in the configuration:

  • Network:The network segment of Pod.

  • ACCESS_KEY_ID: Required

  • ACCESS_KEY_SECRET: Required

TheACCESS_KEY user has the following permissions:

  • Read-only access to cloud server (ECS) permissions
  • Manage Permissions for a Private Network (VPC)

Then use the kubectl command to deploy. After the deployment is successful, multiple route entries have been added to the routing table of the VPN. The next hop is the pod IP segment of each node.

  1. kubectl apply -f kube-flannel-aliyun.yml

Next, in the ECS security group, add the address of the pod network segment in the inbound rule. Otherwise, the ports of other nodes’ pods cannot be accessed in the pod container. For example:

Authorization Policy Protocol Type Port Range Authorization Type Authorization Object
Allow All -1/-1 Address Segment Access 192.168.0.0/20

Binding Cloud Storage

Under normal circumstances, pv are stored using nfs, but the efficiency of reading and writing is not very high, for pv with high performance requirements for reading and writing, you can configure the cloud disk as a mount volume.

If use aliyun cloud storage, also need to deploy aliyun-controller components.

First, execute the following command on all nodes. Copy the aliyun-flexv binary file into the kubele plugin directory:

  1. FLEXPATH=/usr/libexec/kubernetes/kubelet-plugins/volume/exec/aliyun~flexv;
  2. sudo mkdir $FLEXPATH -p;
  3. docker run --rm -v $FLEXPATH:/opt registry.aliyuncs.com/kubeup/kube-aliyun cp /flexv /opt/

Then, modify the aliyun-controller.yml file under roles/addons/kubeup under this project, and fill in the relevant values with the relevant variables. If are not sure, log in to aliyun to view the corresponding management console, or request the address on the server to query curl --retry 5 -sSL http://100.100.100.200/latest/meta-data/{{META_ID}}.

--cluster-cidr: IP section of pod

ALIYUN_ACCESS_KEY: The API Access Key of Alibaba Cloud

ALIYUN_ACCESS_KEY_SECRET: The API Access Key of Alibaba Cloud

ALIYUN_ZONE: Availability id of cloud server ECS

ALIYUN_ROUTER: Private network vpc routing id

ALIYUN_ROUTE_TABLE: Private network vpc routing id

ALIYUN_REGION: Availability id of cloud server ECS

ALIYUN_VPC: Private network vpc routing id

ALIYUN_VSWITCH: The switch id of private network vpc

After filling in the variables, copy the above file to /etc/kubernetes/manifests/ of all master nodes.

The ACCESS_KEY user has the following permissions:

  • Read-only access to cloud server (ECS) permissions
  • Manage Permissions for a Private Network (VPC)

Edit the /etc/kubernetes/manifests/kube-controller-manager.yaml file under all master nodes. Add the following two commands and environment variables in the command command:

command:

  1. --allocate-node-cidrs=true
  2. --configure-cloud-routes=false

Environment variables:

  1. env:
  2. - name: ALIYUN_ACCESS_KEY
  3. value: [YOUR_ALIYUN_ACCESS_KEY]
  4. - name: ALIYUN_ACCESS_KEY_SECRET
  5. value: [YOUR_ALIYUN_ACCESS_KEY_SECRET]

Restart all the kubelets of the master node:

  1. systemctl restart kubelet

Check if the kube-controller is healthy:

  1. kubectl get po -n kube-system | grep aliyun-controller

Bind the examples of cloud disk , each cloud disk can only be bound once:

  1. # Using pv binding, diskId is the id of the cloud disk
  2. kind: PersistentVolume
  3. apiVersion: v1
  4. metadata:
  5. name: test-pv-volume
  6. labels:
  7. type: flexVolume
  8. spec:
  9. capacity:
  10. storage: 20Gi
  11. accessModes:
  12. - ReadWriteOnce
  13. flexVolume:
  14. driver: "aliyun/flexv"
  15. fsType: "ext4"
  16. options:
  17. diskId: "d-bp1i23j39i30if"
  18. # Directly bind pod
  19. apiVersion: v1
  20. kind: Pod
  21. metadata:
  22. name: nginx
  23. spec:
  24. containers:
  25. - name: nginx
  26. image: nginx
  27. volumeMounts:
  28. - name: test
  29. mountPath: /data
  30. ports:
  31. - containerPort: 80
  32. volumes:
  33. - name: test
  34. flexVolume:
  35. driver: "aliyun/flexv"
  36. fsType: "ext4"
  37. options:
  38. diskId: "d-1ierokwer8234jowe"

Reporting issues

If you find any shortcomings or bugs, please describe them in the issue.

How to contribute

Pull requests are welcome! Follow this link for more information on how to contribute.
Pull requests are welcome! Follow this link for more information on how to contribute.

8. Refresh cluster certificate

The prerequisite for refreshing the certificate is to ensure that the CA root certificate exists. After the certificate is refreshed, the master node kubelet is restarted to apply the new certificate. At this time, the cluster may not be operated for 1-2 minutes, but the business application is not affected.

  1. ansible-playbook -i inventory/hosts -e @inventory/vars renew-certs.yml