Skip to content

Commit

Permalink
Reduce the cluster size to 1
Browse files Browse the repository at this point in the history
Signed-off-by: Ruoqing He <[email protected]>
  • Loading branch information
TimePrinciple committed Jul 12, 2023
1 parent 31f0183 commit d0d2def
Show file tree
Hide file tree
Showing 26 changed files with 210 additions and 241 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ Then you should now have a working `rk8s` ready for deploying.

3. `ssh-keygen` to generate a key for ssh connection across machines, and `ssh-copy-id -i <path/to/.pub> root@<IP address>` notifies machines to be deployed.

4. Run `rk8s deploy`.
4. `rk8s install cfssl` to install cfssl-related tools for later use.

5. Run `rk8s deploy`.

Then you should have a working cluster, ssh to the master node and run `kubectl get nodes`, you should see the master node is ready.

Expand Down
12 changes: 2 additions & 10 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,13 @@ impl Config {
pub fn generate_config_template() {
let config = Config {
instance_name: "master01".to_owned(),
instance_ip: "192.168.221.143".to_owned(),
instance_ip: "192.168.157.130".to_owned(),
instance_hosts: {
let mut map = HashMap::new();
map.insert(
"192.168.221.143".to_owned(),
"192.168.157.130".to_owned(),
"master01".to_owned(),
);
map.insert(
"192.168.221.147".to_owned(),
"worker01".to_owned(),
);
map.insert(
"192.168.221.148".to_owned(),
"worker02".to_owned(),
);
map
},

Expand Down
8 changes: 4 additions & 4 deletions src/deploy/docker.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::process::Command;
use std::path::{Path, PathBuf};
use std::env;
use std::fs;
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::Command;

use crate::config::Config;

Expand Down Expand Up @@ -78,7 +78,7 @@ pub fn start(config: &Config) {
tracing::info!("Change working directory into `docker`");
let prev_dir = Path::new("/rk8s");
let work_dir = Path::new("/rk8s/docker");
env::set_current_dir(&work_dir).expect("Error happened when trying to change into `etcd`");
env::set_current_dir(work_dir).expect("Error happened when trying to change into `etcd`");
tracing::info!("Changed to {}", env::current_dir().unwrap().display());

// Prepare directory to be sent.
Expand Down Expand Up @@ -156,7 +156,7 @@ pub fn start(config: &Config) {
tracing::info!("Docker started on {}", ip);
}

env::set_current_dir(&prev_dir).expect("Error happened when trying to change into `etcd`");
env::set_current_dir(prev_dir).expect("Error happened when trying to change into `etcd`");
tracing::info!(
"Change working directory back to {}",
env::current_dir().unwrap().display()
Expand Down
22 changes: 8 additions & 14 deletions src/deploy/etcd.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use crate::config::Config;
use serde::{Deserialize, Serialize};
use std::env;
use std::fs::{self, File};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::env;
use std::process::{Command, Stdio};
use std::thread;


#[derive(Serialize, Deserialize, Debug)]
struct CAConfig {
signing: Signing,
Expand Down Expand Up @@ -107,7 +106,7 @@ impl ServerCsr {
CN: config.etcd_CN.to_owned(),
hosts: {
let mut hosts = Vec::new();
for (ip, _) in &config.instance_hosts {
for ip in config.instance_hosts.keys() {
hosts.push(ip.to_owned());
}
hosts
Expand All @@ -134,12 +133,8 @@ impl ETCDCfg {

writeln!(&mut etcd_conf, "#[Member]")
.expect("Error happened when trying to write `etcd.conf`");
writeln!(
&mut etcd_conf,
"ETCD_NAME=\"etcd_{}\"",
current_name
)
.expect("Error happened when trying to write `etcd.conf`");
writeln!(&mut etcd_conf, "ETCD_NAME=\"etcd_{}\"", current_name)
.expect("Error happened when trying to write `etcd.conf`");
writeln!(
&mut etcd_conf,
"ETCD_DATA_DIR=\"/var/lib/etcd/default.etcd\""
Expand All @@ -157,7 +152,7 @@ impl ETCDCfg {
current_ip
)
.expect("Error happened when trying to write `etcd.conf`");
writeln!(&mut etcd_conf, "").expect("Error happened when trying to write `etcd.conf`");
writeln!(&mut etcd_conf).expect("Error happened when trying to write `etcd.conf`");
writeln!(&mut etcd_conf, "#[Clustering]")
.expect("Error happened when trying to write `etcd.conf`");
writeln!(
Expand Down Expand Up @@ -224,15 +219,14 @@ WantedBy=multi-user.target
}
}


pub fn start(config: &Config) {
// Deploy etcd to all hosts according to their name.
// Etcd does not distinguish masters or workers.
tracing::info!("Preparing mutual .pem, .service and etcd binaries...");
tracing::info!("Change working directory into `etcd`");
let prev_dir = Path::new("/rk8s");
let work_dir = Path::new("/rk8s/etcd");
env::set_current_dir(&work_dir).expect("Error happened when trying to change into `etcd`");
env::set_current_dir(work_dir).expect("Error happened when trying to change into `etcd`");
tracing::info!("Changed to {}", env::current_dir().unwrap().display());

tracing::info!("Start generating `ca-config.json`...");
Expand Down Expand Up @@ -311,7 +305,7 @@ pub fn start(config: &Config) {
check_dir_exist_or_create(bin_path);
let ssl_path = PathBuf::from("to_send/etcd/ssl");
check_dir_exist_or_create(ssl_path);
for (ip, _) in &config.instance_hosts {
for ip in config.instance_hosts.keys() {
let path = PathBuf::from("to_send");
let path = path.join(ip);
check_dir_exist_or_create(path);
Expand Down Expand Up @@ -401,7 +395,7 @@ pub fn start(config: &Config) {
handle.join().unwrap();
}

env::set_current_dir(&prev_dir).expect("Error happened when trying to change into `etcd`");
env::set_current_dir(prev_dir).expect("Error happened when trying to change into `etcd`");
tracing::info!(
"Change working directory back to {}",
env::current_dir().unwrap().display()
Expand Down
28 changes: 13 additions & 15 deletions src/deploy/kube_apiserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl ServerCsr {
"kubernetes.default.svc.cluster".to_string(),
"kubernetes.default.svc.cluster.local".to_string(),
];
for (ip, _) in &config.instance_hosts {
for ip in config.instance_hosts.keys() {
hosts.push(ip.to_owned());
}
hosts
Expand All @@ -141,8 +141,11 @@ struct KubeApiserverCfg;

impl KubeApiserverCfg {
fn generate(current_ip: &String, config: &Config) {
let mut apiserver_conf = File::create(format!("to_send/{}/apiserver/kube-apiserver.conf", current_ip))
.expect("Error happened when trying to create kube-apiserver configuration file");
let mut apiserver_conf = File::create(format!(
"to_send/{}/apiserver/kube-apiserver.conf",
current_ip
))
.expect("Error happened when trying to create kube-apiserver configuration file");

writeln!(
&mut apiserver_conf,
Expand All @@ -152,7 +155,7 @@ impl KubeApiserverCfg {
)
.expect("Error happened when trying to write `kube-apiserver.conf`");
let mut buffer = String::new();
for (ip, _) in &config.instance_hosts {
for ip in config.instance_hosts.keys() {
buffer.push_str(format!("https://{}:2379,", ip).as_str());
}
buffer.pop();
Expand All @@ -162,12 +165,8 @@ impl KubeApiserverCfg {
.expect("Error happened when trying to write `kube-apiserver.conf`");
writeln!(&mut apiserver_conf, "--secure-port=6443")
.expect("Error happened when trying to write `kube-apiserver.conf`");
writeln!(
&mut apiserver_conf,
"--advertise-address={}",
current_ip
)
.expect("Error happened when trying to write `kube-apiserver.conf`");
writeln!(&mut apiserver_conf, "--advertise-address={}", current_ip)
.expect("Error happened when trying to write `kube-apiserver.conf`");
writeln!(
&mut apiserver_conf,
r#"--allow-privileged=true \
Expand Down Expand Up @@ -229,13 +228,12 @@ WantedBy=multi-user.target
}
}


pub fn start(config: &Config) {
tracing::info!("kube_apiserver phase started");
tracing::info!("Change working directory into `k8s`");
let prev_dir = Path::new("/rk8s");
let work_dir = Path::new("/rk8s/k8s");
env::set_current_dir(&work_dir).expect("Error happened when trying to change into `k8s`");
env::set_current_dir(work_dir).expect("Error happened when trying to change into `k8s`");
tracing::info!("Changed to {}", env::current_dir().unwrap().display());

tracing::info!("Start generating `ca-config.json`...");
Expand Down Expand Up @@ -308,8 +306,8 @@ pub fn start(config: &Config) {
tracing::info!("Self-signed CA certificate generated");

tracing::info!("Generating `token.csv` to to_send/...");
let mut token = File::create("to_send/token.csv")
.expect("Error happened when trying to write token file");
let mut token =
File::create("to_send/token.csv").expect("Error happened when trying to write token file");
token.write_all(b"4136692876ad4b01bb9dd0988480ebba,kubelet-bootstrap,10001,\"system:node-bootstrapper\"").expect("Error happened when trying to write `token.csv`");
tracing::info!("`token.csv` generated");

Expand Down Expand Up @@ -370,7 +368,7 @@ pub fn start(config: &Config) {
}
}

env::set_current_dir(&prev_dir).expect("Error happened when trying to change into `/rk8s`");
env::set_current_dir(prev_dir).expect("Error happened when trying to change into `/rk8s`");
tracing::info!(
"Change working directory back to {}",
env::current_dir().unwrap().display()
Expand Down
17 changes: 8 additions & 9 deletions src/deploy/kube_controller_manager.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
use crate::config::Config;
use serde::{Deserialize, Serialize};
use std::env;
use std::path::Path;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::process::{Command, Stdio};

struct KubeControllerManagerCfg;

impl KubeControllerManagerCfg {
fn generate() {
let mut controller_conf = File::create("to_send/kube-controller-manager.conf")
.expect(
"Error happened when trying to create kube-controller-manager configuration file",
);
let mut controller_conf = File::create("to_send/kube-controller-manager.conf").expect(
"Error happened when trying to create kube-controller-manager configuration file",
);

writeln!(
&mut controller_conf,
r#"KUBE_CONTROLLER_MANAGER_OPTS="--logtostderr=false \
r#"KUBE_CONTROLLER_MANAGER_OPTS="--logtostderr=false \
--v=2 \
--log-dir=/opt/kubernetes/logs \
--leader-elect=true \
Expand Down Expand Up @@ -111,7 +110,7 @@ pub fn start(config: &Config) {
tracing::info!("Change working directory into `k8s`");
let prev_dir = Path::new("/rk8s");
let work_dir = Path::new("/rk8s/k8s");
env::set_current_dir(&work_dir).expect("Error happened when trying to change into `k8s`");
env::set_current_dir(work_dir).expect("Error happened when trying to change into `k8s`");
tracing::info!("Changed to {}", env::current_dir().unwrap().display());

tracing::info!("Start generating `kube-controller-manager-csr.json`...");
Expand Down Expand Up @@ -224,9 +223,9 @@ pub fn start(config: &Config) {
}
}

env::set_current_dir(&prev_dir).expect("Error happened when trying to change into `/rk8s`");
env::set_current_dir(prev_dir).expect("Error happened when trying to change into `/rk8s`");
tracing::info!(
"Change working directory back to {}",
env::current_dir().unwrap().display()
);
}
}
39 changes: 19 additions & 20 deletions src/deploy/kube_proxy.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
use crate::config::Config;
use serde::{Deserialize, Serialize};
use std::env;
use std::path::Path;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::process::{Command, Stdio};

struct KubeProxyCfg;

impl KubeProxyCfg {
fn generate() {
let mut kube_proxy_conf = File::create("to_send/kube-proxy.conf")
.expect(
"Error happened when trying to create kube-proxy configuration file",
);
.expect("Error happened when trying to create kube-proxy configuration file");

writeln!(
&mut kube_proxy_conf,
r#"KUBE_PROXY_OPTS="--logtostderr=false \
r#"KUBE_PROXY_OPTS="--logtostderr=false \
--v=2 \
--log-dir=/opt/kubernetes/logs \
--config=/opt/kubernetes/cfg/kube-proxy-config.yml"
Expand All @@ -31,14 +29,15 @@ struct KubeProxyConfig;

impl KubeProxyConfig {
fn generate(current_ip: &String, current_name: &String) {
let mut kube_proxy_config = File::create(format!("to_send/{}/kube_proxy/kube-proxy-config.yml", current_ip))
.expect(
"Error happened when trying to create kube-proxy configuration file",
);
let mut kube_proxy_config = File::create(format!(
"to_send/{}/kube_proxy/kube-proxy-config.yml",
current_ip
))
.expect("Error happened when trying to create kube-proxy configuration file");

writeln!(
&mut kube_proxy_config,
r#"kind: KubeProxyConfiguration
r#"kind: KubeProxyConfiguration
apiVersion: kubeproxy.config.k8s.io/v1alpha1
bindAddress: 0.0.0.0
metricsBindAddress: 0.0.0.0:10249
Expand All @@ -54,7 +53,7 @@ clientConnection:
.expect("Error happened when trying to write `kube-proxy-config.yml`");
writeln!(
&mut kube_proxy_config,
r#"clusterCIDR: 10.244.0.0/16
r#"clusterCIDR: 10.244.0.0/16
"#,
)
.expect("Error happened when trying to write `kube-proxy-config.yml`");
Expand Down Expand Up @@ -136,7 +135,7 @@ pub fn start(config: &Config) {
tracing::info!("Change working directory into `k8s`");
let prev_dir = Path::new("/rk8s");
let work_dir = Path::new("/rk8s/k8s");
env::set_current_dir(&work_dir).expect("Error happened when trying to change into `k8s`");
env::set_current_dir(work_dir).expect("Error happened when trying to change into `k8s`");
tracing::info!("Changed to {}", env::current_dir().unwrap().display());

tracing::info!("Start generating `kube-proxy-csr.json`...");
Expand All @@ -147,13 +146,11 @@ pub fn start(config: &Config) {
.expect("Error happened when trying to create `kube-proxy-csr.json`");
kube_proxy_csr_file
.write_all(content.as_bytes())
.expect(
"Error happened when trying to write content to `kube-proxy-csr.json`",
);
.expect("Error happened when trying to write content to `kube-proxy-csr.json`");
tracing::info!("`kube-proxy-csr.json` generated");

tracing::info!("Generating self-signed kube_proxy https certificate...");
let cfssl_kube_proxy= Command::new("cfssl")
let cfssl_kube_proxy = Command::new("cfssl")
.arg("gencert")
.arg("-ca=ca.pem")
.arg("-ca-key=ca-key.pem")
Expand Down Expand Up @@ -223,8 +220,10 @@ pub fn start(config: &Config) {
.expect("Error happened when trying to execute kubectl");
Command::new("ssh")
.arg(format!("root@{}", ip))
.arg("kubectl config set-context default --cluster=kubernetes --user=kube-proxy \
--kubeconfig=/opt/kubernetes/cfg/kube-proxy.kubeconfig")
.arg(
"kubectl config set-context default --cluster=kubernetes --user=kube-proxy \
--kubeconfig=/opt/kubernetes/cfg/kube-proxy.kubeconfig",
)
.status()
.expect("Error happened when trying to execute kubectl");
Command::new("ssh")
Expand Down Expand Up @@ -267,9 +266,9 @@ pub fn start(config: &Config) {
}
}

env::set_current_dir(&prev_dir).expect("Error happened when trying to change into `/rk8s`");
env::set_current_dir(prev_dir).expect("Error happened when trying to change into `/rk8s`");
tracing::info!(
"Change working directory back to {}",
env::current_dir().unwrap().display()
);
}
}
Loading

0 comments on commit d0d2def

Please sign in to comment.