搭配grep,awk強制刪除k8s中,所有非Running狀態的pod
kubectl get pods --all-namespaces -o wide | grep -v Running | grep -v NAME | awk '{print $1 " " $2}' | while read namespace podname; do kubectl delete -n $namespace pod $namespace --grace-period=0 --force; done
搭配grep,awk強制刪除k8s中,所有非Running狀態的pod
kubectl get pods --all-namespaces -o wide | grep -v Running | grep -v NAME | awk '{print $1 " " $2}' | while read namespace podname; do kubectl delete -n $namespace pod $namespace --grace-period=0 --force; done
在終端機內下達下面指令:
defaults write com.apple.screencapture location /Volumes/GoogleDrive/我的雲端硬碟/photos/截圖/
在終端機下,下達下列指令. 可清除docker所有容器,影像,網路.
docker system prune -a
參考下列指令:
# 環境準備
docker rm -f mongodb_mongo_1
rm -r -f $PWD/db
mkdir -p $PWD/db
# 建立mongoDB
docker run \
-d \
--name mongodb_mongo_1 \
-p 27017:27017 \
-v $PWD/db:/data/db \
mongo
sleep 5
# 建立超級管理者
docker exec -it mongodb_mongo_1 bash
mongo admin
db.createUser({ user: "admin" , pwd: "admin1234", roles: ["userAdminAnyDatabase", "dbAdminAnyDatabase", "readWriteAnyDatabase"]})
quit()
# 重新登入
mongo admin
db.auth("admin","admin1234")
use octblog
db.createUser({
user: "gevin",
pwd: "gevin1234",
roles: [ { role: "readWrite", db: "octblog" },
{ role: "readWrite", db: "octblog-log" } ]
})
quit()
mongo octblog
db.auth("gevin","gevin1234")
use octblog
db.collection.insert({'test':1234});
quit()
exit
# 重啟mongoDB
docker rm -f mongodb_mongo_1
docker run \
-d \
--name mongodb_mongo_1 \
-p 27017:27017 \
-v $PWD/db:/data/db \
mongo mongod --auth
因為新版的k8s的token已經有時間效期,
所以在安裝完後,隔一陣子想要加入新的節點就會出現舊的join token無法使用.
這時候可以用kubeadm token generate重新產生新的token,並用下列語法印出join指令.
kubeadm token create `kubeadm token generate` --print-join-command --ttl=0
看到一個不錯的範例
kubectl get pods --all-namespaces -o wide | awk '{print $1 " " $2}' | while read AA BB; do echo "$AA" "$BB"; done
環境變數:
PASSWORD="1234567890"
NAME="db"
SERVICE="${NAME}-mariadb"
NAMESPACE="default"
儲存裝置為nfs
helm install \
--namespace ${NAMESPACE} \
--name ${NAME} stable/mariadb \
--set slave.replicas=3 \
--set rootUser.password=replicator \
--set replication.user="${PASSWORD}" \
--set replication.password="${PASSWORD}" \
--set master.persistence.storageClass=managed-nfs-storage \
--set slave.persistence.storageClass=managed-nfs-storage
允許root@%登入(注意:密碼不能隨意變動)
kubectl exec -it -n ${NAMESPACE} ${SERVICE}-master-0 -- mysql -uroot -p${PASSWORD} -e "grant all privileges on *.* to 'root' @'%' identified by '${PASSWORD}'; FLUSH PRIVILEGES;"
變更密碼正確方式:
NEW_PASSWORD="0987654321"
PASSWORD=$(kubectl get secret -n ${NAMESPACE} ${SERVICE} -o jsonpath="{.data.mariadb-root-password}" | base64 --decode)
echo "Password: ${PASSWORD}"
helm upgrade ${NAME} stable/mariadb --set rootUser.password=${NEW_PASSWORD}
echo "[info] 建立測試環境";
docker network create mongo-cluster >/dev/null 2>&1
docker rm -f mongo1 mongo2 mongo3 >/dev/null 2>&1
for i in 1 2 3
do
echo " mongo${i} 建立"
docker run \
-d \
-p 3000$i:27017 \
--name mongo$i \
--net mongo-cluster \
mongo mongod --replSet my-mongo-set >/dev/null 2>&1
done
echo " 設定mongo cluster"
docker exec -it mongo3 mongo --eval "config={\"_id\":\"my-mongo-set\",\"members\":[{\"_id\":0,\"host\":\"mongo1:27017\"},{\"_id\":1,\"host\":\"mongo2:27017\"},{\"_id\":2,\"host\":\"mongo3:27017\"}],\"settings\": { \"electionTimeoutMillis\": 500 }}; rs.initiate(config)"
#https://github.com/mongodb/mongo-php-library
require_once __DIR__ . "/vendor/autoload.php";
try{
$client = new MongoDB\Client("mongodb://mongo1:27017,mongo2:27017,mongo3:27017",[],[
'typeMap' => [
'array' => 'array',
'document' => 'array',
'root' => 'array',
],
]);
$db = $client->selectDatabase('test');
$cursor = $db->command(['isMaster' => 1]);
echo "[info] 成功連線至: {$cursor->toArray()[0]['primary']}\n";
} catch(Exception $e) {
echo "[error] 無法線連\n";
exit;
}
{ //若Collection:testA不存在則建立.
if((iterator_count($db->listCollections([
'filter'=>[
'name'=>'testA'
]
])))==0) //當數量為0時,建立testA
$db->createCollection('testA');
}
//連線至Collection:testA
$collection=$db->selectCollection('testA');
//若沒有任何資料,則建立100則隨機資料
if($collection->count()==0){
for($i=0;$i<100;$i++){
$insertOneResult = $collection->insertOne([
'idx' => $i,
'value' => "{$i}, rand: ".rand(0,9999),
]);
}
}
//取得最後三筆資料
foreach($collection->find([],[
'limit' => 3,
'projection' => ['_id'=>0],
'sort' => [
'idx'=>-1
]
]) as $rs){
echo "\tidx:{$rs['idx']}\tvalue:{$rs['value']}\n";
}
測試dd image的建立與擴展
#建立
dd if=/dev/zero of=test001.img bs=10M count=10
#格式化
mkfs -F test001.img
#建立掛載資料夾
mkdir -p /tmp/disk/test001
#掛載
mount -t ext2 -o loop $PWD/test001.img /tmp/disk/test001
#隨便產生一個90Mb的檔案
dd if=/dev/zero of=/tmp/disk/test001/xxxx.img bs=10M count=9
#查看可用空間
df -h | grep /dev/loop
#解除掛載
umount /tmp/disk/test001
#磁碟檢查
e2fsck -f test001.img -y
#調整空間大小至200M
resize2fs test001.img 200M
#重新掛載
mount -t ext2 -o loop $PWD/test001.img /tmp/disk/test001
#調整空間
losetup -c /dev/loop0
resize2fs /dev/loop0
#查看可用空間
df -h | grep /dev/loop
#解除掛載
umount /tmp/disk/test001
#刪除測試檔案
rm -f -r test001.img
剛剛因為要測試mysql跟mariaDB的語法是否有不同
因此用下列語法快速建立mysql來進行測試
#建立mysql server
docker run \
--name some-mysql \
-e MYSQL_ROOT_PASSWORD=abcdefg \
-d mysql:5.7
sleep 10
#建立mysql client
docker run \
-it \
--rm \
--link some-mysql:mysqlA \
mysql sh -c 'exec mysql -h mysqlA -uroot -pabcdefg'
SQL測試語法
create database TEST1;
use TEST1;
CREATE TABLE `test` (
`id` int(11) NOT NULL,
`geo` point DEFAULT NULL
);
INSERT INTO `test`(`id`, `geo`) VALUES (1,ST_GEOMFROMTEXT('POINT(121 23)',4326));
select * from `test` where 1;