Skip to content

Instantly share code, notes, and snippets.

@nemtsov
Last active April 20, 2018 04:32
Show Gist options
  • Save nemtsov/9fab749a5625abd53f567e86fab2ece5 to your computer and use it in GitHub Desktop.
Save nemtsov/9fab749a5625abd53f567e86fab2ece5 to your computer and use it in GitHub Desktop.
Kubernetes Administration Helpers
#!/usr/bin/env bash
USERNAME="yuriy"
GROUP="wr"
CA_DIR="/etc/kubernetes/pki"
USER_CERTS_DIR="/home/$USERNAME/.certs"
mkdir -p $USER_CERTS_DIR
pushd $USER_CERTS_DIR
openssl genrsa -out "$USERNAME.key" 2048
openssl req -new -key "$USERNAME.key" -out "$USERNAME.csr" -subj "/CN=$USERNAME/O=$GROUP"
openssl x509 -req -in "$USERNAME.csr" -CA "$CA_DIR/ca.crt" -CAkey "$CA_DIR/ca.key" -CAcreateserial -out "$USERNAME.crt" -days 365
popd
kind: Namespace
apiVersion: v1
metadata:
name: development
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
namespace: development
name: development-admin
rules:
- apiGroups: ["*"]
resources: ["*"]
verbs: ["*"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
namespace: development
name: deployment-admin-binding
subjects:
- kind: User
name: yuriy
apiGroup: ""
roleRef:
kind: Role
name: development-admin
apiGroup: ""
#!/usr/bin/env bash
USERNAME=yuriy
GROUP=wr
REMOTE_CA_CERT_PATH=/etc/kubernetes/pki/ca.crt
REMOTE_CERTS_DIR=/home/$USERNAME/.certs
LOCAL_CERTS_DIR=~/.ssh/$GROUP-kube-$USERNAME
CLUSTER_IP=159.65.252.151
mkdir -p $LOCAL_CERTS_DIR
scp -r -i ~/.ssh/id_rsa "root@$CLUSTER_IP:$REMOTE_CERTS_DIR/*" $LOCAL_CERTS_DIR
scp -i ~/.ssh/id_rsa "root@$CLUSTER_IP:$REMOTE_CA_CERT_PATH" $LOCAL_CERTS_DIR
kubectl config set-cluster $GROUP-kube --server=https://$CLUSTER_IP:6443 --certificate-authority=$LOCAL_CERTS_DIR/ca.crt
kubectl config set-credentials $USERNAME --client-certificate=$LOCAL_CERTS_DIR/$USERNAME.crt --client-key=$LOCAL_CERTS_DIR/$USERNAME.key
kubectl config set-context $USERNAME-kube-context --cluster=$GROUP-kube --namespace=development --user=$USERNAME
kubectl config use-context $USERNAME-kube-context
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment