交通部選前強推軌道建設,發生程序爭議,學者連署要求建立公開審議機制

摘錄自2019年11月25日聯合晚報報導

交通部選前強推軌道建設,發生程序爭議,學者連署要求建立公開審議機制,重新檢討台鐵立體化與捷運化政策,已有700多人連署。交通部次長黃玉霖表示,他也曾是學者,希望大家了解審查會程序後再討論,不要有誤解的資訊。

參與連署的淡江大學運輸管理系教授陶治中表示,他覺得整個台鐵立體化等交通審查程序必須檢討,既然請來這麼多專業的學者專家,絕大多數對於這項政策都是反對的,就不該貿然推行。

陶治中說,林佳龍發言對學者專業不尊重,一般軌道建設如果沒有長遠規畫,很容易變成蚊子軌道,交通鐵道建設需要站在高度專業的角度。關於自償率取消,他指出,當初花的錢如果沒有回收,就變成國家負擔,必須嚴格遵守國家的財政紀律。

張勝雄說,連署發起不到一天,超過700人連署,顯示並非只有他一個人持這樣的看法,而是許多人共同的意見。

本站聲明:網站內容來源於https://e-info.org.tw/,如有侵權,請聯繫我們,我們將及時處理

【其他文章推薦】

※找工作! 想知道堆高機駕駛日薪是多少嗎? 哪裡有職缺?幫你快速媒合

※隨時健康喝好水,高品質飲水機推薦,優質安全有把關

新北市轉軸代工廠商詢價平台?

防爆隔熱紙規格資訊說明

※好的茗茶,更需要密封性高的茶葉罐,才能留住香氣!

※【找人才】台北塑膠射出廠徵選技師,薪資優,福利佳

為了改善西螺果菜市場的空污與噪音污染,雲林縣議會訂出西螺果菜市場進出車輛管理自治條例

摘錄自2019年11月27日公視報導

為了改善西螺果菜市場的空污與噪音污染,雲林縣議會訂出西螺果菜市場進出車輛管理自治條例,今年開始禁止拼裝車與二行程機車。明年柴油車須取檢驗合格報告才能進入市場。

環保局稽查人員,一早就來到西螺果菜市場門口,隨機攔查將進場柴油車輛,利用手機即時查詢柴油車,一年內是否有取得檢驗合格標章,如果沒相關資料就馬上開勸導單要求限期改善。

雲林縣環保局空噪科科長廖崇圜表示,「拼裝車違規進場的話,我們會處3000元罰鍰,針對二行程機車我們處500元部分,而針對柴油車,不是兩年內新車,兩年外車子沒有一年以內檢測合格標章,我們是處1000元罰鍰。」

雲林縣環保局表示,西螺果菜市場是全台三大市場,經統計每日進出的菜車約4000輛次,其中柴油車佔3000輛次,今年已禁止拼裝車入場,讓市場空氣品質改善超過五成,現在透過管制柴油車,要讓西螺果菜市場空氣品質徹底改善。

本站聲明:網站內容來源於https://e-info.org.tw/,如有侵權,請聯繫我們,我們將及時處理

【其他文章推薦】

※有廠商專門客製化橡膠製品嗎?

噴霧洗滌塔實際應用案例分享

※票選推薦煮婦最愛手壓封口機,省荷包不犧牲品質

※選購空壓機需注意八大事項 !

※DIY自行施工建築隔熱紙,簡易教學大公開 !

※選擇好的茶葉罐,有效地保持茶葉的鮮度與風味!

FreeSql v0.11 幾個實用功能說明

FreeSql 開源發布快一年了,立志成為 .Net 平台方便好用的 ORM,倉庫地址:

隨着不斷的迭代更新,越來越穩定,也越來越強大。預計在一周年的時候(2020年1月1日)發布 1.0 正式版本。

金九銀十的日子過去了,在這個銅一般的月份里,鄙人做了幾個重大功能,希望對使用者開發提供更大的便利。

  • 一、Dto 映射查詢
  • 二、IncludeMany 聯級加載
  • 三、Where(a => true) 邏輯表達式解析優化
  • 四、SaveManyToMany 聯級保存多對多集合屬性
  • 五、遷移實體 – 到指定表名
  • 六、MySql 特有功能 On Duplicate Key Update,和 Pgsql upsert
  • 七、ISelect.ToDelete 高級刪除
  • 八、全局過濾器

以下的代碼,先決定義代碼如下 :

IFreeSql fsql = new FreeSql.FreeSqlBuilder()
    .UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=|DataDirectory|\db1.db;Max Pool Size=10";)
    .UseAutoSyncStructure(true) //自動同步實體結構到數據庫
    .Build();

public class Blog
{
    public Guid Id { get; set; }
    public string Url { get; set; }
    public int Rating { get; set; }
}

一、Dto 映射查詢

class Dto
{
    public Guid Id { get; set; }
    public string Url { get; set; }
    public int xxx { get; set; }
}

fsql.Select<Blog>().ToList<Dto>();
//SELECT Id, Url FROM Blog

fsql.Select<Blog>().ToList(a => new Dto { xxx = a.Rating} );
//SELECT Id, Url, Rating as xxx FROM Blog
//這樣寫,附加所有映射,再額外映射 xxx

fsql.Select<Blog>().ToList(a => new Blog { Id = a.Id }) 
//這樣寫,只查詢 id

fsql.Select<Blog>().ToList(a => new { a.Id }) 
//這樣寫,只查詢 id,返回匿名對象

映射支持單表/多表,是在查詢數據之前映射(不是先查詢所有字段再到內存映射)

查找規則,查找屬性名,會循環內部對象 _tables(join 查詢後會增長),以 主表優先查,直到查到相同的字段。

如:

A, B, C 都有 id,Dto { id, a1, a2, b1, b2 },A.id 被映射。也可以指定 id = C.id 映射。

友情提醒:在 dto 可以直接映射一個導航屬性

二、IncludeMany 聯級加載

之前已經實現,有設置關係,和未設置關係 的導航集合屬性聯級加載。

有設置關係的(支持一對多、多對多):

fsql.Select<Tag>().IncludeMany(a => a.Goods).ToList();

未設置關係的,臨時指定關係(只支持一對多):

fsql.Select<Goods>().IncludeMany(a => a.Comment.Where(b => b.TagId == a.Id));

只查詢每項子集合的前幾條數據,避免像EfCore加載所有數據導致IO性能低下(比如某商品下有2000條評論):

fsql.Select<Goods>().IncludeMany(a => a.Comment.Take(10));

上面已有的 IncludeMany 功能還不夠自由靈活。

新功能1:在 Dto 上做映射 IncludeMany

老的 IncludeMany 限制只能在 ISelect 內使用,必須要先查上級數據,解決這個問題我們做了直接在 Dto 上做映射:

查詢 Goods 商品表,分類1、分類2、分類3 各10條數據

//定義臨時類,也可以是 Dto 類
class Dto {
    public int TypeId { get; set; }
    public List<Goods > GoodsList { get; set; }
}

var dto = new [] { 1,2,3 }.Select(a => new Dto { TypeId = a }).ToList();
dto.IncludeMany(d => d.GoodsList.Take(10).Where(gd => gd.TypeId == d.TypeId));

//執行后,dto 每個元素.Vods 將只有 10條記錄

現在 IncludeMany 不再是 ISelect 的專利,普通的 List<T> 也可以用它來貪婪加載數據,並準確填充到內部各元素中。

新功能2:查詢子集合表的指定字段

老的 IncludeMany 限制只能查子表的所有字段,子表過段多過的話比較浪費 IO 性能。

新功能可以設置子集合返回部分字段,避免子集合字段過多的問題。

fsql.Select<Tag>().IncludeMany(a => a.Goods.Select(b => new Goods { Id = b.Id, Title = b.Title }));
//只查詢 goods 表 id, title 字段,再作填充

三、Where(a => true) 邏輯表達式解析優化

相信很多 ORM 解析表達式的時候處理不了這個問題,我們之前已經解決了 99%。

這個月發現還有一餘孽未清,發現問題后及時解決了,並增加單元測試代碼以絕後患。

四、SaveManyToMany 聯級保存多對多集合屬性

在此之前,FreeSql.DbContext 和 倉儲實現,已經實現了聯級保存功能,如下:

聯級保存功能可實現保存對象的時候,將其【OneToMany】、【ManyToMany】導航屬性集合也一併保存。

全局關閉:

fsql.SetDbContextOptions(opt => opt.EnableAddOrUpdateNavigateList = false);

局部關閉:

var repo = fsql.GetRepository<T>();
repo.DbContextOptions.EnableAddOrUpdateNavigateList = false;

新功能:

保存實體的指定【多對多】導航屬性,SaveManyToMany 方法實現在 BaseRepository、DbContext。

解決問題:當實體類導航數據過於複雜的時候,選擇關閉聯級保存的功能是明智之選,但是此時【多對多】數據保存功能寫起來非常繁瑣麻煩(因為要與現有數據對比后保存)。

var song = new Song { Id = 1 };
song.Tags = new List<Tag>();
song.Tags.Add(new Tag ...);
song.Tags.Add(new Tag ...);
song.Tags.Add(new Tag ...);
repo.SaveManyToMany(song, "Tags");
//輕鬆保存 song 與 tag 表的關聯

機制規則與聯級保存的【多對多】一樣,如下:

我們對中間表的保存是完整對比操作,對外部實體的操作只作新增(注意不會更新)

  • 屬性集合為空時,刪除他們的所有關聯數據(中間表)
  • 屬性集合不為空時,與數據庫存在的關聯數據(中間表)完全對比,計算出應該刪除和添加的記錄

五、遷移實體 – 到指定表名

fsql.CodeFirst.SyncStructure(typeof(Log), "Log_1"); //遷移到 Log_1 表
fsql.CodeFirst.SyncStructure(typeof(Log), "Log_2"); //遷移到 Log_2 表

在此功能上,我們對分表功能做了點升級,以下動作都會做遷移動作:

fsql.Select<Log>().AsTable((_, oldname) => $"{oldname}_1");
fsql.GetRepository<Log>(null, oldname => $"{oldname}_1");

六、MySql 特有功能 On Duplicate Key Update,和 Pgsql upsert

FreeSql 提供了多種插入或更新方法,v0.11 之前主要使用 FreeSql.Repository/FreeSql.DbContext 庫提供的方法實現。

FreeSql.Repository 之 InsertOrUpdate

此方法與 FreeSql.DbContext AddOrUpdate 方法功能一樣。

var repo = fsql.GetRepository<T>();
repo.InsertOrUpdate(實體);

如果內部的狀態管理存在數據,則更新。

如果內部的狀態管理不存在數據,同查詢數據庫,是否存在。

存在則更新,不存在則插入

缺點:不支持批量操作

新功能:MySql 特有功能 On Duplicate Key Update

FreeSql.Provider.MySql 和 FreeSql.Provider.MySqlConnector 在 v0.11.11 版本已支持 MySql 特有的功能,On Duplicate Key Update。

這個功能也可以實現插入或更新數據,並且支持批量操作。

class TestOnDuplicateKeyUpdateInfo
{
    [Column(IsIdentity = true)]
    public int id { get; set; }
    public string title { get; set; }
    public DateTime time { get; set; }
}

var item = new TestOnDuplicateKeyUpdateInfo { id = 100, title = "title-100", time = DateTime.Parse("2000-01-01") };
fsql.Insert(item)
    .NoneParameter()
    .OnDuplicateKeyUpdate().ToSql();
//INSERT INTO `TestOnDuplicateKeyUpdateInfo`(`id`, `title`, `time`) VALUES(100, 'title-100', '2000-01-01 00:00:00.000')
//ON DUPLICATE KEY UPDATE
//`title` = VALUES(`title`), 
//`time` = VALUES(`time`)

OnDuplicateKeyUpdate() 之後可以調用的方法:

方法名 描述
IgnoreColumns 忽略更新的列,機制和 IUpdate.IgnoreColumns 一樣
UpdateColumns 指定更新的列,機制和 IUpdate.UpdateColumns 一樣
Set 手工指定更新的列,與 IUpdate.Set 功能一樣
SetRaw 作為 Set 方法的補充,可傳入 SQL 字符串
ToSql 返回即將執行的 SQL 語句
ExecuteAffrows 執行,返回影響的行數

IInsert 與 OnDuplicateKeyUpdate 都有 IgnoreColumns、UpdateColumns 方法。

當插入實體/集合實體的時候,忽略了 time 列,代碼如下:

fsql.Insert(item)
    .IgnoreColumns(a => a.time)
    .NoneParameter()
    .OnDuplicateKeyUpdate().ToSql();
//INSERT INTO `TestOnDuplicateKeyUpdateInfo`(`id`, `title`) VALUES(200, 'title-200')
//ON DUPLICATE KEY UPDATE
//`title` = VALUES(`title`), 
//`time` = '2000-01-01 00:00:00.000'

我們發現,UPDATE time 部分變成了常量,而不是 VALUES(`time`),機制如下:

當 insert 部分中存在的列,在 update 中將以 VALUES(`字段`) 的形式設置;

當 insert 部分中不存在的列,在 update 中將為常量形式設置,當操作實體數組的時候,此常量為 case when … end 執行(與 IUpdate 一樣);

新功能2:PostgreSQL 特有功能 On Conflict Do Update

使用方法 MySql OnDuplicateKeyUpdate 大致相同。

七、ISelect.ToDelete 高級刪除

默認 IDelete 不支持導航對象,多表關聯等。ISelect.ToDelete 可將查詢轉為刪除對象,以便支持導航對象或其他查詢功能刪除數據,如下:

fsql.Select<T1>().Where(a => a.Options.xxx == 1).ToDelete().ExecuteAffrows();

注意:此方法不是將數據查詢到內存循環刪除,上面的代碼產生如下 SQL 執行:

DELETE FROM `T1` WHERE id in (select a.id from T1 a left join Options b on b.t1id = a.id where b.xxx = 1)

複雜刪除使用該方案的好處:

  • 刪除前可預覽測試數據,防止錯誤刪除操作;
  • 支持更加複雜的刪除操作(IDelete 默認只支持簡單的操作),甚至在 ISelect 上使用 Limit(10) 將只刪除附合條件的前 10條記錄;

還有 ISelect.ToUpdate 高級更新數據功能,使用方法類似

八、全局過濾器

FreeSql 基礎層實現了 Select/Update/Delete 可設置的全局過濾器功能。

public static AsyncLocal<Guid> TenantId { get; set; } = new AsyncLocal<Guid>();

fsql.GlobalFilter
    .Apply<TestAddEnum>("test1", a => a.Id == TenantId.Value)
    .Apply<AuthorTest>("test2", a => a.Id == 111)
    .Apply<AuthorTest>("test3", a => a.Name == "11");

Apply 泛型參數可以設置為任何類型,當使用 Select/Update/Delete 方法時會進行過濾器匹配嘗試(try catch):

  • 匹配成功的,將附加 where 條件;
  • 匹配失敗的,標記下次不再匹配,避免性能損耗;

如何禁用?

fsql.Select<TestAddEnum>().ToList(); //所有生效
fsql.Select<TestAddEnum>().DisableGlobalFilter("test1").ToList(); //禁用 test1
fsql.Select<TestAddEnum>().DisableGlobalFilter().ToList(); //禁用所有

fsql.Update/Delete 方法效果同上。

注意:IFreeSql.GlobalFilter 與 倉儲過濾器 不是一個功能,可以同時生效

鳴謝

感謝反饋 bug 的朋友!

倉庫地址:

請移步更新日誌:https://github.com/2881099/FreeSql/wiki/%e6%9b%b4%e6%96%b0%e6%97%a5%e5%bf%97

本站聲明:網站內容來源於博客園,如有侵權,請聯繫我們,我們將及時處理

【其他文章推薦】

※帶您來了解什麼是 USB CONNECTOR  ?

※自行創業 缺乏曝光? 下一步”網站設計“幫您第一時間規劃公司的門面形象

※如何讓商品強力曝光呢? 網頁設計公司幫您建置最吸引人的網站,提高曝光率!!

※綠能、環保無空污,成為電動車最新代名詞,目前市場使用率逐漸普及化

※廣告預算用在刀口上,網站設計公司幫您達到更多曝光效益

※試算大陸海運運費!

cocos creator 3D | 螞蟻莊園運動會星星球

上一篇文章寫了一個簡易版的螞蟻莊園登山賽,有小夥伴留言說想要看星星球的,那麼就寫起來吧!

效果預覽

配置環境 cocos creator 3d 1.0.0

小球點擊

3d里節點無法用 cc.Node.EventType.TOUCH_START 監聽。最終在論壇上找到一個 raycast 解決方法。參考代碼如下。

start() {
    systemEvent.on(SystemEventType.TOUCH_START, this.onClickBall, this);
}
private _ray = new geometry.ray();
private onClickBall(touch: Touch, event: EventTouch) {
    const pos = touch.getLocation();
    this.camera.screenPointToRay(pos.x, pos.y, this._ray);
    const result: { node: Node }[] = this.node_ball_click.scene.renderScene['raycast'](this._ray);
    if (result.some((i) => {
        if (i.node === this.node_ball_click) {
            return true;
        }
    })) {
        //點擊到小球處理邏輯
    }
}

其中 result 返回的是一個包含node節點的結果數組。獲取后需要判斷一下是否為小球節點。

據說這個方案消耗性能比較大,後續應該會有更好的解決方案。

動畫系統

採用了編輯器的動畫編輯器,對需要部分增加動畫效果。由於我的資源是網上找的,那隻雞有些身體部分切割的不好,所以小雞的動畫比較差一些。

需要注意的是動畫編輯器里的rotation屬性,與節點里的屬性面板的rotation對應不上,而應該採用eulerAngles的屬性。

據說後續版本會處理?

小球軌跡

採用tween控制小球坐標數值,先移動到最高點,然後再移動到最低點。

在運動軌跡中加入一些隨機值,就可以達到不同位置的效果啦。

tweenUtil(this._node_balll_pos)
    .stop()
    .to(time, new math.Vec3((this.node_ball.position.x + BALL_INIT_X) / 2, BALL_MAX_Y * (0.8 + 0.2 * Math.random()), targetZ / 2))
    .to(time, new math.Vec3(BALL_GAMEOVER_X, BALL_MIN_Y, targetZ))
    .start();

小結

完成這個小功能主要遇到的問題是3d節點點擊事件,和動畫系統的rotation的問題。不過這些都在論壇里找到了相應的解決方法。

以上就是我最新的學習成果!如有問題或新的想法歡迎留言!我有了好想法會第一時間分享給大家的!

本站聲明:網站內容來源於博客園,如有侵權,請聯繫我們,我們將及時處理
【其他文章推薦】

※為什麼 USB CONNECTOR 是電子產業重要的元件?

網頁設計一頭霧水??該從何著手呢? 找到專業技術的網頁設計公司,幫您輕鬆架站!

※想要讓你的商品成為最夯、最多人討論的話題?網頁設計公司讓你強力曝光

※想知道最厲害的台北網頁設計公司推薦台中網頁設計公司推薦專業設計師”嚨底家”!!

※專營大陸快遞台灣服務

台灣快遞大陸的貨運公司有哪些呢?

Docker學習(五)-Kubernetes 集群搭建 – Spring Boot 應用,Docker學習-Docker搭建Consul集群,Docker學習-簡單的私有DockerHub搭建,Docker學習-Spring Boot on Docker,Docker學習-Kubernetes – 集群部署,Docker學習-Kubernetes – Spring Boot 應用,Docker學習-Kubernetes – Spring Boot 應用

 

Docker學習

 

 

簡介

kubernetes,簡稱K8s,是用8代替8個字符“ubernete”而成的縮寫。是一個開源的,用於管理雲平台中多個主機上的容器化的應用,Kubernetes的目標是讓部署容器化的應用簡單並且高效(powerful),Kubernetes提供了應用部署,規劃,更新,維護的一種機制。

Kubernetes是Google開源的一個容器編排引擎,它支持自動化部署、大規模可伸縮、應用容器化管理。在生產環境中部署一個應用程序時,通常要部署該應用的多個實例以便對應用請求進行負載均衡。 在Kubernetes中,我們可以創建多個容器,每個容器裏面運行一個應用實例,然後通過內置的負載均衡策略,實現對這一組應用實例的管理、發現、訪問,而這些細節都不需要運維人員去進行複雜的手工配置和處理。  

基本概念

Kubernetes 中的絕大部分概念都抽象成 Kubernetes 管理的一種資源對象

  • Master:Master 節點是 Kubernetes 集群的控制節點,負責整個集群的管理和控制。Master 節點上包含以下組件:
  • kube-apiserver:集群控制的入口,提供 HTTP REST 服務
  • kube-controller-manager:Kubernetes 集群中所有資源對象的自動化控制中心
  • kube-scheduler:負責 Pod 的調度
  • Node:Node 節點是 Kubernetes 集群中的工作節點,Node 上的工作負載由 Master 節點分配,工作負載主要是運行容器應用。Node 節點上包含以下組件:

    • kubelet:負責 Pod 的創建、啟動、監控、重啟、銷毀等工作,同時與 Master 節點協作,實現集群管理的基本功能。
    • kube-proxy:實現 Kubernetes Service 的通信和負載均衡
    • 運行容器化(Pod)應用
  • Pod: Pod 是 Kubernetes 最基本的部署調度單元。每個 Pod 可以由一個或多個業務容器和一個根容器(Pause 容器)組成。一個 Pod 表示某個應用的一個實例

  • ReplicaSet:是 Pod 副本的抽象,用於解決 Pod 的擴容和伸縮
  • Deployment:Deployment 表示部署,在內部使用ReplicaSet 來實現。可以通過 Deployment 來生成相應的 ReplicaSet 完成 Pod 副本的創建
  • Service:Service 是 Kubernetes 最重要的資源對象。Kubernetes 中的 Service 對象可以對應微服務架構中的微服務。Service 定義了服務的訪問入口,服務的調用者通過這個地址訪問 Service 後端的 Pod 副本實例。Service 通過 Label Selector 同後端的 Pod 副本建立關係,Deployment 保證後端Pod 副本的數量,也就是保證服務的伸縮性。

