在开始安装 Kubernetes 之前,需要先将一些必要系统创建完成,其中 Etcd 就是 Kubernetes 最重要的一环,Kubernetes 会将大部分信息储存于 Etcd 上,来提供给其他节点索取,以确保整个集群运作与沟通正常。

创建集群 CA 与 Certificates

将会需要产生 client 与 server 的各组件 certificates,并且替 Kubernetes admin user 产生 client 证书。

create_upload_cert.sh

生成证书本来直接通过链接从官方下载,但是由于网路问题,提前下好,自己写到文件中

  1. #!/bin/sh
  2. ROOT=$(cd `dirname $0`/../&&pwd)
  3. pushd $ROOT/ssl/
  4. # 生成 CA 证书和私钥:
  5. cat > ca-config.json <<EOF
  6. {
  7. "signing": {
  8. "default": {
  9. "expiry": "8760h"
  10. },
  11. "profiles": {
  12. "kubernetes": {
  13. "usages": [
  14. "signing",
  15. "key encipherment",
  16. "server auth",
  17. "client auth"
  18. ],
  19. "expiry": "8760h"
  20. }
  21. }
  22. }
  23. }
  24. EOF
  25. cat > ca-csr.json <<EOF
  26. {
  27. "CN": "kubernetes",
  28. "key": {
  29. "algo": "rsa",
  30. "size": 2048
  31. },
  32. "names": [
  33. {
  34. "C": "CN",
  35. "ST": "Zhejiang",
  36. "L": "Hangzhou",
  37. "O": "k8s",
  38. "OU": "System"
  39. }
  40. ]
  41. }
  42. EOF
  43. /usr/local/bin/cfssl gencert -initca ca-csr.json | /usr/local/bin/cfssljson -bare ca
  44. rm ca-csr.json ca.csr
  45. # copy ssl certificates
  46. mkdir -p /etc/kubernetes/ssl
  47. cp ca* /etc/kubernetes/ssl
  48. # 创建 admin 证书签名请求
  49. cat > admin-csr.json << EOF
  50. {
  51. "CN": "admin",
  52. "hosts": [],
  53. "key": {
  54. "algo": "rsa",
  55. "size": 2048
  56. },
  57. "names": [
  58. {
  59. "C": "CN",
  60. "ST": "Zhejiang",
  61. "L": "Hangzhou",
  62. "O": "system:masters",
  63. "OU": "System"
  64. }
  65. ]
  66. }
  67. EOF
  68. /usr/local/bin/cfssl gencert -ca=/etc/kubernetes/ssl/ca.pem \
  69. -ca-key=/etc/kubernetes/ssl/ca-key.pem \
  70. -config=/etc/kubernetes/ssl/ca-config.json \
  71. -profile=kubernetes admin-csr.json | /usr/local/bin/cfssljson -bare admin
  72. cp admin*.pem /etc/kubernetes/ssl/
  73. rm admin.csr admin-csr.json
  74. # generate kubernetes csr
  75. cat > kubernetes-csr.json <<EOF
  76. {
  77. "CN": "kubernetes",
  78. "hosts": [
  79. "127.0.0.1",
  80. "${G_MASTER_IP}",
  81. "${CLUSTER_KUBERNETES_SVC_IP}",
  82. "kubernetes",
  83. "kubernetes.default",
  84. "kubernetes.default.svc",
  85. "kubernetes.default.svc.cluster",
  86. "kubernetes.default.svc.cluster.local"
  87. ],
  88. "key": {
  89. "algo": "rsa",
  90. "size": 2048
  91. },
  92. "names": [
  93. {
  94. "C": "CN",
  95. "ST": "Zhejiang",
  96. "L": "Hangzhou",
  97. "O": "k8s",
  98. "OU": "System"
  99. }
  100. ]
  101. }
  102. EOF
  103. # create kubernetes cert
  104. /usr/local/bin/cfssl gencert -ca=/etc/kubernetes/ssl/ca.pem \
  105. -ca-key=/etc/kubernetes/ssl/ca-key.pem \
  106. -config=/etc/kubernetes/ssl/ca-config.json \
  107. -profile=kubernetes kubernetes-csr.json | /usr/local/bin/cfssljson -bare kubernetes
  108. # rm kubernetes-csr.json kubernetes.csr
  109. mkdir -p /etc/kubernetes/ssl/
  110. cp kubernetes*.pem /etc/kubernetes/ssl/
  111. rm kubernetes.csr kubernetes-csr.json
  112. # generate kube-proxy csr
  113. cat > kube-proxy-csr.json <<EOF
  114. {
  115. "CN": "system:kube-proxy",
  116. "hosts": [],
  117. "key": {
  118. "algo": "rsa",
  119. "size": 2048
  120. },
  121. "names": [
  122. {
  123. "C": "CN",
  124. "ST": "Zhejiang",
  125. "L": "Hangzhou",
  126. "O": "k8s",
  127. "OU": "System"
  128. }
  129. ]
  130. }
  131. EOF
  132. # generate kube-proxy cert
  133. /usr/local/bin/cfssl gencert -ca=/etc/kubernetes/ssl/ca.pem \
  134. -ca-key=/etc/kubernetes/ssl/ca-key.pem \
  135. -config=/etc/kubernetes/ssl/ca-config.json \
  136. -profile=kubernetes kube-proxy-csr.json | /usr/local/bin/cfssljson -bare kube-proxy
  137. cp kube-proxy*.pem /etc/kubernetes/ssl/
  138. rm kube-proxy.csr kube-proxy-csr.json
  139. # create cert and key of etcd
  140. cat > etcd-csr.json <<EOF
  141. {
  142. "CN": "etcd",
  143. "hosts": [
  144. "127.0.0.1",
  145. "${ETCD_NODE_IP}"
  146. ],
  147. "key": {
  148. "algo": "rsa",
  149. "size": 2048
  150. },
  151. "names": [
  152. {
  153. "C": "CN",
  154. "ST": "Zhejiang",
  155. "L": "Hangzhou",
  156. "O": "k8s",
  157. "OU": "System"
  158. }
  159. ]
  160. }
  161. EOF
  162. # generate cert and private key of etcd
  163. /usr/local/bin/cfssl gencert -ca=/etc/kubernetes/ssl/ca.pem \
  164. -ca-key=/etc/kubernetes/ssl/ca-key.pem \
  165. -config=/etc/kubernetes/ssl/ca-config.json \
  166. -profile=kubernetes $ROOT/ssl/etcd-csr.json | /usr/local/bin/cfssljson -bare etcd
  167. mkdir -p /etc/etcd/ssl
  168. cp $ROOT/ssl/etcd*.pem /etc/etcd/ssl
  169. rm $ROOT/ssl/etcd.csr $ROOT/ssl/etcd-csr.json
  170. popd
  171. # SSL=$ROOT/ssl/
  172. # eval `find $SSL -type f | awk '{print "curl --request PUT --data-binary @"$1" http://'$G_CONSUL':8500/v1/kv/k8s/ssl/"substr($1,'$((${#SSL}+1))')";"}'`

