Skip to content

Commit

Permalink
feat(autok3s): allow running with none-root user
Browse files Browse the repository at this point in the history
Signed-off-by: Jason-ZW <zhenyang@rancher.com>
  • Loading branch information
rancher-sy-bot committed Sep 28, 2020
1 parent 640d6fa commit 1e7506e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 39 deletions.
48 changes: 24 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ It can help users quickly complete the personalized configuration of the K3s clu

## Design Ideas
This tool uses the cloud provider's SDK to create and manage hosts, and then uses SSH to install the K3s cluster to the remote host.
You can also use it to join hosts as masters/agents to the K3s cluster. It will automatically merge and store the `kubeconfig` in `/var/lib/rancher/autok3s/.kube/config` which necessary for user to access the cluster.
You can also use it to join hosts as masters/agents to the K3s cluster. It will automatically merge and store the `kubeconfig` in `$HOME/.autok3s/.kube/config` which necessary for user to access the cluster.
Then user can use `autok3s kubectl` command quickly access the cluster.

Use [viper](https://github.com/spf13/viper) to bind flags and configuration file. `autok3s` will generate a configuration file to store cloud-providers' access information at the specified location(`/var/lib/rancher/autok3s/config.yaml`) to reduce the number of flags to be passed for multiple runs.
Use [viper](https://github.com/spf13/viper) to bind flags and configuration file. `autok3s` will generate a configuration file to store cloud-providers' access information at the specified location(`$HOME/.autok3s/config.yaml`) to reduce the number of flags to be passed for multiple runs.

It's also generated a state file `/var/lib/rancher/autok3s/.state` to record the clusters' information created on this host.
It's also generated a state file `$HOME/.autok3s/.state` to record the clusters' information created on this host.

## Providers
- alibaba
Expand All @@ -22,9 +22,9 @@ It's also generated a state file `/var/lib/rancher/autok3s/.state` to record the
User can get the commands available for different providers according to the `--provider <provider> --help`.

### Setup K3s Cluster
If already have access information in `/var/lib/rancher/autok3s/config.yaml` you can use the simplified command.
If already have access information in `$HOME/.autok3s/config.yaml` you can use the simplified command.
```bash
sudo autok3s create \
autok3s create \
--provider alibaba \
--region <region> \
--name <cluster name> \
Expand All @@ -37,7 +37,7 @@ sudo autok3s create \

Generic commands can be used anywhere.
```bash
sudo autok3s create \
autok3s create \
--provider alibaba \
--region <region> \
--name <cluster name> \
Expand All @@ -52,21 +52,21 @@ sudo autok3s create \

HA(embedded etcd: >= 1.19.1-k3s1) mode need `--master` at least 3 master nodes, e.g.
```bash
sudo autok3s ... \
autok3s ... \
--master 3
```

HA(external database) mode need `--master` greater than 1 node, also need to specify `--datastore`, e.g.
```bash
sudo autok3s ... \
autok3s ... \
--master 2 \
--datastore "mysql://<user>:<password>@tcp(<ip>:<port>)/<db>"
```

### Join K3s Nodes
If you have ever created a cluster using `autok3s` on your current machine, you can use the simplified command.
```bash
sudo autok3s join \
autok3s join \
--provider alibaba \
--region <region> \
--name <cluster name> \
Expand All @@ -76,7 +76,7 @@ sudo autok3s join \

Generic commands can be used anywhere.
```bash
sudo autok3s join \
autok3s join \
--provider alibaba \
--region <region> \
--name <cluster name> \
Expand All @@ -92,29 +92,29 @@ sudo autok3s join \

Join master nodes to (embedded etcd: >= 1.19.1-k3s1) HA cluster e.g.
```bash
sudo autok3s ... \
autok3s ... \
--master 2
```

Join master nodes to (external database) HA cluster, also need to specify `--datastore`, e.g.
```bash
sudo autok3s ... \
autok3s ... \
--master 2 \
--datastore "mysql://<user>:<password>@tcp(<ip>:<port>)/<db>"
```

### Start K3s Cluster
If you have ever created a cluster using `autok3s` on your current machine, you can use the simplified command.
```bash
sudo autok3s start \
autok3s start \
--provider alibaba \
--region <region> \
--name <cluster name>
```

Generic commands can be used anywhere.
```bash
sudo autok3s start \
autok3s start \
--provider alibaba \
--region <region> \
--name <cluster name> \
Expand All @@ -125,15 +125,15 @@ sudo autok3s start \
### Stop K3s Cluster
If you have ever created a cluster using `autok3s` on your current machine, you can use the simplified command.
```bash
sudo autok3s stop \
autok3s stop \
--provider alibaba \
--region <region> \
--name <cluster name>
```

Generic commands can be used anywhere.
```bash
sudo autok3s stop \
autok3s stop \
--provider alibaba \
--region <region> \
--name <cluster name> \
Expand All @@ -144,15 +144,15 @@ sudo autok3s stop \
### Delete K3s Cluster
If you have ever created a cluster using `autok3s` on your current machine, you can use the simplified command.
```bash
sudo autok3s delete \
autok3s delete \
--provider alibaba \
--region <region> \
--name <cluster name>
```

Generic commands can be used anywhere.
```bash
sudo autok3s delete \
autok3s delete \
--provider alibaba \
--region <region> \
--name <cluster name> \
Expand All @@ -163,33 +163,33 @@ sudo autok3s delete \
### List K3s Clusters
This command will list the clusters that you have created on this machine.
```bash
sudo autok3s list
autok3s list
```

### Access K3s Cluster
After the cluster created, `autok3s` will automatically merge the `kubeconfig` which necessary for us to access the cluster.
```bash
sudo autok3s kubectl <sub-commands> <flags>
autok3s kubectl <sub-commands> <flags>
```

In the scenario of multiple clusters, the access to different clusters can be completed by switching context.
```bash
sudo autok3s kubectl config get-contexts
sudo autok3s kubectl config use-context <context>
autok3s kubectl config get-contexts
autok3s kubectl config use-context <context>
```

### SSH K3s Cluster's Node
If you have ever created a cluster using `autok3s` on your current machine, you can use the simplified command.
```bash
sudo autok3s ssh \
autok3s ssh \
--provider alibaba \
--region <region> \
--name <cluster name>
```

Generic commands can be used anywhere.
```bash
sudo autok3s ssh \
autok3s ssh \
--provider alibaba \
--region <region> \
--name <cluster name> \
Expand Down
12 changes: 0 additions & 12 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,6 @@ func init() {
}

func Command() *cobra.Command {
cmd.PreRun = func(cmd *cobra.Command, args []string) {
if os.Getuid() != 0 {
logrus.Errorf("%s: need to be root", os.Args[0])
os.Exit(1)
}
}

cmd.Run = func(cmd *cobra.Command, args []string) {
printASCII()
if err := cmd.Help(); err != nil {
Expand All @@ -59,11 +52,6 @@ func Command() *cobra.Command {
}

func initCfg() {
if os.Getuid() != 0 {
logrus.Errorf("%s: need to be root", os.Args[0])
os.Exit(1)
}

viper.SetConfigType("yaml")
viper.SetConfigFile(fmt.Sprintf("%s/%s", common.CfgPath, common.ConfigFile))
viper.AutomaticEnv()
Expand Down
4 changes: 3 additions & 1 deletion pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package common
import (
"time"

"github.com/cnrancher/autok3s/pkg/utils"

"k8s.io/apimachinery/pkg/util/wait"
)

Expand All @@ -20,7 +22,7 @@ const (

var (
Debug = false
CfgPath = "/var/lib/rancher/autok3s"
CfgPath = utils.UserHome() + "/.autok3s"
Backoff = wait.Backoff{
Duration: 30 * time.Second,
Factor: 1,
Expand Down
4 changes: 2 additions & 2 deletions pkg/providers/alibaba/alibaba.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ const (
eipStatusAvailable = "Available"
eipStatusInUse = "InUse"
usageInfo = `=========================== Prompt Info ===========================
Use 'sudo autok3s kubectl config use-context %s'
Use 'sudo autok3s kubectl get pods -A' get POD status`
Use 'autok3s kubectl config use-context %s'
Use 'autok3s kubectl get pods -A' get POD status`
)

// ProviderName is the name of this provider.
Expand Down

0 comments on commit 1e7506e

Please # to comment.