Kubernetes 主要由以下幾個核心組件組成:

  • etcd 保存了整個集群的狀態,就是一個數據庫;
  • apiserver 提供了資源操作的唯一入口,並提供認證、授權、訪問控制、API 註冊和發現等機制;
  • controller manager 負責維護集群的狀態,比如故障檢測、自動擴展、滾動更新等;
  • scheduler 負責資源的調度,按照預定的調度策略將 Pod 調度到相應的機器上;
  • kubelet 負責維護容器的生命周期,同時也負責 Volume(CSI)和網絡(CNI)的管理;
  • Container runtime 負責鏡像管理以及 Pod 和容器的真正運行(CRI);
  • kube-proxy 負責為 Service 提供 cluster 內部的服務發現和負載均衡;

當然了除了上面的這些核心組件,還有一些推薦的插件:

  • kube-dns 負責為整個集群提供 DNS 服務
  • Ingress Controller 為服務提供外網入口
  • Heapster 提供資源監控
  • Dashboard 提供 GUI

組件通信

Kubernetes 多組件之間的通信原理:

  • apiserver 負責 etcd 存儲的所有操作,且只有 apiserver 才直接操作 etcd 集群
  • apiserver 對內(集群中的其他組件)和對外(用戶)提供統一的 REST API,其他組件均通過 apiserver 進行通信

    • controller manager、scheduler、kube-proxy 和 kubelet 等均通過 apiserver watch API 監測資源變化情況,並對資源作相應的操作
    • 所有需要更新資源狀態的操作均通過 apiserver 的 REST API 進行
  • apiserver 也會直接調用 kubelet API(如 logs, exec, attach 等),默認不校驗 kubelet 證書,但可以通過 --kubelet-certificate-authority 開啟(而 GKE 通過 SSH 隧道保護它們之間的通信)

比如最典型的創建 Pod 的流程:​​

  • 用戶通過 REST API 創建一個 Pod
  • apiserver 將其寫入 etcd
  • scheduluer 檢測到未綁定 Node 的 Pod,開始調度並更新 Pod 的 Node 綁定
  • kubelet 檢測到有新的 Pod 調度過來,通過 container runtime 運行該 Pod
  • kubelet 通過 container runtime 取到 Pod 狀態,並更新到 apiserver 中

 

集群部署

 

使用kubeadm工具安裝

1. master和node 都用yum 安裝kubelet,kubeadm,docker
2. master 上初始化:kubeadm init
3. master 上啟動一個flannel的pod
4. node上加入集群:kubeadm join

 

準備環境

Centos7  192.168.50.21 k8s-master  
Centos7  192.168.50.22 k8s-node01
Centos7  192.168.50.23 k8s-node02

修改主機名(3台機器都需要修改)

hostnamectl set-hostname k8s-master
hostnamectl set-hostname k8s-node01
hostnamectl set-hostname k8s-node02

關閉防火牆

systemctl stop firewalld.service

配置docker yum源

yum install -y yum-utils device-mapper-persistent-data lvm2 wget
cd /etc/yum.repos.d
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

配置kubernetes yum 源

cd /opt/
wget https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
wget https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
rpm --import yum-key.gpg
rpm --import rpm-package-key.gpg
cd /etc/yum.repos.d
vi kubernetes.repo 
輸入以下內容
[kubernetes]
name=Kubernetes Repo
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
enabled=1

yum repolist

master和node 安裝kubelet,kubeadm,docker

yum install docker 
yum install kubelet-1.13.1
yum install kubeadm-1.13.1

master 上安裝kubectl

yum install kubectl-1.13.1

docker的配置

配置私有倉庫和鏡像加速地址,私有倉庫配置參見 

vi /etc/docker/daemon.json

 

{
    "registry-mirror":[
        "http://hub-mirror.c.163.com"
    ],
    "insecure-registries":[
        "192.168.50.24:5000"
    ]
}

 

啟動docker

systemctl daemon-reload
systemctl start docker 
docker info

master 上初始化:kubeadm init 

vi /etc/sysconfig/kubelet
KUBELET_EXTRA_ARGS="--fail-swap-on=false"
kubeadm init \
    --apiserver-advertise-address=192.168.50.21 \
    --image-repository registry.aliyuncs.com/google_containers \
    --kubernetes-version v1.13.1 \
    --pod-network-cidr=10.244.0.0/16

初始化命令說明:

--apiserver-advertise-address

指明用 Master 的哪個 interface 與 Cluster 的其他節點通信。如果 Master 有多個 interface,建議明確指定,如果不指定,kubeadm 會自動選擇有默認網關的 interface。

--pod-network-cidr

指定 Pod 網絡的範圍。Kubernetes 支持多種網絡方案,而且不同網絡方案對 –pod-network-cidr 有自己的要求,這裏設置為 10.244.0.0/16 是因為我們將使用 flannel 網絡方案,必須設置成這個 CIDR。

--image-repository

Kubenetes默認Registries地址是 k8s.gcr.io,在國內並不能訪問 gcr.io,在1.13版本中我們可以增加–image-repository參數,默認值是 k8s.gcr.io,將其指定為阿里雲鏡像地址:registry.aliyuncs.com/google_containers。

--kubernetes-version=v1.13.1 

關閉版本探測,因為它的默認值是stable-1,會導致從https://dl.k8s.io/release/stable-1.txt下載最新的版本號,我們可以將其指定為固定版本(最新版:v1.13.1)來跳過網絡請求。

初始化過程中

[preflight] You can also perform this action in beforehand using 'kubeadm config images pull' 是在下載鏡像文件,過程比較慢。
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[apiclient] All control plane components are healthy after 24.002300 seconds 這個過程也比較慢 可以忽略
 
[init] Using Kubernetes version: v1.13.1
[preflight] Running pre-flight checks
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Activating the kubelet service
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [k8s-master kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.50.21]
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [k8s-master localhost] and IPs [192.168.50.21 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [k8s-master localhost] and IPs [192.168.50.21 127.0.0.1 ::1]
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
[control-plane] Creating static Pod manifest for "kube-scheduler"
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[apiclient] All control plane components are healthy after 24.002300 seconds
[uploadconfig] storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.13" in namespace kube-system with the configuration for the kubelets in the cluster
[patchnode] Uploading the CRI Socket information "/var/run/dockershim.sock" to the Node API object "k8s-master" as an annotation
[mark-control-plane] Marking the node k8s-master as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node k8s-master as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: 7ax0k4.nxpjjifrqnbrpojv
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstraptoken] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstraptoken] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstraptoken] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstraptoken] creating the "cluster-info" ConfigMap in the "kube-public" namespace
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy

Your Kubernetes master has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  https://kubernetes.io/docs/concepts/cluster-administration/addons/

You can now join any number of machines by running the following on each node
as root:

  kubeadm join 192.168.50.21:6443 --token 7ax0k4.nxpjjifrqnbrpojv --discovery-token-ca-cert-hash sha256:95942f10859a71879c316e75498de02a8b627725c37dee33f74cd040e1cd9d6b

初始化過程說明:

1) [preflight] kubeadm 執行初始化前的檢查。
2) [kubelet-start] 生成kubelet的配置文件”/var/lib/kubelet/config.yaml”
3) [certificates] 生成相關的各種token和證書
4) [kubeconfig] 生成 KubeConfig 文件,kubelet 需要這個文件與 Master 通信
5) [control-plane] 安裝 Master 組件,會從指定的 Registry 下載組件的 Docker 鏡像。
6) [bootstraptoken] 生成token記錄下來,後邊使用kubeadm join往集群中添加節點時會用到
7) [addons] 安裝附加組件 kube-proxy 和 kube-dns。
8) Kubernetes Master 初始化成功,提示如何配置常規用戶使用kubectl訪問集群。
9) 提示如何安裝 Pod 網絡。
10) 提示如何註冊其他節點到 Cluster。

 

異常情況:

 [WARNING Service-Docker]: docker service is not enabled, please run 'systemctl enable docker.service'
        [WARNING Swap]: running with swap on is not supported. Please disable swap
        [WARNING Hostname]: hostname "k8s-master" could not be reached
        [WARNING Hostname]: hostname "k8s-master": lookup k8s-master on 114.114.114.114:53: no such host
        [WARNING Service-Kubelet]: kubelet service is not enabled, please run 'systemctl enable kubelet.service'

運行

systemctl enable docker.service
systemctl enable kubelet.service

會提示以下錯誤

  [WARNING Hostname]: hostname "k8s-master" could not be reached
        [WARNING Hostname]: hostname "k8s-master": lookup k8s-master on 114.114.114.114:53: no such host
error execution phase preflight: [preflight] Some fatal errors occurred:

配置host

cat >> /etc/hosts << EOF
192.168.50.21 k8s-master
192.168.50.22 k8s-node01
192.168.50.23 k8s-node02
EOF

再次運行初始化命令會出現

  [ERROR NumCPU]: the number of available CPUs 1 is less than the required 2   --設置虛擬機CPU個數大於2
        [ERROR FileContent--proc-sys-net-bridge-bridge-nf-call-iptables]: /proc/sys/net/bridge/bridge-nf-call-iptables contents are not set to 1
echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables echo 1 > /proc/sys/net/bridge/bridge-nf-call-ip6tables

設置好虛擬機CPU個數,重啟后再次運行:

kubeadm init \
    --apiserver-advertise-address=192.168.50.21 \
    --image-repository registry.aliyuncs.com/google_containers \
    --kubernetes-version v1.13.1 \
    --pod-network-cidr=10.244.0.0/16

 

[init] Using Kubernetes version: v1.13.1
[preflight] Running pre-flight checks
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull

解決辦法:docker.io倉庫對google的容器做了鏡像,可以通過下列命令下拉取相關鏡像

先看下需要用到哪些

kubeadm config images list

配置yum源

[root@k8s-master opt]# vi kubeadm-config.yaml
apiVersion: kubeadm.k8s.io/v1beta1
kind: ClusterConfiguration
kubernetesVersion: v1.13.1
imageRepository: registry.aliyuncs.com/google_containers
apiServer:
  certSANs:
  - 192.168.50.21
controlPlaneEndpoint: "192.168.50.20:16443"
networking:
  # This CIDR is a Calico default. Substitute or remove for your CNI provider.
  podSubnet: "172.168.0.0/16"
 kubeadm config images pull --config /opt/kubeadm-config.yaml

初始化master

kubeadm init --config=kubeadm-config.yaml  --upload-certs

 

xecution phase preflight: [preflight] Some fatal errors occurred:
        [ERROR FileAvailable--etc-kubernetes-manifests-kube-apiserver.yaml]: /etc/kubernetes/manifests/kube-apiserver.yaml already exists
        [ERROR FileAvailable--etc-kubernetes-manifests-kube-controller-manager.yaml]: /etc/kubernetes/manifests/kube-controller-manager.yaml already exists
        [ERROR FileAvailable--etc-kubernetes-manifests-kube-scheduler.yaml]: /etc/kubernetes/manifests/kube-scheduler.yaml already exists
        [ERROR FileAvailable--etc-kubernetes-manifests-etcd.yaml]: /etc/kubernetes/manifests/etcd.yaml already exists
        [ERROR Port-10250]: Port 10250 is in use

kubeadm會自動檢查當前環境是否有上次命令執行的“殘留”。如果有,必須清理后再行執行init。我們可以通過”kubeadm reset”來清理環境,以備重來。