Etcd 安装与设定

install_etcd.sh
#!/bin/sh

ROOT=$(cd `dirname $0`/../&&pwd)
BIN_DIR=/usr/local/bin

# create systemed unit of etcd
mkdir -p /var/lib/etcd  # 必须先创建工作目录

# 创建 etcd 的 systemd unit 文件
sh replace_env_variables.sh -s etcd

# launch etcd service
systemctl daemon-reload
systemctl enable etcd
systemctl start etcd
systemctl status etcd

replace_env_variables.sh
#!/bin/sh

ROOT=$(cd `dirname $0`/../&&pwd)
S_NAME=
M_NAME=
while getopts "s:m:" arg
do
    case $arg in
        s)
            S_NAME=$OPTARG;;
        m)
            M_NAME=$OPTARG;;
        ?) 
            echo "unkonw argument"
            exit 1
            ;;
    esac
done

# 声明方法
replace_service_variables(){    
    # Use alternative command character "~", since these maybe include a "/".
    sed -i s~__G_HOST_IP__~${G_HOST_IP:-}~g $TMP_SERVICE_FILE
    sed -i s~__G_HOSTNAME__~${G_HOSTNAME:-}~g $TMP_SERVICE_FILE
    sed -i s~__G_ETCD__~${G_ETCD:-}~g $TMP_SERVICE_FILE
    sed -i s~__G_DOCKER_REGISTRY__~${G_DOCKER_REGISTRY:-}~g $TMP_SERVICE_FILE
    sed -i s~__G_CONSUL__~${G_CONSUL:-}~g $TMP_SERVICE_FILE
    sed -i s~__G_MASTER_IP__~${G_MASTER_IP:-}~g $TMP_SERVICE_FILE
    sed -i s~__BOOTSTRAP_TOKEN__~${BOOTSTRAP_TOKEN:-}~g $TMP_SERVICE_FILE
    sed -i s~__SERVICE_CIDR__~${SERVICE_CIDR:-}~g $TMP_SERVICE_FILE
    sed -i s~__CLUSTER_CIDR__~${CLUSTER_CIDR:-}~g $TMP_SERVICE_FILE
    sed -i s~__NODE_PORT_RANGE__~${NODE_PORT_RANGE:-}~g $TMP_SERVICE_FILE
    sed -i s~__CLUSTER_KUBERNETES_SVC_IP__~${CLUSTER_KUBERNETES_SVC_IP:-}~g $TMP_SERVICE_FILE
    sed -i s~__CLUSTER_DNS_SVC_IP__~${CLUSTER_DNS_SVC_IP:-}~g $TMP_SERVICE_FILE
    sed -i s~__CLUSTER_DNS_DOMAIN__~${CLUSTER_DNS_DOMAIN:-}~g $TMP_SERVICE_FILE
    sed -i s~__ETCD_ENDPOINTS__~${ETCD_ENDPOINTS:-}~g $TMP_SERVICE_FILE
    sed -i s~__ETCD_NODE_NAME__~${ETCD_NODE_NAME:-}~g $TMP_SERVICE_FILE
    sed -i s~__ETCD_NODE_IP__~${ETCD_NODE_IP:-}~g $TMP_SERVICE_FILE
    sed -i s~__ETCD_NODE_IPS__~${ETCD_NODE_IPS:-}~g $TMP_SERVICE_FILE
    sed -i s~__ETCD_NODES__~${ETCD_NODES:-}~g $TMP_SERVICE_FILE
    sed -i s~__KUBE_APISERVER__~${KUBE_APISERVER:-}~g $TMP_SERVICE_FILE
}

