Skip to content

Instantly share code, notes, and snippets.

@akaron
akaron / asg_get_ip.tf
Last active June 5, 2020 15:38
terraform aws_autoscaling_group get private and public ip
# In terraform, the attribute of autoscaling group does not contain detail information of EC2 instances,
# i.e., cannot get the ip of instances for further usage (such as ansible). Of course it's better to make
# it no need to know the ip by using tools such as user-data of EC2 instance (for bootstrap) and LB and
# route 53.
#
# In case still want to know the ip addresses, one can use `data "aws_instances"` which
# `depends_on = [aws_autoscaling_group.name]`, then use tags to get the information of instances.
# ref: https://github.com/terraform-providers/terraform-provider-aws/issues/511#issuecomment-447934405
provider "aws" {
@akaron
akaron / Vagrantfile
Created August 6, 2020 10:17
an example Vagrant file which uses ansible to provision
# -*- mode: ruby -*-
# vi: set ft=ruby :
# ENV["LC_ALL"] = "en_US.UTF-8"
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/xenial64"
config.vm.provider "virtualbox" do |v|
v.name = "tst_terraform_packer"
@akaron
akaron / Vagrantfile
Created October 28, 2020 14:11
use vagrant and k3s to deploy a kubernetes cluster in local VM
# run a kubernetes cluster in local VM using vagrant + k3s
# require: Virtualbox, vagrant
# steps:
# * in the folder contain this `Vagrantfile`, run `vagrant up` and wait a bit for provisioning.
# Once done, `vagrant ssh master` into the node and start using the k8s cluster,
# such as `kubectl get nodes`, `kubectl get pods -A`, `kubectl get componentstatus`,
# or `kubectl run busybox --image=busybox:1.28 --rm --restart=Never -it -- nslookup kubernetes`
# * To clean up, logout these nodes, then run `vagrant destroy` to destroy the VMs.
# - next time run `vagrant up` again to provision again
# - or run `vagrant halt` to halt these VMs, and bring them back to the same state
@akaron
akaron / jenkins
Created November 10, 2020 15:36
nginx reverse proxy for jenkins with SSL enabled
# nginx configuration for a reverse proxy for an existing jenkins, with SSL enabled
# modified from https://www.jenkins.io/doc/book/system-administration/reverse-proxy-configuration-nginx/
# Steps:
# 0. confirm jenkins is running; confirm a domain name is properly configured (apicat.xyz in this example)
# - for instance, add the domain in aws route 53 or DO domain
# - then add the NS record (aws or DO or others) back to where you register the domain (namecheap/godaddy/...)
# - this may take some time (at least several minutes)
# 1. Put these config to nginx configuration (such as a new file in /etc/nginx/nginx-sites-enabled or append to /etc/nginx/nginx.conf)
# 2. update config if necessary: jenkins ip and port, root directories, server_name.
# - run `sudo nginx -t` to verify the file
@akaron
akaron / ansible.yaml
Last active January 6, 2021 04:13
Use ansible to install ansible in another ubuntu machine (such as a Vagrant provisioned VM)
---
- hosts: master1
become: yes
vars:
- user_name: vagrant
tasks:
- name: install python virtualenv
block:
- name: install python virtualenv
apt:
@akaron
akaron / k8s_prepare_for_ubuntu.sh
Last active April 22, 2021 03:02
ubuntu1804_prepare_k8s
#!/bin/bash
# For ubuntu-18.04.5-live-server-amd64.iso
set -ex
# set-up repositories
apt-get update
apt-get upgrade -y
apt-get install -y apt-transport-https ca-certificates curl gnupg lsb-release ntp nfs-common
# add nfs-common if use nfs-provisioner
@akaron
akaron / log_watcher.service
Created July 22, 2021 08:10
monitor log for keyword and send email
# put this to /etc/systemd/system
[Unit]
Description=watch log events and sendout mail when something happened
[Service]
ExecStart=/root/log_watcher.sh # change the path, and make the file executable
[Install]
WantedBy=multi-user.target
@akaron
akaron / patch_redis.sh
Created October 20, 2021 08:24
example: k8s patch script
#!/bin/bash
# get all deployments with name `redis-sentinel-manager` or `redis-cluster-manager`
# and patch these deploy with a nodeSelector with name similar to group-<ns>: <ns>
read -r -d '' tmpl <<'EOF'
kubectl -n i_ns patch deploy i_deploy --type='json' -p='[{"op": "add", "path": "/spec/template/spec/nodeSelector/group-i_ns", "value": "i_ns" }]'
EOF
# redis
ns_deploy=$(kubectl get deploy --all-namespaces|grep -E 'redis-.*-manager' | awk '{printf "%s+%s ", $1,$2}')
@akaron
akaron / gist:8adb6cf7a008efd6d4772bf508965f2e
Created November 9, 2021 02:42
elasticsearch 6.2.2 health status examples
# use `kibana -> dev-tools -> console` or curl
# get cluster health
GET _cluster/health
# indices: the health should be green
GET _cat/indices?v
# shard: the state should be "STARTED"
GET _cat/shards?v
#!/usr/bin/env bash
# modified from: https://itnext.io/how-to-cold-start-fast-a-java-service-on-k8s-eks-3a7b4450845d
ns=$1
pod=$2
scheduled=`kubectl -n $ns get pod $pod -o json | jq -r '.status.conditions[] | select(.type=="PodScheduled") | .lastTransitionTime' | sed 's/T/ /g' | tr -d 'Z'`
ready=`kubectl -n $ns get pod $pod -o json | jq -r '.status.conditions[] | select(.type=="Ready") | .lastTransitionTime' | sed 's/T/ /g' | tr -d 'Z'`
scheduled_epoch=`date -u -d "$scheduled" +%s`
echo "${pod} sechedualed at ${scheduled_epoch}"
ready_epoch=`date -u -d "$ready" +%s`
load_seconds=$((ready_epoch-scheduled_epoch))