[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[kubelet-check] Initial timeout of 40s passed.

==原因==

這是因為kubelet沒啟動

==解決==

systemctl restart kubelet

如果啟動不了kubelet

kubelet.service - kubelet: The Kubernetes Node Agent

 

則可能是swap交換分區還開啟的原因
-關閉swap

swapoff -a

-配置kubelet

vi /etc/sysconfig/kubelet
KUBELET_EXTRA_ARGS="--fail-swap-on=false"

 再次運行

kubeadm init \
    --apiserver-advertise-address=192.168.50.21 \
    --image-repository registry.aliyuncs.com/google_containers \
    --kubernetes-version v1.13.1 \
    --pod-network-cidr=10.244.0.0/16

 

 

配置 kubectl

kubectl 是管理 Kubernetes Cluster 的命令行工具,前面我們已經在所有的節點安裝了 kubectl。Master 初始化完成后需要做一些配置工作,然後 kubectl 就能使用了。
依照 kubeadm init 輸出的最後提示,推薦用 Linux 普通用戶執行 kubectl。

  • 創建普通用戶centos
#創建普通用戶並設置密碼123456
useradd centos && echo "centos:123456" | chpasswd centos

#追加sudo權限,並配置sudo免密
sed -i '/^root/a\centos  ALL=(ALL)       NOPASSWD:ALL' /etc/sudoers

#保存集群安全配置文件到當前用戶.kube目錄
su - centos
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

#啟用 kubectl 命令自動補全功能(註銷重新登錄生效)
echo "source <(kubectl completion bash)" >> ~/.bashrc

需要這些配置命令的原因是:Kubernetes 集群默認需要加密方式訪問。所以,這幾條命令,就是將剛剛部署生成的 Kubernetes 集群的安全配置文件,保存到當前用戶的.kube 目錄下,kubectl 默認會使用這個目錄下的授權信息訪問 Kubernetes 集群。
如果不這麼做的話,我們每次都需要通過 export KUBECONFIG 環境變量告訴 kubectl 這個安全配置文件的位置。
配置完成后centos用戶就可以使用 kubectl 命令管理集群了。

查看集群狀態:

kubectl get cs

 

 

 

 

 

 部署網絡插件

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

  

kubectl get 重新檢查 Pod 的狀態

 

 

部署worker節點

 在master機器保存生成號的鏡像文件

docker save -o master.tar registry.aliyuncs.com/google_containers/kube-proxy:v1.13.1 registry.aliyuncs.com/google_containers/kube-apiserver:v1.13.1 registry.aliyuncs.com/google_containers/kube-controller-manager:v1.13.1 registry.aliyuncs.com/google_containers/kube-scheduler:v1.13.1  registry.aliyuncs.com/google_containers/coredns:1.2.6  registry.aliyuncs.com/google_containers/etcd:3.2.24 registry.aliyuncs.com/google_containers/pause:3.1

注意對應的版本號

將master上保存的鏡像同步到節點上

scp master.tar node01:/root/
scp master.tar node02:/root/

將鏡像導入本地,node01,node02

 docker load< master.tar

配置host,node01,node02

cat >> /etc/hosts << EOF
192.168.50.21 k8s-master
192.168.50.22 k8s-node01
192.168.50.23 k8s-node02
EOF

配置iptables,node01,node02

echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
echo 1 > /proc/sys/net/bridge/bridge-nf-call-ip6tables

-關閉swap,node01,node02

swapoff -a

-配置kubelet,node01,node02

vi /etc/sysconfig/kubelet
KUBELET_EXTRA_ARGS="--fail-swap-on=false" 
systemctl enable docker.service
systemctl enable kubelet.service

啟動docker,node01,node02

service docker strat

部署網絡插件,node01,node02

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

獲取join指令,master

kubeadm token create --print-join-command
kubeadm token create --print-join-command
kubeadm join 192.168.50.21:6443 --token n9g4nq.kf8ppgpgb3biz0n5 --discovery-token-ca-cert-hash sha256:95942f10859a71879c316e75498de02a8b627725c37dee33f74cd040e1cd9d6b

 

在子節點運行指令 ,node01,node02

kubeadm join 192.168.50.21:6443 --token n9g4nq.kf8ppgpgb3biz0n5 --discovery-token-ca-cert-hash sha256:95942f10859a71879c316e75498de02a8b627725c37dee33f74cd040e1cd9d6b
[preflight] Running pre-flight checks
[discovery] Trying to connect to API Server "192.168.50.21:6443"
[discovery] Created cluster-info discovery client, requesting info from "https://192.168.50.21:6443"
[discovery] Requesting info from "https://192.168.50.21:6443" again to validate TLS against the pinned public key
[discovery] Cluster info signature and contents are valid and TLS certificate validates against pinned roots, will use API Server "192.168.50.21:6443"
[discovery] Successfully established connection with API Server "192.168.50.21:6443"
[join] Reading configuration from the cluster...
[join] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
[kubelet-start] WARNING: unable to stop the kubelet service momentarily: [exit status 4]
[kubelet] Downloading configuration for the kubelet from the "kubelet-config-1.13" ConfigMap in the kube-system namespace
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Activating the kubelet service
[tlsbootstrap] Waiting for the kubelet to perform the TLS Bootstrap...
[patchnode] Uploading the CRI Socket information "/var/run/dockershim.sock" to the Node API object "k8s-node01" as an annotation

This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.

Run 'kubectl get nodes' on the master to see this node join the cluster.

在master上查看節點狀態

kubectl get nodes

 

 這種狀態是錯誤的 ,只有一台聯機正確

查看node01,和node02發現 node01有些進程沒有完全啟動

刪除node01所有運行的容器,node01

docker stop $(docker ps -q) & docker rm $(docker ps -aq)

重置 kubeadm ,node01

kubeadm reset

獲取join指令,master

kubeadm token create --print-join-command

再次在node01上運行join

 

 

 

查看node01鏡像運行狀態

  

查看master狀態

  

nodes狀態全部為ready,由於每個節點都需要啟動若干組件,如果node節點的狀態是 NotReady,可以查看所有節點pod狀態,確保所有pod成功拉取到鏡像並處於running狀態:

kubectl get pod --all-namespaces -o wide

  

配置kubernetes UI圖形化界面

創建kubernetes-dashboard.yaml

# Copyright 2017 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# ------------------- Dashboard Secret ------------------- #

apiVersion: v1
kind: Secret
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard-certs
  namespace: kube-system
type: Opaque

---
# ------------------- Dashboard Service Account ------------------- #

apiVersion: v1
kind: ServiceAccount
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kube-system

---
# ------------------- Dashboard Role & Role Binding ------------------- #

kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: kubernetes-dashboard-minimal
  namespace: kube-system
rules:
  # Allow Dashboard to create 'kubernetes-dashboard-key-holder' secret.
- apiGroups: [""]
  resources: ["secrets"]
  verbs: ["create"]
  # Allow Dashboard to create 'kubernetes-dashboard-settings' config map.
- apiGroups: [""]
  resources: ["configmaps"]
  verbs: ["create"]
  # Allow Dashboard to get, update and delete Dashboard exclusive secrets.
- apiGroups: [""]
  resources: ["secrets"]
  resourceNames: ["kubernetes-dashboard-key-holder", "kubernetes-dashboard-certs"]
  verbs: ["get", "update", "delete"]
  # Allow Dashboard to get and update 'kubernetes-dashboard-settings' config map.
- apiGroups: [""]
  resources: ["configmaps"]
  resourceNames: ["kubernetes-dashboard-settings"]
  verbs: ["get", "update"]
  # Allow Dashboard to get metrics from heapster.
- apiGroups: [""]
  resources: ["services"]
  resourceNames: ["heapster"]
  verbs: ["proxy"]
- apiGroups: [""]
  resources: ["services/proxy"]
  resourceNames: ["heapster", "http:heapster:", "https:heapster:"]
  verbs: ["get"]

---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: kubernetes-dashboard-minimal
  namespace: kube-system
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: kubernetes-dashboard-minimal
subjects:
- kind: ServiceAccount
  name: kubernetes-dashboard
  namespace: kube-system

---
# ------------------- Dashboard Deployment ------------------- #

kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kube-system
spec:
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      k8s-app: kubernetes-dashboard
  template:
    metadata:
      labels:
        k8s-app: kubernetes-dashboard
    spec:
      containers:
      - name: kubernetes-dashboard
        image: registry.cn-hangzhou.aliyuncs.com/rsqlh/kubernetes-dashboard:v1.10.1  
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8443
          protocol: TCP
        args:
          - --auto-generate-certificates
          # Uncomment the following line to manually specify Kubernetes API server Host
          # If not specified, Dashboard will attempt to auto discover the API server and connect
          # to it. Uncomment only if the default does not work.
          # - --apiserver-host=http://my-address:port
        volumeMounts:
        - name: kubernetes-dashboard-certs
          mountPath: /certs
          # Create on-disk volume to store exec logs
        - mountPath: /tmp
          name: tmp-volume
        livenessProbe:
          httpGet:
            scheme: HTTPS
            path: /
            port: 8443
          initialDelaySeconds: 30
          timeoutSeconds: 30
      volumes:
      - name: kubernetes-dashboard-certs
        secret:
          secretName: kubernetes-dashboard-certs
      - name: tmp-volume
        emptyDir: {}
      serviceAccountName: kubernetes-dashboard
      # Comment the following tolerations if Dashboard must not be deployed on master
      tolerations:
      - key: node-role.kubernetes.io/master
        effect: NoSchedule

---
# ------------------- Dashboard Service ------------------- #

kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kube-system
spec:
  type: NodePort
  ports:
  - port: 443
    targetPort: 8443
    nodePort: 30000
  selector:
    k8s-app: kubernetes-dashboard

 

執行以下命令創建kubernetes-dashboard:

kubectl create -f kubernetes-dashboard.yaml

如果出現

Error from server (AlreadyExists): error when creating "kubernetes-dashboard.yaml": secrets "kubernetes-dashboard-certs" already exists
Error from server (AlreadyExists): error when creating "kubernetes-dashboard.yaml": serviceaccounts "kubernetes-dashboard" already exists
Error from server (AlreadyExists): error when creating "kubernetes-dashboard.yaml": roles.rbac.authorization.k8s.io "kubernetes-dashboard-minimal" already exists
Error from server (AlreadyExists): error when creating "kubernetes-dashboard.yaml": rolebindings.rbac.authorization.k8s.io "kubernetes-dashboard-minimal" already exists
Error from server (AlreadyExists): error when creating "kubernetes-dashboard.yaml": deployments.apps "kubernetes-dashboard" already exists

運行delete清理

kubectl delete -f kubernetes-dashboard.yaml

查看組件運行狀態

kubectl get pods --all-namespaces

 

 

 ErrImagePull 拉取鏡像失敗

手動拉取 並重置tag

docker pull registry.cn-hangzhou.aliyuncs.com/rsqlh/kubernetes-dashboard:v1.10.1
docker tag registry.cn-hangzhou.aliyuncs.com/rsqlh/kubernetes-dashboard:v1.10.1 k8s.gcr.io/kubernetes-dashboard-amd64:v1.10.1

重新創建

  

 

ImagePullBackOff

默認情況是會根據配置文件中的鏡像地址去拉取鏡像,如果設置為IfNotPresent 和Never就會使用本地鏡像。

IfNotPresent :如果本地存在鏡像就優先使用本地鏡像。
Never:直接不再去拉取鏡像了,使用本地的;如果本地不存在就報異常了。

 spec:
      containers:
      - name: kubernetes-dashboard
        image: registry.cn-hangzhou.aliyuncs.com/rsqlh/kubernetes-dashboard:v1.10.1  
        imagePullPolicy: IfNotPresent

 

查看映射狀態 

 kubectl get service  -n kube-system

 

 

 

 

創建能夠訪問 Dashboard 的用戶

 新建文件 account.yaml ,內容如下:

# Create Service Account
apiVersion: v1
kind: ServiceAccount
metadata:
  name: admin-user
  namespace: kube-system
---
# Create ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: admin-user
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: admin-user
  namespace: kube-system
kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep admin-user | awk '{print $1}')

 

 複製token登陸

 

 