# 声明方法
replace_manifest_variables(){
    sed -i s~__G_HOST_IP__~${G_HOST_IP:-}~g $MANIFEST_DIR/*.yaml
    sed -i s~__G_HOSTNAME__~${G_HOSTNAME:-}~g $MANIFEST_DIR/*.yaml
    sed -i s~__G_ETCD__~${G_ETCD:-}~g $MANIFEST_DIR/*.yaml
    sed -i s~__G_DOCKER_REGISTRY__~${G_DOCKER_REGISTRY:-}~g $MANIFEST_DIR/*.yaml
    sed -i s~__G_CONSUL__~${G_CONSUL:-}~g $MANIFEST_DIR/*.yaml
    sed -i s~__G_MASTER_IP__~${G_MASTER_IP:-}~g $MANIFEST_DIR/*.yaml
    sed -i s~__G_BOOTSTRAP_TOKEN__~${BOOTSTRAP_TOKEN:-}~g $MANIFEST_DIR/*.yaml
    sed -i s~__G_SERVICE_CIDR__~${SERVICE_CIDR:-}~g $MANIFEST_DIR/*.yaml
    sed -i s~__G_CLUSTER_CIDR__~${CLUSTER_CIDR:-}~g $MANIFEST_DIR/*.yaml
    sed -i s~__G_NODE_PORT_RANGE__~${NODE_PORT_RANGE:-}~g $MANIFEST_DIR/*.yaml
    sed -i s~__G_CLUSTER_KUBERNETES_SVC_IP__~${CLUSTER_KUBERNETES_SVC_IP:-}~g $MANIFEST_DIR/*.yaml
    sed -i s~__G_CLUSTER_DNS_SVC_IP__~${CLUSTER_DNS_SVC_IP:-}~g $MANIFEST_DIR/*.yaml
    sed -i s~__G_CLUSTER_DNS_DOMAIN__~${CLUSTER_DNS_DOMAIN:-}~g $MANIFEST_DIR/*.yaml
    sed -i s~__G_ETCD_ENDPOINTS__~${ETCD_ENDPOINTS:-}~g $MANIFEST_DIR/*.yaml
    sed -i s~__G_ETCD_NODE_NAME__~${ETCD_NODE_NAME:-}~g $MANIFEST_DIR/*.yaml
    sed -i s~__G_ETCD_NODE_IP__~${ETCD_NODE_IP:-}~g $MANIFEST_DIR/*.yaml
    sed -i s~__G_ETCD_NODE_IPS__~${ETCD_NODE_IPS:-}~g $MANIFEST_DIR/*.yaml
    sed -i s~__G_ETCD_NODES__~${ETCD_NODES:-}~g $MANIFEST_DIR/*.yaml
    sed -i s~__G_KUBE_APISERVER__~${KUBE_APISERVER:-}~g $MANIFEST_DIR/*.yaml
}


if [ "" != "$S_NAME"  ]; then
    TMP_SERVICE_FILE=/tmp/${S_NAME}.service
    cp $ROOT/systemd/${S_NAME}.service ${TMP_SERVICE_FILE}
    replace_service_variables
    mv ${TMP_SERVICE_FILE} /etc/systemd/system/ # 将最终替换好的service copy到/etc/systemd/system/下
fi
if [ "" != "$M_NAME"  ]; then
    MANIFEST_DIR=$ROOT/manifests/${M_NAME}/
    if [ -d $MANIFEST_DIR ]; then
        replace_manifest_variables
    fi
fi