configmaps is forbidden: User "system:serviceaccount:kube-system:admin-user" cannot list resource "configmaps" in API group "" in the namespace "default" 

 授權用戶

kubectl create clusterrolebinding test:admin-user --clusterrole=cluster-admin --serviceaccount=kube-system:admin-user

 

 

 NodePort方式,可以到任意一個節點的XXXX端口查看

 

 

本文參考:

 

本站聲明:網站內容來源於博客園,如有侵權,請聯繫我們,我們將及時處理
【其他文章推薦】

USB CONNECTOR掌控什麼技術要點? 帶您認識其相關發展及效能

※評比前十大台北網頁設計台北網站設計公司知名案例作品心得分享

※智慧手機時代的來臨,RWD網頁設計已成為網頁設計推薦首選

台灣海運大陸貨務運送流程

兩岸物流進出口一站式服務

為什麼 HTTPS 比 HTTP 安全

HTTP(超文本傳輸協議)是目前互聯網應用最廣泛的協議,伴隨着人們網絡安全意識的加強,HTTPS 被越來越多地採納。不論是訪問一些購物網站,或是登錄一些博客、論壇等,我們都被 HTTPS 保護着,甚至 Google Chrome、Firefox 等主流瀏覽器已經將所有基於 HTTP 的站點都標記為不安全。

為什麼 HTTPS 比 HTTP 安全?在回答這個問題之前,首先我們得了解 HTTP 和 HTTPS 是什麼。

HTTP 和 HTTPS 的訪問過程

從互聯網發展至今,HTTP 一直擔任互聯網傳輸信息的標準協議。傳輸的信息可以是互聯網內計算機之間的文檔,文件,圖像,視頻等。

HTTP 請求過程中,客戶端與服務器之間沒有任何身份確認的過程,數據全部明文傳輸,“裸奔”在互聯網上,所以很容易遭到黑客的攻擊。

從上圖中可以看到,客戶端發出的請求很容易被黑客截獲,如果此時黑客冒充服務器,則其可返回任意信息給客戶端,而不被客戶端察覺,所以我們經常會聽到一詞“劫持”。

而 HTTPS 實際上是帶有 SSL 的 HTTP(HTTP + SSL=HTTPS)。當您在瀏覽器的地址欄中看到 HTTPS 時,這就意味着與該網站的所有通信都將被加密,整個訪問過程更加安全。

為什麼 HTTPS 比 HTTP 安全

HTTPS 的安全性往往體現在三個方面:

  • 服務器身份驗證,通過服務器身份驗證,用戶可以明確當前它正在與對應的服務器進行通信。

  • 數據機密性,其他方無法理解發送的數據內容,因為提交的數據是加密的。

  • 數據完整性,傳輸會攜帶 Message Authentication Code(MAC)用於驗證,因此傳輸的數據不會被另一方更改。
    可以舉個例子來比較下。一個 HTTP 請求,其組成則是多個遵循 HTTP 協議的文本行,例如下面的 GET 請求:

GET /helloupyun.txt HTTP/1.1

User-Agent: curl/7.73.0 libcurl/7.73.0 OpenSSL/1.1.l zlib/1.2.11

Host: www.upyun.com

Accept-Language: en

請求會以明文的形式直接發送,既然是明文的形式,對於協議命令和語法有基本了解的人,只要監控了請求發送的過程,就能獲取並讀懂請求的意義。因此用 HTTP 的方式發送密碼一類的數據時,安全性極低。

相對的,HTTPS 使用了 SSL(或 TLS)來加密 HTTP 請求和響應,因此在上面的示例中,監控請求的人將會看到一串隨機的数字,而不是可讀性的文本。

GsERHg9YDMpYk0VVDiRvw1H5miNieJeJ/FNUjgH0BmVAWII6+T4MnDwmCMZUI/orxP3HGwYCSIvyzS3MpmmSe4iaWKCOHH==

其中加密過程採用的 SSL(安全套接字層)這一標準的安全技術,涵蓋了非對稱密鑰和對稱密鑰。

對稱加密
對稱加密是指加密與解密使用同一個密鑰的加密算法。

目前常見的加密算法有:DES、AES、IDEA 等

非對稱加密
非對稱加密使用的是兩個密鑰,公鑰與私鑰,我們會使用公鑰對網站賬號密碼等數據進行加密,再用私鑰對數據進行解密。這個公鑰會發給查看網站的所有人,而私鑰是只有網站服務器自己擁有的。

目前常見非對稱加密算法:RSA,DSA,DH 等。

而常用的套件,例如 ChaCha20-Poly1305 加密套件就使用了這兩種算法,其中 Chacha20 是指對稱加密算法,而Poly1305 是指身份認證算法。

參考 RFC 文檔,我們可以了解 ChaCha20 提供了 256 位的加密強度,這作為對稱加密算法來保障 HTTPS 安全性是足夠了。

而 Poly1305 作為身份認證算法提供身份驗證,可以防止攻擊者在 TLS 握手過程中,將虛假信息插入到安全的數據流中,Poly1305 算法提供了大約 100 位的安全性加密強度,足以阻止這類攻擊。

總的來看,HTTPS 相比 HTTP ,它作為一種加密手段不僅加密了數據,還給了網站一張安全可信賴的身份證。

聊聊 HTTPS 的一些優缺點

整體來看 HTTPS 有以下五個優點:

  • 最大限度地提高 Web 上數據和事務的安全性;

  • 加密用戶敏感或者機密信息;

  • 提高搜索引擎中的排名

  • 避免在瀏覽器中出現“不安全”的提示;

  • 提升用戶對網站的信賴。

相對的,缺點也是必不可少的:

  • HTTPS 協議在握手階段耗時相對較大,會影響頁面整體加載速度;

  • 在瀏覽器和服務器上會更多的 CPU 周期來加密/解密數據;

  • SSL 證書一般都需要支付一定費用來獲取,並且費用往往不低;

  • 並不是絕對意義上的安全,在網站遭受攻擊,服務器被劫持時,HTTPS 基本起不到任何安全防護作用。

將 HTTP 升級成 HTTPS

如何將網站從 HTTP 升級成 HTTPS 呢?相比起常規的升級步驟,又拍雲提供一套更為簡潔明了的流程,從 SSL 證書的申購、管理到部署,三步即可完成。同時,又拍雲與國際頂級 CA 機構合作,證書類型豐富,操作流程簡單方便。

推薦閱讀

本站聲明:網站內容來源於博客園,如有侵權,請聯繫我們,我們將及時處理
【其他文章推薦】

台北網頁設計公司這麼多,該如何挑選?? 網頁設計報價省錢懶人包”嚨底家”

網頁設計公司推薦更多不同的設計風格,搶佔消費者視覺第一線

※想知道購買電動車哪裡補助最多?台中電動車補助資訊懶人包彙整

小三通海運與一般國際貿易有何不同?

小三通快遞通關作業有哪些?

使用JaCoCo Maven插件創建代碼覆蓋率報告

這篇博客文章描述了我們如何使用JaCoCo Maven插件為單元和集成測試創建代碼覆蓋率報告。

我們的構建要求如下:

運行測試時,我們的構建必須為單元測試和集成測試創建代碼覆蓋率報告。
代碼覆蓋率報告必須在單獨的目錄中創建。換句話說,必須將用於單元測試的代碼覆蓋率報告創建到與用於集成測試的代碼覆蓋率報告不同的目錄中。
讓我們開始吧。

配置JaCoCo Maven插件

我們使用JaCoCo Maven插件有兩個目的:

  • 它使我們可以訪問JaCoCo運行時代理,該代理記錄了執行覆蓋率數據。
  • 它根據JaCo​​Co運行時代理記錄的執行數據創建代碼覆蓋率報告。
  • 我們可以按照以下步驟配置JaCoCo Maven插件:

將JaCoCo Maven插件添加到我們的POM文件的插件部分。

  • 為單元測試配置代碼覆蓋率報告。
  • 配置代碼覆蓋率報告以進行集成測試。
    下面將更詳細地描述這些步驟。

將JaCoCo Maven插件添加到POM文件

通過將以下插件聲明添加到其“ 插件”部分,我們可以將JaCoCo Maven插件添加到我們的POM文件中:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.6.3.201306030806</version>
</plugin>

配置單元測試的代碼覆蓋率報告

我們可以通過將兩個執行添加到插件聲明中來為單元測試配置代碼覆蓋率報告。這些執行方式如下所述:

  • 第一次執行將創建一個指向JaCoCo運行時代理的屬性。確保執行數據已寫入文件target / coverage-reports / jacoco-ut.exec。將該屬性的名稱設置為surefireArgLine。運行單元測試時,此屬性的值作為VM參數傳遞。
  • 運行單元測試后,第二次執行將為單元測試創建代碼覆蓋率報告。確保從文件target / coverage-reports / jacoco-ut.exec中讀取執行數據,並將代碼覆蓋率報告寫入目錄target / site / jacoco-ut中。

我們的插件配置的相關部分如下所示:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.6.3.201306030806</version>
    <executions>
        <!--
           Prepares the property pointing to the JaCoCo runtime agent which
           is passed as VM argument when Maven the Surefire plugin is executed.
       -->
        <execution>
            <id>pre-unit-test</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <!-- Sets the path to the file which contains the execution data. -->
                <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
                <!--
                   Sets the name of the property containing the settings
                   for JaCoCo runtime agent.
               -->
                <propertyName>surefireArgLine</propertyName>
            </configuration>
        </execution>
        <!--
           Ensures that the code coverage report for unit tests is created after
           unit tests have been run.
       -->
        <execution>
            <id>post-unit-test</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
            <configuration>
                <!-- Sets the path to the file which contains the execution data. -->
                <dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
                <!-- Sets the output directory for the code coverage report. -->
                <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

讓我們找出如何為集成測試配置代碼覆蓋率報告。

配置集成測試的代碼覆蓋率報告

我們可以通過在插件聲明中添加兩個執行來為集成測試配置代碼覆蓋率報告。這些執行方式如下所述:

  • 第一次執行將創建一個指向JaCoCo運行時代理的屬性。確保將執行數據寫入文件target / coverage-reports / jacoco-it.exec。將該屬性的名稱設置為failsafeArgLine。運行我們的集成測試時,此屬性的值作為VM參數傳遞。
  • 創建一個執行,該執行在集成測試運行後為集成測試創建代碼覆蓋率報告。確保從文件target / coverage-reports / jacoco-it.exec中讀取執行數據,並將代碼覆蓋率報告寫入目錄target / site / jacoco-it。

我們的插件配置的相關部分如下所示:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.6.3.201306030806</version>
    <executions>
        <!-- The Executions required by unit tests are omitted. -->
        <!--
           Prepares the property pointing to the JaCoCo runtime agent which
           is passed as VM argument when Maven the Failsafe plugin is executed.
       -->
        <execution>
            <id>pre-integration-test</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <!-- Sets the path to the file which contains the execution data. -->
                <destFile>${project.build.directory}/coverage-reports/jacoco-it.exec</destFile>
                <!--
                   Sets the name of the property containing the settings
                   for JaCoCo runtime agent.
               -->
                <propertyName>failsafeArgLine</propertyName>
            </configuration>
        </execution>
        <!--
           Ensures that the code coverage report for integration tests after
           integration tests have been run.
       -->
        <execution>
            <id>post-integration-test</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>report</goal>
            </goals>
            <configuration>
                <!-- Sets the path to the file which contains the execution data. -->
                <dataFile>${project.build.directory}/coverage-reports/jacoco-it.exec</dataFile>
                <!-- Sets the output directory for the code coverage report. -->
                <outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

現在,我們已經配置了JaCoCo Maven插件。下一步是配置Maven Surefire插件。讓我們找出如何做到這一點。

配置Maven Surefire插件

我們使用Maven Surefire插件運行示例應用程序的單元測試。因為我們要為單元測試創​​建代碼覆蓋率報告,所以我們必須確保在運行單元測試時JaCoCo代理正在運行。我們可以通過添加的價值保證本surefireArgLine財產作為價值argLine配置參數。

Maven Surefire插件的配置如下所示(突出显示了所需的更改):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.15</version>
    <configuration>
        <!-- Sets the VM argument line used when unit tests are run. -->
        <argLine>${surefireArgLine}</argLine>
        <!-- Skips unit tests if the value of skip.unit.tests property is true -->
        <skipTests>${skip.unit.tests}</skipTests>
        <!-- Excludes integration tests when unit tests are run. -->
        <excludes>
            <exclude>**/IT*.java</exclude>
        </excludes>
    </configuration>
</plugin>

我們快完成了。剩下要做的就是配置Maven Failsafe插件。讓我們找出如何做到這一點。

配置Maven故障安全插件

我們的示例應用程序的集成測試由Maven Failsafe插件運行。因為我們要為集成測試創建代碼覆蓋率報告,所以我們必須確保在運行集成測試時JaCoCo代理正在運行。我們可以通過將failsafeArgLine屬性的值添加為argLine配置參數的值來實現。

Maven Failsafe插件的配置如下所示(突出显示了所需的更改):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.15</version>
    <executions>
        <!--
            Ensures that both integration-test and verify goals of the Failsafe Maven
            plugin are executed.
        -->
        <execution>
            <id>integration-tests</id>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
            <configuration>
                <!-- Sets the VM argument line used when integration tests are run. -->
                <argLine>${failsafeArgLine}</argLine>
                <!--
                    Skips integration tests if the value of skip.integration.tests property
                    is true
                -->
                <skipTests>${skip.integration.tests}</skipTests>
            </configuration>
        </execution>
    </executions>
</plugin>

創建代碼覆蓋率報告

現在,我們已成功完成所需的配置。讓我們看看如何為單元測試和集成測試創建代碼覆蓋率報告。

此博客文章的示例應用程序具有三個構建配置文件,下面對此進行了描述:

  • 在開發配置文件開發過程中使用,這是我們構建的默認配置文件。當此配置文件處於活動狀態時,僅運行單元測試。
  • 在集成測試配置文件用於運行集成測試。
  • 在所有的測試配置文件用於為運行單元測試和集成測試。
    我們可以通過在命令提示符處運行以下命令來創建不同的代碼覆蓋率報告:

  • 命令mvn clean test運行單元測試,併為目錄target / site / jacoco-ut創建單元測試的代碼覆蓋率報告。
  • 命令mvn clean verify -P integration-test運行集成測試,併為目錄target / site / jacoco-it創建用於集成測試的代碼覆蓋率報告。
  • 命令mvn clean verify -P all-tests運行單元測試和集成測試,併為單元測試和集成測試創建代碼覆蓋率報告。

技術類文章精選

非技術文章精選

本站聲明:網站內容來源於博客園,如有侵權,請聯繫我們,我們將及時處理
【其他文章推薦】

※想知道網站建置網站改版該如何進行嗎?將由專業工程師為您規劃客製化網頁設計後台網頁設計

※不管是台北網頁設計公司台中網頁設計公司,全省皆有專員為您服務

※Google地圖已可更新顯示潭子電動車充電站設置地點!!

※帶您來看台北網站建置台北網頁設計,各種案例分享

小三通物流營運型態?

※快速運回,大陸空運推薦?

如何構建自己的 react hooks

我們組的前端妹子在組內分享時談到了 react 的鈎子,趁此機會我也對我所理解的內容進行下總結,方便更多的同學了解。在 React 的 v16.8.0 版本里添加了 hooks 的這種新的 API,我們非常有必要了解下他的使用方法,並能夠結合我們的業務編寫幾個自定義的 hooks。

1. 常用的一個 hooks

官方中提供了幾個內置的鈎子,我們簡單了解下他們的用法。

1.1 useState: 狀態鈎子

需要更新頁面狀態的數據,我們可以把他放到 useState 的鈎子里。例如點擊按鈕一下,數據加 1 的操作:

const [count, setCount] = useState(0);

return (<>
    <p>{ count}</p>
    <button onClick = {
        () => setCount(count + 1)
    }> add 1 </button>
    </>
);

在 typescript 的體系中,count 的類型,默認就是當前初始值的類型,例如上面例子中的變量就是 number 類型。如果我們想自定義這個變量的類型,可以在 useState 後面進行定義:

const [count, setCount] = useState<number | null>(null); // 變量count為number類型或者null類型

同時,使用 useState 改變狀態時,是整個把 state 替換掉的,因此,若狀態變量是個 object 類型的數據,我只想修改其中的某個字段,在之前 class 組件內調用 setState 時,他內部會自動合併數據。

class Home extends React.Component {
    state = {
        name: 'wenzi',
        age: 20,
        score: 89
    };

    update() {
        this.setState({
            score: 98
        }); // 內部自動合併
    }
}

但在 function 組件內使用 useState 時,需要自己先合併數據,然後再調用方法,否則會造成字段的丟失。

const [person, setPerson] = useState({
    name: 'wenzi',
    age: 20,
    score: 89
});

setPerson({
    ...person,
    {
        score: 98
    }
}); // 先合併數據 { name: 'wenzi', age: 20, score: 98 }
setPerson({
    score: 98
}); // 僅傳入要修改的字段,后name和age字段丟失

1.2 useEffect: 副作用鈎子

useEffect 可以看做是 componentDidMount,componentDidUpdate 和 componentWillUnmount 這三個函數的組合。

useEffect 鈎子在組件初始化完畢時,一定會執行一次,在組件重新渲染的過程中,是否還要 update,還要看傳入的第 2 個參數。

  1. 當只有回調函數這一個參數時,組件的每次更新,回調都會執行;
  2. 當有 2 個參數時,只有第 2 參數里的數據發生變化時,回調才執行;
  3. 只想在組件初始化完畢時只執行一次,第 2 個參數可以傳入一個空的數組;

我們可以看下這個例子,無論點擊 add按鈕 還是 settime按鈕 ,useEffect 的回調都會執行:

const Home = () => {
    const [count, setCount] = useState(0);
    const [nowtime, setNowtime] = useState(0);

    useEffect(() => {
        console.log('count', count);
        console.log('nowtime', nowtime);
    });

    return ( <>
        <p>count: {count} </p>
        <p>nowtime: {nowtime} </p>
        <button onClick = {() => setCount(count + 1)}> add 1 </button>
        <button onClick = {() => setNowtime(Date.now())} > set now time </button>
    </>);
};

若改成下面的這樣,回調僅會在 count 發生變化時才會在控制台輸出,僅修改 nowtime 的值時沒有輸出:

useEffect(() => {
    console.log('count', count);
    console.log('nowtime', nowtime);
}, [count]);

useEffect 的回調函數還可以返回一個函數,這個函數在 effect 生命周期結束之前調用。為防止內存泄漏,清除函數會在組件卸載前執行。另外,如果組件多次渲染,則在執行下一個 effect 之前,上一個 effect 就已被清除

基於上面的代碼,我們稍微修改一下:

useEffect(() => {
    console.log('count', count);
    console.log('nowtime', nowtime);

    return () => console.log('effect callback will be cleared');
}, [count]);

基於這個機制,在一些存在添加綁定和取消綁定的案例上特別合適,例如監聽頁面的窗口大小變化、設置定時器、與後端的 websocket 接口建立連接和斷開連接等,都可以預計 useEffect 進行二次的封裝,形成自定義的 hook。關於自定義 hook,下面我們會講到。

1.3 useMemo 和 useCallback

function 組件中定義的變量和方法,在組件重新渲染時,都會重新重新進行計算,例如下面的這個例子:

const Home = () => {
    const [count, setCount] = useState(0);
    const [nowtime, setNowtime] = useState(0);

    const getSum = () => {
        const sum = ((1 + count) * count) / 2;
        return sum + ' , ' + Math.random(); // 這個random是為了看到區別
    };

    return ( <>
        <p> count: {count}< /p>
        <p> sum: {getSum()}</p>
        <p> nowtime: {nowtime}</p>
        <button onClick = {() => setCount(count + 1)} > add 1 </button>
        <button onClick = {() => setNowtime(Date.now())}> set now time </button>
    </>);
};

這裡有 2 個按鈕,一個是 count+1,一個設置當前的時間戳, getSun() 方法是計算從 1 到 count 的和,我們每次點擊 add 按鈕后,sum 方法都會重新計算和。可是當我們點擊 settime 按鈕時,getSum 方法也會重新計算,這是沒有必要的。

這裏我們可以使用 useMemo 來修改下:

const sum = useMemo(() => ((1 + count) * count) / 2 + ' , ' + Math.random(), [count]);

<p> {sum} </p>;

修改后就可以看到,sum 的值只有在 count 發生變化的時候才重新計算,當點擊 settime 按鈕的時候,sum 並沒有重新計算。這要得益於 useMemo 鈎子的特性:

const memoizedValue = useMemo(() => computeExpensiveValue(a, b), [a, b]);

useMemo 返回回調里 return 的值,而且 memoizedValue 它僅會在某個依賴項改變時才重新計算。這種優化有助於避免在每次渲染時都進行高開銷的計算。如果沒有提供依賴項數組,useMemo 在每次渲染時都會計算新的值。

在上面的例子里,只有 count 變量發生變化時,才重新計算 sum,否則 sum 的值保持不變。

useCallback 與 useMemo 類型,只不過 useCallback 返回的是一個函數,例如:

const fn = useCallback(() => {
    return ((1 + count) * count) / 2 + ' , ' + nowtime;
}, [count]);

2. 實現幾個自定義的 hook

在官方文檔里,實現了好友的在線與離線功能。這裏我們自己也學着實現幾個 hook。

2.1 獲取窗口變化的寬高

我們通過監聽resize事件來獲取實時獲取window窗口的寬高,對這個方法進行封裝后可以在生命周期結束前能自動解綁resize事件:

const useWinResize = () => {
    const [size, setSize] = useState({
        width: document.documentElement.clientWidth,
        height: document.documentElement.clientHeight
    });
    const resize = useCallback(() => {
        setSize({
        width: document.documentElement.clientWidth,
        height: document.documentElement.clientHeight
    })
    }, [])
    useEffect(() => {
        window.addEventListener('resize', resize);
        return () => window.removeEventListener('resize', resize);
    }, []);
    return size;
}

使用起來也非常方便:

const Home = () => {
    const {width, height} = useWinResize();

    return <div>
        <p>width: {width}</p>
        <p>height: {height}</p>
    </div>;
};

點擊鏈接可以查看demo演示。

2.2 定時器 useInterval

在前端中使用定時器時,通常要在組件生命周期結束前清除定時器,如果定時器的周期發生變化了,還要先清除定時器再重新按照新的周期來啟動。這種最常用的場景就是九宮格抽獎,用戶點擊開始抽獎后,先緩慢啟動,然後逐漸變快,接口返回中獎結果后,再開始減速,最後停止。

我們很容易想到用 useEffect 來實現這樣的一個 hook:

const useInterval = (callback, delay) => {
    useEffect(() => {
        if (delay !== null) {
            let id = setInterval(callback, delay);
            return () => clearInterval(id);
        }
    }, [delay]);
};

我們把這段代碼用到項目中試試:

const Home = () => {
    const [count, setCount] = useState(0);

    useInterval(() => {
        console.log(count);
        setCount(count + 1);
    }, 500);

    return <div > {
        count
    } < /div>;
};

可是這段運行后很奇怪,頁面從 0 到 1 后,就再也不變了, console.log(count) 的輸出表明代碼並沒有卡死,那麼問題出在哪兒了?

React 組件中的 props 和 state 是可以改變的, React 會重渲染它們且「丟棄」任何關於上一次渲染的結果,它們之間不再有相關性。

useEffect() Hook 也「丟棄」上一次渲染結果,它會清除上一次 effect 再建立下一個 effect,下一個 effect 鎖住新的 props 和 state,這也是我們第一次嘗試簡單示例可以正確工作的原因。

但 setInterval 不會「丟棄」。 它會一直引用老的 props 和 state 直到你把它換掉 —— 不重置時間你是無法做到的。

這裏就要用到這個 hook 了,我們把 callback 存儲到 ref 中,當 callback 更新時去更新 ref.current 的值:

const useInterval = (callback, delay) => {
    const saveCallback = useRef();

    useEffect(() => {
        // 每次渲染后,保存新的回調到我們的 ref 里
        saveCallback.current = callback;
    });

    useEffect(() => {
        function tick() {
            saveCallback.current();
        }
        if (delay !== null) {
            let id = setInterval(tick, delay);
            return () => clearInterval(id);
        }
    }, [delay]);
};

當我們使用新的 useInterval 時,發現就可以自增了,點擊查看樣例。

這裏我們使用一個變量來控制增加的速度:

const [count, setCount] = useState(0);
const [diff, setDiff] = useState(500);

useInterval(() => {
    setCount(count + 1);
}, diff);

return ( <div>
    <p> count: {count} </p>
    <p> diff: {diff}ms </p> 
    <p>
        <button onClick = {() => setDiff(diff - 50)}> 加快50ms </button> 
        <button onClick = {() => setDiff(diff + 50)} > 減慢50ms </button>
    </p>
</div>);

分別點擊兩個按鈕,可以調整count增加的速度。

3. 總結

使用react hook可以做很多有意思的事情,這裏我們也僅僅是舉幾個簡單的例子,後續我們也會更加深入了解hook的原理。

▼我是來騰訊的小小前端開發工程師,
長按識別二維碼關注,與大家共同學習、討論▼

本站聲明:網站內容來源於博客園,如有侵權,請聯繫我們,我們將及時處理

【其他文章推薦】

網頁設計公司推薦更多不同的設計風格,搶佔消費者視覺第一線

※廣告預算用在刀口上,網站設計公司幫您達到更多曝光效益

※自行創業 缺乏曝光? 下一步"網站設計"幫您第一時間規劃公司的門面形象

台灣寄大陸海運貨物規則及重量限制?

大陸寄台灣海運費用試算一覽表

南投搬家前需注意的眉眉角角,別等搬了再說!

Javascript模塊化開發2——Gruntfile.js詳解

一、grunt模塊簡介

grunt插件,是一種npm環境下的自動化工具。對於需要反覆重複的任務,例如壓縮、編譯、單元測試、linting等,自動化工具可以減輕你的勞動,簡化你的工作。grunt模塊根據Gruntfile.js文件中的配置進行任務。
如果在package.json中定義如下命令:

"scripts": {
    "build": "npm install && grunt"
}

因為運行npm run build會先安裝devDependencies中定義的一些模塊,則運行npm run build這個命令相當於做如下操作:

  • npm install grunt-cli -g
  • npm install
  • grunt

二、gruntfile.js的結構:

  • “wrapper” 函數
  • 項目和任務配置
  • 加載 grunt 插件和任務
  • 自定義任務

三、”wrapper” 函數

每一份 Gruntfile.js(和grunt插件)都遵循同樣的格式,你所書寫的Grunt代碼必須放在此函數內:

module.exports = function(grunt){
         //do grunt-related things in here
}

四、項目和任務配置

大部分的Grunt任務都依賴某些配置數據,我們通過grunt.initConfig 方法來配置Grunt任務的參數。
grunt.initConfig 方法的參數是一個JSON對象,你可以在這個配置對象中存儲任意的數據。此外,由於這本身就是JavaScript,你還可以在這裏使用任意的有效的JS代碼。甚至你可以用<% %>模板字符串來引用已經配置過的屬性,例如:

// 項目和任務配置
grunt.initConfig({
  pkg: grunt.file.readJSON('package.json'), //獲取 package.json 中的元數據(js代碼)
  proj:{
    name:'hello',
    description:'a hello demo'
  },
  hello: {
    options: {
      name: '<%= proj.name %>'  //用<% %>模板字符串匹配hello
    },
    srcs: ['1.txt', '2.txt']
  }
});

在grunt.initConfig 方法中配置的屬性,在任務模塊中,可用grunt.config方法進行訪問,例如:

grunt.config("proj.name");

另外,grunt任務模塊會自動根據任務名來提取配置對象中和任務名對應的屬性,比如定義任務hello,則在配置對象對應的屬性”hello”中配置任務執行函數中所需用到的配置和數據。

五、加載grunt插件任務

為了減少重複勞動,我們可以加載已有的插件任務。

1.加載自己私有的grunt插件

可將自己定義的一些task腳本放在同一個目錄下,通過grunt.loadTasks方法從指定目錄加載該目錄下所有的grunt任務腳本。

2.加載在npm中發布的grunt插件

像 grunt-contrib-copy和grunt-contrib-uglify這些常用的任務都已經以grunt插件的形式被開發出來了,且被發布在npm公開庫中,只要在 package.json 文件中將需要使用的插件列在dependency中,並通過npm install安裝之後,就可以直接加載該任務。

// 加載能夠提供"copy"任務的插件。
grunt.loadNpmTasks('grunt-contrib-copy');

3.直接加載所有以”grunt-“打頭的插件

npm上有個load-grunt-tasks插件可以用來加載dependency列表中所有以”grunt-“打頭的插件。
將需要使用的”grunt-“打頭的插件列在dependency中,然後在Gruntfile.js中進行調用。

//Load grunt tasks from NPM packages
load-grunt-tasks

六、自定義任務

1.直接定義任務的行為

grunt.registerTask('hello', 'Show some msg', function() {
  console.log(this.options().name); //輸出hello
});

2.定義為任務列表

可以將一個任務定義為一系列任務的組合,這一系列任務將按照順序執行。

grunt.registerTask('dothings', 'copy and Show some msg', ['copy','hello']);

3.定義默認任務

通過定義 default 任務,可以讓Grunt默認執行一個或多個任務。執行 grunt 命令時如果不指定一個任務的話,將會執行默認任務。如進行下面定義的話執行grunt 相當於執行grunt hello。

grunt.registerTask('default', ['hello']);

4.定義複合任務

registerMultiTask方法可以定義一個複合任務,複合任務將會對grunt.initConfig 方法中配置的相應屬性中除了options外定義的屬性依次作為target:data對進行處理。

module.exports = function(grunt) {
    grunt.initConfig({
        Log: {
            options: {
                sep: ';'
            },
            srcs: ['1.txt', '2.txt'],
            dests: ['d1.txt', 'd2.txt']
        }
    });
    grunt.registerMultiTask("Log", function() {
        var options = this.options({ sep: '&' });
        console.log(this.target); 
        console.log(this.data.join(options.sep));
    });
};

執行grunt Log將會輸出:

Running “Log:srcs” (Log) task
srcs
1.txt;2.txt
Running “Log:dests” (Log) task
dests
d1.txt;d2.txt

定義任務行為Tips

1.任務內部可以執行其他的任務。

grunt.registerTask('mytask', function() {
  grunt.task.run('task1', 'task2');
  // Or:
  grunt.task.run(['task1', 'task2']);
});

2.定義異步任務

grunt.registerTask('mytask', function() {
  var done = this.async();
  //do something
  done();
});

3.當任務失敗時,所有後續任務都將終止

在任務中,當執行失敗,可以return false來表明當前任務執行失敗,一般,多個任務按順序執行,如果有任務失敗時,所有後續任務都將終止。可以通過在命令行后加上–force來使有任務失敗時,後續任務能繼續進行。

4.任務中檢查前置任務狀態

有些任務可以依賴於其他任務的成功執行。通過grunt.task.requires方法來檢查其前置任務是否已經執行,並且沒有失敗。

5.任務中檢查配置屬性

可以用方法grunt.task.requiresConfig指定一個或者多個字符串或者數組的配置屬性為必需的。如果一個或多個必需的配置屬性缺失,就通知系統當前任務失敗。

本站聲明:網站內容來源於博客園,如有侵權,請聯繫我們,我們將及時處理

【其他文章推薦】

※如何讓商品強力曝光呢? 網頁設計公司幫您建置最吸引人的網站,提高曝光率!!

網頁設計一頭霧水??該從何著手呢? 找到專業技術的網頁設計公司,幫您輕鬆架站!

※想知道最厲害的台北網頁設計公司推薦台中網頁設計公司推薦專業設計師”嚨底家”!!

大陸寄台灣空運注意事項

大陸海運台灣交貨時間多久?

詳細講解 Redis 的兩種安裝部署方式

Redis 是一款比較常用的 NoSQL 數據庫,我們通常使用 Redis 來做緩存,這是一篇關於 Redis 安裝的文章,所以不會涉及到 Redis 的高級特性和使用場景,Redis 能夠兼容絕大部分的 POSIX 系統,例如 Linux、OS X 等,但是很遺憾不支持在 Windows 上安裝,當然如果你需要在 windows 下安裝 redis 的話,也是可以的,微軟公司的開源技術組在 GitHub 上 維護一個 Redis 的分支,GitHub 地址為:https://github.com/microsoftarchive/redis,我看了一下這上面的版本比較舊,所以我個人不推薦使用這個來安裝 Redis ,Windows 用戶可以使用 Docker 容器來安裝,也是非常方便、簡單的,接下來我們就一起來看看 Redis 的安裝方式吧。

1、Linux 系統下安裝 redis

安裝

在 redis 官網中有安裝教程,鏈接:https://redis.io/download,安裝步驟我拷貝過來了,步驟如下:

$ wget http://download.redis.io/releases/redis-5.0.6.tar.gz
$ tar xzf redis-5.0.6.tar.gz
$ cd redis-5.0.6
$ make

我在 /usr/local 目錄下面操作的這些命令,也就是說 Redis 的安裝目錄為 /usr/local ,這些命令執行完之後你的機器上安裝好 Redis ,在安裝的過程中,如果你的機器上沒有安裝 gcc ,你安裝好 gcc 之後再 make 可能會報以下錯誤

jemalloc/jemalloc.h: No such file or directory

當時沒有截取詳細的錯誤信息,只把主要的一段截取出來了,這個錯誤的原因是我們上一次 make 報錯之後,有編譯后的文件,所以我們需要清除上一次的殘留文件再重新編譯,將 make 換成 make distclean && make 就可以了。

redis.conf 文件

redis.conf 是 Redis 的配置文件,redis 的所有配置有在這個文件裏面,這個文件挺大的有接近 1400 行,有關 redis 的操作、使用說明都在裏面,可以詳細的閱讀閱讀這個配置文件,大部分情況下我們使用默認配置就行,只需要設置少量配置就行。redis.conf 的存放位置在 Redis 的安裝目錄下,我這裡是 /usr/local/redis-5.0.5 目錄下,一起來看看我們可能會修改的幾個配置:

  • bind 127.0.0.1:允許訪問機器的IP,默認只有本機才能訪問,你可以修改 ip 來運行其他機器也能訪問,但是如果你想讓所有機器都可以訪問的話,直接設置為 bind 0.0.0.0 就行了。
  • port 6379:redis 實例啟動的端口,默認為 6379
  • daemonize no:是否以守護進程的方式運行,默認是 no,也就是說你把啟動窗口關閉了,redis 實例也就關閉了,一般這個選項我們設置為 yes,以守護進程的方式運行,說俗一點就是後台運行。
  • pidfile /var/run/redis_6379.pid:如果我們使用守護進程方式運行的話 ,就會產生一個後綴名為 .pid 的文件,這個使用默認的也行
  • dir ./:持久化文件存放位置,這個配置我們還是設置一下為好,我這裏設置為 dir /usr/local/redis_data
  • appendonly no:是否開啟 AOF 持久化方式,redis 默認只開啟了 RDB 模式,這裏我們設置為 yes,兩種方式都開啟,雙重保險,關於這兩種方式的區別,我們後面在學習

好像大概設置這幾個就好了,更多關於 redis.conf 的配置,你可以詳細閱讀 redis.conf 配置文件或者查閱相關手冊。

redis 的啟動

Redis 的啟動非常簡單,Redis 安裝完成之後,會在 /usr/local/redis-5.0.5/src 存放 Redis 的 shell 交互命令,其中有一個 redis-server ,這個就是 Redis 的啟動命令,執行:

./redis-server /usr/local/redis-5.0.5/redis.conf

後面跟的是 redis.conf 的文件路徑,不出意外的話我們將啟動成功,你會看到如下界面:

這裏我們使用的是守護進程的方式啟動,所以不會出現帶有 redis logo 的啟動界面,我們可以使用 shell 命令登錄到 Redis 中,還是在 src 目錄下面,執行下面這條命令:

./redis-cli

這命令你就進入了 shell 交互界面,./redis-cli 命令可以帶一些參數,例如 -h IP 這個就可以進入指定機器的 Redis 實例,進入之後你就可以進行一些操作了,如下圖所示:

redis 關閉

Redis 的關閉方式有兩種,一種是在 shell 交互界面關閉,另一種是 kill + 進程號關閉 Redis 實例的方式

shell 交互界面關閉

shutdown [nosave|save]

在 shell 交互界面輸入 shutdown 命令就可以關閉 Redis 實例,後面有一個可選參數,nosave 就是不將內存中的數據持久化,save 就是將內存中的數據持久化。shutdown 關閉方式是比較優雅的關閉方式,建議使用這種關閉方式

Kill + 進程號關閉 Redis 實例

使用 ps -ef|grep redis 查看 Redis 進程號,如下圖所示:

在這裏找到我們需要關閉 redis 實例的進程號,比如這裏我們的進程號為 27133,那麼我們就直接使用 kill 27133 關閉 Redis 實例服務,這種方式我們需要注意一個地方,那就是需要我們去把 pid 文件刪掉,pid 文件存放的位置我們在 redis.conf 里配置的 pidfile /var/run/redis_6379.pid,我們需要到 /var/run 目錄下把 redis_6379.pid 刪掉,這樣下一次才能正常重啟 Redis 服務。

上面兩種方式都可以關閉 Redis 服務,隨便選一種都行,但是切記不要使用 Kill 9 方式關閉 Redis 進程,這樣 Redis 不會進行持久化操作,除此之外,還會造成緩衝區等資源不能優雅關閉,極端情況下會造成 AOF 和複製丟失數據的情況

redis 開機自啟動

在服務器上我們可能需要將 Redis 設置為開機自啟動,其實這個也非常簡單,我們只需要做以下四步操作即可。

1、 編寫配置腳本 vim /etc/init.d/redis
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
#chkconfig: 2345 80 90
#description:auto_run
# 端口號
REDISPORT=6379
# 啟動命令
EXEC=/usr/local/redis-5.0.5/src/redis-server
# shell 交付命令
CLIEXEC=/usr/local/redis-5.0.5/src/redis-cli
# pid 存放位置
PIDFILE=/var/run/redis_${REDISPORT}.pid
# redis 配置文件
CONF="/usr/local/redis-5.0.5/redis.conf"

case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis server..."
                $EXEC $CONF
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac
2、修改 redis.conf,設置 redis 為守護進程方式運行
################################# GENERAL #####################################

# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes
3、修改文件執行權限
chmod +x /etc/init.d/redis
4、設置開機啟動
# 啟動 redis
service redis start
# 停止 redis
service redis stop
# 開啟服務自啟動
chkconfig redis on

2、Docker 安裝 Redis

Docker 安裝 Redis 整體來說比較方便,我說的是非生產環境,就是自己搭着測試或者學習的環境,下面的步驟全部建立在你已經在你的電腦上安裝了 Docker 的基礎上,下面就來開啟安裝之旅。

1、拉取 redis 鏡像

docker pull redis

2、快速啟動

docker run -p 6379:6379 –name myredis -d redis redis-server –appendonly yes

這種方式啟動使用的默認的 redis.conf 配置,我們先來看看這幾個參數的意思

  • -p 6379:6379:映射端口,前面的 6379 是外部 redis 端口,後面的 6379 是容器內部的 redis 端口
  • –name myredis :容器對應的名稱
  • redis redis-server:redis 代表着 redis 鏡像 redis-server 表示的是執行的命令,也是就 redis 的啟動命令,跟我們 linux 下面的 ./redis-server 一樣
  • –appendonly yes:開啟 AOF 持久化
3、使用 redis

通過上面的步驟,我們已經在 Docker 中啟動了 Redis 服務,下面我們就來通過 redis-cli 訪問一下,使用下面這條命令就可以啟動 redis-cli

docker exec -it dockerRedis redis-cli

其中 dockerRedis 是你啟動 Redis 容器名稱,不出意外的話,你可以啟動一個 redis-cli 客戶端,如下圖所示:

上面就是使用 Docker 簡單的啟動 Redis ,整體來說比 linux 上面安裝啟動要方便不少,主要是你可以在 windows 系統上運行,雖然最終它還是運行在 linux 上面的,但是這個過程我們是無感知的。你可以能會問:我想在啟動的時候知道 redis.conf 可行不?答案是可行的,但是如果你對 Docker 不了解的話,可能會遇到一些坑,我就遇到了,因為我對 Docker 不是太了解,平時使用 docker 都是只需要傳入參數就好了,沒有傳過文件。關於啟動時指定配置文件,在 redis 鏡像那裡有說明,但是是 linux 下面的,並不是 windows 系統下的 Docker 配置方式,所以我就百度到了下面這段命令

docker run -v /d:/dockerdata/redis/config/redis.conf:/usr/local/etc/redis/redis.conf –name myredis redis redis-server /usr/local/etc/redis/redis.conf

這段命令就是一個坑,壓根就沒有,啟動這條命令,你將得到如下反饋:

顯然這條命令是沒有用的,當然這隻是我個人認為,也許是我操作失誤,也許是我知識面不夠,如果朋友們發現錯誤還請多多指教,這裏我就先當它是錯誤的,正確的做法是在 Docker 的宿主機上存放 redis.conf 文件,顯然 Docker 的宿主機並不是 windows 系統,而是啟動在 windows 系統上的虛擬機,所以我們需要進入到虛擬機裏面,Docker Quickstart Terminal 啟動默認界面並沒有正真的登錄到虛擬機,所以我們需要更改登錄方式,使用 docker-machine ssh 命令,如下圖所示:

這樣我們就進入到了真正的虛擬機裏面,我們就在一台虛擬機上操作了,跟我們在 linux 上的安裝一樣,我們先建立兩個目錄用來存放 Redis 配置:

/usr/local/redis:存放redis.conf
/usr/local/redis/data :存放持久化文件

建立好兩個目錄后, 我們把 redis.conf 放在 /usr/local/redis 目錄下,使用下面這條 Docker 命令啟動 Redis 鏡像:

docker run -p 6379:6379 -v /usr/local/redis/redis.conf:/usr/local/etc/redis/redis.conf -v /usr/local/redis/ –name dockerRedis -d redis redis-server /usr/local/etc/redis/redis.conf

這條 docker 啟動命令跟上面的有一點區別,有兩個參數我在這裏說明一下:

  • -v /usr/local/redis/redis.conf:/usr/local/etc/redis/redis.conf:這參數是將 /usr/local/redis/redis.conf 複製到 /usr/local/etc/redis/redis.conf 下
  • -v /usr/local/redis/ /usr/local/redis/data 也會存放持久化文件

到此,Docker 安裝 Redis 的複雜操作也做完了,如果沒什麼特別要求的話,使用簡單的 docker 啟動就好了,簡單方便,完全夠用了。

最後

目前互聯網上很多大佬都有 Redis 系列教程,如有雷同,請多多包涵了。原創不易,碼字不易,還希望大家多多支持。若文中有所錯誤之處,還望提出,謝謝。

歡迎掃碼關注微信公眾號:「平頭哥的技術博文」,和平頭哥一起學習,一起進步。

本站聲明:網站內容來源於博客園,如有侵權,請聯繫我們,我們將及時處理

【其他文章推薦】

※專營大陸空運台灣貨物推薦

台灣空運大陸一條龍服務

※評比南投搬家公司費用收費行情懶人包大公開

南投搬家費用,距離,噸數怎麼算?達人教你簡易估價知識!